summaryrefslogtreecommitdiffstats
path: root/parser/html/java/htmlparser/doc/tokenization.txt
blob: 21cd7f6e26c2724ec6de814841a81bc4d88053c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
136
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
287
288
289
290
291
292
293
294
295
296
297
298
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
342
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
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
790
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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
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
864
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
   #8.2 Parsing HTML documents Table of contents 8.2.5 Tree construction

   WHATWG

HTML 5

Draft Recommendation — 7 February 2009

   ← 8.2 Parsing HTML documents – Table of contents – 8.2.5 Tree
   construction →

    8.2.4 Tokenization

   Implementations must act as if they used the following state machine to
   tokenise HTML. The state machine must start in the data state. Most
   states consume a single character, which may have various side-effects,
   and either switches the state machine to a new state to reconsume the
   same character, or switches it to a new state (to consume the next
   character), or repeats the same state (to consume the next character).
   Some states have more complicated behavior and can consume several
   characters before switching to another state.

   The exact behavior of certain states depends on a content model flag
   that is set after certain tokens are emitted. The flag has several
   states: PCDATA, RCDATA, CDATA, and PLAINTEXT. Initially it must be in
   the PCDATA state. In the RCDATA and CDATA states, a further escape flag
   is used to control the behavior of the tokeniser. It is either true or
   false, and initially must be set to the false state. The insertion mode
   and the stack of open elements also affects tokenization.

   The output of the tokenization step is a series of zero or more of the
   following tokens: DOCTYPE, start tag, end tag, comment, character,
   end-of-file. DOCTYPE tokens have a name, a public identifier, a system
   identifier, and a force-quirks flag. When a DOCTYPE token is created,
   its name, public identifier, and system identifier must be marked as
   missing (which is a distinct state from the empty string), and the
   force-quirks flag must be set to off (its other state is on). Start and
   end tag tokens have a tag name, a self-closing flag, and a list of
   attributes, each of which has a name and a value. When a start or end
   tag token is created, its self-closing flag must be unset (its other
   state is that it be set), and its attributes list must be empty.
   Comment and character tokens have data.

   When a token is emitted, it must immediately be handled by the tree
   construction stage. The tree construction stage can affect the state of
   the content model flag, and can insert additional characters into the
   stream. (For example, the script element can result in scripts
   executing and using the dynamic markup insertion APIs to insert
   characters into the stream being tokenised.)

   When a start tag token is emitted with its self-closing flag set, if
   the flag is not acknowledged when it is processed by the tree
   construction stage, that is a parse error.

   When an end tag token is emitted, the content model flag must be
   switched to the PCDATA state.

   When an end tag token is emitted with attributes, that is a parse
   error.

   When an end tag token is emitted with its self-closing flag set, that
   is a parse error.

   Before each step of the tokeniser, the user agent must first check the
   parser pause flag. If it is true, then the tokeniser must abort the
   processing of any nested invocations of the tokeniser, yielding control
   back to the caller. If it is false, then the user agent may then check
   to see if either one of the scripts in the list of scripts that will
   execute as soon as possible or the first script in the list of scripts
   that will execute asynchronously, has completed loading. If one has,
   then it must be executed and removed from its list.

   The tokeniser state machine consists of the states defined in the
   following subsections.

      8.2.4.1 Data state

   Consume the next input character:

   U+0026 AMPERSAND (&)
          When the content model flag is set to one of the PCDATA or
          RCDATA states and the escape flag is false: switch to the
          character reference data state.
          Otherwise: treat it as per the "anything else" entry below.

   U+002D HYPHEN-MINUS (-)
          If the content model flag is set to either the RCDATA state or
          the CDATA state, and the escape flag is false, and there are at
          least three characters before this one in the input stream, and
          the last four characters in the input stream, including this
          one, are U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+002D
          HYPHEN-MINUS, and U+002D HYPHEN-MINUS ("<!--"), then set the
          escape flag to true.

          In any case, emit the input character as a character token. Stay
          in the data state.

   U+003C LESS-THAN SIGN (<)
          When the content model flag is set to the PCDATA state: switch
          to the tag open state.
          When the content model flag is set to either the RCDATA state or
          the CDATA state, and the escape flag is false: switch to the tag
          open state.
          Otherwise: treat it as per the "anything else" entry below.

   U+003E GREATER-THAN SIGN (>)
          If the content model flag is set to either the RCDATA state or
          the CDATA state, and the escape flag is true, and the last three
          characters in the input stream including this one are U+002D
          HYPHEN-MINUS, U+002D HYPHEN-MINUS, U+003E GREATER-THAN SIGN
          ("-->"), set the escape flag to false.

          In any case, emit the input character as a character token. Stay
          in the data state.

   EOF
          Emit an end-of-file token.

   Anything else
          Emit the input character as a character token. Stay in the data
          state.

      8.2.4.2 Character reference data state

   (This cannot happen if the content model flag is set to the CDATA
   state.)

   Attempt to consume a character reference, with no additional allowed
   character.

   If nothing is returned, emit a U+0026 AMPERSAND character token.

   Otherwise, emit the character token that was returned.

   Finally, switch to the data state.

      8.2.4.3 Tag open state

   The behavior of this state depends on the content model flag.

   If the content model flag is set to the RCDATA or CDATA states
          Consume the next input character. If it is a U+002F SOLIDUS (/)
          character, switch to the close tag open state. Otherwise, emit a
          U+003C LESS-THAN SIGN character token and reconsume the current
          input character in the data state.

   If the content model flag is set to the PCDATA state
          Consume the next input character:

        U+0021 EXCLAMATION MARK (!)
                Switch to the markup declaration open state.

        U+002F SOLIDUS (/)
                Switch to the close tag open state.

        U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL
                LETTER Z
                Create a new start tag token, set its tag name to the
                lowercase version of the input character (add 0x0020 to
                the character's code point), then switch to the tag name
                state. (Don't emit the token yet; further details will be
                filled in before it is emitted.)

        U+0061 LATIN SMALL LETTER A through to U+007A LATIN SMALL LETTER Z
                Create a new start tag token, set its tag name to the
                input character, then switch to the tag name state. (Don't
                emit the token yet; further details will be filled in
                before it is emitted.)

        U+003E GREATER-THAN SIGN (>)
                Parse error. Emit a U+003C LESS-THAN SIGN character token
                and a U+003E GREATER-THAN SIGN character token. Switch to
                the data state.

        U+003F QUESTION MARK (?)
                Parse error. Switch to the bogus comment state.

        Anything else
                Parse error. Emit a U+003C LESS-THAN SIGN character token
                and reconsume the current input character in the data
                state.

      8.2.4.4 Close tag open state

   If the content model flag is set to the RCDATA or CDATA states but no
   start tag token has ever been emitted by this instance of the tokeniser
   (fragment case), or, if the content model flag is set to the RCDATA or
   CDATA states and the next few characters do not match the tag name of
   the last start tag token emitted (compared in an ASCII case-insensitive
   manner), or if they do but they are not immediately followed by one of
   the following characters:
     * U+0009 CHARACTER TABULATION
     * U+000A LINE FEED (LF)
     * U+000C FORM FEED (FF)
     * U+0020 SPACE
     * U+003E GREATER-THAN SIGN (>)
     * U+002F SOLIDUS (/)
     * EOF

   ...then emit a U+003C LESS-THAN SIGN character token, a U+002F SOLIDUS
   character token, and switch to the data state to process the next input
   character.

   Otherwise, if the content model flag is set to the PCDATA state, or if
   the next few characters do match that tag name, consume the next input
   character:

   U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
          Create a new end tag token, set its tag name to the lowercase
          version of the input character (add 0x0020 to the character's
          code point), then switch to the tag name state. (Don't emit the
          token yet; further details will be filled in before it is
          emitted.)

   U+0061 LATIN SMALL LETTER A through to U+007A LATIN SMALL LETTER Z
          Create a new end tag token, set its tag name to the input
          character, then switch to the tag name state. (Don't emit the
          token yet; further details will be filled in before it is
          emitted.)

   U+003E GREATER-THAN SIGN (>)
          Parse error. Switch to the data state.

   EOF
          Parse error. Emit a U+003C LESS-THAN SIGN character token and a
          U+002F SOLIDUS character token. Reconsume the EOF character in
          the data state.

   Anything else
          Parse error. Switch to the bogus comment state.

      8.2.4.5 Tag name state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Switch to the before attribute name state.

   U+002F SOLIDUS (/)
          Switch to the self-closing start tag state.

   U+003E GREATER-THAN SIGN (>)
          Emit the current tag token. Switch to the data state.

   U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
          Append the lowercase version of the current input character (add
          0x0020 to the character's code point) to the current tag token's
          tag name. Stay in the tag name state.

   EOF
          Parse error. Emit the current tag token. Reconsume the EOF
          character in the data state.

   Anything else
          Append the current input character to the current tag token's
          tag name. Stay in the tag name state.

      8.2.4.6 Before attribute name state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Stay in the before attribute name state.

   U+002F SOLIDUS (/)
          Switch to the self-closing start tag state.

   U+003E GREATER-THAN SIGN (>)
          Emit the current tag token. Switch to the data state.

   U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
          Start a new attribute in the current tag token. Set that
          attribute's name to the lowercase version of the current input
          character (add 0x0020 to the character's code point), and its
          value to the empty string. Switch to the attribute name state.

   U+0022 QUOTATION MARK (")
   U+0027 APOSTROPHE (')
   U+003D EQUALS SIGN (=)
          Parse error. Treat it as per the "anything else" entry below.

   EOF
          Parse error. Emit the current tag token. Reconsume the EOF
          character in the data state.

   Anything else
          Start a new attribute in the current tag token. Set that
          attribute's name to the current input character, and its value
          to the empty string. Switch to the attribute name state.

      8.2.4.7 Attribute name state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Switch to the after attribute name state.

   U+002F SOLIDUS (/)
          Switch to the self-closing start tag state.

   U+003D EQUALS SIGN (=)
          Switch to the before attribute value state.

   U+003E GREATER-THAN SIGN (>)
          Emit the current tag token. Switch to the data state.

   U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
          Append the lowercase version of the current input character (add
          0x0020 to the character's code point) to the current attribute's
          name. Stay in the attribute name state.

   U+0022 QUOTATION MARK (")
   U+0027 APOSTROPHE (')
          Parse error. Treat it as per the "anything else" entry below.

   EOF
          Parse error. Emit the current tag token. Reconsume the EOF
          character in the data state.

   Anything else
          Append the current input character to the current attribute's
          name. Stay in the attribute name state.

   When the user agent leaves the attribute name state (and before
   emitting the tag token, if appropriate), the complete attribute's name
   must be compared to the other attributes on the same token; if there is
   already an attribute on the token with the exact same name, then this
   is a parse error and the new attribute must be dropped, along with the
   value that gets associated with it (if any).

      8.2.4.8 After attribute name state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Stay in the after attribute name state.

   U+002F SOLIDUS (/)
          Switch to the self-closing start tag state.

   U+003D EQUALS SIGN (=)
          Switch to the before attribute value state.

   U+003E GREATER-THAN SIGN (>)
          Emit the current tag token. Switch to the data state.

   U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
          Start a new attribute in the current tag token. Set that
          attribute's name to the lowercase version of the current input
          character (add 0x0020 to the character's code point), and its
          value to the empty string. Switch to the attribute name state.

   U+0022 QUOTATION MARK (")
   U+0027 APOSTROPHE (')
          Parse error. Treat it as per the "anything else" entry below.

   EOF
          Parse error. Emit the current tag token. Reconsume the EOF
          character in the data state.

   Anything else
          Start a new attribute in the current tag token. Set that
          attribute's name to the current input character, and its value
          to the empty string. Switch to the attribute name state.

      8.2.4.9 Before attribute value state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Stay in the before attribute value state.

   U+0022 QUOTATION MARK (")
          Switch to the attribute value (double-quoted) state.

   U+0026 AMPERSAND (&)
          Switch to the attribute value (unquoted) state and reconsume
          this input character.

   U+0027 APOSTROPHE (')
          Switch to the attribute value (single-quoted) state.

   U+003E GREATER-THAN SIGN (>)
          Parse error. Emit the current tag token. Switch to the data
          state.

   U+003D EQUALS SIGN (=)
          Parse error. Treat it as per the "anything else" entry below.

   EOF
          Parse error. Emit the current tag token. Reconsume the character
          in the data state.

   Anything else
          Append the current input character to the current attribute's
          value. Switch to the attribute value (unquoted) state.

      8.2.4.10 Attribute value (double-quoted) state

   Consume the next input character:

   U+0022 QUOTATION MARK (")
          Switch to the after attribute value (quoted) state.

   U+0026 AMPERSAND (&)
          Switch to the character reference in attribute value state, with
          the additional allowed character being U+0022 QUOTATION MARK
          (").

   EOF
          Parse error. Emit the current tag token. Reconsume the character
          in the data state.

   Anything else
          Append the current input character to the current attribute's
          value. Stay in the attribute value (double-quoted) state.

      8.2.4.11 Attribute value (single-quoted) state

   Consume the next input character:

   U+0027 APOSTROPHE (')
          Switch to the after attribute value (quoted) state.

   U+0026 AMPERSAND (&)
          Switch to the character reference in attribute value state, with
          the additional allowed character being U+0027 APOSTROPHE (').

   EOF
          Parse error. Emit the current tag token. Reconsume the character
          in the data state.

   Anything else
          Append the current input character to the current attribute's
          value. Stay in the attribute value (single-quoted) state.

      8.2.4.12 Attribute value (unquoted) state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Switch to the before attribute name state.

   U+0026 AMPERSAND (&)
          Switch to the character reference in attribute value state, with
          no additional allowed character.

   U+003E GREATER-THAN SIGN (>)
          Emit the current tag token. Switch to the data state.

   U+0022 QUOTATION MARK (")
   U+0027 APOSTROPHE (')
   U+003D EQUALS SIGN (=)
          Parse error. Treat it as per the "anything else" entry below.

   EOF
          Parse error. Emit the current tag token. Reconsume the character
          in the data state.

   Anything else
          Append the current input character to the current attribute's
          value. Stay in the attribute value (unquoted) state.

      8.2.4.13 Character reference in attribute value state

   Attempt to consume a character reference.

   If nothing is returned, append a U+0026 AMPERSAND character to the
   current attribute's value.

   Otherwise, append the returned character token to the current
   attribute's value.

   Finally, switch back to the attribute value state that you were in when
   were switched into this state.

      8.2.4.14 After attribute value (quoted) state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Switch to the before attribute name state.

   U+002F SOLIDUS (/)
          Switch to the self-closing start tag state.

   U+003E GREATER-THAN SIGN (>)
          Emit the current tag token. Switch to the data state.

   EOF
          Parse error. Emit the current tag token. Reconsume the EOF
          character in the data state.

   Anything else
          Parse error. Reconsume the character in the before attribute
          name state.

      8.2.4.15 Self-closing start tag state

   Consume the next input character:

   U+003E GREATER-THAN SIGN (>)
          Set the self-closing flag of the current tag token. Emit the
          current tag token. Switch to the data state.

   EOF
          Parse error. Emit the current tag token. Reconsume the EOF
          character in the data state.

   Anything else
          Parse error. Reconsume the character in the before attribute
          name state.

      8.2.4.16 Bogus comment state

   (This can only happen if the content model flag is set to the PCDATA
   state.)

   Consume every character up to and including the first U+003E
   GREATER-THAN SIGN character (>) or the end of the file (EOF), whichever
   comes first. Emit a comment token whose data is the concatenation of
   all the characters starting from and including the character that
   caused the state machine to switch into the bogus comment state, up to
   and including the character immediately before the last consumed
   character (i.e. up to the character just before the U+003E or EOF
   character). (If the comment was started by the end of the file (EOF),
   the token is empty.)

   Switch to the data state.

   If the end of the file was reached, reconsume the EOF character.

      8.2.4.17 Markup declaration open state

   (This can only happen if the content model flag is set to the PCDATA
   state.)

   If the next two characters are both U+002D HYPHEN-MINUS (-) characters,
   consume those two characters, create a comment token whose data is the
   empty string, and switch to the comment start state.

   Otherwise, if the next seven characters are an ASCII case-insensitive
   match for the word "DOCTYPE", then consume those characters and switch
   to the DOCTYPE state.

   Otherwise, if the insertion mode is "in foreign content" and the
   current node is not an element in the HTML namespace and the next seven
   characters are an ASCII case-sensitive match for the string "[CDATA["
   (the five uppercase letters "CDATA" with a U+005B LEFT SQUARE BRACKET
   character before and after), then consume those characters and switch
   to the CDATA section state (which is unrelated to the content model
   flag's CDATA state).

   Otherwise, this is a parse error. Switch to the bogus comment state.
   The next character that is consumed, if any, is the first character
   that will be in the comment.

      8.2.4.18 Comment start state

   Consume the next input character:

   U+002D HYPHEN-MINUS (-)
          Switch to the comment start dash state.

   U+003E GREATER-THAN SIGN (>)
          Parse error. Emit the comment token. Switch to the data state.

   EOF
          Parse error. Emit the comment token. Reconsume the EOF character
          in the data state.

   Anything else
          Append the input character to the comment token's data. Switch
          to the comment state.

      8.2.4.19 Comment start dash state

   Consume the next input character:

   U+002D HYPHEN-MINUS (-)
          Switch to the comment end state

   U+003E GREATER-THAN SIGN (>)
          Parse error. Emit the comment token. Switch to the data state.

   EOF
          Parse error. Emit the comment token. Reconsume the EOF character
          in the data state.

   Anything else
          Append a U+002D HYPHEN-MINUS (-) character and the input
          character to the comment token's data. Switch to the comment
          state.

      8.2.4.20 Comment state

   Consume the next input character:

   U+002D HYPHEN-MINUS (-)
          Switch to the comment end dash state

   EOF
          Parse error. Emit the comment token. Reconsume the EOF character
          in the data state.

   Anything else
          Append the input character to the comment token's data. Stay in
          the comment state.

      8.2.4.21 Comment end dash state

   Consume the next input character:

   U+002D HYPHEN-MINUS (-)
          Switch to the comment end state

   EOF
          Parse error. Emit the comment token. Reconsume the EOF character
          in the data state.

   Anything else
          Append a U+002D HYPHEN-MINUS (-) character and the input
          character to the comment token's data. Switch to the comment
          state.

      8.2.4.22 Comment end state

   Consume the next input character:

   U+003E GREATER-THAN SIGN (>)
          Emit the comment token. Switch to the data state.

   U+002D HYPHEN-MINUS (-)
          Parse error. Append a U+002D HYPHEN-MINUS (-) character to the
          comment token's data. Stay in the comment end state.

   EOF
          Parse error. Emit the comment token. Reconsume the EOF character
          in the data state.

   Anything else
          Parse error. Append two U+002D HYPHEN-MINUS (-) characters and
          the input character to the comment token's data. Switch to the
          comment state.

      8.2.4.23 DOCTYPE state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Switch to the before DOCTYPE name state.

   Anything else
          Parse error. Reconsume the current character in the before
          DOCTYPE name state.

      8.2.4.24 Before DOCTYPE name state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Stay in the before DOCTYPE name state.

   U+003E GREATER-THAN SIGN (>)
          Parse error. Create a new DOCTYPE token. Set its force-quirks
          flag to on. Emit the token. Switch to the data state.

   U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
          Create a new DOCTYPE token. Set the token's name to the
          lowercase version of the input character (add 0x0020 to the
          character's code point). Switch to the DOCTYPE name state.

   EOF
          Parse error. Create a new DOCTYPE token. Set its force-quirks
          flag to on. Emit the token. Reconsume the EOF character in the
          data state.

   Anything else
          Create a new DOCTYPE token. Set the token's name to the current
          input character. Switch to the DOCTYPE name state.

      8.2.4.25 DOCTYPE name state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Switch to the after DOCTYPE name state.

   U+003E GREATER-THAN SIGN (>)
          Emit the current DOCTYPE token. Switch to the data state.

   U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
          Append the lowercase version of the input character (add 0x0020
          to the character's code point) to the current DOCTYPE token's
          name. Stay in the DOCTYPE name state.

   EOF
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          Append the current input character to the current DOCTYPE
          token's name. Stay in the DOCTYPE name state.

      8.2.4.26 After DOCTYPE name state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Stay in the after DOCTYPE name state.

   U+003E GREATER-THAN SIGN (>)
          Emit the current DOCTYPE token. Switch to the data state.

   EOF
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          If the six characters starting from the current input character
          are an ASCII case-insensitive match for the word "PUBLIC", then
          consume those characters and switch to the before DOCTYPE public
          identifier state.

          Otherwise, if the six characters starting from the current input
          character are an ASCII case-insensitive match for the word
          "SYSTEM", then consume those characters and switch to the before
          DOCTYPE system identifier state.

          Otherwise, this is the parse error. Set the DOCTYPE token's
          force-quirks flag to on. Switch to the bogus DOCTYPE state.

      8.2.4.27 Before DOCTYPE public identifier state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Stay in the before DOCTYPE public identifier state.

   U+0022 QUOTATION MARK (")
          Set the DOCTYPE token's public identifier to the empty string
          (not missing), then switch to the DOCTYPE public identifier
          (double-quoted) state.

   U+0027 APOSTROPHE (')
          Set the DOCTYPE token's public identifier to the empty string
          (not missing), then switch to the DOCTYPE public identifier
          (single-quoted) state.

   U+003E GREATER-THAN SIGN (>)
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Switch to the data state.

   EOF
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Switch to the bogus DOCTYPE state.

      8.2.4.28 DOCTYPE public identifier (double-quoted) state

   Consume the next input character:

   U+0022 QUOTATION MARK (")
          Switch to the after DOCTYPE public identifier state.

   U+003E GREATER-THAN SIGN (>)
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Switch to the data state.

   EOF
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          Append the current input character to the current DOCTYPE
          token's public identifier. Stay in the DOCTYPE public identifier
          (double-quoted) state.

      8.2.4.29 DOCTYPE public identifier (single-quoted) state

   Consume the next input character:

   U+0027 APOSTROPHE (')
          Switch to the after DOCTYPE public identifier state.

   U+003E GREATER-THAN SIGN (>)
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Switch to the data state.

   EOF
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          Append the current input character to the current DOCTYPE
          token's public identifier. Stay in the DOCTYPE public identifier
          (single-quoted) state.

      8.2.4.30 After DOCTYPE public identifier state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Stay in the after DOCTYPE public identifier state.

   U+0022 QUOTATION MARK (")
          Set the DOCTYPE token's system identifier to the empty string
          (not missing), then switch to the DOCTYPE system identifier
          (double-quoted) state.

   U+0027 APOSTROPHE (')
          Set the DOCTYPE token's system identifier to the empty string
          (not missing), then switch to the DOCTYPE system identifier
          (single-quoted) state.

   U+003E GREATER-THAN SIGN (>)
          Emit the current DOCTYPE token. Switch to the data state.

   EOF
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Switch to the bogus DOCTYPE state.

      8.2.4.31 Before DOCTYPE system identifier state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Stay in the before DOCTYPE system identifier state.

   U+0022 QUOTATION MARK (")
          Set the DOCTYPE token's system identifier to the empty string
          (not missing), then switch to the DOCTYPE system identifier
          (double-quoted) state.

   U+0027 APOSTROPHE (')
          Set the DOCTYPE token's system identifier to the empty string
          (not missing), then switch to the DOCTYPE system identifier
          (single-quoted) state.

   U+003E GREATER-THAN SIGN (>)
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Switch to the data state.

   EOF
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Switch to the bogus DOCTYPE state.

      8.2.4.32 DOCTYPE system identifier (double-quoted) state

   Consume the next input character:

   U+0022 QUOTATION MARK (")
          Switch to the after DOCTYPE system identifier state.

   U+003E GREATER-THAN SIGN (>)
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Switch to the data state.

   EOF
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          Append the current input character to the current DOCTYPE
          token's system identifier. Stay in the DOCTYPE system identifier
          (double-quoted) state.

      8.2.4.33 DOCTYPE system identifier (single-quoted) state

   Consume the next input character:

   U+0027 APOSTROPHE (')
          Switch to the after DOCTYPE system identifier state.

   U+003E GREATER-THAN SIGN (>)
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Switch to the data state.

   EOF
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          Append the current input character to the current DOCTYPE
          token's system identifier. Stay in the DOCTYPE system identifier
          (single-quoted) state.

      8.2.4.34 After DOCTYPE system identifier state

   Consume the next input character:

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
          Stay in the after DOCTYPE system identifier state.

   U+003E GREATER-THAN SIGN (>)
          Emit the current DOCTYPE token. Switch to the data state.

   EOF
          Parse error. Set the DOCTYPE token's force-quirks flag to on.
          Emit that DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          Parse error. Switch to the bogus DOCTYPE state. (This does not
          set the DOCTYPE token's force-quirks flag to on.)

      8.2.4.35 Bogus DOCTYPE state

   Consume the next input character:

   U+003E GREATER-THAN SIGN (>)
          Emit the DOCTYPE token. Switch to the data state.

   EOF
          Emit the DOCTYPE token. Reconsume the EOF character in the data
          state.

   Anything else
          Stay in the bogus DOCTYPE state.

      8.2.4.36 CDATA section state

   (This can only happen if the content model flag is set to the PCDATA
   state, and is unrelated to the content model flag's CDATA state.)

   Consume every character up to the next occurrence of the three
   character sequence U+005D RIGHT SQUARE BRACKET U+005D RIGHT SQUARE
   BRACKET U+003E GREATER-THAN SIGN (]]>), or the end of the file (EOF),
   whichever comes first. Emit a series of character tokens consisting of
   all the characters consumed except the matching three character
   sequence at the end (if one was found before the end of the file).

   Switch to the data state.

   If the end of the file was reached, reconsume the EOF character.

      8.2.4.37 Tokenizing character references

   This section defines how to consume a character reference. This
   definition is used when parsing character references in text and in
   attributes.

   The behavior depends on the identity of the next character (the one
   immediately after the U+0026 AMPERSAND character):

   U+0009 CHARACTER TABULATION
   U+000A LINE FEED (LF)
   U+000C FORM FEED (FF)
   U+0020 SPACE
   U+003C LESS-THAN SIGN
   U+0026 AMPERSAND
   EOF
   The additional allowed character, if there is one
          Not a character reference. No characters are consumed, and
          nothing is returned. (This is not an error, either.)

   U+0023 NUMBER SIGN (#)
          Consume the U+0023 NUMBER SIGN.

          The behavior further depends on the character after the U+0023
          NUMBER SIGN:

        U+0078 LATIN SMALL LETTER X
        U+0058 LATIN CAPITAL LETTER X
                Consume the X.

                Follow the steps below, but using the range of characters
                U+0030 DIGIT ZERO through to U+0039 DIGIT NINE, U+0061
                LATIN SMALL LETTER A through to U+0066 LATIN SMALL LETTER
                F, and U+0041 LATIN CAPITAL LETTER A, through to U+0046
                LATIN CAPITAL LETTER F (in other words, 0-9, A-F, a-f).

                When it comes to interpreting the number, interpret it as
                a hexadecimal number.

        Anything else
                Follow the steps below, but using the range of characters
                U+0030 DIGIT ZERO through to U+0039 DIGIT NINE (i.e. just
                0-9).

                When it comes to interpreting the number, interpret it as
                a decimal number.

          Consume as many characters as match the range of characters
          given above.

          If no characters match the range, then don't consume any
          characters (and unconsume the U+0023 NUMBER SIGN character and,
          if appropriate, the X character). This is a parse error; nothing
          is returned.

          Otherwise, if the next character is a U+003B SEMICOLON, consume
          that too. If it isn't, there is a parse error.

          If one or more characters match the range, then take them all
          and interpret the string of characters as a number (either
          hexadecimal or decimal as appropriate).

          If that number is one of the numbers in the first column of the
          following table, then this is a parse error. Find the row with
          that number in the first column, and return a character token
          for the Unicode character given in the second column of that
          row.

          Number                   Unicode character
          0x0D   U+000A LINE FEED (LF)
          0x80   U+20AC EURO SIGN ('€')
          0x81   U+FFFD REPLACEMENT CHARACTER
          0x82   U+201A SINGLE LOW-9 QUOTATION MARK ('‚')
          0x83   U+0192 LATIN SMALL LETTER F WITH HOOK ('ƒ')
          0x84   U+201E DOUBLE LOW-9 QUOTATION MARK ('„')
          0x85   U+2026 HORIZONTAL ELLIPSIS ('…')
          0x86   U+2020 DAGGER ('†')
          0x87   U+2021 DOUBLE DAGGER ('‡')
          0x88   U+02C6 MODIFIER LETTER CIRCUMFLEX ACCENT ('ˆ')
          0x89   U+2030 PER MILLE SIGN ('‰')
          0x8A   U+0160 LATIN CAPITAL LETTER S WITH CARON ('Š')
          0x8B   U+2039 SINGLE LEFT-POINTING ANGLE QUOTATION MARK ('‹')
          0x8C   U+0152 LATIN CAPITAL LIGATURE OE ('Œ')
          0x8D   U+FFFD REPLACEMENT CHARACTER
          0x8E   U+017D LATIN CAPITAL LETTER Z WITH CARON ('Ž')
          0x8F   U+FFFD REPLACEMENT CHARACTER
          0x90   U+FFFD REPLACEMENT CHARACTER
          0x91   U+2018 LEFT SINGLE QUOTATION MARK ('‘')
          0x92   U+2019 RIGHT SINGLE QUOTATION MARK ('’')
          0x93   U+201C LEFT DOUBLE QUOTATION MARK ('“')
          0x94   U+201D RIGHT DOUBLE QUOTATION MARK ('”')
          0x95   U+2022 BULLET ('•')
          0x96   U+2013 EN DASH ('–')
          0x97   U+2014 EM DASH ('—')
          0x98   U+02DC SMALL TILDE ('˜')
          0x99   U+2122 TRADE MARK SIGN ('™')
          0x9A   U+0161 LATIN SMALL LETTER S WITH CARON ('š')
          0x9B   U+203A SINGLE RIGHT-POINTING ANGLE QUOTATION MARK ('›')
          0x9C   U+0153 LATIN SMALL LIGATURE OE ('œ')
          0x9D   U+FFFD REPLACEMENT CHARACTER
          0x9E   U+017E LATIN SMALL LETTER Z WITH CARON ('ž')
          0x9F   U+0178 LATIN CAPITAL LETTER Y WITH DIAERESIS ('Ÿ')

          Otherwise, if the number is in the range 0x0000 to 0x0008,
          0x000E to 0x001F, 0x007F to 0x009F, 0xD800 to 0xDFFF, 0xFDD0 to
          0xFDEF, or is one of 0x000B, 0xFFFE, 0xFFFF, 0x1FFFE, 0x1FFFF,
          0x2FFFE, 0x2FFFF, 0x3FFFE, 0x3FFFF, 0x4FFFE, 0x4FFFF, 0x5FFFE,
          0x5FFFF, 0x6FFFE, 0x6FFFF, 0x7FFFE, 0x7FFFF, 0x8FFFE, 0x8FFFF,
          0x9FFFE, 0x9FFFF, 0xAFFFE, 0xAFFFF, 0xBFFFE, 0xBFFFF, 0xCFFFE,
          0xCFFFF, 0xDFFFE, 0xDFFFF, 0xEFFFE, 0xEFFFF, 0xFFFFE, 0xFFFFF,
          0x10FFFE, or 0x10FFFF, or is higher than 0x10FFFF, then this is
          a parse error; return a character token for the U+FFFD
          REPLACEMENT CHARACTER character instead.

          Otherwise, return a character token for the Unicode character
          whose code point is that number.

   Anything else
          Consume the maximum number of characters possible, with the
          consumed characters matching one of the identifiers in the first
          column of the named character references table (in a
          case-sensitive manner).

          If no match can be made, then this is a parse error. No
          characters are consumed, and nothing is returned.

          If the last character matched is not a U+003B SEMICOLON (;),
          there is a parse error.

          If the character reference is being consumed as part of an
          attribute, and the last character matched is not a U+003B
          SEMICOLON (;), and the next character is in the range U+0030
          DIGIT ZERO to U+0039 DIGIT NINE, U+0041 LATIN CAPITAL LETTER A
          to U+005A LATIN CAPITAL LETTER Z, or U+0061 LATIN SMALL LETTER A
          to U+007A LATIN SMALL LETTER Z, then, for historical reasons,
          all the characters that were matched after the U+0026 AMPERSAND
          (&) must be unconsumed, and nothing is returned.

          Otherwise, return a character token for the character
          corresponding to the character reference name (as given by the
          second column of the named character references table).

          If the markup contains I'm &notit; I tell you, the character
          reference is parsed as "not", as in, I'm ¬it; I tell you. But if
          the markup was I'm &notin; I tell you, the character reference
          would be parsed as "notin;", resulting in I'm ∉ I tell you.