/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm.src
Suika

Diff of /markup/html/whatpm/Whatpm/HTML.pm.src

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.50 by wakaba, Sat Jul 21 10:59:40 2007 UTC revision 1.61 by wakaba, Sun Nov 4 04:15:06 2007 UTC
# Line 147  sub new ($) { Line 147  sub new ($) {
147    $self->{parse_error} = sub {    $self->{parse_error} = sub {
148      #      #
149    };    };
150      $self->{application_cache_selection} = sub {
151        #
152      };
153    return $self;    return $self;
154  } # new  } # new
155    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 162  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
162  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
163  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
164    
165    sub DATA_STATE () { 0 }
166    sub ENTITY_DATA_STATE () { 1 }
167    sub TAG_OPEN_STATE () { 2 }
168    sub CLOSE_TAG_OPEN_STATE () { 3 }
169    sub TAG_NAME_STATE () { 4 }
170    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
171    sub ATTRIBUTE_NAME_STATE () { 6 }
172    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
173    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
174    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
175    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
176    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
177    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
178    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
179    sub COMMENT_START_STATE () { 14 }
180    sub COMMENT_START_DASH_STATE () { 15 }
181    sub COMMENT_STATE () { 16 }
182    sub COMMENT_END_STATE () { 17 }
183    sub COMMENT_END_DASH_STATE () { 18 }
184    sub BOGUS_COMMENT_STATE () { 19 }
185    sub DOCTYPE_STATE () { 20 }
186    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
187    sub DOCTYPE_NAME_STATE () { 22 }
188    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
189    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
190    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
191    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
192    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
193    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
194    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
195    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
196    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
197    sub BOGUS_DOCTYPE_STATE () { 32 }
198    
199    sub DOCTYPE_TOKEN () { 1 }
200    sub COMMENT_TOKEN () { 2 }
201    sub START_TAG_TOKEN () { 3 }
202    sub END_TAG_TOKEN () { 4 }
203    sub END_OF_FILE_TOKEN () { 5 }
204    sub CHARACTER_TOKEN () { 6 }
205    
206    sub AFTER_HTML_IMS () { 0b100 }
207    sub HEAD_IMS ()       { 0b1000 }
208    sub BODY_IMS ()       { 0b10000 }
209    sub BODY_TABLE_IMS () { 0b100000 }
210    sub TABLE_IMS ()      { 0b1000000 }
211    sub ROW_IMS ()        { 0b10000000 }
212    sub BODY_AFTER_IMS () { 0b100000000 }
213    sub FRAME_IMS ()      { 0b1000000000 }
214    
215    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
216    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
217    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
218    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
219    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
220    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
221    sub IN_BODY_IM () { BODY_IMS }
222    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
223    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
224    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
225    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
226    sub IN_TABLE_IM () { TABLE_IMS }
227    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
228    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
229    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
230    sub IN_SELECT_IM () { 0b01 }
231    sub IN_COLUMN_GROUP_IM () { 0b10 }
232    
233  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
234    
235  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
236    my $self = shift;    my $self = shift;
237    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
238    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
239    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
240    undef $self->{current_attribute};    undef $self->{current_attribute};
# Line 177  sub _initialize_tokenizer ($) { Line 248  sub _initialize_tokenizer ($) {
248  } # _initialize_tokenizer  } # _initialize_tokenizer
249    
250  ## A token has:  ## A token has:
251  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
252  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
253  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
254  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
255  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
256  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
257  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
258  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
259    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
260    
261  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
262    
# Line 194  sub _initialize_tokenizer ($) { Line 266  sub _initialize_tokenizer ($) {
266  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
267  ## and removed from the list.  ## and removed from the list.
268    
269    ## NOTE: HTML5 "Writing HTML documents" section, applied to
270    ## documents and not to user agents and conformance checkers,
271    ## contains some requirements that are not detected by the
272    ## parsing algorithm:
273    ## - Some requirements on character encoding declarations. ## TODO
274    ## - "Elements MUST NOT contain content that their content model disallows."
275    ##   ... Some are parse error, some are not (will be reported by c.c.).
276    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
277    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
278    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
279    
280    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
281    ## be detected by the HTML5 parsing algorithm:
282    ## - Text,
283    
284  sub _get_next_token ($) {  sub _get_next_token ($) {
285    my $self = shift;    my $self = shift;
286    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 288  sub _get_next_token ($) {
288    }    }
289    
290    A: {    A: {
291      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
292        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
293          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
294            $self->{state} = 'entity data';            $self->{state} = ENTITY_DATA_STATE;
295            !!!next-input-character;            !!!next-input-character;
296            redo A;            redo A;
297          } else {          } else {
# Line 226  sub _get_next_token ($) { Line 313  sub _get_next_token ($) {
313          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
314              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
315               not $self->{escape})) {               not $self->{escape})) {
316            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
317            !!!next-input-character;            !!!next-input-character;
318            redo A;            redo A;
319          } else {          } else {
# Line 243  sub _get_next_token ($) { Line 330  sub _get_next_token ($) {
330                    
331          #          #
332        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
333          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
334          last A; ## TODO: ok?          last A; ## TODO: ok?
335        }        }
336        # Anything else        # Anything else
337        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
338                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
339        ## Stay in the data state        ## Stay in the data state
340        !!!next-input-character;        !!!next-input-character;
# Line 255  sub _get_next_token ($) { Line 342  sub _get_next_token ($) {
342        !!!emit ($token);        !!!emit ($token);
343    
344        redo A;        redo A;
345      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
346        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
347                
348        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
349    
350        $self->{state} = 'data';        $self->{state} = DATA_STATE;
351        # next-input-character is already done        # next-input-character is already done
352    
353        unless (defined $token) {        unless (defined $token) {
354          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
355        } else {        } else {
356          !!!emit ($token);          !!!emit ($token);
357        }        }
358    
359        redo A;        redo A;
360      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
361        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
362          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
363            !!!next-input-character;            !!!next-input-character;
364            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
365            redo A;            redo A;
366          } else {          } else {
367            ## reconsume            ## reconsume
368            $self->{state} = 'data';            $self->{state} = DATA_STATE;
369    
370            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
371    
372            redo A;            redo A;
373          }          }
374        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
375          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
376            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
377            !!!next-input-character;            !!!next-input-character;
378            redo A;            redo A;
379          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
380            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
381            !!!next-input-character;            !!!next-input-character;
382            redo A;            redo A;
383          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
384                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
385            $self->{current_token}            $self->{current_token}
386              = {type => 'start tag',              = {type => START_TAG_TOKEN,
387                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
388            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
389            !!!next-input-character;            !!!next-input-character;
390            redo A;            redo A;
391          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
392                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
393            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
394                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
395            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
396            !!!next-input-character;            !!!next-input-character;
397            redo A;            redo A;
398          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
399            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
400            $self->{state} = 'data';            $self->{state} = DATA_STATE;
401            !!!next-input-character;            !!!next-input-character;
402    
403            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
404    
405            redo A;            redo A;
406          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
407            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
408            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
409            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
410            redo A;            redo A;
411          } else {          } else {
412            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
413            $self->{state} = 'data';            $self->{state} = DATA_STATE;
414            ## reconsume            ## reconsume
415    
416            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
417    
418            redo A;            redo A;
419          }          }
420        } else {        } else {
421          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
422        }        }
423      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
424        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
425          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
426            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
# Line 348  sub _get_next_token ($) { Line 435  sub _get_next_token ($) {
435              } else {              } else {
436                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
437                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
438                $self->{state} = 'data';                $self->{state} = DATA_STATE;
439    
440                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
441        
442                redo A;                redo A;
443              }              }
# Line 367  sub _get_next_token ($) { Line 454  sub _get_next_token ($) {
454                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
455              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
456              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
457              $self->{state} = 'data';              $self->{state} = DATA_STATE;
458              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
459              redo A;              redo A;
460            } else {            } else {
461              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 378  sub _get_next_token ($) { Line 465  sub _get_next_token ($) {
465          } else {          } else {
466            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
467            # next-input-character is already done            # next-input-character is already done
468            $self->{state} = 'data';            $self->{state} = DATA_STATE;
469            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
470            redo A;            redo A;
471          }          }
472        }        }
473                
474        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
475            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
476          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
477                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
478          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
479          !!!next-input-character;          !!!next-input-character;
480          redo A;          redo A;
481        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
482                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
483          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
484                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
485          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
486          !!!next-input-character;          !!!next-input-character;
487          redo A;          redo A;
488        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
489          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
490          $self->{state} = 'data';          $self->{state} = DATA_STATE;
491          !!!next-input-character;          !!!next-input-character;
492          redo A;          redo A;
493        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
494          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
495          $self->{state} = 'data';          $self->{state} = DATA_STATE;
496          # reconsume          # reconsume
497    
498          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
499    
500          redo A;          redo A;
501        } else {        } else {
502          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
503          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
504          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
505          redo A;          redo A;
506        }        }
507      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
508        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
509            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
510            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
511            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
512            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
513          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
514          !!!next-input-character;          !!!next-input-character;
515          redo A;          redo A;
516        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
517          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
518            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
519                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
520            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
521          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
522            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
523            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
524              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 439  sub _get_next_token ($) { Line 526  sub _get_next_token ($) {
526          } else {          } else {
527            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
528          }          }
529          $self->{state} = 'data';          $self->{state} = DATA_STATE;
530          !!!next-input-character;          !!!next-input-character;
531    
532          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 454  sub _get_next_token ($) { Line 541  sub _get_next_token ($) {
541          redo A;          redo A;
542        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
543          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
544          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
545            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
546                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
547            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
548          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
549            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
550            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
551              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 466  sub _get_next_token ($) { Line 553  sub _get_next_token ($) {
553          } else {          } else {
554            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
555          }          }
556          $self->{state} = 'data';          $self->{state} = DATA_STATE;
557          # reconsume          # reconsume
558    
559          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 475  sub _get_next_token ($) { Line 562  sub _get_next_token ($) {
562        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
563          !!!next-input-character;          !!!next-input-character;
564          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
565              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
566              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
567            # permitted slash            # permitted slash
568            #            #
569          } else {          } else {
570            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
571          }          }
572          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
573          # next-input-character is already done          # next-input-character is already done
574          redo A;          redo A;
575        } else {        } else {
# Line 492  sub _get_next_token ($) { Line 579  sub _get_next_token ($) {
579          !!!next-input-character;          !!!next-input-character;
580          redo A;          redo A;
581        }        }
582      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
583        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
584            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
585            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 502  sub _get_next_token ($) { Line 589  sub _get_next_token ($) {
589          !!!next-input-character;          !!!next-input-character;
590          redo A;          redo A;
591        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
592          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
593            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
594                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
595            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
596          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
597            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
598            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
599              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 514  sub _get_next_token ($) { Line 601  sub _get_next_token ($) {
601          } else {          } else {
602            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
603          }          }
604          $self->{state} = 'data';          $self->{state} = DATA_STATE;
605          !!!next-input-character;          !!!next-input-character;
606    
607          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 524  sub _get_next_token ($) { Line 611  sub _get_next_token ($) {
611                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
612          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
613                                value => ''};                                value => ''};
614          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
615          !!!next-input-character;          !!!next-input-character;
616          redo A;          redo A;
617        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
618          !!!next-input-character;          !!!next-input-character;
619          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
620              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
621              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
622            # permitted slash            # permitted slash
623            #            #
# Line 542  sub _get_next_token ($) { Line 629  sub _get_next_token ($) {
629          redo A;          redo A;
630        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
631          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
632          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
633            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
634                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
635            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
636          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
637            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
638            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
639              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 554  sub _get_next_token ($) { Line 641  sub _get_next_token ($) {
641          } else {          } else {
642            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
643          }          }
644          $self->{state} = 'data';          $self->{state} = DATA_STATE;
645          # reconsume          # reconsume
646    
647          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 563  sub _get_next_token ($) { Line 650  sub _get_next_token ($) {
650        } else {        } else {
651          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
652                                value => ''};                                value => ''};
653          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
654          !!!next-input-character;          !!!next-input-character;
655          redo A;          redo A;
656        }        }
657      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
658        my $before_leave = sub {        my $before_leave = sub {
659          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
660              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
# Line 585  sub _get_next_token ($) { Line 672  sub _get_next_token ($) {
672            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
673            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
674          $before_leave->();          $before_leave->();
675          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
676          !!!next-input-character;          !!!next-input-character;
677          redo A;          redo A;
678        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
679          $before_leave->();          $before_leave->();
680          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
681          !!!next-input-character;          !!!next-input-character;
682          redo A;          redo A;
683        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
684          $before_leave->();          $before_leave->();
685          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
686            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
687                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
688            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
689          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
690            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
691            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
692              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 694  sub _get_next_token ($) {
694          } else {          } else {
695            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
696          }          }
697          $self->{state} = 'data';          $self->{state} = DATA_STATE;
698          !!!next-input-character;          !!!next-input-character;
699    
700          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 623  sub _get_next_token ($) { Line 710  sub _get_next_token ($) {
710          $before_leave->();          $before_leave->();
711          !!!next-input-character;          !!!next-input-character;
712          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
713              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
714              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
715            # permitted slash            # permitted slash
716            #            #
717          } else {          } else {
718            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
719          }          }
720          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
721          # next-input-character is already done          # next-input-character is already done
722          redo A;          redo A;
723        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
724          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
725          $before_leave->();          $before_leave->();
726          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
727            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
728                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
729            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
730          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
731            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
732            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
733              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 648  sub _get_next_token ($) { Line 735  sub _get_next_token ($) {
735          } else {          } else {
736            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
737          }          }
738          $self->{state} = 'data';          $self->{state} = DATA_STATE;
739          # reconsume          # reconsume
740    
741          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 660  sub _get_next_token ($) { Line 747  sub _get_next_token ($) {
747          !!!next-input-character;          !!!next-input-character;
748          redo A;          redo A;
749        }        }
750      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
751        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
752            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
753            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 670  sub _get_next_token ($) { Line 757  sub _get_next_token ($) {
757          !!!next-input-character;          !!!next-input-character;
758          redo A;          redo A;
759        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
760          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
761          !!!next-input-character;          !!!next-input-character;
762          redo A;          redo A;
763        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
764          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
765            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
766                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
767            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
768          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
769            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
770            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
771              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 686  sub _get_next_token ($) { Line 773  sub _get_next_token ($) {
773          } else {          } else {
774            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
775          }          }
776          $self->{state} = 'data';          $self->{state} = DATA_STATE;
777          !!!next-input-character;          !!!next-input-character;
778    
779          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 696  sub _get_next_token ($) { Line 783  sub _get_next_token ($) {
783                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
784          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
785                                value => ''};                                value => ''};
786          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
787          !!!next-input-character;          !!!next-input-character;
788          redo A;          redo A;
789        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
790          !!!next-input-character;          !!!next-input-character;
791          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
792              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
793              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
794            # permitted slash            # permitted slash
795            #            #
# Line 710  sub _get_next_token ($) { Line 797  sub _get_next_token ($) {
797            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
798            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
799          }          }
800          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
801          # next-input-character is already done          # next-input-character is already done
802          redo A;          redo A;
803        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
804          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
805          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
806            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
807                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
808            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
809          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
810            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
811            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
812              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 727  sub _get_next_token ($) { Line 814  sub _get_next_token ($) {
814          } else {          } else {
815            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
816          }          }
817          $self->{state} = 'data';          $self->{state} = DATA_STATE;
818          # reconsume          # reconsume
819    
820          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 736  sub _get_next_token ($) { Line 823  sub _get_next_token ($) {
823        } else {        } else {
824          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
825                                value => ''};                                value => ''};
826          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
827          !!!next-input-character;          !!!next-input-character;
828          redo A;                  redo A;        
829        }        }
830      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
831        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
832            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
833            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 750  sub _get_next_token ($) { Line 837  sub _get_next_token ($) {
837          !!!next-input-character;          !!!next-input-character;
838          redo A;          redo A;
839        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
840          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
841          !!!next-input-character;          !!!next-input-character;
842          redo A;          redo A;
843        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
844          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
845          ## reconsume          ## reconsume
846          redo A;          redo A;
847        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
848          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
849          !!!next-input-character;          !!!next-input-character;
850          redo A;          redo A;
851        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
852          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
853            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
854                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
855            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
856          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
857            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
858            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
859              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 774  sub _get_next_token ($) { Line 861  sub _get_next_token ($) {
861          } else {          } else {
862            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
863          }          }
864          $self->{state} = 'data';          $self->{state} = DATA_STATE;
865          !!!next-input-character;          !!!next-input-character;
866    
867          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 782  sub _get_next_token ($) { Line 869  sub _get_next_token ($) {
869          redo A;          redo A;
870        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
871          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
872          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
873            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
874                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
875            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
876          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
877            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
878            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
879              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 794  sub _get_next_token ($) { Line 881  sub _get_next_token ($) {
881          } else {          } else {
882            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
883          }          }
884          $self->{state} = 'data';          $self->{state} = DATA_STATE;
885          ## reconsume          ## reconsume
886    
887          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 802  sub _get_next_token ($) { Line 889  sub _get_next_token ($) {
889          redo A;          redo A;
890        } else {        } else {
891          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
892          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
893          !!!next-input-character;          !!!next-input-character;
894          redo A;          redo A;
895        }        }
896      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
897        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
898          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
899          !!!next-input-character;          !!!next-input-character;
900          redo A;          redo A;
901        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
902          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
903          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
904          !!!next-input-character;          !!!next-input-character;
905          redo A;          redo A;
906        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
907          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
908          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
909            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
910                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
911            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
912          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
913            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
914            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
915              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 830  sub _get_next_token ($) { Line 917  sub _get_next_token ($) {
917          } else {          } else {
918            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
919          }          }
920          $self->{state} = 'data';          $self->{state} = DATA_STATE;
921          ## reconsume          ## reconsume
922    
923          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 842  sub _get_next_token ($) { Line 929  sub _get_next_token ($) {
929          !!!next-input-character;          !!!next-input-character;
930          redo A;          redo A;
931        }        }
932      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
933        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
934          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
935          !!!next-input-character;          !!!next-input-character;
936          redo A;          redo A;
937        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
938          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
939          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
940          !!!next-input-character;          !!!next-input-character;
941          redo A;          redo A;
942        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
943          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
944          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
945            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
946                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
947            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
948          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
949            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
950            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
951              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 866  sub _get_next_token ($) { Line 953  sub _get_next_token ($) {
953          } else {          } else {
954            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
955          }          }
956          $self->{state} = 'data';          $self->{state} = DATA_STATE;
957          ## reconsume          ## reconsume
958    
959          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 878  sub _get_next_token ($) { Line 965  sub _get_next_token ($) {
965          !!!next-input-character;          !!!next-input-character;
966          redo A;          redo A;
967        }        }
968      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
969        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
970            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
971            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
972            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
973            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
974          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
975          !!!next-input-character;          !!!next-input-character;
976          redo A;          redo A;
977        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
978          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
979          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
980          !!!next-input-character;          !!!next-input-character;
981          redo A;          redo A;
982        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
983          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
984            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
985                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
986            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
987          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
988            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
989            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
990              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 905  sub _get_next_token ($) { Line 992  sub _get_next_token ($) {
992          } else {          } else {
993            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
994          }          }
995          $self->{state} = 'data';          $self->{state} = DATA_STATE;
996          !!!next-input-character;          !!!next-input-character;
997    
998          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 913  sub _get_next_token ($) { Line 1000  sub _get_next_token ($) {
1000          redo A;          redo A;
1001        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1002          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1003          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1004            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1005                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1006            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1007          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1008            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1009            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1010              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 925  sub _get_next_token ($) { Line 1012  sub _get_next_token ($) {
1012          } else {          } else {
1013            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1014          }          }
1015          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1016          ## reconsume          ## reconsume
1017    
1018          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 937  sub _get_next_token ($) { Line 1024  sub _get_next_token ($) {
1024          !!!next-input-character;          !!!next-input-character;
1025          redo A;          redo A;
1026        }        }
1027      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1028        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1029    
1030        unless (defined $token) {        unless (defined $token) {
# Line 950  sub _get_next_token ($) { Line 1037  sub _get_next_token ($) {
1037        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1038        # next-input-character is already done        # next-input-character is already done
1039        redo A;        redo A;
1040      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1041        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1042                
1043        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1044    
1045        BC: {        BC: {
1046          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1047            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1048            !!!next-input-character;            !!!next-input-character;
1049    
1050            !!!emit ($token);            !!!emit ($token);
1051    
1052            redo A;            redo A;
1053          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1054            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1055            ## reconsume            ## reconsume
1056    
1057            !!!emit ($token);            !!!emit ($token);
# Line 976  sub _get_next_token ($) { Line 1063  sub _get_next_token ($) {
1063            redo BC;            redo BC;
1064          }          }
1065        } # BC        } # BC
1066      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1067        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1068    
1069        my @next_char;        my @next_char;
# Line 986  sub _get_next_token ($) { Line 1073  sub _get_next_token ($) {
1073          !!!next-input-character;          !!!next-input-character;
1074          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1075          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1076            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1077            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1078            !!!next-input-character;            !!!next-input-character;
1079            redo A;            redo A;
1080          }          }
# Line 1018  sub _get_next_token ($) { Line 1105  sub _get_next_token ($) {
1105                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1106                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1107                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1108                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1109                      !!!next-input-character;                      !!!next-input-character;
1110                      redo A;                      redo A;
1111                    }                    }
# Line 1032  sub _get_next_token ($) { Line 1119  sub _get_next_token ($) {
1119        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1120        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1121        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1122        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1123        redo A;        redo A;
1124                
1125        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1126        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1127      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1128        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1129          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1130          !!!next-input-character;          !!!next-input-character;
1131          redo A;          redo A;
1132        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1133          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1134          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1135          !!!next-input-character;          !!!next-input-character;
1136    
1137          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1052  sub _get_next_token ($) { Line 1139  sub _get_next_token ($) {
1139          redo A;          redo A;
1140        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1141          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1142          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1143          ## reconsume          ## reconsume
1144    
1145          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1061  sub _get_next_token ($) { Line 1148  sub _get_next_token ($) {
1148        } else {        } else {
1149          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1150              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1151          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1152          !!!next-input-character;          !!!next-input-character;
1153          redo A;          redo A;
1154        }        }
1155      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1156        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1157          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1158          !!!next-input-character;          !!!next-input-character;
1159          redo A;          redo A;
1160        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1161          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1162          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1163          !!!next-input-character;          !!!next-input-character;
1164    
1165          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1080  sub _get_next_token ($) { Line 1167  sub _get_next_token ($) {
1167          redo A;          redo A;
1168        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1169          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1170          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1171          ## reconsume          ## reconsume
1172    
1173          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1089  sub _get_next_token ($) { Line 1176  sub _get_next_token ($) {
1176        } else {        } else {
1177          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1178              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1179          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1180          !!!next-input-character;          !!!next-input-character;
1181          redo A;          redo A;
1182        }        }
1183      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1184        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1185          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1186          !!!next-input-character;          !!!next-input-character;
1187          redo A;          redo A;
1188        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1189          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1190          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1191          ## reconsume          ## reconsume
1192    
1193          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1112  sub _get_next_token ($) { Line 1199  sub _get_next_token ($) {
1199          !!!next-input-character;          !!!next-input-character;
1200          redo A;          redo A;
1201        }        }
1202      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1203        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1204          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1205          !!!next-input-character;          !!!next-input-character;
1206          redo A;          redo A;
1207        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1208          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1209          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1210          ## reconsume          ## reconsume
1211    
1212          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1127  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214          redo A;          redo A;
1215        } else {        } else {
1216          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1217          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1218          !!!next-input-character;          !!!next-input-character;
1219          redo A;          redo A;
1220        }        }
1221      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1222        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1223          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1224          !!!next-input-character;          !!!next-input-character;
1225    
1226          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1147  sub _get_next_token ($) { Line 1234  sub _get_next_token ($) {
1234          redo A;          redo A;
1235        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1236          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238          ## reconsume          ## reconsume
1239    
1240          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1156  sub _get_next_token ($) { Line 1243  sub _get_next_token ($) {
1243        } else {        } else {
1244          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1245          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1246          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1247          !!!next-input-character;          !!!next-input-character;
1248          redo A;          redo A;
1249        }        }
1250      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1251        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1252            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1253            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1254            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1255            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1256          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1257          !!!next-input-character;          !!!next-input-character;
1258          redo A;          redo A;
1259        } else {        } else {
1260          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1261          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1262          ## reconsume          ## reconsume
1263          redo A;          redo A;
1264        }        }
1265      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1266        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1267            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1268            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1186  sub _get_next_token ($) { Line 1273  sub _get_next_token ($) {
1273          redo A;          redo A;
1274        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1275          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1276          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1277          !!!next-input-character;          !!!next-input-character;
1278    
1279          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1280    
1281          redo A;          redo A;
1282        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1283          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1284          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1285          ## reconsume          ## reconsume
1286    
1287          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1288    
1289          redo A;          redo A;
1290        } else {        } else {
1291          $self->{current_token}          $self->{current_token}
1292              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1293                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1294                 correct => 1};                 correct => 1};
1295  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1296          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1297          !!!next-input-character;          !!!next-input-character;
1298          redo A;          redo A;
1299        }        }
1300      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1301  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1302        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1303            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1304            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1305            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1306            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1307          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1308          !!!next-input-character;          !!!next-input-character;
1309          redo A;          redo A;
1310        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1311          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1312          !!!next-input-character;          !!!next-input-character;
1313    
1314          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1229  sub _get_next_token ($) { Line 1316  sub _get_next_token ($) {
1316          redo A;          redo A;
1317        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1318          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1319          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1320          ## reconsume          ## reconsume
1321    
1322          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1243  sub _get_next_token ($) { Line 1330  sub _get_next_token ($) {
1330          !!!next-input-character;          !!!next-input-character;
1331          redo A;          redo A;
1332        }        }
1333      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1334        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1335            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1336            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1253  sub _get_next_token ($) { Line 1340  sub _get_next_token ($) {
1340          !!!next-input-character;          !!!next-input-character;
1341          redo A;          redo A;
1342        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1343          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1344          !!!next-input-character;          !!!next-input-character;
1345    
1346          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1261  sub _get_next_token ($) { Line 1348  sub _get_next_token ($) {
1348          redo A;          redo A;
1349        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1350          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1351          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1352          ## reconsume          ## reconsume
1353    
1354          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1285  sub _get_next_token ($) { Line 1372  sub _get_next_token ($) {
1372                  !!!next-input-character;                  !!!next-input-character;
1373                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1374                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1375                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1376                    !!!next-input-character;                    !!!next-input-character;
1377                    redo A;                    redo A;
1378                  }                  }
# Line 1312  sub _get_next_token ($) { Line 1399  sub _get_next_token ($) {
1399                  !!!next-input-character;                  !!!next-input-character;
1400                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1401                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1402                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1403                    !!!next-input-character;                    !!!next-input-character;
1404                    redo A;                    redo A;
1405                  }                  }
# Line 1328  sub _get_next_token ($) { Line 1415  sub _get_next_token ($) {
1415        }        }
1416    
1417        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1418        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1419        # next-input-character is already done        # next-input-character is already done
1420        redo A;        redo A;
1421      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1422        if ({        if ({
1423              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1424              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1341  sub _get_next_token ($) { Line 1428  sub _get_next_token ($) {
1428          redo A;          redo A;
1429        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1430          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1431          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1432          !!!next-input-character;          !!!next-input-character;
1433          redo A;          redo A;
1434        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1435          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1436          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1437          !!!next-input-character;          !!!next-input-character;
1438          redo A;          redo A;
1439        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1440          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1441    
1442          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1443          !!!next-input-character;          !!!next-input-character;
1444    
1445          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1362  sub _get_next_token ($) { Line 1449  sub _get_next_token ($) {
1449        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1450          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1451    
1452          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1453          ## reconsume          ## reconsume
1454    
1455          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1371  sub _get_next_token ($) { Line 1458  sub _get_next_token ($) {
1458          redo A;          redo A;
1459        } else {        } else {
1460          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1461          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1462          !!!next-input-character;          !!!next-input-character;
1463          redo A;          redo A;
1464        }        }
1465      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1466        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1467          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1468          !!!next-input-character;          !!!next-input-character;
1469          redo A;          redo A;
1470        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1471          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1472    
1473          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1474          ## reconsume          ## reconsume
1475    
1476          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1484  sub _get_next_token ($) {
1484          !!!next-input-character;          !!!next-input-character;
1485          redo A;          redo A;
1486        }        }
1487      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1488        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1489          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1490          !!!next-input-character;          !!!next-input-character;
1491          redo A;          redo A;
1492        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1493          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1494    
1495          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1496          ## reconsume          ## reconsume
1497    
1498          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1419  sub _get_next_token ($) { Line 1506  sub _get_next_token ($) {
1506          !!!next-input-character;          !!!next-input-character;
1507          redo A;          redo A;
1508        }        }
1509      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1510        if ({        if ({
1511              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1512              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516          redo A;          redo A;
1517        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1518          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1519          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1520          !!!next-input-character;          !!!next-input-character;
1521          redo A;          redo A;
1522        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1523          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1524          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;          redo A;
1527        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1528          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1529          !!!next-input-character;          !!!next-input-character;
1530    
1531          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1534  sub _get_next_token ($) {
1534        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1535          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1536    
1537          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1538          ## reconsume          ## reconsume
1539    
1540          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1456  sub _get_next_token ($) { Line 1543  sub _get_next_token ($) {
1543          redo A;          redo A;
1544        } else {        } else {
1545          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1546          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1547          !!!next-input-character;          !!!next-input-character;
1548          redo A;          redo A;
1549        }        }
1550      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1551        if ({        if ({
1552              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1553              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1557  sub _get_next_token ($) {
1557          redo A;          redo A;
1558        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1559          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1560          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1561          !!!next-input-character;          !!!next-input-character;
1562          redo A;          redo A;
1563        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1564          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1565          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1566          !!!next-input-character;          !!!next-input-character;
1567          redo A;          redo A;
1568        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1569          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1570          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1571          !!!next-input-character;          !!!next-input-character;
1572    
1573          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1490  sub _get_next_token ($) { Line 1577  sub _get_next_token ($) {
1577        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1578          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1579    
1580          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1581          ## reconsume          ## reconsume
1582    
1583          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1499  sub _get_next_token ($) { Line 1586  sub _get_next_token ($) {
1586          redo A;          redo A;
1587        } else {        } else {
1588          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1589          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1590          !!!next-input-character;          !!!next-input-character;
1591          redo A;          redo A;
1592        }        }
1593      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1594        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1595          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1596          !!!next-input-character;          !!!next-input-character;
1597          redo A;          redo A;
1598        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1599          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1600    
1601          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1602          ## reconsume          ## reconsume
1603    
1604          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1612  sub _get_next_token ($) {
1612          !!!next-input-character;          !!!next-input-character;
1613          redo A;          redo A;
1614        }        }
1615      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1616        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1617          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1618          !!!next-input-character;          !!!next-input-character;
1619          redo A;          redo A;
1620        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1621          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1622    
1623          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1624          ## reconsume          ## reconsume
1625    
1626          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1547  sub _get_next_token ($) { Line 1634  sub _get_next_token ($) {
1634          !!!next-input-character;          !!!next-input-character;
1635          redo A;          redo A;
1636        }        }
1637      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1638        if ({        if ({
1639              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1640              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1643  sub _get_next_token ($) {
1643          !!!next-input-character;          !!!next-input-character;
1644          redo A;          redo A;
1645        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1646          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1647          !!!next-input-character;          !!!next-input-character;
1648    
1649          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1652  sub _get_next_token ($) {
1652        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1653          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1654    
1655          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1656          ## reconsume          ## reconsume
1657    
1658          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1574  sub _get_next_token ($) { Line 1661  sub _get_next_token ($) {
1661          redo A;          redo A;
1662        } else {        } else {
1663          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1664          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1665          !!!next-input-character;          !!!next-input-character;
1666          redo A;          redo A;
1667        }        }
1668      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1669        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1670          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1671          !!!next-input-character;          !!!next-input-character;
1672    
1673          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1589  sub _get_next_token ($) { Line 1676  sub _get_next_token ($) {
1676          redo A;          redo A;
1677        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1678          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1679          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1680          ## reconsume          ## reconsume
1681    
1682          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1757  sub _tokenize_attempt_to_consume_an_enti
1757            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1758          }          }
1759    
1760          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code};
1761        } # X        } # X
1762      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1763               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1792  sub _tokenize_attempt_to_consume_an_enti
1792          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1793        }        }
1794                
1795        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1796      } else {      } else {
1797        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1798        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1753  sub _tokenize_attempt_to_consume_an_enti Line 1840  sub _tokenize_attempt_to_consume_an_enti
1840      }      }
1841            
1842      if ($match > 0) {      if ($match > 0) {
1843        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value};
1844      } elsif ($match < 0) {      } elsif ($match < 0) {
1845        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1846        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1847          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1848        } else {        } else {
1849          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value};
1850        }        }
1851      } else {      } else {
1852        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1853        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1854        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1855      }      }
1856    } else {    } else {
1857      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 1893  sub _construct_tree ($) {
1893        
1894    !!!next-token;    !!!next-token;
1895    
1896    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1897    undef $self->{form_element};    undef $self->{form_element};
1898    undef $self->{head_element};    undef $self->{head_element};
1899    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 1907  sub _construct_tree ($) {
1907  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1908    my $self = shift;    my $self = shift;
1909    INITIAL: {    INITIAL: {
1910      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1911        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1912        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
1913        ## language.        ## language.
# Line 1947  sub _tree_construction_initial ($) { Line 2034  sub _tree_construction_initial ($) {
2034        !!!next-token;        !!!next-token;
2035        return;        return;
2036      } elsif ({      } elsif ({
2037                'start tag' => 1,                START_TAG_TOKEN, 1,
2038                'end tag' => 1,                END_TAG_TOKEN, 1,
2039                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2040               }->{$token->{type}}) {               }->{$token->{type}}) {
2041        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2042        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2043        ## Go to the root element phase        ## Go to the root element phase
2044        ## reprocess        ## reprocess
2045        return;        return;
2046      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2047        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2048          ## Ignore the token          ## Ignore the token
2049    
# Line 1972  sub _tree_construction_initial ($) { Line 2059  sub _tree_construction_initial ($) {
2059        ## Go to the root element phase        ## Go to the root element phase
2060        ## reprocess        ## reprocess
2061        return;        return;
2062      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2063        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2064        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2065                
# Line 1980  sub _tree_construction_initial ($) { Line 2067  sub _tree_construction_initial ($) {
2067        !!!next-token;        !!!next-token;
2068        redo INITIAL;        redo INITIAL;
2069      } else {      } else {
2070        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2071      }      }
2072    } # INITIAL    } # INITIAL
2073  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2076  sub _tree_construction_root_element ($)
2076    my $self = shift;    my $self = shift;
2077        
2078    B: {    B: {
2079        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2080          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2081          ## Ignore the token          ## Ignore the token
2082          ## Stay in the phase          ## Stay in the phase
2083          !!!next-token;          !!!next-token;
2084          redo B;          redo B;
2085        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2086          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2087          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2088          ## Stay in the phase          ## Stay in the phase
2089          !!!next-token;          !!!next-token;
2090          redo B;          redo B;
2091        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2092          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2093            ## Ignore the token.            ## Ignore the token.
2094    
# Line 2011  sub _tree_construction_root_element ($) Line 2098  sub _tree_construction_root_element ($)
2098              redo B;              redo B;
2099            }            }
2100          }          }
2101    
2102            $self->{application_cache_selection}->(undef);
2103    
2104            #
2105          } elsif ($token->{type} == START_TAG_TOKEN) {
2106            if ($token->{tag_name} eq 'html' and
2107                $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"
2108              $self->{application_cache_selection}
2109                   ->($token->{attributes}->{manifest}->{value});
2110              ## ISSUE: No relative reference resolution?
2111            } else {
2112              $self->{application_cache_selection}->(undef);
2113            }
2114    
2115            ## ISSUE: There is an issue in the spec
2116          #          #
2117        } elsif ({        } elsif ({
2118                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2119                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2120                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2121            $self->{application_cache_selection}->(undef);
2122    
2123          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2124          #          #
2125        } else {        } else {
2126          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2127        }        }
2128    
2129        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2130        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2131        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2166  sub _reset_insertion_mode ($) {
2166            
2167        ## Step 4..13        ## Step 4..13
2168        my $new_mode = {        my $new_mode = {
2169                        select => 'in select',                        select => IN_SELECT_IM,
2170                        td => 'in cell',                        td => IN_CELL_IM,
2171                        th => 'in cell',                        th => IN_CELL_IM,
2172                        tr => 'in row',                        tr => IN_ROW_IM,
2173                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2174                        thead => 'in table body',                        thead => IN_TABLE_BODY_IM,
2175                        tfoot => 'in table body',                        tfoot => IN_TABLE_BODY_IM,
2176                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2177                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2178                        table => 'in table',                        table => IN_TABLE_IM,
2179                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2180                        body => 'in body',                        body => IN_BODY_IM,
2181                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2182                       }->{$node->[1]};                       }->{$node->[1]};
2183        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2184                
2185        ## Step 14        ## Step 14
2186        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2187          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2188            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2189          } else {          } else {
2190            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2191          }          }
2192          return;          return;
2193        }        }
2194                
2195        ## Step 15        ## Step 15
2196        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2197                
2198        ## Step 16        ## Step 16
2199        $i--;        $i--;
# Line 2103  sub _reset_insertion_mode ($) { Line 2207  sub _reset_insertion_mode ($) {
2207  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2208    my $self = shift;    my $self = shift;
2209    
   my $previous_insertion_mode;  
   
2210    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2211    
2212    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2205  sub _tree_construction_main ($) { Line 2307  sub _tree_construction_main ($) {
2307      ## Step 4      ## Step 4
2308      my $text = '';      my $text = '';
2309      !!!next-token;      !!!next-token;
2310      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2311        $text .= $token->{data};        $text .= $token->{data};
2312        !!!next-token;        !!!next-token;
2313      }      }
# Line 2220  sub _tree_construction_main ($) { Line 2322  sub _tree_construction_main ($) {
2322      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2323    
2324      ## Step 7      ## Step 7
2325      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2326        ## Ignore the token        ## Ignore the token
2327      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2328        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2243  sub _tree_construction_main ($) { Line 2345  sub _tree_construction_main ($) {
2345            
2346      my $text = '';      my $text = '';
2347      !!!next-token;      !!!next-token;
2348      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2349        $text .= $token->{data};        $text .= $token->{data};
2350        !!!next-token;        !!!next-token;
2351      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2253  sub _tree_construction_main ($) { Line 2355  sub _tree_construction_main ($) {
2355                                
2356      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2357    
2358      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2359          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2360        ## Ignore the token        ## Ignore the token
2361      } else {      } else {
# Line 2496  sub _tree_construction_main ($) { Line 2598  sub _tree_construction_main ($) {
2598                         }                         }
2599    }; # $insert_to_foster    }; # $insert_to_foster
2600    
2601    my $in_body = sub {    my $insert;
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         ## NOTE: This is an "as if in head" code clone  
         $script_start_tag->($insert);  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ({  
                 base => 1, link => 1,  
                }->{$token->{tag_name}}) {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'meta') {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
         unless ($self->{confident}) {  
           my $charset;  
           if ($token->{attributes}->{charset}) { ## TODO: And if supported  
             $charset = $token->{attributes}->{charset}->{value};  
           }  
           if ($token->{attributes}->{'http-equiv'}) {  
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
             if ($token->{attributes}->{'http-equiv'}->{value}  
                 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
               $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
             } ## TODO: And if supported  
           }  
           ## TODO: Change the encoding  
         }  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {  
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model} = PLAINTEXT_CONTENT_MODEL;  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         ## has a |nobr| element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
   
         ## NOTE: There is an "as if <br>" code clone.  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
           } else {  
             push @tokens, {type => 'character',  
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model} = RCDATA_CONTENT_MODEL;  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
           }  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model} = PCDATA_CONTENT_MODEL;  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There are two "as if in body" code clones.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
   
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
           } else {  
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           }  
         }  
           
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
   
         ## As if <br>  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         my $el;  
         !!!create-element ($el, 'br');  
         $insert->($el);  
           
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
2602    
2603    B: {    B: {
2604      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2605        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2606        ## Ignore the token        ## Ignore the token
2607        ## Stay in the phase        ## Stay in the phase
2608        !!!next-token;        !!!next-token;
2609        redo B;        redo B;
2610      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2611        if ($token->{insertion_mode} ne 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2612            #
2613          } else {
2614          ## Generate implied end tags          ## Generate implied end tags
2615          if ({          if ({
2616               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2617               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2618              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2619            !!!back-token;            !!!back-token;
2620            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2621            redo B;            redo B;
2622          }          }
2623                    
# Line 3375  sub _tree_construction_main ($) { Line 2635  sub _tree_construction_main ($) {
2635    
2636        ## Stop parsing        ## Stop parsing
2637        last B;        last B;
2638      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2639               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2640        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2641          ## Turn into the main phase          ## Turn into the main phase
2642          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2643          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_BODY_IM;
2644          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2645            ## Turn into the main phase
2646            !!!parse-error (type => 'after html:html');
2647            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2648        }        }
2649    
2650  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 3398  sub _tree_construction_main ($) { Line 2662  sub _tree_construction_main ($) {
2662        }        }
2663        !!!next-token;        !!!next-token;
2664        redo B;        redo B;
2665      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2666        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2667        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2668          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2669        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2670          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2671        } else {        } else {
2672          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2673        }        }
2674        !!!next-token;        !!!next-token;
2675        redo B;        redo B;
2676      } elsif ($self->{insertion_mode} eq 'in head' or      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2677               $self->{insertion_mode} eq 'in head noscript' or        if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'after head' or  
              $self->{insertion_mode} eq 'before head') {  
       if ($token->{type} eq 'character') {  
2678          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2679            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2680            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 3422  sub _tree_construction_main ($) { Line 2683  sub _tree_construction_main ($) {
2683            }            }
2684          }          }
2685    
2686          if ($self->{insertion_mode} eq 'before head') {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2687            ## As if <head>            ## As if <head>
2688            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
2689            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3432  sub _tree_construction_main ($) { Line 2693  sub _tree_construction_main ($) {
2693            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2694    
2695            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2696          } elsif ($self->{insertion_mode} eq 'in head noscript') {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2697            ## As if </noscript>            ## As if </noscript>
2698            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2699            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 3442  sub _tree_construction_main ($) { Line 2703  sub _tree_construction_main ($) {
2703            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2704    
2705            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2706          } elsif ($self->{insertion_mode} eq 'in head') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2707            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2708    
2709            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
# Line 3451  sub _tree_construction_main ($) { Line 2712  sub _tree_construction_main ($) {
2712              ## "after head" insertion mode              ## "after head" insertion mode
2713              ## As if <body>              ## As if <body>
2714              !!!insert-element ('body');              !!!insert-element ('body');
2715              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
2716              ## reprocess              ## reprocess
2717              redo B;              redo B;
2718            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2719              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2720                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2721                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2722                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2723                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2724                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2725                  !!!next-token;                  !!!next-token;
2726                  redo B;                  redo B;
2727                } elsif ($self->{insertion_mode} ne 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2728                    #
2729                  } else {
2730                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
2731                  ## Ignore the token                  ## Ignore the token
2732                  !!!next-token;                  !!!next-token;
2733                  redo B;                  redo B;
               } else {  
                 #  
2734                }                }
2735              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2736                ## As if <head>                ## As if <head>
2737                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2738                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2739                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2740    
2741                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = IN_HEAD_IM;
2742                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
2743              }              }
2744    
2745              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
2746                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2747                  ## As if </noscript>                  ## As if </noscript>
2748                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2749                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
2750                                
2751                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2752                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2753                }                }
2754    
2755                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2756                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2757                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2758                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2759                }                }
2760                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2761                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2762                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2763                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2764                !!!next-token;                !!!next-token;
2765                redo B;                redo B;
2766              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
2767                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2768                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2769                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2770                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2771                }                }
2772                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2773                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2774                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2775                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2776                !!!next-token;                !!!next-token;
2777                redo B;                redo B;
2778              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2779                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2780                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2781                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2782                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2783                }                }
# Line 3542  sub _tree_construction_main ($) { Line 2803  sub _tree_construction_main ($) {
2803    
2804                ## TODO: Extracting |charset| from |meta|.                ## TODO: Extracting |charset| from |meta|.
2805                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2806                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2807                !!!next-token;                !!!next-token;
2808                redo B;                redo B;
2809              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
2810                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2811                  ## As if </noscript>                  ## As if </noscript>
2812                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2813                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
2814                                
2815                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2816                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2817                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2818                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2819                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2820                }                }
# Line 3564  sub _tree_construction_main ($) { Line 2825  sub _tree_construction_main ($) {
2825                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2826                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2827                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2828                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2829                redo B;                redo B;
2830              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2831                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2832                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2833                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2834                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2835                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2836                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2837                }                }
2838                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2839                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2840                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2841                redo B;                redo B;
2842              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2843                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2844                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2845                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2846                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2847                  !!!next-token;                  !!!next-token;
2848                  redo B;                  redo B;
2849                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2850                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
2851                  ## Ignore the token                  ## Ignore the token
2852                  !!!next-token;                  !!!next-token;
# Line 3594  sub _tree_construction_main ($) { Line 2855  sub _tree_construction_main ($) {
2855                  #                  #
2856                }                }
2857              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
2858                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2859                  ## As if </noscript>                  ## As if </noscript>
2860                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2861                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
2862                                
2863                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2864                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2865                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2866                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2867                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2868                }                }
# Line 3609  sub _tree_construction_main ($) { Line 2870  sub _tree_construction_main ($) {
2870                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2871                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
2872                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2873                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2874                redo B;                redo B;
2875              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
2876                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
2877                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2878                  ## As if </noscript>                  ## As if </noscript>
2879                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2880                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 3623  sub _tree_construction_main ($) { Line 2884  sub _tree_construction_main ($) {
2884                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2885                                    
2886                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
2887                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2888                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2889                                    
2890                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
# Line 3631  sub _tree_construction_main ($) { Line 2892  sub _tree_construction_main ($) {
2892    
2893                ## "after head" insertion mode                ## "after head" insertion mode
2894                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2895                $self->{insertion_mode} = 'in '.$token->{tag_name};                if ($token->{tag_name} eq 'body') {
2896                    $self->{insertion_mode} = IN_BODY_IM;
2897                  } elsif ($token->{tag_name} eq 'frameset') {
2898                    $self->{insertion_mode} = IN_FRAMESET_IM;
2899                  } else {
2900                    die "$0: tag name: $self->{tag_name}";
2901                  }
2902                !!!next-token;                !!!next-token;
2903                redo B;                redo B;
2904              } else {              } else {
2905                #                #
2906              }              }
2907    
2908              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2909                ## As if </noscript>                ## As if </noscript>
2910                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2911                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3648  sub _tree_construction_main ($) { Line 2915  sub _tree_construction_main ($) {
2915                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2916    
2917                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
2918              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2919                ## As if </head>                ## As if </head>
2920                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2921    
# Line 3658  sub _tree_construction_main ($) { Line 2925  sub _tree_construction_main ($) {
2925              ## "after head" insertion mode              ## "after head" insertion mode
2926              ## As if <body>              ## As if <body>
2927              !!!insert-element ('body');              !!!insert-element ('body');
2928              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
2929              ## reprocess              ## reprocess
2930              redo B;              redo B;
2931            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
2932              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2933                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2934                  ## As if <head>                  ## As if <head>
2935                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
2936                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3671  sub _tree_construction_main ($) { Line 2938  sub _tree_construction_main ($) {
2938    
2939                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2940                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2941                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
2942                  !!!next-token;                  !!!next-token;
2943                  redo B;                  redo B;
2944                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2945                  ## As if </noscript>                  ## As if </noscript>
2946                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2947                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
2948                                    
2949                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2950                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2951                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
2952                  !!!next-token;                  !!!next-token;
2953                  redo B;                  redo B;
2954                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2955                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2956                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
2957                  !!!next-token;                  !!!next-token;
2958                  redo B;                  redo B;
2959                } else {                } else {
2960                  #                  #
2961                }                }
2962              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2963                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2964                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2965                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2966                  !!!next-token;                  !!!next-token;
2967                  redo B;                  redo B;
2968                } elsif ($self->{insertion_mode} eq 'before head') {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2969                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript');
2970                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
2971                  !!!next-token;                  !!!next-token;
# Line 3709  sub _tree_construction_main ($) { Line 2976  sub _tree_construction_main ($) {
2976              } elsif ({              } elsif ({
2977                        body => 1, html => 1,                        body => 1, html => 1,
2978                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2979                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2980                  ## As if <head>                  ## As if <head>
2981                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
2982                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2983                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2984    
2985                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2986                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2987                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2988                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2989                  ## Ignore the token                  ## Ignore the token
2990                  !!!next-token;                  !!!next-token;
# Line 3728  sub _tree_construction_main ($) { Line 2995  sub _tree_construction_main ($) {
2995              } elsif ({              } elsif ({
2996                        p => 1, br => 1,                        p => 1, br => 1,
2997                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2998                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2999                  ## As if <head>                  ## As if <head>
3000                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3001                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3002                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3003    
3004                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3005                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3006                }                }
3007    
3008                #                #
3009              } else {              } else {
3010                if ($self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3011                    #
3012                  } else {
3013                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3014                  ## Ignore the token                  ## Ignore the token
3015                  !!!next-token;                  !!!next-token;
3016                  redo B;                  redo B;
               } else {  
                 #  
3017                }                }
3018              }              }
3019    
3020              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3021                ## As if </noscript>                ## As if </noscript>
3022                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3023                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3760  sub _tree_construction_main ($) { Line 3027  sub _tree_construction_main ($) {
3027                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3028    
3029                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3030              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3031                ## As if </head>                ## As if </head>
3032                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3033    
3034                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3035              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3036                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3037                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3038                !!!next-token;                !!!next-token;
# Line 3775  sub _tree_construction_main ($) { Line 3042  sub _tree_construction_main ($) {
3042              ## "after head" insertion mode              ## "after head" insertion mode
3043              ## As if <body>              ## As if <body>
3044              !!!insert-element ('body');              !!!insert-element ('body');
3045              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3046              ## reprocess              ## reprocess
3047              redo B;              redo B;
3048            } else {            } else {
# Line 3783  sub _tree_construction_main ($) { Line 3050  sub _tree_construction_main ($) {
3050            }            }
3051    
3052            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3053      } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
3054               $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
3055              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3056              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3057                            
# Line 3794  sub _tree_construction_main ($) { Line 3059  sub _tree_construction_main ($) {
3059    
3060              !!!next-token;              !!!next-token;
3061              redo B;              redo B;
3062            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3063              if ({              if ({
3064                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3065                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3066                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3067                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3068                  ## have an element in table scope                  ## have an element in table scope
3069                  my $tn;                  my $tn;
3070                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3822  sub _tree_construction_main ($) { Line 3087  sub _tree_construction_main ($) {
3087                                    
3088                  ## Close the cell                  ## Close the cell
3089                  !!!back-token; # <?>                  !!!back-token; # <?>
3090                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3091                  redo B;                  redo B;
3092                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3093                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3094                                    
3095                  ## As if </caption>                  ## As if </caption>
# Line 3855  sub _tree_construction_main ($) { Line 3120  sub _tree_construction_main ($) {
3120                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3121                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3122                    !!!back-token; # <?>                    !!!back-token; # <?>
3123                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3124                    !!!back-token;                    !!!back-token;
3125                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3126                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3127                    redo B;                    redo B;
3128                  }                  }
# Line 3870  sub _tree_construction_main ($) { Line 3135  sub _tree_construction_main ($) {
3135                                    
3136                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3137                                    
3138                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3139                                    
3140                  ## reprocess                  ## reprocess
3141                  redo B;                  redo B;
# Line 3880  sub _tree_construction_main ($) { Line 3145  sub _tree_construction_main ($) {
3145              } else {              } else {
3146                #                #
3147              }              }
3148            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3149              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3150                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3151                  ## have an element in table scope                  ## have an element in table scope
3152                  my $i;                  my $i;
3153                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3912  sub _tree_construction_main ($) { Line 3177  sub _tree_construction_main ($) {
3177                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3178                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3179                    !!!back-token;                    !!!back-token;
3180                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3181                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3182                    redo B;                    redo B;
3183                  }                  }
# Line 3925  sub _tree_construction_main ($) { Line 3190  sub _tree_construction_main ($) {
3190                                    
3191                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3192                                    
3193                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3194                                    
3195                  !!!next-token;                  !!!next-token;
3196                  redo B;                  redo B;
3197                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3198                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3199                  ## Ignore the token                  ## Ignore the token
3200                  !!!next-token;                  !!!next-token;
# Line 3938  sub _tree_construction_main ($) { Line 3203  sub _tree_construction_main ($) {
3203                  #                  #
3204                }                }
3205              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3206                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3207                  ## have a table element in table scope                  ## have a table element in table scope
3208                  my $i;                  my $i;
3209                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3966  sub _tree_construction_main ($) { Line 3231  sub _tree_construction_main ($) {
3231                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3232                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3233                    !!!back-token;                    !!!back-token;
3234                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3235                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3236                    redo B;                    redo B;
3237                  }                  }
# Line 3979  sub _tree_construction_main ($) { Line 3244  sub _tree_construction_main ($) {
3244                                    
3245                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3246                                    
3247                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3248                                    
3249                  !!!next-token;                  !!!next-token;
3250                  redo B;                  redo B;
3251                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3252                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3253                  ## Ignore the token                  ## Ignore the token
3254                  !!!next-token;                  !!!next-token;
# Line 3995  sub _tree_construction_main ($) { Line 3260  sub _tree_construction_main ($) {
3260                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3261                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3262                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3263                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3264                ## have an element in table scope                ## have an element in table scope
3265                my $i;                my $i;
3266                my $tn;                my $tn;
# Line 4023  sub _tree_construction_main ($) { Line 3288  sub _tree_construction_main ($) {
3288    
3289                ## Close the cell                ## Close the cell
3290                !!!back-token; # </?>                !!!back-token; # </?>
3291                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3292                redo B;                redo B;
3293              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3294                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3295                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3296    
3297                ## As if </caption>                ## As if </caption>
# Line 4057  sub _tree_construction_main ($) { Line 3322  sub _tree_construction_main ($) {
3322                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3323                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3324                  !!!back-token; # </table>                  !!!back-token; # </table>
3325                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3326                  !!!back-token;                  !!!back-token;
3327                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3328                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3329                  redo B;                  redo B;
3330                }                }
# Line 4072  sub _tree_construction_main ($) { Line 3337  sub _tree_construction_main ($) {
3337    
3338                $clear_up_to_marker->();                $clear_up_to_marker->();
3339    
3340                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3341    
3342                ## reprocess                ## reprocess
3343                redo B;                redo B;
3344              } elsif ({              } elsif ({
3345                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3346                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3347                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3348                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3349                  ## Ignore the token                  ## Ignore the token
3350                  !!!next-token;                  !!!next-token;
# Line 4092  sub _tree_construction_main ($) { Line 3356  sub _tree_construction_main ($) {
3356                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3357                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3358                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3359                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3360                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3361                ## Ignore the token                ## Ignore the token
3362                !!!next-token;                !!!next-token;
# Line 4100  sub _tree_construction_main ($) { Line 3364  sub _tree_construction_main ($) {
3364              } else {              } else {
3365                #                #
3366              }              }
3367            } else {        } else {
3368              #          die "$0: $token->{type}: Unknown token type";
3369            }        }
3370              
3371            $in_body->($insert_to_current);        $insert = $insert_to_current;
3372            redo B;        #
3373          } elsif ($self->{insertion_mode} eq 'in row' or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3374                   $self->{insertion_mode} eq 'in table body' or        if ($token->{type} == CHARACTER_TOKEN) {
                  $self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
3375              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3376                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3377                                
# Line 4167  sub _tree_construction_main ($) { Line 3428  sub _tree_construction_main ($) {
3428                            
3429              !!!next-token;              !!!next-token;
3430              redo B;              redo B;
3431            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3432              if ({              if ({
3433                   tr => ($self->{insertion_mode} ne 'in row'),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3434                   th => 1, td => 1,                   th => 1, td => 1,
3435                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3436                if ($self->{insertion_mode} eq 'in table') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3437                  ## Clear back to table context                  ## Clear back to table context
3438                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3439                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
# Line 4181  sub _tree_construction_main ($) { Line 3442  sub _tree_construction_main ($) {
3442                  }                  }
3443                                    
3444                  !!!insert-element ('tbody');                  !!!insert-element ('tbody');
3445                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3446                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3447                }                }
3448    
3449                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3450                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
3451                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
3452                  }                  }
# Line 4198  sub _tree_construction_main ($) { Line 3459  sub _tree_construction_main ($) {
3459                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3460                  }                  }
3461                                    
3462                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3463                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3464                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3465                    !!!next-token;                    !!!next-token;
# Line 4218  sub _tree_construction_main ($) { Line 3479  sub _tree_construction_main ($) {
3479                }                }
3480                                
3481                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3482                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3483    
3484                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3485                                
# Line 4227  sub _tree_construction_main ($) { Line 3488  sub _tree_construction_main ($) {
3488              } elsif ({              } elsif ({
3489                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3490                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3491                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3492                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3493                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3494                  ## As if </tr>                  ## As if </tr>
3495                  ## have an element in table scope                  ## have an element in table scope
3496                  my $i;                  my $i;
# Line 4260  sub _tree_construction_main ($) { Line 3521  sub _tree_construction_main ($) {
3521                  }                  }
3522                                    
3523                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3524                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3525                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3526                    ## reprocess                    ## reprocess
3527                    redo B;                    redo B;
# Line 4269  sub _tree_construction_main ($) { Line 3530  sub _tree_construction_main ($) {
3530                  }                  }
3531                }                }
3532    
3533                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3534                  ## have an element in table scope                  ## have an element in table scope
3535                  my $i;                  my $i;
3536                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4308  sub _tree_construction_main ($) { Line 3569  sub _tree_construction_main ($) {
3569                  ## nop by definition                  ## nop by definition
3570                                    
3571                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3572                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3573                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
3574                }                }
3575    
# Line 4321  sub _tree_construction_main ($) { Line 3582  sub _tree_construction_main ($) {
3582                  }                  }
3583                                    
3584                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup');
3585                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3586                  ## reprocess                  ## reprocess
3587                  redo B;                  redo B;
3588                } elsif ({                } elsif ({
# Line 4341  sub _tree_construction_main ($) { Line 3602  sub _tree_construction_main ($) {
3602                                    
3603                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3604                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
3605                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
3606                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
3607                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
3608                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
3609                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
3610                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
3611                  !!!next-token;                  !!!next-token;
3612                  redo B;                  redo B;
# Line 4353  sub _tree_construction_main ($) { Line 3614  sub _tree_construction_main ($) {
3614                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3615                }                }
3616              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3617                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3618    
3619                ## As if </table>                ## As if </table>
# Line 4384  sub _tree_construction_main ($) { Line 3644  sub _tree_construction_main ($) {
3644                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3645                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3646                  !!!back-token; # <table>                  !!!back-token; # <table>
3647                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3648                  !!!back-token;                  !!!back-token;
3649                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3650                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3651                  redo B;                  redo B;
3652                }                }
# Line 4401  sub _tree_construction_main ($) { Line 3661  sub _tree_construction_main ($) {
3661    
3662                ## reprocess                ## reprocess
3663                redo B;                redo B;
3664              } else {          } else {
3665                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3666              }  
3667            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3668              #
3669            }
3670          } elsif ($token->{type} == END_TAG_TOKEN) {
3671              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3672                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
3673                ## have an element in table scope                ## have an element in table scope
3674                my $i;                my $i;
3675                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4436  sub _tree_construction_main ($) { Line 3699  sub _tree_construction_main ($) {
3699                }                }
3700    
3701                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3702                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3703                !!!next-token;                !!!next-token;
3704                redo B;                redo B;
3705              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3706                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3707                  ## As if </tr>                  ## As if </tr>
3708                  ## have an element in table scope                  ## have an element in table scope
3709                  my $i;                  my $i;
# Line 4471  sub _tree_construction_main ($) { Line 3734  sub _tree_construction_main ($) {
3734                  }                  }
3735                                    
3736                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3737                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3738                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3739                }                }
3740    
3741                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3742                  ## have an element in table scope                  ## have an element in table scope
3743                  my $i;                  my $i;
3744                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4514  sub _tree_construction_main ($) { Line 3777  sub _tree_construction_main ($) {
3777                  ## nop by definition                  ## nop by definition
3778                                    
3779                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3780                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3781                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
3782                }                }
3783    
# Line 4545  sub _tree_construction_main ($) { Line 3808  sub _tree_construction_main ($) {
3808                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3809                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3810                  !!!back-token;                  !!!back-token;
3811                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3812                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3813                  redo B;                  redo B;
3814                }                }
# Line 4563  sub _tree_construction_main ($) { Line 3826  sub _tree_construction_main ($) {
3826              } elsif ({              } elsif ({
3827                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3828                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3829                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
3830                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
3831                  ## have an element in table scope                  ## have an element in table scope
3832                  my $i;                  my $i;
3833                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4616  sub _tree_construction_main ($) { Line 3878  sub _tree_construction_main ($) {
3878                  }                  }
3879                                    
3880                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3881                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3882                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3883                }                }
3884    
# Line 4649  sub _tree_construction_main ($) { Line 3911  sub _tree_construction_main ($) {
3911                }                }
3912    
3913                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3914                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3915                !!!next-token;                !!!next-token;
3916                redo B;                redo B;
3917              } elsif ({              } elsif ({
3918                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3919                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3920                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3921                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3922                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3923                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3924                ## Ignore the token                ## Ignore the token
3925                !!!next-token;                !!!next-token;
3926                redo B;                redo B;
3927              } else {          } else {
3928                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
3929    
3930            !!!parse-error (type => 'in table:'.$token->{tag_name});            $insert = $insert_to_foster;
3931            $in_body->($insert_to_foster);            #
3932            redo B;          }
3933          } elsif ($self->{insertion_mode} eq 'in column group') {        } else {
3934            if ($token->{type} eq 'character') {          die "$0: $token->{type}: Unknown token type";
3935          }
3936        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3937              if ($token->{type} == CHARACTER_TOKEN) {
3938              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3939                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3940                unless (length $token->{data}) {                unless (length $token->{data}) {
# Line 4683  sub _tree_construction_main ($) { Line 3944  sub _tree_construction_main ($) {
3944              }              }
3945                            
3946              #              #
3947            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3948              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
3949                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3950                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
# Line 4692  sub _tree_construction_main ($) { Line 3953  sub _tree_construction_main ($) {
3953              } else {              } else {
3954                #                #
3955              }              }
3956            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3957              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
3958                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3959                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
# Line 4701  sub _tree_construction_main ($) { Line 3962  sub _tree_construction_main ($) {
3962                  redo B;                  redo B;
3963                } else {                } else {
3964                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
3965                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3966                  !!!next-token;                  !!!next-token;
3967                  redo B;                              redo B;            
3968                }                }
# Line 4725  sub _tree_construction_main ($) { Line 3986  sub _tree_construction_main ($) {
3986              redo B;              redo B;
3987            } else {            } else {
3988              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
3989              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
3990              ## reprocess              ## reprocess
3991              redo B;              redo B;
3992            }            }
3993          } elsif ($self->{insertion_mode} eq 'in select') {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3994            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
3995              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3996              !!!next-token;          !!!next-token;
3997              redo B;          redo B;
3998            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3999              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4000                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4001                  ## As if </option>                  ## As if </option>
# Line 4787  sub _tree_construction_main ($) { Line 4048  sub _tree_construction_main ($) {
4048    
4049                !!!next-token;                !!!next-token;
4050                redo B;                redo B;
4051              } else {          } else {
4052                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4053              }            ## Ignore the token
4054            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4055              redo B;
4056            }
4057          } elsif ($token->{type} == END_TAG_TOKEN) {
4058              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4059                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4060                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4892  sub _tree_construction_main ($) { Line 4156  sub _tree_construction_main ($) {
4156    
4157                ## reprocess                ## reprocess
4158                redo B;                redo B;
4159              } else {          } else {
4160                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4161            ## Ignore the token            ## Ignore the token
4162            !!!next-token;            !!!next-token;
4163            redo B;            redo B;
4164          } elsif ($self->{insertion_mode} eq 'after body') {          }
4165            if ($token->{type} eq 'character') {        } else {
4166              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4167                my $data = $1;        }
4168                ## As if in body      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4169                $reconstruct_active_formatting_elements->($insert_to_current);        if ($token->{type} == CHARACTER_TOKEN) {
4170            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4171              my $data = $1;
4172              ## As if in body
4173              $reconstruct_active_formatting_elements->($insert_to_current);
4174                                
4175                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4176              
4177              unless (length $token->{data}) {
4178                !!!next-token;
4179                redo B;
4180              }
4181            }
4182            
4183            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4184              !!!parse-error (type => 'after html:#character');
4185    
4186                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4187                  !!!next-token;          }
4188                  redo B;          
4189                }          ## "after body" insertion mode
4190              }          !!!parse-error (type => 'after body:#character');
4191                
4192              #          $self->{insertion_mode} = IN_BODY_IM;
4193              !!!parse-error (type => 'after body:#character');          ## reprocess
4194            } elsif ($token->{type} eq 'start tag') {          redo B;
4195              !!!parse-error (type => 'after body:'.$token->{tag_name});        } elsif ($token->{type} == START_TAG_TOKEN) {
4196              #          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4197            } elsif ($token->{type} eq 'end tag') {            !!!parse-error (type => 'after html:'.$token->{tag_name});
4198              if ($token->{tag_name} eq 'html') {            
4199                if (defined $self->{inner_html_node}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4200                  !!!parse-error (type => 'unmatched end tag:html');          }
4201                  ## Ignore the token  
4202                  !!!next-token;          ## "after body" insertion mode
4203                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
4204                } else {  
4205                  $previous_insertion_mode = $self->{insertion_mode};          $self->{insertion_mode} = IN_BODY_IM;
4206                  $self->{insertion_mode} = 'trailing end';          ## reprocess
4207                  !!!next-token;          redo B;
4208                  redo B;        } elsif ($token->{type} == END_TAG_TOKEN) {
4209                }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4210              } else {            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4211                !!!parse-error (type => 'after body:/'.$token->{tag_name});            
4212              }            $self->{insertion_mode} = AFTER_BODY_IM;
4213              ## Reprocess in the "main" phase, "after body" insertion mode...
4214            }
4215    
4216            ## "after body" insertion mode
4217            if ($token->{tag_name} eq 'html') {
4218              if (defined $self->{inner_html_node}) {
4219                !!!parse-error (type => 'unmatched end tag:html');
4220                ## Ignore the token
4221                !!!next-token;
4222                redo B;
4223            } else {            } else {
4224              die "$0: $token->{type}: Unknown token type";              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4225                !!!next-token;
4226                redo B;
4227            }            }
4228            } else {
4229              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4230    
4231            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4232            ## reprocess            ## reprocess
4233            redo B;            redo B;
4234      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4235        if ($token->{type} eq 'character') {        } else {
4236            die "$0: $token->{type}: Unknown token type";
4237          }
4238        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4239          if ($token->{type} == CHARACTER_TOKEN) {
4240          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4241            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4242              
4243            unless (length $token->{data}) {            unless (length $token->{data}) {
4244              !!!next-token;              !!!next-token;
4245              redo B;              redo B;
4246            }            }
4247          }          }
4248            
4249            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4250              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4251                !!!parse-error (type => 'in frameset:#character');
4252              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4253                !!!parse-error (type => 'after frameset:#character');
4254              } else { # "after html frameset"
4255                !!!parse-error (type => 'after html:#character');
4256    
4257                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4258                ## Reprocess in the "main" phase, "after frameset"...
4259                !!!parse-error (type => 'after frameset:#character');
4260              }
4261              
4262              ## Ignore the token.
4263              if (length $token->{data}) {
4264                ## reprocess the rest of characters
4265              } else {
4266                !!!next-token;
4267              }
4268              redo B;
4269            }
4270            
4271            die qq[$0: Character "$token->{data}"];
4272          } elsif ($token->{type} == START_TAG_TOKEN) {
4273            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4274              !!!parse-error (type => 'after html:'.$token->{tag_name});
4275    
4276          !!!parse-error (type => 'in frameset:#character');            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4277          ## Ignore the token            ## Process in the "main" phase, "after frameset" insertion mode...
4278          !!!next-token;          }
4279          redo B;  
4280        } elsif ($token->{type} eq 'start tag') {          if ($token->{tag_name} eq 'frameset' and
4281          if ($token->{tag_name} eq 'frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4282            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4283            !!!next-token;            !!!next-token;
4284            redo B;            redo B;
4285          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
4286                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4287            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4288            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4289            !!!next-token;            !!!next-token;
# Line 4976  sub _tree_construction_main ($) { Line 4293  sub _tree_construction_main ($) {
4293            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4294            redo B;            redo B;
4295          } else {          } else {
4296            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4297                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4298              } else {
4299                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4300              }
4301            ## Ignore the token            ## Ignore the token
4302            !!!next-token;            !!!next-token;
4303            redo B;            redo B;
4304          }          }
4305        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4306          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4307              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4308    
4309              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4310              ## Process in the "main" phase, "after frameset" insertion mode...
4311            }
4312    
4313            if ($token->{tag_name} eq 'frameset' and
4314                $self->{insertion_mode} == IN_FRAMESET_IM) {
4315            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4316                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4317              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 4995  sub _tree_construction_main ($) { Line 4324  sub _tree_construction_main ($) {
4324    
4325            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4326                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4327              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4328            }            }
4329            redo B;            redo B;
4330            } elsif ($token->{tag_name} eq 'html' and
4331                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4332              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4333              !!!next-token;
4334              redo B;
4335          } else {          } else {
4336            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4337                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4338              } else {
4339                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4340              }
4341            ## Ignore the token            ## Ignore the token
4342            !!!next-token;            !!!next-token;
4343            redo B;            redo B;
# Line 5007  sub _tree_construction_main ($) { Line 4345  sub _tree_construction_main ($) {
4345        } else {        } else {
4346          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4347        }        }
     } elsif ($self->{insertion_mode} eq 'after frameset') {  
       if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
4348    
4349                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4350                  !!!next-token;      } else {
4351                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4352                }      }
             }  
