Class: HTML5::HTMLTokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb

Overview

This class takes care of tokenizing HTML.

  • @current_token Holds the token that is currently being processed.

  • @state Holds a reference to the method to be invoked… XXX

  • @states Holds a mapping between states and methods that implement the state.

  • @stream Points to HTMLInputStream object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, options = {}) ⇒ HTMLTokenizer

XXX need to fix documentation



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 26

def initialize(stream, options = {})
  @stream = HTMLInputStream.new(stream, options)

  # Setup the initial tokenizer state
  @content_model_flag = :PCDATA
  @state              = :data_state
  @escapeFlag         = false
  @lastFourChars      = []

  # The current token being created
  @current_token = nil

  # Tokens to be processed.
  @token_queue             = []
  @lowercase_element_name = options[:lowercase_element_name] != false
  @lowercase_attr_name    = options[:lowercase_attr_name]    != false
end

Instance Attribute Details

#content_model_flagObject

Returns the value of attribute content_model_flag.



21
22
23
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 21

def content_model_flag
  @content_model_flag
end

#current_tokenObject

Returns the value of attribute current_token.



21
22
23
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 21

def current_token
  @current_token
end

#streamObject (readonly)

Returns the value of attribute stream.



22
23
24
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 22

def stream
  @stream
end

Instance Method Details

#after_attribute_name_stateObject



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 491

def after_attribute_name_state
  data = @stream.char
  if SPACE_CHARACTERS.include? data
    @stream.chars_until(SPACE_CHARACTERS, true)
  elsif data == "="
    @state = :before_attribute_value_state
  elsif data == ">"
    emit_current_token
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "expected-end-of-tag-but-got-eof"}
    emit_current_token
  elsif ASCII_LETTERS.include? data
    @current_token[:data].push([data, ""])
    @state = :attribute_name_state
  elsif data == "/"
    process_solidus_in_tag
    @state = :before_attribute_name_state
  else
    @current_token[:data].push([data, ""])
    @state = :attribute_name_state
  end
  return true
end

#after_doctype_name_stateObject



762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 762

def after_doctype_name_state
  data = @stream.char
  if SPACE_CHARACTERS.include? data
  elsif data == ">"
    @token_queue << @current_token
    @state = :data_state
  elsif data == :EOF
    @current_token[:correct] = false
    @stream.unget(data)
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @token_queue << @current_token
    @state = :data_state
  else
    char_stack = [data]  
    5.times { char_stack << stream.char }
    token = char_stack.join('').tr(ASCII_UPPERCASE,ASCII_LOWERCASE)
    if token == "public" and !char_stack.include?(:EOF)
      @state = :before_doctype_public_identifier_state
    elsif token == "system" and !char_stack.include?(:EOF)
      @state = :before_doctype_system_identifier_state
    else
      @stream.unget(char_stack)
      @token_queue << {:type => :ParseError, :data => "expected-space-or-right-bracket-in-doctype", "datavars" => {"data" => data}}
      @state = :bogus_doctype_state
    end
  end
  return true
end

#after_doctype_public_identifier_stateObject



849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 849

def after_doctype_public_identifier_state
  data = @stream.char
  if SPACE_CHARACTERS.include?(data)
  elsif data == "\""
    @current_token[:systemId] = ""
    @state = :doctype_system_identifier_double_quoted_state
  elsif data == "'"
    @current_token[:systemId] = ""
    @state = :doctype_system_identifier_single_quoted_state
  elsif data == ">"
    @token_queue << @current_token
    @state = :data_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  else
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @state = :bogus_doctype_state
  end
  return true
end

#after_doctype_system_identifier_stateObject



929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 929

def after_doctype_system_identifier_state
  data = @stream.char
  if SPACE_CHARACTERS.include?(data)
  elsif data == ">"
    @token_queue << @current_token
    @state = :data_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  else
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @state = :bogus_doctype_state
  end
  return true
end

#attribute_name_stateObject



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 444

