Class: HTML5::HTMLTokenizer

Inherits:
Object
  • Object
show all
Defined in:
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/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/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/html5/tokenizer.rb', line 21

def current_token
  @current_token
end

#streamObject (readonly)

Returns the value of attribute stream.



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

def stream
  @stream
end

Instance Method Details

#after_attribute_name_stateObject



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/html5/tokenizer.rb', line 519

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 == "/"
    @state = :self_closing_tag_state
  else
    @current_token[:data].push([data, ""])
    @state = :attribute_name_state
  end
  return true
end

#after_attribute_value_stateObject



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

def after_attribute_value_state
  data = self.stream.char()
  if SPACE_CHARACTERS.include? data
    @state = :before_attribute_name_state
  elsif data == ">"
    emit_current_token
    @state = :data_state
  elsif data == "/"
    @state = :self_closing_tag_state
  elsif data == :EOF
    @token_queue << {:type =>  :ParseError, :data => "unexpected-EOF-after-attribute-value"}
    emit_current_token
    @stream.unget(data)
    @state = :data_state
  else
    @token_queue.push({:type => :ParseError, :data => "unexpected-character-after-attribute-value"})
    @stream.unget(data)
    @state = :before_attribute_name_state
  end
  true
end

#after_doctype_name_stateObject



835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
# File 'lib/html5/tokenizer.rb', line 835

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}}
      @current_token[:correct] = false
      @state = :bogus_doctype_state
    end
  end
  return true
end

#after_doctype_public_identifier_stateObject



934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
# File 'lib/html5/tokenizer.rb', line 934

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"}
    @current_token[:correct] = false
    @state = :bogus_doctype_state
  end
  return true
end

#after_doctype_system_identifier_stateObject



1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/html5/tokenizer.rb', line 1026

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



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/html5/tokenizer.rb', line 467

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 == "/"
    if !process_solidus_in_tag
      @state = :before_attribute_name_state
    end
 elsif data == "'" or data == '"'
    @token_queue.push({:type => :ParseError, :data => "invalid-character-in-attribute-name"})
    @current_token[:data][-1][0] += data
    leavingThisState = false
  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



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

def attribute_value_double_quoted_state
  data = @stream.char
  if data == "\""
    @state = :after_attribute_value_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



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

def attribute_value_single_quoted_state
  data = @stream.char
  if data == "'"
    @state = :after_attribute_value_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



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

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 == '"' || data == "'" || data == "="
    @token_queue.push({:type => :ParseError, :data => "unexpected-character-in-unquoted-attribute-value"})
    @current_token[:data][-1][1] += data
  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



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/html5/tokenizer.rb', line 442

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 == "/"
    @state = :self_closing_tag_state
  elsif data == "'" || data == '"' || data == "="
    @token_queue.push({:type => :ParseError, :data => "invalid-character-in-attribute-name"})
    @current_token[:data].push([data, ""])
    @state = :attribute_name_state
  else
    @current_token[:data].push([data, ""])
    @state = :attribute_name_state
  end
  return true
end

#before_attribute_value_stateObject



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/html5/tokenizer.rb', line 542

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 == "="
    @token_queue.push({:type => :ParseError, :data => "equals-in-unquoted-attribute-value"})
      @current_token[:data][-1][1] += data
      @state = :attribute_value_unquoted_state
  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



796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
# File 'lib/html5/tokenizer.rb', line 796

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



865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/html5/tokenizer.rb', line 865

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"}
    @current_token[:correct] = false
    @state = :bogus_doctype_state
  end

  return true
end

#before_doctype_system_identifier_stateObject



959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
# File 'lib/html5/tokenizer.rb', line 959

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"}
    @current_token[:correct] = false
    @state = :bogus_doctype_state
  end
  return true
end

#bogus_comment_stateObject



660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/html5/tokenizer.rb', line 660

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



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/html5/tokenizer.rb', line 1044

def bogus_doctype_state
  data = @stream.char
  if data == ">"
    @token_queue << @current_token
    @state = :data_state
  elsif data == :EOF
    @stream.unget(data)
    @token_queue << @current_token
    @state = :data_state
  end
  return true
end

#close_tag_open_stateObject



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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/html5/tokenizer.rb', line 363

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



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

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



763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/html5/tokenizer.rb', line 763

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



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/html5/tokenizer.rb', line 712

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



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

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



731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/html5/tokenizer.rb', line 731

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(allowed_char = nil, from_attribute = false) ⇒ Object



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
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/html5/tokenizer.rb', line 148