4353    
4354              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
4355                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
4356          if ($token->{tag_name} eq 'script') {
4357            ## NOTE: This is an "as if in head" code clone
4358            $script_start_tag->($insert);
4359            redo B;
4360          } elsif ($token->{tag_name} eq 'style') {
4361            ## NOTE: This is an "as if in head" code clone
4362            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4363            redo B;
4364          } elsif ({
4365                    base => 1, link => 1,
4366                   }->{$token->{tag_name}}) {
4367            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4368            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4369            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4370            !!!next-token;
4371            redo B;
4372          } elsif ($token->{tag_name} eq 'meta') {
4373            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4374            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4375            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4376    
4377                ## Ignore the token.          unless ($self->{confident}) {
4378                if (length $token->{data}) {            my $charset;
4379                  ## reprocess the rest of characters            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4380                } else {              $charset = $token->{attributes}->{charset}->{value};
4381                  !!!next-token;            }
4382                }            if ($token->{attributes}->{'http-equiv'}) {
4383                redo B;              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4384              }              if ($token->{attributes}->{'http-equiv'}->{value}
4385                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4386                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4387                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4388                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4389                } ## TODO: And if supported
4390              }
4391              ## TODO: Change the encoding
4392            }
4393    
4394          die qq[$0: Character "$token->{data}"];          !!!next-token;
4395        } elsif ($token->{type} eq 'start tag') {          redo B;
4396          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
4397            ## NOTE: As if in body.          !!!parse-error (type => 'in body:title');
4398            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);          ## NOTE: This is an "as if in head" code clone
4399            redo B;          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4400          } else {            if (defined $self->{head_element}) {
4401            !!!parse-error (type => 'after frameset:'.$token->{tag_name});              $self->{head_element}->append_child ($_[0]);
4402              } else {
4403                $insert->($_[0]);
4404              }
4405            });
4406            redo B;
4407          } elsif ($token->{tag_name} eq 'body') {
4408            !!!parse-error (type => 'in body:body');
4409                  
4410            if (@{$self->{open_elements}} == 1 or
4411                $self->{open_elements}->[1]->[1] ne 'body') {
4412            ## Ignore the token            ## Ignore the token
4413            } else {
4414              my $body_el = $self->{open_elements}->[1]->[0];
4415              for my $attr_name (keys %{$token->{attributes}}) {
4416                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4417                  $body_el->set_attribute_ns
4418                    (undef, [undef, $attr_name],
4419                     $token->{attributes}->{$attr_name}->{value});
4420                }
4421              }
4422            }
4423            !!!next-token;
4424            redo B;
4425          } elsif ({
4426                    address => 1, blockquote => 1, center => 1, dir => 1,
4427                    div => 1, dl => 1, fieldset => 1, listing => 1,
4428                    menu => 1, ol => 1, p => 1, ul => 1,
4429                    pre => 1,
4430                   }->{$token->{tag_name}}) {
4431            ## has a p element in scope
4432            INSCOPE: for (reverse @{$self->{open_elements}}) {
4433              if ($_->[1] eq 'p') {
4434                !!!back-token;
4435                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4436                redo B;
4437              } elsif ({
4438                        table => 1, caption => 1, td => 1, th => 1,
4439                        button => 1, marquee => 1, object => 1, html => 1,
4440                       }->{$_->[1]}) {
4441                last INSCOPE;
4442              }
4443            } # INSCOPE
4444              
4445            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4446            if ($token->{tag_name} eq 'pre') {
4447              !!!next-token;
4448              if ($token->{type} == CHARACTER_TOKEN) {
4449                $token->{data} =~ s/^\x0A//;
4450                unless (length $token->{data}) {
4451                  !!!next-token;
4452                }
4453              }
4454            } else {
4455            !!!next-token;            !!!next-token;
           redo B;  
4456          }          }
4457        } elsif ($token->{type} eq 'end tag') {          redo B;
4458          if ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'form') {
4459            $previous_insertion_mode = $self->{insertion_mode};          if (defined $self->{form_element}) {
4460            $self->{insertion_mode} = 'trailing end';            !!!parse-error (type => 'in form:form');
4461              ## Ignore the token
4462            !!!next-token;            !!!next-token;
4463            redo B;            redo B;
4464          } else {          } else {
4465            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            ## has a p element in scope
4466            ## Ignore the token            INSCOPE: for (reverse @{$self->{open_elements}}) {
4467                if ($_->[1] eq 'p') {
4468                  !!!back-token;
4469                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4470                  redo B;
4471                } elsif ({
4472                          table => 1, caption => 1, td => 1, th => 1,
4473                          button => 1, marquee => 1, object => 1, html => 1,
4474                         }->{$_->[1]}) {
4475                  last INSCOPE;
4476                }
4477              } # INSCOPE
4478                
4479              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4480              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4481            !!!next-token;            !!!next-token;
4482            redo B;            redo B;
4483          }          }
4484        } else {        } elsif ($token->{tag_name} eq 'li') {
4485          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
4486        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
4487              if ($_->[1] eq 'p') {
4488                !!!back-token;
4489                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4490                redo B;
4491              } elsif ({
4492                        table => 1, caption => 1, td => 1, th => 1,
4493                        button => 1, marquee => 1, object => 1, html => 1,
4494                       }->{$_->[1]}) {
4495                last INSCOPE;
4496              }
4497            } # INSCOPE
4498              
4499            ## Step 1
4500            my $i = -1;
4501            my $node = $self->{open_elements}->[$i];
4502            LI: {
4503              ## Step 2
4504              if ($node->[1] eq 'li') {
4505                if ($i != -1) {
4506                  !!!parse-error (type => 'end tag missing:'.
4507                                  $self->{open_elements}->[-1]->[1]);
4508                }
4509                splice @{$self->{open_elements}}, $i;
4510                last LI;
4511              }
4512              
4513              ## Step 3
4514              if (not $formatting_category->{$node->[1]} and
4515                  #not $phrasing_category->{$node->[1]} and
4516                  ($special_category->{$node->[1]} or
4517                   $scoping_category->{$node->[1]}) and
4518                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4519                last LI;
4520              }
4521              
4522              ## Step 4
4523              $i--;
4524              $node = $self->{open_elements}->[$i];
4525              redo LI;
4526            } # LI
4527              
4528            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4529            !!!next-token;
4530            redo B;
4531          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4532            ## has a p element in scope
4533            INSCOPE: for (reverse @{$self->{open_elements}}) {
4534              if ($_->[1] eq 'p') {
4535                !!!back-token;
4536                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4537                redo B;
4538              } elsif ({
4539                        table => 1, caption => 1, td => 1, th => 1,
4540                        button => 1, marquee => 1, object => 1, html => 1,
4541                       }->{$_->[1]}) {
4542                last INSCOPE;
4543              }
4544            } # INSCOPE
4545              
4546            ## Step 1
4547            my $i = -1;
4548            my $node = $self->{open_elements}->[$i];
4549            LI: {
4550              ## Step 2
4551              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4552                if ($i != -1) {
4553                  !!!parse-error (type => 'end tag missing:'.
4554                                  $self->{open_elements}->[-1]->[1]);
4555                }
4556                splice @{$self->{open_elements}}, $i;
4557                last LI;
4558              }
4559              
4560              ## Step 3
4561              if (not $formatting_category->{$node->[1]} and
4562                  #not $phrasing_category->{$node->[1]} and
4563                  ($special_category->{$node->[1]} or
4564                   $scoping_category->{$node->[1]}) and
4565                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4566                last LI;
4567              }
4568              
4569              ## Step 4
4570              $i--;
4571              $node = $self->{open_elements}->[$i];
4572              redo LI;
4573            } # LI
4574              
4575            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4576            !!!next-token;
4577            redo B;
4578          } elsif ($token->{tag_name} eq 'plaintext') {
4579            ## has a p element in scope
4580            INSCOPE: for (reverse @{$self->{open_elements}}) {
4581              if ($_->[1] eq 'p') {
4582                !!!back-token;
4583                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4584                redo B;
4585              } elsif ({
4586                        table => 1, caption => 1, td => 1, th => 1,
4587                        button => 1, marquee => 1, object => 1, html => 1,
4588                       }->{$_->[1]}) {
4589                last INSCOPE;
4590              }
4591            } # INSCOPE
4592              
4593            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4594              
4595            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4596              
4597            !!!next-token;
4598            redo B;
4599          } elsif ({
4600                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4601                   }->{$token->{tag_name}}) {
4602            ## has a p element in scope
4603            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4604              my $node = $self->{open_elements}->[$_];
4605              if ($node->[1] eq 'p') {
4606                !!!back-token;
4607                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4608                redo B;
4609              } elsif ({
4610                        table => 1, caption => 1, td => 1, th => 1,
4611                        button => 1, marquee => 1, object => 1, html => 1,
4612                       }->{$node->[1]}) {
4613                last INSCOPE;
4614              }
4615            } # INSCOPE
4616              
4617            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4618            ## has an element in scope
4619            #my $i;
4620            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4621            #  my $node = $self->{open_elements}->[$_];
4622            #  if ({
4623            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4624            #      }->{$node->[1]}) {
4625            #    $i = $_;
4626            #    last INSCOPE;
4627            #  } elsif ({
4628            #            table => 1, caption => 1, td => 1, th => 1,
4629            #            button => 1, marquee => 1, object => 1, html => 1,
4630            #           }->{$node->[1]}) {
4631            #    last INSCOPE;
4632            #  }
4633            #} # INSCOPE
4634            #  
4635            #if (defined $i) {
4636            #  !!! parse-error (type => 'in hn:hn');
4637            #  splice @{$self->{open_elements}}, $i;
4638            #}
4639              
4640            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4641              
4642            !!!next-token;
4643            redo B;
4644          } elsif ($token->{tag_name} eq 'a') {
4645            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4646              my $node = $active_formatting_elements->[$i];
4647              if ($node->[1] eq 'a') {
4648                !!!parse-error (type => 'in a:a');
4649                
4650                !!!back-token;
4651                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4652                $formatting_end_tag->($token->{tag_name});
4653                
4654                AFE2: for (reverse 0..$#$active_formatting_elements) {
4655                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4656                    splice @$active_formatting_elements, $_, 1;
4657                    last AFE2;
4658                  }
4659                } # AFE2
4660                OE: for (reverse 0..$#{$self->{open_elements}}) {
4661                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4662                    splice @{$self->{open_elements}}, $_, 1;
4663                    last OE;
4664                  }
4665                } # OE
4666                last AFE;
4667              } elsif ($node->[0] eq '#marker') {
4668                last AFE;
4669              }
4670            } # AFE
4671              
4672            $reconstruct_active_formatting_elements->($insert_to_current);
4673    
4674        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4675      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
4676        ## states in the main stage is preserved yet # MUST  
4677                  !!!next-token;
4678        if ($token->{type} eq 'character') {          redo B;
4679          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
4680            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
4681            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
4682            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
4683            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4684            ## end phase is either "after body" or "after frameset".          $reconstruct_active_formatting_elements->($insert_to_current);
4685            $reconstruct_active_formatting_elements->($insert_to_current);          
4686            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4687            push @$active_formatting_elements, $self->{open_elements}->[-1];
4688            
4689            !!!next-token;
4690            redo B;
4691          } elsif ($token->{tag_name} eq 'nobr') {
4692            $reconstruct_active_formatting_elements->($insert_to_current);
4693    
4694            ## has a |nobr| element in scope
4695            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4696              my $node = $self->{open_elements}->[$_];
4697              if ($node->[1] eq 'nobr') {
4698                !!!parse-error (type => 'in nobr:nobr');
4699                !!!back-token;
4700                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4701                redo B;
4702              } elsif ({
4703                        table => 1, caption => 1, td => 1, th => 1,
4704                        button => 1, marquee => 1, object => 1, html => 1,
4705                       }->{$node->[1]}) {
4706                last INSCOPE;
4707              }
4708            } # INSCOPE
4709            
4710            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4711            push @$active_formatting_elements, $self->{open_elements}->[-1];
4712            
4713            !!!next-token;
4714            redo B;
4715          } elsif ($token->{tag_name} eq 'button') {
4716            ## has a button element in scope
4717            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4718              my $node = $self->{open_elements}->[$_];
4719              if ($node->[1] eq 'button') {
4720                !!!parse-error (type => 'in button:button');
4721                !!!back-token;
4722                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4723                redo B;
4724              } elsif ({
4725                        table => 1, caption => 1, td => 1, th => 1,
4726                        button => 1, marquee => 1, object => 1, html => 1,
4727                       }->{$node->[1]}) {
4728                last INSCOPE;
4729              }
4730            } # INSCOPE
4731              
4732            $reconstruct_active_formatting_elements->($insert_to_current);
4733              
4734            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4735            push @$active_formatting_elements, ['#marker', ''];
4736    
4737            !!!next-token;
4738            redo B;
4739          } elsif ($token->{tag_name} eq 'marquee' or
4740                   $token->{tag_name} eq 'object') {
4741            $reconstruct_active_formatting_elements->($insert_to_current);
4742            
4743            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4744            push @$active_formatting_elements, ['#marker', ''];
4745            
4746            !!!next-token;
4747            redo B;
4748          } elsif ($token->{tag_name} eq 'xmp') {
4749            $reconstruct_active_formatting_elements->($insert_to_current);
4750            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4751            redo B;
4752          } elsif ($token->{tag_name} eq 'table') {
4753            ## has a p element in scope
4754            INSCOPE: for (reverse @{$self->{open_elements}}) {
4755              if ($_->[1] eq 'p') {
4756                !!!back-token;
4757                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4758                redo B;
4759              } elsif ({
4760                        table => 1, caption => 1, td => 1, th => 1,
4761                        button => 1, marquee => 1, object => 1, html => 1,
4762                       }->{$_->[1]}) {
4763                last INSCOPE;
4764              }
4765            } # INSCOPE
4766                        
4767            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4768              
4769            $self->{insertion_mode} = IN_TABLE_IM;
4770                        
4771            !!!next-token;
4772            redo B;
4773          } elsif ({
4774                    area => 1, basefont => 1, bgsound => 1, br => 1,
4775                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4776                    image => 1,
4777                   }->{$token->{tag_name}}) {
4778            if ($token->{tag_name} eq 'image') {
4779              !!!parse-error (type => 'image');
4780              $token->{tag_name} = 'img';
4781            }
4782    
4783            ## NOTE: There is an "as if <br>" code clone.
4784            $reconstruct_active_formatting_elements->($insert_to_current);
4785            
4786            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4787            pop @{$self->{open_elements}};
4788            
4789            !!!next-token;
4790            redo B;
4791          } elsif ($token->{tag_name} eq 'hr') {
4792            ## has a p element in scope
4793            INSCOPE: for (reverse @{$self->{open_elements}}) {
4794              if ($_->[1] eq 'p') {
4795                !!!back-token;
4796                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4797                redo B;
4798              } elsif ({
4799                        table => 1, caption => 1, td => 1, th => 1,
4800                        button => 1, marquee => 1, object => 1, html => 1,
4801                       }->{$_->[1]}) {
4802                last INSCOPE;
4803              }
4804            } # INSCOPE
4805              
4806            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4807            pop @{$self->{open_elements}};
4808              
4809            !!!next-token;
4810            redo B;
4811          } elsif ($token->{tag_name} eq 'input') {
4812            $reconstruct_active_formatting_elements->($insert_to_current);
4813            
4814            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4815            ## TODO: associate with $self->{form_element} if defined
4816            pop @{$self->{open_elements}};
4817            
4818            !!!next-token;
4819            redo B;
4820          } elsif ($token->{tag_name} eq 'isindex') {
4821            !!!parse-error (type => 'isindex');
4822            
4823            if (defined $self->{form_element}) {
4824              ## Ignore the token
4825              !!!next-token;
4826              redo B;
4827            } else {
4828              my $at = $token->{attributes};
4829              my $form_attrs;
4830              $form_attrs->{action} = $at->{action} if $at->{action};
4831              my $prompt_attr = $at->{prompt};
4832              $at->{name} = {name => 'name', value => 'isindex'};
4833              delete $at->{action};
4834              delete $at->{prompt};
4835              my @tokens = (
4836                            {type => START_TAG_TOKEN, tag_name => 'form',
4837                             attributes => $form_attrs},
4838                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4839                            {type => START_TAG_TOKEN, tag_name => 'p'},
4840                            {type => START_TAG_TOKEN, tag_name => 'label'},
4841                           );
4842              if ($prompt_attr) {
4843                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4844              } else {
4845                push @tokens, {type => CHARACTER_TOKEN,
4846                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4847                ## TODO: make this configurable
4848              }
4849              push @tokens,
4850                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4851                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4852                            {type => END_TAG_TOKEN, tag_name => 'label'},
4853                            {type => END_TAG_TOKEN, tag_name => 'p'},
4854                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4855                            {type => END_TAG_TOKEN, tag_name => 'form'};
4856              $token = shift @tokens;
4857              !!!back-token (@tokens);
4858              redo B;
4859            }
4860          } elsif ($token->{tag_name} eq 'textarea') {
4861            my $tag_name = $token->{tag_name};
4862            my $el;
4863            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4864            
4865            ## TODO: $self->{form_element} if defined
4866            $self->{content_model} = RCDATA_CONTENT_MODEL;
4867            delete $self->{escape}; # MUST
4868            
4869            $insert->($el);
4870            
4871            my $text = '';
4872            !!!next-token;
4873            if ($token->{type} == CHARACTER_TOKEN) {
4874              $token->{data} =~ s/^\x0A//;
4875            unless (length $token->{data}) {            unless (length $token->{data}) {
4876              !!!next-token;              !!!next-token;
             redo B;  
4877            }            }
4878          }          }
4879            while ($token->{type} == CHARACTER_TOKEN) {
4880              $text .= $token->{data};
4881              !!!next-token;
4882            }
4883            if (length $text) {
4884              $el->manakai_append_text ($text);
4885            }
4886            
4887            $self->{content_model} = PCDATA_CONTENT_MODEL;
4888            
4889            if ($token->{type} == END_TAG_TOKEN and
4890                $token->{tag_name} eq $tag_name) {
4891              ## Ignore the token
4892            } else {
4893              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4894            }
4895            !!!next-token;
4896            redo B;
4897          } elsif ({
4898                    iframe => 1,
4899                    noembed => 1,
4900                    noframes => 1,
4901                    noscript => 0, ## TODO: 1 if scripting is enabled
4902                   }->{$token->{tag_name}}) {
4903            ## NOTE: There is an "as if in body" code clone.
4904            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4905            redo B;
4906          } elsif ($token->{tag_name} eq 'select') {
4907            $reconstruct_active_formatting_elements->($insert_to_current);
4908            
4909            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4910            
4911            $self->{insertion_mode} = IN_SELECT_IM;
4912            !!!next-token;
4913            redo B;
4914          } elsif ({
4915                    caption => 1, col => 1, colgroup => 1, frame => 1,
4916                    frameset => 1, head => 1, option => 1, optgroup => 1,
4917                    tbody => 1, td => 1, tfoot => 1, th => 1,
4918                    thead => 1, tr => 1,
4919                   }->{$token->{tag_name}}) {
4920            !!!parse-error (type => 'in body:'.$token->{tag_name});
4921            ## Ignore the token
4922            !!!next-token;
4923            redo B;
4924            
4925            ## ISSUE: An issue on HTML5 new elements in the spec.
4926          } else {
4927            $reconstruct_active_formatting_elements->($insert_to_current);
4928            
4929            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4930            
4931            !!!next-token;
4932            redo B;
4933          }
4934        } elsif ($token->{type} == END_TAG_TOKEN) {
4935          if ($token->{tag_name} eq 'body') {
4936            if (@{$self->{open_elements}} > 1 and
4937                $self->{open_elements}->[1]->[1] eq 'body') {
4938              for (@{$self->{open_elements}}) {
4939                unless ({
4940                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4941                           th => 1, tr => 1, body => 1, html => 1,
4942                         tbody => 1, tfoot => 1, thead => 1,
4943                        }->{$_->[1]}) {
4944                  !!!parse-error (type => 'not closed:'.$_->[1]);
4945                }
4946              }
4947    
4948          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
4949          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
4950          ## reprocess            redo B;
4951            } else {
4952              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4953              ## Ignore the token
4954              !!!next-token;
4955              redo B;
4956            }
4957          } elsif ($token->{tag_name} eq 'html') {
4958            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4959              ## ISSUE: There is an issue in the spec.
4960              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4961                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4962              }
4963              $self->{insertion_mode} = AFTER_BODY_IM;
4964              ## reprocess
4965              redo B;
4966            } else {
4967              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4968              ## Ignore the token
4969              !!!next-token;
4970              redo B;
4971            }
4972          } elsif ({
4973                    address => 1, blockquote => 1, center => 1, dir => 1,
4974                    div => 1, dl => 1, fieldset => 1, listing => 1,
4975                    menu => 1, ol => 1, pre => 1, ul => 1,
4976                    p => 1,
4977                    dd => 1, dt => 1, li => 1,
4978                    button => 1, marquee => 1, object => 1,
4979                   }->{$token->{tag_name}}) {
4980            ## has an element in scope
4981            my $i;
4982            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4983              my $node = $self->{open_elements}->[$_];
4984              if ($node->[1] eq $token->{tag_name}) {
4985                ## generate implied end tags
4986                if ({
4987                     dd => ($token->{tag_name} ne 'dd'),
4988                     dt => ($token->{tag_name} ne 'dt'),
4989                     li => ($token->{tag_name} ne 'li'),
4990                     p => ($token->{tag_name} ne 'p'),
4991                     td => 1, th => 1, tr => 1,
4992                     tbody => 1, tfoot=> 1, thead => 1,
4993                    }->{$self->{open_elements}->[-1]->[1]}) {
4994                  !!!back-token;
4995                  $token = {type => END_TAG_TOKEN,
4996                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4997                  redo B;
4998                }
4999                $i = $_;
5000                last INSCOPE unless $token->{tag_name} eq 'p';
5001              } elsif ({
5002                        table => 1, caption => 1, td => 1, th => 1,
5003                        button => 1, marquee => 1, object => 1, html => 1,
5004                       }->{$node->[1]}) {
5005                last INSCOPE;
5006              }
5007            } # INSCOPE
5008            
5009            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5010              if (defined $i) {
5011                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5012              } else {
5013                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5014              }
5015            }
5016            
5017            if (defined $i) {
5018              splice @{$self->{open_elements}}, $i;
5019            } elsif ($token->{tag_name} eq 'p') {
5020              ## As if <p>, then reprocess the current token
5021              my $el;
5022              !!!create-element ($el, 'p');
5023              $insert->($el);
5024            }
5025            $clear_up_to_marker->()
5026              if {
5027                button => 1, marquee => 1, object => 1,
5028              }->{$token->{tag_name}};
5029            !!!next-token;
5030          redo B;          redo B;
5031        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'form') {
5032          !!!parse-error (type => 'after html:'.$token->{tag_name});          ## has an element in scope
5033          $self->{insertion_mode} = $previous_insertion_mode;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5034          ## reprocess            my $node = $self->{open_elements}->[$_];
5035              if ($node->[1] eq $token->{tag_name}) {
5036                ## generate implied end tags
5037                if ({
5038                     dd => 1, dt => 1, li => 1, p => 1,
5039                     td => 1, th => 1, tr => 1,
5040                     tbody => 1, tfoot=> 1, thead => 1,
5041                    }->{$self->{open_elements}->[-1]->[1]}) {
5042                  !!!back-token;
5043                  $token = {type => END_TAG_TOKEN,
5044                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5045                  redo B;
5046                }
5047                last INSCOPE;
5048              } elsif ({
5049                        table => 1, caption => 1, td => 1, th => 1,
5050                        button => 1, marquee => 1, object => 1, html => 1,
5051                       }->{$node->[1]}) {
5052                last INSCOPE;
5053              }
5054            } # INSCOPE
5055            
5056            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5057              pop @{$self->{open_elements}};
5058            } else {
5059              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5060            }
5061    
5062            undef $self->{form_element};
5063            !!!next-token;
5064          redo B;          redo B;
5065        } elsif ($token->{type} eq 'end tag') {        } elsif ({
5066          !!!parse-error (type => 'after html:/'.$token->{tag_name});                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5067          $self->{insertion_mode} = $previous_insertion_mode;                 }->{$token->{tag_name}}) {
5068          ## reprocess          ## has an element in scope
5069            my $i;
5070            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5071              my $node = $self->{open_elements}->[$_];
5072              if ({
5073                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5074                  }->{$node->[1]}) {
5075                ## generate implied end tags
5076                if ({
5077                     dd => 1, dt => 1, li => 1, p => 1,
5078                     td => 1, th => 1, tr => 1,
5079                     tbody => 1, tfoot=> 1, thead => 1,
5080                    }->{$self->{open_elements}->[-1]->[1]}) {
5081                  !!!back-token;
5082                  $token = {type => END_TAG_TOKEN,
5083                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5084                  redo B;
5085                }
5086                $i = $_;
5087                last INSCOPE;
5088              } elsif ({
5089                        table => 1, caption => 1, td => 1, th => 1,
5090                        button => 1, marquee => 1, object => 1, html => 1,
5091                       }->{$node->[1]}) {
5092                last INSCOPE;
5093              }
5094            } # INSCOPE
5095            
5096            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5097              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5098            }
5099            
5100            splice @{$self->{open_elements}}, $i if defined $i;
5101            !!!next-token;
5102            redo B;
5103          } elsif ({
5104                    a => 1,
5105                    b => 1, big => 1, em => 1, font => 1, i => 1,
5106                    nobr => 1, s => 1, small => 1, strile => 1,
5107                    strong => 1, tt => 1, u => 1,
5108                   }->{$token->{tag_name}}) {
5109            $formatting_end_tag->($token->{tag_name});
5110            redo B;
5111          } elsif ($token->{tag_name} eq 'br') {
5112            !!!parse-error (type => 'unmatched end tag:br');
5113    
5114            ## As if <br>
5115            $reconstruct_active_formatting_elements->($insert_to_current);
5116            
5117            my $el;
5118            !!!create-element ($el, 'br');
5119            $insert->($el);
5120            
5121            ## Ignore the token.
5122            !!!next-token;
5123            redo B;
5124          } elsif ({
5125                    caption => 1, col => 1, colgroup => 1, frame => 1,
5126                    frameset => 1, head => 1, option => 1, optgroup => 1,
5127                    tbody => 1, td => 1, tfoot => 1, th => 1,
5128                    thead => 1, tr => 1,
5129                    area => 1, basefont => 1, bgsound => 1,
5130                    embed => 1, hr => 1, iframe => 1, image => 1,
5131                    img => 1, input => 1, isindex => 1, noembed => 1,
5132                    noframes => 1, param => 1, select => 1, spacer => 1,
5133                    table => 1, textarea => 1, wbr => 1,
5134                    noscript => 0, ## TODO: if scripting is enabled
5135                   }->{$token->{tag_name}}) {
5136            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5137            ## Ignore the token
5138            !!!next-token;
5139          redo B;          redo B;
5140            
5141            ## ISSUE: Issue on HTML5 new elements in spec
5142            
5143        } else {        } else {
5144          die "$0: $token->{type}: Unknown token";          ## Step 1
5145            my $node_i = -1;
5146            my $node = $self->{open_elements}->[$node_i];
5147    
5148            ## Step 2
5149            S2: {
5150              if ($node->[1] eq $token->{tag_name}) {
5151                ## Step 1
5152                ## generate implied end tags
5153                if ({
5154                     dd => 1, dt => 1, li => 1, p => 1,
5155                     td => 1, th => 1, tr => 1,
5156                     tbody => 1, tfoot => 1, thead => 1,
5157                    }->{$self->{open_elements}->[-1]->[1]}) {
5158                  !!!back-token;
5159                  $token = {type => END_TAG_TOKEN,
5160                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5161                  redo B;
5162                }
5163            
5164                ## Step 2
5165                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5166                  ## NOTE: <x><y></x>
5167                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5168                }
5169                
5170                ## Step 3
5171                splice @{$self->{open_elements}}, $node_i;
5172    
5173                !!!next-token;
5174                last S2;
5175              } else {
5176                ## Step 3
5177                if (not $formatting_category->{$node->[1]} and
5178                    #not $phrasing_category->{$node->[1]} and
5179                    ($special_category->{$node->[1]} or
5180                     $scoping_category->{$node->[1]})) {
5181                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5182                  ## Ignore the token
5183                  !!!next-token;
5184                  last S2;
5185                }
5186              }
5187              
5188              ## Step 4
5189              $node_i--;
5190              $node = $self->{open_elements}->[$node_i];
5191              
5192              ## Step 5;
5193              redo S2;
5194            } # S2
5195            redo B;
5196        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
5197      }      }
5198        redo B;
5199    } # B    } # B
5200    
5201      ## NOTE: The "trailing end" phase in HTML5 is split into
5202      ## two insertion modes: "after html body" and "after html frameset".
5203      ## NOTE: States in the main stage is preserved while
5204      ## the parser stays in the trailing end phase. # MUST
5205    
5206    ## Stop parsing # MUST    ## Stop parsing # MUST
5207        
5208    ## TODO: script stuffs    ## TODO: script stuffs

Legend:
Removed from v.1.50  
changed lines
  Added in v.1.61

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24