def attribute_name_state
  data = @stream.char
  leavingThisState = true
  emitToken = false
  if data == "="
    @state = :before_attribute_value_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-attribute-name"}
    @state = :data_state
    emitToken = true
  elsif ASCII_LETTERS.include? data
    @current_token[:data][-1][0] += data + @stream.chars_until(ASCII_LETTERS, true)
    leavingThisState = false
  elsif data == ">"
    # XXX If we emit here the attributes are converted to a dict
    # without being checked and when the code below runs we error
    # because data is a dict not a list
    emitToken = true
  elsif SPACE_CHARACTERS.include? data
    @state = :after_attribute_name_state
  elsif data == "/"
    process_solidus_in_tag
    @state = :before_attribute_name_state
  else
    @current_token[:data][-1][0] += data
    leavingThisState = false
  end

  if leavingThisState
    # Attributes are not dropped at this stage. That happens when the
    # start tag token is emitted so values can still be safely appended
    # to attributes, but we do want to report the parse error in time.
    if @lowercase_attr_name
        @current_token[:data][-1][0] = @current_token[:data].last.first.downcase
    end
    @current_token[:data][0...-1].each {|name,value|
      if @current_token[:data].last.first == name
        @token_queue << {:type => :ParseError, :data => "duplicate-attribute"}
        break # don't report an error more than once
      end
    }
    # XXX Fix for above XXX
    emit_current_token if emitToken
  end
  return true
end

#attribute_value_double_quoted_stateObject



538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 538

def attribute_value_double_quoted_state
  data = @stream.char
  if data == "\""
    @state = :before_attribute_name_state
  elsif data == "&"
    process_entity_in_attribute
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-attribute-value-double-quote"}
    emit_current_token
  else
    @current_token[:data][-1][1] += data + @stream.chars_until(["\"", "&"])
  end
  return true
end

#attribute_value_single_quoted_stateObject



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 553

def attribute_value_single_quoted_state
  data = @stream.char
  if data == "'"
    @state = :before_attribute_name_state
  elsif data == "&"
    process_entity_in_attribute
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-attribute-value-single-quote"}
    emit_current_token
  else
    @current_token[:data][-1][1] += data +\
      @stream.chars_until(["'", "&"])
  end
  return true
end

#attribute_value_unquoted_stateObject



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 569

def attribute_value_unquoted_state
  data = @stream.char
  if SPACE_CHARACTERS.include? data
    @state = :before_attribute_name_state
  elsif data == "&"
    process_entity_in_attribute
  elsif data == ">"
    emit_current_token
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-attribute-value-no-quotes"}
    emit_current_token
  else
    @current_token[:data][-1][1] += data +  @stream.chars_until(["&", ">","<"] + SPACE_CHARACTERS)
  end
  return true
end

#before_attribute_name_stateObject



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 423

def before_attribute_name_state
  data = @stream.char
  if SPACE_CHARACTERS.include? data
    @stream.chars_until(SPACE_CHARACTERS, true)
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "expected-attribute-name-but-got-eof"}
    emit_current_token
  elsif ASCII_LETTERS.include? data
    @current_token[:data].push([data, ""])
    @state = :attribute_name_state
  elsif data == ">"
    emit_current_token
  elsif data == "/"
    process_solidus_in_tag
  else
    @current_token[:data].push([data, ""])
    @state = :attribute_name_state
  end
  return true
end

#before_attribute_value_stateObject



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 515

def before_attribute_value_state
  data = @stream.char
  if SPACE_CHARACTERS.include? data
    @stream.chars_until(SPACE_CHARACTERS, true)
  elsif data == "\""
    @state = :attribute_value_double_quoted_state
  elsif data == "&"
    @state = :attribute_value_unquoted_state
    @stream.unget(data);
  elsif data == "'"
    @state = :attribute_value_single_quoted_state
  elsif data == ">"
    emit_current_token
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "expected-attribute-value-but-got-eof"}
    emit_current_token
  else
    @current_token[:data][-1][1] += data
    @state = :attribute_value_unquoted_state
  end
  return true
end

#before_doctype_name_stateObject



723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 723