def consume_entity(allowed_char=nil, from_attribute=false)
  char = nil
  char_stack = [@stream.char]
  if SPACE_CHARACTERS.include?(char_stack[0]) or [:EOF, '<', '&'].include?(char_stack[0]) ||
     (allowed_char && allowed_char == 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.



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
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/html5/tokenizer.rb', line 90

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 && charAsInt <= 1114111 && !(55296 <= charAsInt && charAsInt <= 57343) &&
    ![0x10FFFF].include?(charAsInt) # TODO add more entity replacements here
    if String.method_defined? :force_encoding
      char = charAsInt.chr('utf-8')
    else
      char = [charAsInt].pack('U')
    end
  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.



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/html5/tokenizer.rb', line 265

def data_state
  data = @stream.char

  if @content_model_flag == :CDATA or @content_model_flag == :RCDATA
    @lastFourChars.shift if @lastFourChars.length == 4
    @lastFourChars << data
  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
    chars = @stream.chars_until(["&", "<", ">", "-"])
    @token_queue << {:type => :Characters, :data => data + chars}
    @lastFourChars += (chars[chars.length > 4 ? -4 : -chars.length, 4] || '').scan(/./)
    @lastFourChars = @lastFourChars[(@lastFourChars.length > 4 ? -4 : -@lastFourChars.length), 4] || []
  end
  return true
end

#doctype_name_stateObject



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

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



894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
# File 'lib/html5/tokenizer.rb', line 894

def doctype_public_identifier_double_quoted_state
  data = @stream.char
  if data == "\""
    @state = :after_doctype_public_identifier_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
    @current_token[:publicId] += data
  end
  return true
end

#doctype_public_identifier_single_quoted_stateObject



914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
# File 'lib/html5/tokenizer.rb', line 914

def doctype_public_identifier_single_quoted_state
  data = @stream.char
  if data == "'"
    @state = :after_doctype_public_identifier_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
    @current_token[:publicId] += data
  end
  return true
end

#doctype_stateObject



784
785
786
787
788
789
790
791
792
793
794
# File 'lib/html5/tokenizer.rb', line 784

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



986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
# File 'lib/html5/tokenizer.rb', line 986

def doctype_system_identifier_double_quoted_state
  data = @stream.char
  if data == "\""
    @state = :after_doctype_system_identifier_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
    @current_token[:systemId] += data
  end
  return true
end

#doctype_system_identifier_single_quoted_stateObject



1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/html5/tokenizer.rb', line 1006

def doctype_system_identifier_single_quoted_state
  data = @stream.char
  if data == "'"
    @state = :after_doctype_system_identifier_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
    @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/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.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/html5/tokenizer.rb', line 243

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
    
    if token[:type] == :EndTag && token[:self_closing]
      @token_queue << {:type => :ParseError, :data => "self-closing-end-tag"}
    end
    @token_queue << token
    @state = :data_state
  end
  
end

#entity_data_stateObject



307
308
309
310
311
312
313
314
315
316
# File 'lib/html5/tokenizer.rb', line 307

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



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/html5/tokenizer.rb', line 673

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_attribute(allowed_char) ⇒ Object

This method replaces the need for “entityInAttributeValueState”.



231
232
233
234
235
236
237
238
# File 'lib/html5/tokenizer.rb', line 231

def process_entity_in_attribute allowed_char
  entity = consume_entity(allowed_char, true)
  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
79
80
81
82
83
84
# File '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
  rv = false
  if @current_token[:type] == :StartTag and data == ">"
    @current_token[:type] = :EmptyTag
  elsif data == :EOF
    @token_queue << ({:type => :ParseError, :data => "eof-following-solidus"})
    @state = :data_state
    emit_current_token
    rv = true
  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)
  rv
end

#self_closing_tag_stateObject



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/html5/tokenizer.rb', line 641

def self_closing_tag_state
  c = @stream.char
  case c
  when ">"
    @current_token[:self_closing] = true
    emit_current_token
    @state = :data_state
  when :EOF
    @token_queue << {:type => :ParseError, :data => "eof-in-tag-name"}
    @stream.unget(c)
    @state = :data_state
  else
    @token_queue << {:type => :ParseError, :data => "expected-self-closing-tag"}
    @stream.unget(c)
    @state = :before_attribute_name_state
  end
  true
end

#tag_name_stateObject



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

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 == "/"
    @state = :self_closing_tag_state
  else
    @current_token[:name] += data
  end
  return true
end

#tag_open_stateObject



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/html5/tokenizer.rb', line 318

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