def before_doctype_name_state
  data = @stream.char
  if SPACE_CHARACTERS.include? data
  elsif data == ">"
    @token_queue << {:type => :ParseError, :data => "expected-doctype-name-but-got-right-bracket"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "expected-doctype-name-but-got-eof"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  else
    @current_token[:name] = data
    @state = :doctype_name_state
  end
  return true
end

#before_doctype_public_identifier_stateObject



791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 791

def before_doctype_public_identifier_state
  data = @stream.char

  if SPACE_CHARACTERS.include?(data)
  elsif data == "\""
    @current_token[:publicId] = ""
    @state = :doctype_public_identifier_double_quoted_state
  elsif data == "'"
    @current_token[:publicId] = ""
    @state = :doctype_public_identifier_single_quoted_state
  elsif data == ">"
    @token_queue << {:type => :ParseError, :data => "unexpected-end-of-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  else
    @token_queue << {:type => :ParseError, :data => "unexpected-char-in-doctype"}
    @state = :bogus_doctype_state
  end

  return true
end

#before_doctype_system_identifier_stateObject



873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 873

def before_doctype_system_identifier_state
  data = @stream.char
  if SPACE_CHARACTERS.include?(data)
  elsif data == "\""
    @current_token[:systemId] = ""
    @state = :doctype_system_identifier_double_quoted_state
  elsif data == "'"
    @current_token[:systemId] = ""
    @state = :doctype_system_identifier_single_quoted_state
  elsif data == ">"
    @token_queue << {:type => :ParseError, :data => "unexpected-char-in-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  else
    @token_queue << {:type => :ParseError, :data => "unexpected-char-in-doctype"}
    @state = :bogus_doctype_state
  end
  return true
end

#bogus_comment_stateObject



586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 586

def bogus_comment_state
  # Make a new comment token and give it as value all the characters
  # until the first > or :EOF (chars_until checks for :EOF automatically)
  # and emit it.
  @token_queue << {:type => :Comment, :data => @stream.chars_until((">"))}

  # Eat the character directly after the bogus comment which is either a
  # ">" or an :EOF.
  @stream.char
  @state = :data_state
  return true
end

#bogus_doctype_stateObject



947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 947

def bogus_doctype_state
  data = @stream.char
  @current_token[:correct] = false
  if data == ">"
    @token_queue << @current_token
    @state = :data_state
  elsif data == :EOF
    # XXX EMIT
    @stream.unget(data)
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  end
  return true
end

#close_tag_open_stateObject



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 343

def close_tag_open_state
  if (@content_model_flag == :RCDATA or @content_model_flag == :CDATA)
    if @current_token
      char_stack = []

      # So far we know that "</" has been consumed. We now need to know
      # whether the next few characters match the name of last emitted
      # start tag which also happens to be the current_token. We also need
      # to have the character directly after the characters that could
      # match the start tag name.
      (@current_token[:name].length + 1).times do
        char_stack.push(@stream.char)
        # Make sure we don't get hit by :EOF
        break if char_stack[-1] == :EOF
      end

      # Since this is just for checking. We put the characters back on
      # the stack.
      @stream.unget(char_stack)
    end

    if @current_token and
      @current_token[:name].downcase == 
      char_stack[0...-1].join('').downcase and
      (SPACE_CHARACTERS + [">", "/", "<", :EOF]).include? char_stack[-1]
      # Because the characters are correct we can safely switch to
      # PCDATA mode now. This also means we don't have to do it when
      # emitting the end tag token.
      @content_model_flag = :PCDATA
    else
      @token_queue << {:type => :Characters, :data => "</"}
      @state = :data_state

      # Need to return here since we don't want the rest of the
      # method to be walked through.
      return true
    end
  end

  data = @stream.char
  if data == :EOF
    @token_queue << {:type => :ParseError, :data => "expected-closing-tag-but-got-eof"}
    @token_queue << {:type => :Characters, :data => "</"}
    @state = :data_state
  elsif ASCII_LETTERS.include? data
    @current_token = {:type => :EndTag, :name => data, :data => []}
    @state = :tag_name_state
  elsif data == ">"
    @token_queue << {:type => :ParseError, :data => "expected-closing-tag-but-got-right-bracket"}
    @state = :data_state
  else
    # XXX data can be _'_...
    @token_queue << {:type => :ParseError, :data => "expected-closing-tag-but-got-char", :datavars => {:data => data}}
    @stream.unget(data)
    @state = :bogus_comment_state
  end

  return true
end

#comment_end_dash_stateObject



671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 671

def comment_end_dash_state
  data = @stream.char
  if data == "-"
    @state = :comment_end_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-comment-end-dash"}
    @token_queue << @current_token
    @state = :data_state
  else
    @current_token[:data] += "-" + data +\
      @stream.chars_until("-")
    # Consume the next character which is either a "-" or an :EOF as
    # well so if there's a "-" directly after the "-" we go nicely to
    # the "comment end state" without emitting a ParseError there.
    @stream.char
  end
  return true
end

#comment_end_stateObject



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 690

def comment_end_state
  data = @stream.char
  if data == ">"
    @token_queue << @current_token
    @state = :data_state
  elsif data == "-"
    @token_queue << {:type => :ParseError, :data => "unexpected-dash-after-double-dash-in-comment"}
    @current_token[:data] += data
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-comment-double-dash"}
    @token_queue << @current_token
    @state = :data_state
  else
    # XXX
    @token_queue << {:type => :ParseError, :data => "unexpected-char-in-comment"}
    @current_token[:data] += "--" + data
    @state = :comment_state
  end
  return true
end

#comment_start_dash_stateObject



638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 638

def comment_start_dash_state
    data = @stream.char
    if data == "-"
        @state = :comment_end_state
    elsif data == ">"
        @token_queue << {:type => :ParseError, :data => "incorrect-comment"}
        @token_queue << @current_token
        @state = :data_state
    elsif data == :EOF
        @token_queue << {:type => :ParseError, :data => "eof-in-comment"}
        @token_queue << @current_token
        @state = :data_state
    else
        @current_token[:data] += '-' + data + @stream.chars_until("-")
        @state = :comment_state
    end
    return true
end

#comment_start_stateObject



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 619

def comment_start_state
    data = @stream.char
    if data == "-"
        @state = :comment_start_dash_state
    elsif data == ">"
        @token_queue << {:type => :ParseError, :data => "incorrect-comment"}
        @token_queue << @current_token
        @state = :data_state
    elsif data == :EOF
        @token_queue << {:type => :ParseError, :data => "eof-in-comment"}
        @token_queue << @current_token
        @state = :data_state
    else
        @current_token[:data] += data + @stream.chars_until("-")
        @state = :comment_state
    end
    return true
end

#comment_stateObject



657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 657

def comment_state
  data = @stream.char
  if data == "-"
    @state = :comment_end_dash_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-comment"}
    @token_queue << @current_token
    @state = :data_state
  else
    @current_token[:data] += data + @stream.chars_until("-")
  end
  return true
end

#consume_entity(from_attribute = false) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 137

def consume_entity(from_attribute=false)
  char = nil
  char_stack = [@stream.char]
  if SPACE_CHARACTERS.include?(char_stack[0]) or [:EOF, '<', '&'].include?(char_stack[0])
    @stream.unget(char_stack)
  elsif char_stack[0] == '#'
    # We might have a number entity here.
    char_stack += [@stream.char, @stream.char]
    if char_stack[0 .. 1].include? :EOF
      # If we reach the end of the file put everything up to :EOF
      # back in the queue
      char_stack = char_stack[0...char_stack.index(:EOF)]
      @stream.unget(char_stack)
      @token_queue << {:type => :ParseError, :data => "expected-numeric-entity-but-got-eof"}
    else
      if char_stack[1].downcase == "x" and HEX_DIGITS.include? char_stack[2]
        # Hexadecimal entity detected.
        @stream.unget(char_stack[2])
        char = consume_number_entity(true)
      elsif DIGITS.include? char_stack[1]
        # Decimal entity detected.
        @stream.unget(char_stack[1..-1])
        char = consume_number_entity(false)
      else
        # No number entity detected.
        @stream.unget(char_stack)
        @token_queue << {:type => :ParseError, :data => "expected-numeric-entity"}
      end
    end
  else
    # At this point in the process might have named entity. Entities
    # are stored in the global variable "entities".
    #
    # Consume characters and compare to these to a substring of the
    # entity names in the list until the substring no longer matches.
    filteredEntityList = ENTITIES.keys
    filteredEntityList.reject! {|e| e[0].chr != char_stack[0]}
    entityName = nil

    # Try to find the longest entity the string will match to take care
    # of &noti for instance.
    while char_stack.last != :EOF
      name = char_stack.join('')
      if filteredEntityList.any? {|e| e[0...name.length] == name}
        filteredEntityList.reject! {|e| e[0...name.length] != name}
        char_stack.push(@stream.char)
      else
        break
      end

      if ENTITIES.include? name
        entityName = name
        break if entityName[-1] == ';'
      end
    end

    if entityName != nil
      char = ENTITIES[entityName]

      # Check whether or not the last character returned can be
      # discarded or needs to be put back.
      if entityName[-1] != ?;
        @token_queue << {:type => :ParseError, :data => "named-entity-without-semicolon"}
      end

      if entityName[-1] != ";" and from_attribute and
         (ASCII_LETTERS.include?(char_stack[entityName.length]) or
          DIGITS.include?(char_stack[entityName.length]))
        @stream.unget(char_stack)
        char = '&'
      else
        @stream.unget(char_stack[entityName.length..-1])
      end
    else
      @token_queue << {:type => :ParseError, :data => "expected-named-entity"}
      @stream.unget(char_stack)
    end
  end
  return char
end

#consume_number_entity(isHex) ⇒ Object

This function returns either U+FFFD or the character based on the decimal or hexadecimal representation. It also discards “;” if present. If not present @token_queue << => :ParseError“ is invoked.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 84

def consume_number_entity(isHex)

  # XXX More need to be done here. For instance, #13 should prolly be
  # converted to #10 so we don't get \r (#13 is \r right?) in the DOM and
  # such. Thoughts on this appreciated.
  allowed = DIGITS
  radix = 10
  if isHex
    allowed = HEX_DIGITS
    radix = 16
  end

  char_stack = []

  # Consume all the characters that are in range while making sure we
  # don't hit an EOF.
  c = @stream.char
  while allowed.include?(c) and c != :EOF
    char_stack.push(c)
    c = @stream.char
  end

  # Convert the set of characters consumed to an int.
  charAsInt = char_stack.join('').to_i(radix)

  if charAsInt == 13
    @token_queue << {:type => :ParseError, :data => "incorrect-cr-newline-entity"}
    charAsInt = 10
  elsif (128..159).include? charAsInt
    # If the integer is between 127 and 160 (so 128 and bigger and 159
    # and smaller) we need to do the "windows trick".
    @token_queue << {:type => :ParseError, :data => "illegal-windows-1252-entity"}

    charAsInt = ENTITIES_WINDOWS1252[charAsInt - 128]
  end

  if 0 < charAsInt and charAsInt <= 1114111 and not (55296 <= charAsInt and charAsInt <= 57343)
    char = [charAsInt].pack('U')
  else
    char = [0xFFFD].pack('U')
    @token_queue << {:type => :ParseError, :data => "cant-convert-numeric-entity", :datavars => {"charAsInt" => charAsInt}}
  end

  # Discard the ; if present. Otherwise, put it back on the queue and
  # invoke parse_error on parser.
  if c != ";"
    @token_queue << {:type => :ParseError, :data => "numeric-entity-without-semicolon"}
    @stream.unget(c)
  end

  return char
end

#data_stateObject

XXX AT Perhaps we should have Hixie run some evaluation on billions of documents to figure out what the order of the various if and elsif statements should be.



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 249

def data_state
  data = @stream.char

  if @content_model_flag == :CDATA or @content_model_flag == :RCDATA
    @lastFourChars << data
    @lastFourChars.shift if @lastFourChars.length > 4
  end

  if data == "&" and [:PCDATA,:RCDATA].include?(@content_model_flag) and !@escapeFlag
      @state = :entity_data_state
  elsif data == "-" && [:CDATA, :RCDATA].include?(@content_model_flag) && !@escapeFlag && @lastFourChars.join('') == "<!--"
      @escapeFlag = true
      @token_queue << {:type => :Characters, :data => data}
  elsif data == "<" and !@escapeFlag and
    [:PCDATA,:CDATA,:RCDATA].include?(@content_model_flag)
      @state = :tag_open_state
  elsif data == ">" and @escapeFlag and 
    [:CDATA,:RCDATA].include?(@content_model_flag) and
    @lastFourChars[1..-1].join('') == "-->"
      @escapeFlag = false
      @token_queue << {:type => :Characters, :data => data}

  elsif data == :EOF
    # Tokenization ends.
    return false

  elsif SPACE_CHARACTERS.include? data
    # Directly after emitting a token you switch back to the "data
    # state". At that point SPACE_CHARACTERS are important so they are
    # emitted separately.
    # XXX need to check if we don't need a special "spaces" flag on
    # characters.
    @token_queue << {:type => :SpaceCharacters, :data => data + @stream.chars_until(SPACE_CHARACTERS, true)}
  else
    @token_queue << {:type => :Characters, :data => data + @stream.chars_until(%w[& < > -])}
  end
  return true
end

#doctype_name_stateObject



743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 743

def doctype_name_state
  data = @stream.char
  if SPACE_CHARACTERS.include? data
    @state = :after_doctype_name_state
  elsif data == ">"
    @token_queue << @current_token
    @state = :data_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype-name"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  else
    @current_token[:name] += data
  end

  return true
end

#doctype_public_identifier_double_quoted_stateObject



819
820
821
822
823
824
825
826
827
828
829
830
831
832
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 819

def doctype_public_identifier_double_quoted_state
  data = @stream.char
  if data == "\""
    @state = :after_doctype_public_identifier_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  else
    @current_token[:publicId] += data
  end
  return true
end

#doctype_public_identifier_single_quoted_stateObject



834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 834

def doctype_public_identifier_single_quoted_state
  data = @stream.char
  if data == "'"
    @state = :after_doctype_public_identifier_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  else
    @current_token[:publicId] += data
  end
  return true
end

#doctype_stateObject



711
712
713
714
715
716
717
718
719
720
721
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 711

def doctype_state
  data = @stream.char
  if SPACE_CHARACTERS.include? data
    @state = :before_doctype_name_state
  else
    @token_queue << {:type => :ParseError, :data => "need-space-after-doctype"}
    @stream.unget(data)
    @state = :before_doctype_name_state
  end
  return true
end

#doctype_system_identifier_double_quoted_stateObject



899
900
901
902
903
904
905
906
907
908
909
910
911
912
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 899

def doctype_system_identifier_double_quoted_state
  data = @stream.char
  if data == "\""
    @state = :after_doctype_system_identifier_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  else
    @current_token[:systemId] += data
  end
  return true
end

#doctype_system_identifier_single_quoted_stateObject



914
915
916
917
918
919
920
921
922
923
924
925
926
927
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 914

def doctype_system_identifier_single_quoted_state
  data = @stream.char
  if data == "'"
    @state = :after_doctype_system_identifier_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-doctype"}
    @current_token[:correct] = false
    @token_queue << @current_token
    @state = :data_state
  else
    @current_token[:systemId] += data
  end
  return true
end

#eachObject

This is where the magic happens.

We do our usually processing through the states and when we have a token to return we yield the token which pauses processing until the next token is requested.



49
50
51
52
53
54
55
56
57
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 49

def each
  @token_queue = []
  # Start processing. When EOF is reached @state will return false
  # instead of true and the loop will terminate.
  while send @state
    yield :type => :ParseError, :data => @stream.errors.shift until @stream.errors.empty?
    yield @token_queue.shift until @token_queue.empty?
  end
end

#emit_current_tokenObject

This method is a generic handler for emitting the tags. It also sets the state to “data” because that’s what’s needed after a token has been emitted.



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 231

def emit_current_token
  # Add token to the queue to be yielded
  token = @current_token
  if [:StartTag, :EndTag, :EmptyTag].include?(token[:type])
    if @lowercase_element_name
      token[:name] = token[:name].downcase
    end
    @token_queue << token
    @state = :data_state
  end
  
end

#entity_data_stateObject



288
289
290
291
292
293
294
295
296
297
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 288

def entity_data_state
  entity = consume_entity
  if entity
    @token_queue << {:type => :Characters, :data => entity}
  else
    @token_queue << {:type => :Characters, :data => "&"}
  end
  @state = :data_state
  return true
end

#markup_declaration_open_stateObject



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 599

def markup_declaration_open_state
  char_stack = [@stream.char, @stream.char]
  if char_stack == ["-", "-"]
    @current_token = {:type => :Comment, :data => ""}
    @state = :comment_start_state
  else
    5.times { char_stack.push(@stream.char) }
    # Put in explicit :EOF check
    if !char_stack.include?(:EOF) && char_stack.join("").upcase == "DOCTYPE"
      @current_token = {:type => :Doctype, :name => "", :publicId => nil, :systemId => nil, :correct => true}
      @state = :doctype_state
    else
      @token_queue << {:type => :ParseError, :data => "expected-dashes-or-doctype"}
      @stream.unget(char_stack)
      @state = :bogus_comment_state
    end
  end
  return true
end

#process_entity_in_attributeObject

This method replaces the need for “entityInAttributeValueState”.



219
220
221
222
223
224
225
226
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 219

def process_entity_in_attribute
  entity = consume_entity()
  if entity
    @current_token[:data][-1][1] += entity
  else
    @current_token[:data][-1][1] += "&"
  end
end

#process_solidus_in_tagObject

If the next character is a ‘>’, convert the current_token into an EmptyTag



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 64

def process_solidus_in_tag

  # We need to consume another character to make sure it's a ">"
  data = @stream.char

  if @current_token[:type] == :StartTag and data == ">"
    @current_token[:type] = :EmptyTag
  else
    @token_queue << {:type => :ParseError, :data => "incorrectly-placed-solidus"}
  end

  # The character we just consumed need to be put back on the stack so it
  # doesn't get lost...
  @stream.unget(data)
end

#tag_name_stateObject



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 403

def tag_name_state
  data = @stream.char
  if SPACE_CHARACTERS.include? data
    @state = :before_attribute_name_state
  elsif data == :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-tag-name"}
    emit_current_token
  elsif ASCII_LETTERS.include? data
    @current_token[:name] += data + @stream.chars_until(ASCII_LETTERS, true)
  elsif data == ">"
    emit_current_token
  elsif data == "/"
    process_solidus_in_tag
    @state = :before_attribute_name_state
  else
    @current_token[:name] += data
  end
  return true
end

#tag_open_stateObject



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/feed_tools/vendor/html5/lib/html5/tokenizer.rb', line 299

def tag_open_state
  data = @stream.char
  if @content_model_flag == :PCDATA
    if data == "!"
      @state = :markup_declaration_open_state
    elsif data == "/"
      @state = :close_tag_open_state
    elsif data != :EOF and ASCII_LETTERS.include? data
      @current_token = {:type => :StartTag, :name => data, :data => []}
      @state = :tag_name_state
    elsif data == ">"
      # XXX In theory it could be something besides a tag name. But
      # do we really care?
      @token_queue << {:type => :ParseError, :data =>       "expected-tag-name-but-got-right-bracket"}
      @token_queue << {:type => :Characters, :data => "<>"}
      @state = :data_state
    elsif data == "?"
      # XXX In theory it could be something besides a tag name. But
      # do we really care?
      @token_queue.push({:type => :ParseError, :data => "expected-tag-name-but-got-question-mark"})
      @stream.unget(data)
      @state = :bogus_comment_state
    else
      # XXX
      @token_queue << {:type => :ParseError, :data => "expected-tag-name"}
      @token_queue << {:type => :Characters, :data => "<"}
      @stream.unget(data)
      @state = :data_state
    end
  else
    # We know the content model flag is set to either RCDATA or CDATA
    # now because this state can never be entered with the PLAINTEXT
    # flag.
    if data == "/"
      @state = :close_tag_open_state
    else
      @token_queue << {:type => :Characters, :data => "<"}
      @stream.unget(data)
      @state = :data_state
    end
  end
  return true
end