/[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.49 by wakaba, Sat Jul 21 10:39:45 2007 UTC revision 1.60 by wakaba, Sun Oct 14 09:21:46 2007 UTC
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
159  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162    sub DATA_STATE () { 0 }
163    sub ENTITY_DATA_STATE () { 1 }
164    sub TAG_OPEN_STATE () { 2 }
165    sub CLOSE_TAG_OPEN_STATE () { 3 }
166    sub TAG_NAME_STATE () { 4 }
167    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
168    sub ATTRIBUTE_NAME_STATE () { 6 }
169    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
170    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
171    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
172    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
173    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
174    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
175    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
176    sub COMMENT_START_STATE () { 14 }
177    sub COMMENT_START_DASH_STATE () { 15 }
178    sub COMMENT_STATE () { 16 }
179    sub COMMENT_END_STATE () { 17 }
180    sub COMMENT_END_DASH_STATE () { 18 }
181    sub BOGUS_COMMENT_STATE () { 19 }
182    sub DOCTYPE_STATE () { 20 }
183    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
184    sub DOCTYPE_NAME_STATE () { 22 }
185    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
186    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
187    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
188    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
189    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
190    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
191    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
192    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
193    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
194    sub BOGUS_DOCTYPE_STATE () { 32 }
195    
196    sub DOCTYPE_TOKEN () { 1 }
197    sub COMMENT_TOKEN () { 2 }
198    sub START_TAG_TOKEN () { 3 }
199    sub END_TAG_TOKEN () { 4 }
200    sub END_OF_FILE_TOKEN () { 5 }
201    sub CHARACTER_TOKEN () { 6 }
202    
203    sub AFTER_HTML_IMS () { 0b100 }
204    sub HEAD_IMS ()       { 0b1000 }
205    sub BODY_IMS ()       { 0b10000 }
206    sub BODY_TABLE_IMS () { 0b100000 }
207    sub TABLE_IMS ()      { 0b1000000 }
208    sub ROW_IMS ()        { 0b10000000 }
209    sub BODY_AFTER_IMS () { 0b100000000 }
210    sub FRAME_IMS ()      { 0b1000000000 }
211    
212    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
213    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
214    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
215    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
216    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
217    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
218    sub IN_BODY_IM () { BODY_IMS }
219    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
220    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
221    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
222    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
223    sub IN_TABLE_IM () { TABLE_IMS }
224    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
225    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
226    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
227    sub IN_SELECT_IM () { 0b01 }
228    sub IN_COLUMN_GROUP_IM () { 0b10 }
229    
230  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
231    
232  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
233    my $self = shift;    my $self = shift;
234    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
235    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
236    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
237    undef $self->{current_attribute};    undef $self->{current_attribute};
# Line 177  sub _initialize_tokenizer ($) { Line 245  sub _initialize_tokenizer ($) {
245  } # _initialize_tokenizer  } # _initialize_tokenizer
246    
247  ## A token has:  ## A token has:
248  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
249  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
250  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
251  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
252  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
253  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
254  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
255  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
256    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
257    
258  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
259    
# Line 194  sub _initialize_tokenizer ($) { Line 263  sub _initialize_tokenizer ($) {
263  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
264  ## and removed from the list.  ## and removed from the list.
265    
266    ## NOTE: HTML5 "Writing HTML documents" section, applied to
267    ## documents and not to user agents and conformance checkers,
268    ## contains some requirements that are not detected by the
269    ## parsing algorithm:
270    ## - Some requirements on character encoding declarations. ## TODO
271    ## - "Elements MUST NOT contain content that their content model disallows."
272    ##   ... Some are parse error, some are not (will be reported by c.c.).
273    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
274    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
275    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
276    
277    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
278    ## be detected by the HTML5 parsing algorithm:
279    ## - Text,
280    
281  sub _get_next_token ($) {  sub _get_next_token ($) {
282    my $self = shift;    my $self = shift;
283    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 285  sub _get_next_token ($) {
285    }    }
286    
287    A: {    A: {
288      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
289        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
290          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
291            $self->{state} = 'entity data';            $self->{state} = ENTITY_DATA_STATE;
292            !!!next-input-character;            !!!next-input-character;
293            redo A;            redo A;
294          } else {          } else {
# Line 226  sub _get_next_token ($) { Line 310  sub _get_next_token ($) {
310          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
311              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
312               not $self->{escape})) {               not $self->{escape})) {
313            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
314            !!!next-input-character;            !!!next-input-character;
315            redo A;            redo A;
316          } else {          } else {
# Line 243  sub _get_next_token ($) { Line 327  sub _get_next_token ($) {
327                    
328          #          #
329        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
330          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
331          last A; ## TODO: ok?          last A; ## TODO: ok?
332        }        }
333        # Anything else        # Anything else
334        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
335                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
336        ## Stay in the data state        ## Stay in the data state
337        !!!next-input-character;        !!!next-input-character;
# Line 255  sub _get_next_token ($) { Line 339  sub _get_next_token ($) {
339        !!!emit ($token);        !!!emit ($token);
340    
341        redo A;        redo A;
342      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
343        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
344                
345        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
346    
347        $self->{state} = 'data';        $self->{state} = DATA_STATE;
348        # next-input-character is already done        # next-input-character is already done
349    
350        unless (defined $token) {        unless (defined $token) {
351          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
352        } else {        } else {
353          !!!emit ($token);          !!!emit ($token);
354        }        }
355    
356        redo A;        redo A;
357      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
358        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
359          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
360            !!!next-input-character;            !!!next-input-character;
361            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
362            redo A;            redo A;
363          } else {          } else {
364            ## reconsume            ## reconsume
365            $self->{state} = 'data';            $self->{state} = DATA_STATE;
366    
367            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
368    
369            redo A;            redo A;
370          }          }
371        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
372          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
373            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
374            !!!next-input-character;            !!!next-input-character;
375            redo A;            redo A;
376          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
377            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
378            !!!next-input-character;            !!!next-input-character;
379            redo A;            redo A;
380          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
381                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
382            $self->{current_token}            $self->{current_token}
383              = {type => 'start tag',              = {type => START_TAG_TOKEN,
384                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
385            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
386            !!!next-input-character;            !!!next-input-character;
387            redo A;            redo A;
388          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
389                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
390            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
391                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
392            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
393            !!!next-input-character;            !!!next-input-character;
394            redo A;            redo A;
395          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
396            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
397            $self->{state} = 'data';            $self->{state} = DATA_STATE;
398            !!!next-input-character;            !!!next-input-character;
399    
400            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
401    
402            redo A;            redo A;
403          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
404            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
405            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
406            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
407            redo A;            redo A;
408          } else {          } else {
409            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
410            $self->{state} = 'data';            $self->{state} = DATA_STATE;
411            ## reconsume            ## reconsume
412    
413            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
414    
415            redo A;            redo A;
416          }          }
417        } else {        } else {
418          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
419        }        }
420      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
421        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
422          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
423            ## 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 432  sub _get_next_token ($) {
432              } else {              } else {
433                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
434                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
435                $self->{state} = 'data';                $self->{state} = DATA_STATE;
436    
437                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
438        
439                redo A;                redo A;
440              }              }
# Line 367  sub _get_next_token ($) { Line 451  sub _get_next_token ($) {
451                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
452              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
453              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
454              $self->{state} = 'data';              $self->{state} = DATA_STATE;
455              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
456              redo A;              redo A;
457            } else {            } else {
458              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 378  sub _get_next_token ($) { Line 462  sub _get_next_token ($) {
462          } else {          } else {
463            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
464            # next-input-character is already done            # next-input-character is already done
465            $self->{state} = 'data';            $self->{state} = DATA_STATE;
466            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
467            redo A;            redo A;
468          }          }
469        }        }
470                
471        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
472            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
473          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
474                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
475          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
476          !!!next-input-character;          !!!next-input-character;
477          redo A;          redo A;
478        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
479                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
480          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
481                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
482          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
483          !!!next-input-character;          !!!next-input-character;
484          redo A;          redo A;
485        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
486          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
487          $self->{state} = 'data';          $self->{state} = DATA_STATE;
488          !!!next-input-character;          !!!next-input-character;
489          redo A;          redo A;
490        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
491          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
492          $self->{state} = 'data';          $self->{state} = DATA_STATE;
493          # reconsume          # reconsume
494    
495          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
496    
497          redo A;          redo A;
498        } else {        } else {
499          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
500          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
501          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
502          redo A;          redo A;
503        }        }
504      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
505        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
506            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
507            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
508            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
509            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
510          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
511          !!!next-input-character;          !!!next-input-character;
512          redo A;          redo A;
513        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
514          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
515            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
516                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
517            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
518          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
519            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
520            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
521              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 439  sub _get_next_token ($) { Line 523  sub _get_next_token ($) {
523          } else {          } else {
524            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
525          }          }
526          $self->{state} = 'data';          $self->{state} = DATA_STATE;
527          !!!next-input-character;          !!!next-input-character;
528    
529          !!!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 538  sub _get_next_token ($) {
538          redo A;          redo A;
539        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
540          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
541          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
542            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
543                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
544            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
545          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
546            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
547            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
548              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 466  sub _get_next_token ($) { Line 550  sub _get_next_token ($) {
550          } else {          } else {
551            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
552          }          }
553          $self->{state} = 'data';          $self->{state} = DATA_STATE;
554          # reconsume          # reconsume
555    
556          !!!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 559  sub _get_next_token ($) {
559        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
560          !!!next-input-character;          !!!next-input-character;
561          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
562              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
563              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
564            # permitted slash            # permitted slash
565            #            #
566          } else {          } else {
567            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
568          }          }
569          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
570          # next-input-character is already done          # next-input-character is already done
571          redo A;          redo A;
572        } else {        } else {
# Line 492  sub _get_next_token ($) { Line 576  sub _get_next_token ($) {
576          !!!next-input-character;          !!!next-input-character;
577          redo A;          redo A;
578        }        }
579      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
580        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
581            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
582            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 502  sub _get_next_token ($) { Line 586  sub _get_next_token ($) {
586          !!!next-input-character;          !!!next-input-character;
587          redo A;          redo A;
588        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
589          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
590            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
591                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
592            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
593          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
594            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
595            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
596              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 514  sub _get_next_token ($) { Line 598  sub _get_next_token ($) {
598          } else {          } else {
599            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
600          }          }
601          $self->{state} = 'data';          $self->{state} = DATA_STATE;
602          !!!next-input-character;          !!!next-input-character;
603    
604          !!!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 608  sub _get_next_token ($) {
608                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
609          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
610                                value => ''};                                value => ''};
611          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
612          !!!next-input-character;          !!!next-input-character;
613          redo A;          redo A;
614        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
615          !!!next-input-character;          !!!next-input-character;
616          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
617              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
618              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
619            # permitted slash            # permitted slash
620            #            #
# Line 542  sub _get_next_token ($) { Line 626  sub _get_next_token ($) {
626          redo A;          redo A;
627        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
628          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
629          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
630            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
631                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
632            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
633          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
634            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
635            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
636              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 554  sub _get_next_token ($) { Line 638  sub _get_next_token ($) {
638          } else {          } else {
639            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
640          }          }
641          $self->{state} = 'data';          $self->{state} = DATA_STATE;
642          # reconsume          # reconsume
643    
644          !!!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 647  sub _get_next_token ($) {
647        } else {        } else {
648          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
649                                value => ''};                                value => ''};
650          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
651          !!!next-input-character;          !!!next-input-character;
652          redo A;          redo A;
653        }        }
654      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
655        my $before_leave = sub {        my $before_leave = sub {
656          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
657              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
# Line 585  sub _get_next_token ($) { Line 669  sub _get_next_token ($) {
669            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
670            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
671          $before_leave->();          $before_leave->();
672          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
673          !!!next-input-character;          !!!next-input-character;
674          redo A;          redo A;
675        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
676          $before_leave->();          $before_leave->();
677          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
678          !!!next-input-character;          !!!next-input-character;
679          redo A;          redo A;
680        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
681          $before_leave->();          $before_leave->();
682          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
683            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
684                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
685            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
686          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
687            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
688            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
689              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 691  sub _get_next_token ($) {
691          } else {          } else {
692            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
693          }          }
694          $self->{state} = 'data';          $self->{state} = DATA_STATE;
695          !!!next-input-character;          !!!next-input-character;
696    
697          !!!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 707  sub _get_next_token ($) {
707          $before_leave->();          $before_leave->();
708          !!!next-input-character;          !!!next-input-character;
709          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
710              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
711              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
712            # permitted slash            # permitted slash
713            #            #
714          } else {          } else {
715            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
716          }          }
717          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
718          # next-input-character is already done          # next-input-character is already done
719          redo A;          redo A;
720        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
721          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
722          $before_leave->();          $before_leave->();
723          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
724            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
725                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
726            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
727          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
728            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
729            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
730              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 648  sub _get_next_token ($) { Line 732  sub _get_next_token ($) {
732          } else {          } else {
733            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
734          }          }
735          $self->{state} = 'data';          $self->{state} = DATA_STATE;
736          # reconsume          # reconsume
737    
738          !!!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 744  sub _get_next_token ($) {
744          !!!next-input-character;          !!!next-input-character;
745          redo A;          redo A;
746        }        }
747      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
748        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
749            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
750            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 670  sub _get_next_token ($) { Line 754  sub _get_next_token ($) {
754          !!!next-input-character;          !!!next-input-character;
755          redo A;          redo A;
756        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
757          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
758          !!!next-input-character;          !!!next-input-character;
759          redo A;          redo A;
760        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
761          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
762            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
763                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
764            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
765          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
766            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
767            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
768              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 686  sub _get_next_token ($) { Line 770  sub _get_next_token ($) {
770          } else {          } else {
771            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
772          }          }
773          $self->{state} = 'data';          $self->{state} = DATA_STATE;
774          !!!next-input-character;          !!!next-input-character;
775    
776          !!!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 780  sub _get_next_token ($) {
780                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
781          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
782                                value => ''};                                value => ''};
783          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
784          !!!next-input-character;          !!!next-input-character;
785          redo A;          redo A;
786        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
787          !!!next-input-character;          !!!next-input-character;
788          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
789              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
790              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
791            # permitted slash            # permitted slash
792            #            #
# Line 710  sub _get_next_token ($) { Line 794  sub _get_next_token ($) {
794            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
795            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
796          }          }
797          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
798          # next-input-character is already done          # next-input-character is already done
799          redo A;          redo A;
800        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
801          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
802          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
803            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
804                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
805            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
806          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
807            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
808            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
809              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 727  sub _get_next_token ($) { Line 811  sub _get_next_token ($) {
811          } else {          } else {
812            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
813          }          }
814          $self->{state} = 'data';          $self->{state} = DATA_STATE;
815          # reconsume          # reconsume
816    
817          !!!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 820  sub _get_next_token ($) {
820        } else {        } else {
821          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
822                                value => ''};                                value => ''};
823          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
824          !!!next-input-character;          !!!next-input-character;
825          redo A;                  redo A;        
826        }        }
827      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
828        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
829            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
830            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 750  sub _get_next_token ($) { Line 834  sub _get_next_token ($) {
834          !!!next-input-character;          !!!next-input-character;
835          redo A;          redo A;
836        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
837          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
838          !!!next-input-character;          !!!next-input-character;
839          redo A;          redo A;
840        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
841          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
842          ## reconsume          ## reconsume
843          redo A;          redo A;
844        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
845          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
846          !!!next-input-character;          !!!next-input-character;
847          redo A;          redo A;
848        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
849          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
850            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
851                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
852            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
853          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
854            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
855            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
856              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 774  sub _get_next_token ($) { Line 858  sub _get_next_token ($) {
858          } else {          } else {
859            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
860          }          }
861          $self->{state} = 'data';          $self->{state} = DATA_STATE;
862          !!!next-input-character;          !!!next-input-character;
863    
864          !!!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 866  sub _get_next_token ($) {
866          redo A;          redo A;
867        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
868          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
869          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
870            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
871                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
872            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
873          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
874            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
875            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
876              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 794  sub _get_next_token ($) { Line 878  sub _get_next_token ($) {
878          } else {          } else {
879            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
880          }          }
881          $self->{state} = 'data';          $self->{state} = DATA_STATE;
882          ## reconsume          ## reconsume
883    
884          !!!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 886  sub _get_next_token ($) {
886          redo A;          redo A;
887        } else {        } else {
888          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
889          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
890          !!!next-input-character;          !!!next-input-character;
891          redo A;          redo A;
892        }        }
893      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
894        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
895          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
896          !!!next-input-character;          !!!next-input-character;
897          redo A;          redo A;
898        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
899          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
900          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
901          !!!next-input-character;          !!!next-input-character;
902          redo A;          redo A;
903        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
904          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
905          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
906            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
907                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
908            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
909          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
910            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
911            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
912              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 830  sub _get_next_token ($) { Line 914  sub _get_next_token ($) {
914          } else {          } else {
915            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
916          }          }
917          $self->{state} = 'data';          $self->{state} = DATA_STATE;
918          ## reconsume          ## reconsume
919    
920          !!!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 926  sub _get_next_token ($) {
926          !!!next-input-character;          !!!next-input-character;
927          redo A;          redo A;
928        }        }
929      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
930        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
931          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
932          !!!next-input-character;          !!!next-input-character;
933          redo A;          redo A;
934        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
935          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
936          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
937          !!!next-input-character;          !!!next-input-character;
938          redo A;          redo A;
939        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
940          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
941          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
942            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
943                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
944            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
945          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
946            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
947            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
948              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 866  sub _get_next_token ($) { Line 950  sub _get_next_token ($) {
950          } else {          } else {
951            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
952          }          }
953          $self->{state} = 'data';          $self->{state} = DATA_STATE;
954          ## reconsume          ## reconsume
955    
956          !!!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 962  sub _get_next_token ($) {
962          !!!next-input-character;          !!!next-input-character;
963          redo A;          redo A;
964        }        }
965      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
966        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
967            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
968            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
969            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
970            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
971          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
972          !!!next-input-character;          !!!next-input-character;
973          redo A;          redo A;
974        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
975          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
976          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
977          !!!next-input-character;          !!!next-input-character;
978          redo A;          redo A;
979        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
980          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
981            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
982                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
983            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
984          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
985            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
986            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
987              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 905  sub _get_next_token ($) { Line 989  sub _get_next_token ($) {
989          } else {          } else {
990            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
991          }          }
992          $self->{state} = 'data';          $self->{state} = DATA_STATE;
993          !!!next-input-character;          !!!next-input-character;
994    
995          !!!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 997  sub _get_next_token ($) {
997          redo A;          redo A;
998        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
999          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1000          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1001            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1002                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1003            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1004          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1005            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1006            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1007              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 925  sub _get_next_token ($) { Line 1009  sub _get_next_token ($) {
1009          } else {          } else {
1010            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1011          }          }
1012          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1013          ## reconsume          ## reconsume
1014    
1015          !!!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 1021  sub _get_next_token ($) {
1021          !!!next-input-character;          !!!next-input-character;
1022          redo A;          redo A;
1023        }        }
1024      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1025        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1026    
1027        unless (defined $token) {        unless (defined $token) {
# Line 950  sub _get_next_token ($) { Line 1034  sub _get_next_token ($) {
1034        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1035        # next-input-character is already done        # next-input-character is already done
1036        redo A;        redo A;
1037      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1038        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1039                
1040        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1041    
1042        BC: {        BC: {
1043          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1044            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1045            !!!next-input-character;            !!!next-input-character;
1046    
1047            !!!emit ($token);            !!!emit ($token);
1048    
1049            redo A;            redo A;
1050          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1051            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1052            ## reconsume            ## reconsume
1053    
1054            !!!emit ($token);            !!!emit ($token);
# Line 976  sub _get_next_token ($) { Line 1060  sub _get_next_token ($) {
1060            redo BC;            redo BC;
1061          }          }
1062        } # BC        } # BC
1063      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1064        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1065    
1066        my @next_char;        my @next_char;
# Line 986  sub _get_next_token ($) { Line 1070  sub _get_next_token ($) {
1070          !!!next-input-character;          !!!next-input-character;
1071          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1072          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1073            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1074            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1075            !!!next-input-character;            !!!next-input-character;
1076            redo A;            redo A;
1077          }          }
# Line 1018  sub _get_next_token ($) { Line 1102  sub _get_next_token ($) {
1102                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1103                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1104                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1105                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1106                      !!!next-input-character;                      !!!next-input-character;
1107                      redo A;                      redo A;
1108                    }                    }
# Line 1032  sub _get_next_token ($) { Line 1116  sub _get_next_token ($) {
1116        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1117        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1118        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1119        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1120        redo A;        redo A;
1121                
1122        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1123        ## 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?
1124      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1125        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1126          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1127          !!!next-input-character;          !!!next-input-character;
1128          redo A;          redo A;
1129        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1130          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1131          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1132          !!!next-input-character;          !!!next-input-character;
1133    
1134          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1052  sub _get_next_token ($) { Line 1136  sub _get_next_token ($) {
1136          redo A;          redo A;
1137        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1138          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1139          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1140          ## reconsume          ## reconsume
1141    
1142          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1061  sub _get_next_token ($) { Line 1145  sub _get_next_token ($) {
1145        } else {        } else {
1146          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1147              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1148          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1149          !!!next-input-character;          !!!next-input-character;
1150          redo A;          redo A;
1151        }        }
1152      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1153        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1154          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1155          !!!next-input-character;          !!!next-input-character;
1156          redo A;          redo A;
1157        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1158          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1159          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1160          !!!next-input-character;          !!!next-input-character;
1161    
1162          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1080  sub _get_next_token ($) { Line 1164  sub _get_next_token ($) {
1164          redo A;          redo A;
1165        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1166          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1167          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1168          ## reconsume          ## reconsume
1169    
1170          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1089  sub _get_next_token ($) { Line 1173  sub _get_next_token ($) {
1173        } else {        } else {
1174          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1175              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1176          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1177          !!!next-input-character;          !!!next-input-character;
1178          redo A;          redo A;
1179        }        }
1180      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1181        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1182          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1183          !!!next-input-character;          !!!next-input-character;
1184          redo A;          redo A;
1185        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1186          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1187          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1188          ## reconsume          ## reconsume
1189    
1190          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1112  sub _get_next_token ($) { Line 1196  sub _get_next_token ($) {
1196          !!!next-input-character;          !!!next-input-character;
1197          redo A;          redo A;
1198        }        }
1199      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1200        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1201          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1202          !!!next-input-character;          !!!next-input-character;
1203          redo A;          redo A;
1204        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1205          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1206          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1207          ## reconsume          ## reconsume
1208    
1209          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1127  sub _get_next_token ($) { Line 1211  sub _get_next_token ($) {
1211          redo A;          redo A;
1212        } else {        } else {
1213          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1214          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        }        }
1218      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1219        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1220          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1221          !!!next-input-character;          !!!next-input-character;
1222    
1223          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1147  sub _get_next_token ($) { Line 1231  sub _get_next_token ($) {
1231          redo A;          redo A;
1232        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1233          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1234          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1235          ## reconsume          ## reconsume
1236    
1237          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1156  sub _get_next_token ($) { Line 1240  sub _get_next_token ($) {
1240        } else {        } else {
1241          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1242          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1243          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1244          !!!next-input-character;          !!!next-input-character;
1245          redo A;          redo A;
1246        }        }
1247      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1248        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1249            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1250            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1251            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1252            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1253          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1254          !!!next-input-character;          !!!next-input-character;
1255          redo A;          redo A;
1256        } else {        } else {
1257          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1258          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1259          ## reconsume          ## reconsume
1260          redo A;          redo A;
1261        }        }
1262      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1263        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1264            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1265            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1186  sub _get_next_token ($) { Line 1270  sub _get_next_token ($) {
1270          redo A;          redo A;
1271        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1272          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1273          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1274          !!!next-input-character;          !!!next-input-character;
1275    
1276          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1277    
1278          redo A;          redo A;
1279        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1280          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1281          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1282          ## reconsume          ## reconsume
1283    
1284          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1285    
1286          redo A;          redo A;
1287        } else {        } else {
1288          $self->{current_token}          $self->{current_token}
1289              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1290                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1291                 correct => 1};                 correct => 1};
1292  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1293          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1294          !!!next-input-character;          !!!next-input-character;
1295          redo A;          redo A;
1296        }        }
1297      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1298  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1299        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1300            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1301            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1302            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1303            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1304          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1305          !!!next-input-character;          !!!next-input-character;
1306          redo A;          redo A;
1307        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1308          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1309          !!!next-input-character;          !!!next-input-character;
1310    
1311          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1229  sub _get_next_token ($) { Line 1313  sub _get_next_token ($) {
1313          redo A;          redo A;
1314        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1315          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1316          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1317          ## reconsume          ## reconsume
1318    
1319          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1243  sub _get_next_token ($) { Line 1327  sub _get_next_token ($) {
1327          !!!next-input-character;          !!!next-input-character;
1328          redo A;          redo A;
1329        }        }
1330      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1331        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1332            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1333            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1253  sub _get_next_token ($) { Line 1337  sub _get_next_token ($) {
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
1339        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1340          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1341          !!!next-input-character;          !!!next-input-character;
1342    
1343          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1261  sub _get_next_token ($) { Line 1345  sub _get_next_token ($) {
1345          redo A;          redo A;
1346        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1347          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1348          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1349          ## reconsume          ## reconsume
1350    
1351          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1285  sub _get_next_token ($) { Line 1369  sub _get_next_token ($) {
1369                  !!!next-input-character;                  !!!next-input-character;
1370                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1371                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1372                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1373                    !!!next-input-character;                    !!!next-input-character;
1374                    redo A;                    redo A;
1375                  }                  }
# Line 1312  sub _get_next_token ($) { Line 1396  sub _get_next_token ($) {
1396                  !!!next-input-character;                  !!!next-input-character;
1397                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1398                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1399                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1400                    !!!next-input-character;                    !!!next-input-character;
1401                    redo A;                    redo A;
1402                  }                  }
# Line 1328  sub _get_next_token ($) { Line 1412  sub _get_next_token ($) {
1412        }        }
1413    
1414        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1415        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1416        # next-input-character is already done        # next-input-character is already done
1417        redo A;        redo A;
1418      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1419        if ({        if ({
1420              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1421              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1341  sub _get_next_token ($) { Line 1425  sub _get_next_token ($) {
1425          redo A;          redo A;
1426        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1427          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1428          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1429          !!!next-input-character;          !!!next-input-character;
1430          redo A;          redo A;
1431        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1432          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1433          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1434          !!!next-input-character;          !!!next-input-character;
1435          redo A;          redo A;
1436        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1437          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1438    
1439          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1440          !!!next-input-character;          !!!next-input-character;
1441    
1442          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1362  sub _get_next_token ($) { Line 1446  sub _get_next_token ($) {
1446        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1447          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1448    
1449          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1450          ## reconsume          ## reconsume
1451    
1452          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1371  sub _get_next_token ($) { Line 1455  sub _get_next_token ($) {
1455          redo A;          redo A;
1456        } else {        } else {
1457          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1458          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1459          !!!next-input-character;          !!!next-input-character;
1460          redo A;          redo A;
1461        }        }
1462      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1463        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1464          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1465          !!!next-input-character;          !!!next-input-character;
1466          redo A;          redo A;
1467        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1468          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1469    
1470          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1471          ## reconsume          ## reconsume
1472    
1473          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1481  sub _get_next_token ($) {
1481          !!!next-input-character;          !!!next-input-character;
1482          redo A;          redo A;
1483        }        }
1484      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1485        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1486          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1487          !!!next-input-character;          !!!next-input-character;
1488          redo A;          redo A;
1489        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1490          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1491    
1492          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1493          ## reconsume          ## reconsume
1494    
1495          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1419  sub _get_next_token ($) { Line 1503  sub _get_next_token ($) {
1503          !!!next-input-character;          !!!next-input-character;
1504          redo A;          redo A;
1505        }        }
1506      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1507        if ({        if ({
1508              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1509              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1513  sub _get_next_token ($) {
1513          redo A;          redo A;
1514        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1515          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1516          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1517          !!!next-input-character;          !!!next-input-character;
1518          redo A;          redo A;
1519        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1520          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1521          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1522          !!!next-input-character;          !!!next-input-character;
1523          redo A;          redo A;
1524        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1525          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1526          !!!next-input-character;          !!!next-input-character;
1527    
1528          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1531  sub _get_next_token ($) {
1531        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1532          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1533    
1534          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1535          ## reconsume          ## reconsume
1536    
1537          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1456  sub _get_next_token ($) { Line 1540  sub _get_next_token ($) {
1540          redo A;          redo A;
1541        } else {        } else {
1542          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1543          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1544          !!!next-input-character;          !!!next-input-character;
1545          redo A;          redo A;
1546        }        }
1547      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1548        if ({        if ({
1549              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1550              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1554  sub _get_next_token ($) {
1554          redo A;          redo A;
1555        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1556          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1557          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1558          !!!next-input-character;          !!!next-input-character;
1559          redo A;          redo A;
1560        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1561          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1562          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1563          !!!next-input-character;          !!!next-input-character;
1564          redo A;          redo A;
1565        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1566          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1567          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1568          !!!next-input-character;          !!!next-input-character;
1569    
1570          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1490  sub _get_next_token ($) { Line 1574  sub _get_next_token ($) {
1574        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1575          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1576    
1577          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1578          ## reconsume          ## reconsume
1579    
1580          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1499  sub _get_next_token ($) { Line 1583  sub _get_next_token ($) {
1583          redo A;          redo A;
1584        } else {        } else {
1585          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1586          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1587          !!!next-input-character;          !!!next-input-character;
1588          redo A;          redo A;
1589        }        }
1590      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1591        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1592          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1593          !!!next-input-character;          !!!next-input-character;
1594          redo A;          redo A;
1595        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1596          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1597    
1598          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1599          ## reconsume          ## reconsume
1600    
1601          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609          !!!next-input-character;          !!!next-input-character;
1610          redo A;          redo A;
1611        }        }
1612      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1613        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1614          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1615          !!!next-input-character;          !!!next-input-character;
1616          redo A;          redo A;
1617        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1618          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1619    
1620          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1621          ## reconsume          ## reconsume
1622    
1623          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1547  sub _get_next_token ($) { Line 1631  sub _get_next_token ($) {
1631          !!!next-input-character;          !!!next-input-character;
1632          redo A;          redo A;
1633        }        }
1634      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1635        if ({        if ({
1636              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1637              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1640  sub _get_next_token ($) {
1640          !!!next-input-character;          !!!next-input-character;
1641          redo A;          redo A;
1642        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1644          !!!next-input-character;          !!!next-input-character;
1645    
1646          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1649  sub _get_next_token ($) {
1649        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1650          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1651    
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          ## reconsume          ## reconsume
1654    
1655          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1574  sub _get_next_token ($) { Line 1658  sub _get_next_token ($) {
1658          redo A;          redo A;
1659        } else {        } else {
1660          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1661          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        }        }
1665      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1666        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1667          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1668          !!!next-input-character;          !!!next-input-character;
1669    
1670          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1589  sub _get_next_token ($) { Line 1673  sub _get_next_token ($) {
1673          redo A;          redo A;
1674        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1675          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1676          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1677          ## reconsume          ## reconsume
1678    
1679          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1754  sub _tokenize_attempt_to_consume_an_enti
1754            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1755          }          }
1756    
1757          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code};
1758        } # X        } # X
1759      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1760               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1789  sub _tokenize_attempt_to_consume_an_enti
1789          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1790        }        }
1791                
1792        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1793      } else {      } else {
1794        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1795        !!!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 1837  sub _tokenize_attempt_to_consume_an_enti
1837      }      }
1838            
1839      if ($match > 0) {      if ($match > 0) {
1840        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value};
1841      } elsif ($match < 0) {      } elsif ($match < 0) {
1842        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1843        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1844          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1845        } else {        } else {
1846          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value};
1847        }        }
1848      } else {      } else {
1849        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1850        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1851        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1852      }      }
1853    } else {    } else {
1854      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 1890  sub _construct_tree ($) {
1890        
1891    !!!next-token;    !!!next-token;
1892    
1893    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1894    undef $self->{form_element};    undef $self->{form_element};
1895    undef $self->{head_element};    undef $self->{head_element};
1896    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 1904  sub _construct_tree ($) {
1904  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1905    my $self = shift;    my $self = shift;
1906    INITIAL: {    INITIAL: {
1907      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1908        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1909        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
1910        ## language.        ## language.
# Line 1947  sub _tree_construction_initial ($) { Line 2031  sub _tree_construction_initial ($) {
2031        !!!next-token;        !!!next-token;
2032        return;        return;
2033      } elsif ({      } elsif ({
2034                'start tag' => 1,                START_TAG_TOKEN, 1,
2035                'end tag' => 1,                END_TAG_TOKEN, 1,
2036                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2037               }->{$token->{type}}) {               }->{$token->{type}}) {
2038        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2039        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2040        ## Go to the root element phase        ## Go to the root element phase
2041        ## reprocess        ## reprocess
2042        return;        return;
2043      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2044        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2045          ## Ignore the token          ## Ignore the token
2046    
# Line 1972  sub _tree_construction_initial ($) { Line 2056  sub _tree_construction_initial ($) {
2056        ## Go to the root element phase        ## Go to the root element phase
2057        ## reprocess        ## reprocess
2058        return;        return;
2059      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2060        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2061        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2062                
# Line 1980  sub _tree_construction_initial ($) { Line 2064  sub _tree_construction_initial ($) {
2064        !!!next-token;        !!!next-token;
2065        redo INITIAL;        redo INITIAL;
2066      } else {      } else {
2067        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2068      }      }
2069    } # INITIAL    } # INITIAL
2070  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2073  sub _tree_construction_root_element ($)
2073    my $self = shift;    my $self = shift;
2074        
2075    B: {    B: {
2076        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2077          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2078          ## Ignore the token          ## Ignore the token
2079          ## Stay in the phase          ## Stay in the phase
2080          !!!next-token;          !!!next-token;
2081          redo B;          redo B;
2082        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2083          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2084          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2085          ## Stay in the phase          ## Stay in the phase
2086          !!!next-token;          !!!next-token;
2087          redo B;          redo B;
2088        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2089          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2090            ## Ignore the token.            ## Ignore the token.
2091    
# Line 2013  sub _tree_construction_root_element ($) Line 2097  sub _tree_construction_root_element ($)
2097          }          }
2098          #          #
2099        } elsif ({        } elsif ({
2100                  'start tag' => 1,                  START_TAG_TOKEN, 1,
2101                  'end tag' => 1,                  END_TAG_TOKEN, 1,
2102                  'end-of-file' => 1,                  END_OF_FILE_TOKEN, 1,
2103                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2104          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2105          #          #
2106        } else {        } else {
2107          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2108        }        }
2109          ## TODO: application cache selection algorithm
2110        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2111        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2112        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2147  sub _reset_insertion_mode ($) {
2147            
2148        ## Step 4..13        ## Step 4..13
2149        my $new_mode = {        my $new_mode = {
2150                        select => 'in select',                        select => IN_SELECT_IM,
2151                        td => 'in cell',                        td => IN_CELL_IM,
2152                        th => 'in cell',                        th => IN_CELL_IM,
2153                        tr => 'in row',                        tr => IN_ROW_IM,
2154                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2155                        thead => 'in table body',                        thead => IN_TABLE_BODY_IM,
2156                        tfoot => 'in table body',                        tfoot => IN_TABLE_BODY_IM,
2157                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2158                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2159                        table => 'in table',                        table => IN_TABLE_IM,
2160                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2161                        body => 'in body',                        body => IN_BODY_IM,
2162                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2163                       }->{$node->[1]};                       }->{$node->[1]};
2164        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2165                
2166        ## Step 14        ## Step 14
2167        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2168          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2169            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2170          } else {          } else {
2171            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2172          }          }
2173          return;          return;
2174        }        }
2175                
2176        ## Step 15        ## Step 15
2177        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2178                
2179        ## Step 16        ## Step 16
2180        $i--;        $i--;
# Line 2103  sub _reset_insertion_mode ($) { Line 2188  sub _reset_insertion_mode ($) {
2188  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2189    my $self = shift;    my $self = shift;
2190    
   my $previous_insertion_mode;  
   
2191    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2192    
2193    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2205  sub _tree_construction_main ($) { Line 2288  sub _tree_construction_main ($) {
2288      ## Step 4      ## Step 4
2289      my $text = '';      my $text = '';
2290      !!!next-token;      !!!next-token;
2291      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2292        $text .= $token->{data};        $text .= $token->{data};
2293        !!!next-token;        !!!next-token;
2294      }      }
# Line 2220  sub _tree_construction_main ($) { Line 2303  sub _tree_construction_main ($) {
2303      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2304    
2305      ## Step 7      ## Step 7
2306      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) {
2307        ## Ignore the token        ## Ignore the token
2308      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2309        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2243  sub _tree_construction_main ($) { Line 2326  sub _tree_construction_main ($) {
2326            
2327      my $text = '';      my $text = '';
2328      !!!next-token;      !!!next-token;
2329      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2330        $text .= $token->{data};        $text .= $token->{data};
2331        !!!next-token;        !!!next-token;
2332      } # 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 2336  sub _tree_construction_main ($) {
2336                                
2337      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2338    
2339      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2340          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2341        ## Ignore the token        ## Ignore the token
2342      } else {      } else {
# Line 2496  sub _tree_construction_main ($) { Line 2579  sub _tree_construction_main ($) {
2579                         }                         }
2580    }; # $insert_to_foster    }; # $insert_to_foster
2581    
2582    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  
2583    
2584    B: {    B: {
2585      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2586        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2587        ## Ignore the token        ## Ignore the token
2588        ## Stay in the phase        ## Stay in the phase
2589        !!!next-token;        !!!next-token;
2590        redo B;        redo B;
2591      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2592        if ($token->{insertion_mode} ne 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2593            #
2594          } else {
2595          ## Generate implied end tags          ## Generate implied end tags
2596          if ({          if ({
2597               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,
2598               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2599              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2600            !!!back-token;            !!!back-token;
2601            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2602            redo B;            redo B;
2603          }          }
2604                    
# Line 3375  sub _tree_construction_main ($) { Line 2616  sub _tree_construction_main ($) {
2616    
2617        ## Stop parsing        ## Stop parsing
2618        last B;        last B;
2619      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2620               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2621        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2622            ## Turn into the main phase
2623            !!!parse-error (type => 'after html:html');
2624            $self->{insertion_mode} = AFTER_BODY_IM;
2625          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2626          ## Turn into the main phase          ## Turn into the main phase
2627          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2628          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2629        }        }
2630    
2631  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 3398  sub _tree_construction_main ($) { Line 2643  sub _tree_construction_main ($) {
2643        }        }
2644        !!!next-token;        !!!next-token;
2645        redo B;        redo B;
2646      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2647        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2648        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2649          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2650        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2651          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2652        } else {        } else {
2653          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2654        }        }
2655        !!!next-token;        !!!next-token;
2656        redo B;        redo B;
2657      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2658            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
2659              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2660                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2661                unless (length $token->{data}) {            unless (length $token->{data}) {
2662                  !!!next-token;              !!!next-token;
2663                  redo B;              redo B;
2664                }            }
2665              }          }
2666              ## As if <head>  
2667              !!!create-element ($self->{head_element}, 'head');          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2668              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if <head>
2669              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!create-element ($self->{head_element}, 'head');
2670              $self->{insertion_mode} = 'in head';            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2671              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2672    
2673              ## Reprocess in the "in head" insertion mode...
2674              pop @{$self->{open_elements}};
2675    
2676              ## Reprocess in the "after head" insertion mode...
2677            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2678              ## As if </noscript>
2679              pop @{$self->{open_elements}};
2680              !!!parse-error (type => 'in noscript:#character');
2681              
2682              ## Reprocess in the "in head" insertion mode...
2683              ## As if </head>
2684              pop @{$self->{open_elements}};
2685    
2686              ## Reprocess in the "after head" insertion mode...
2687            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2688              pop @{$self->{open_elements}};
2689    
2690              ## Reprocess in the "after head" insertion mode...
2691            }
2692    
2693                ## "after head" insertion mode
2694                ## As if <body>
2695                !!!insert-element ('body');
2696                $self->{insertion_mode} = IN_BODY_IM;
2697              ## reprocess              ## reprocess
2698              redo B;              redo B;
2699            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
2700              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2701                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2702              } else {                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2703                ## reprocess                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2704              }                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2705              redo B;                  $self->{insertion_mode} = IN_HEAD_IM;
2706            } elsif ($token->{type} eq 'end tag') {                  !!!next-token;
2707              if ({                  redo B;
2708                   head => 1, body => 1, html => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2709                   p => 1, br => 1,                  #
2710                  }->{$token->{tag_name}}) {                } else {
2711                    !!!parse-error (type => 'in head:head'); # or in head noscript
2712                    ## Ignore the token
2713                    !!!next-token;
2714                    redo B;
2715                  }
2716                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2717                ## As if <head>                ## As if <head>
2718                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2719                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2720                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head' or  
                  $self->{insertion_mode} eq 'in head noscript' or  
                  $self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             if ($self->{insertion_mode} eq 'in head noscript') {  
               ## As if </noscript>  
               pop @{$self->{open_elements}};  
               !!!parse-error (type => 'in noscript:#character');  
                 
               ## Reprocess in the "in head" insertion mode...  
               ## As if </head>  
               pop @{$self->{open_elements}};  
2721    
2722                ## Reprocess in the "after head" insertion mode...                $self->{insertion_mode} = IN_HEAD_IM;
2723              } elsif ($self->{insertion_mode} eq 'in head') {                ## Reprocess in the "in head" insertion mode...
               pop @{$self->{open_elements}};  
   
               ## Reprocess in the "after head" insertion mode...  
2724              }              }
2725    
             ## 'after head' insertion mode  
             ## As if <body>  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
2726              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
2727                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2728                  ## As if </noscript>                  ## As if </noscript>
2729                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2730                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
2731                                
2732                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2733                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2734                }                }
2735    
2736                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2737                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2738                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2739                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2740                }                }
2741                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2742                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2743                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2744                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2745                !!!next-token;                !!!next-token;
2746                redo B;                redo B;
2747              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
2748                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2749                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2750                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2751                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2752                }                }
2753                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2754                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2755                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2756                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2757                !!!next-token;                !!!next-token;
2758                redo B;                redo B;
2759              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2760                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2761                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2762                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2763                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2764                }                }
# Line 3554  sub _tree_construction_main ($) { Line 2784  sub _tree_construction_main ($) {
2784    
2785                ## TODO: Extracting |charset| from |meta|.                ## TODO: Extracting |charset| from |meta|.
2786                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2787                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2788                !!!next-token;                !!!next-token;
2789                redo B;                redo B;
2790              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
2791                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2792                  ## As if </noscript>                  ## As if </noscript>
2793                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2794                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
2795                                
2796                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2797                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2798                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2799                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2800                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2801                }                }
# Line 3576  sub _tree_construction_main ($) { Line 2806  sub _tree_construction_main ($) {
2806                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2807                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2808                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2809                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2810                redo B;                redo B;
2811              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2812                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2813                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2814                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2815                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2816                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2817                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2818                }                }
2819                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2820                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2821                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2822                redo B;                redo B;
2823              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2824                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2825                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2826                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2827                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2828                  !!!next-token;                  !!!next-token;
2829                  redo B;                  redo B;
2830                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2831                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
2832                  ## Ignore the token                  ## Ignore the token
2833                  !!!next-token;                  !!!next-token;
# Line 3605  sub _tree_construction_main ($) { Line 2835  sub _tree_construction_main ($) {
2835                } else {                } else {
2836                  #                  #
2837                }                }
             } elsif ($token->{tag_name} eq 'head') {  
               if ($self->{insertion_mode} ne 'after head') {  
                 !!!parse-error (type => 'in head:head'); # or in head noscript  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 #  
               }  
2838              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
2839                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2840                  ## As if </noscript>                  ## As if </noscript>
2841                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2842                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
2843                                
2844                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2845                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2846                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2847                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2848                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2849                }                }
# Line 3630  sub _tree_construction_main ($) { Line 2851  sub _tree_construction_main ($) {
2851                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2852                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
2853                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2854                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2855                redo B;                redo B;
2856              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
2857                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
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:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 3644  sub _tree_construction_main ($) { Line 2865  sub _tree_construction_main ($) {
2865                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2866                                    
2867                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
2868                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2869                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2870                                    
2871                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
# Line 3652  sub _tree_construction_main ($) { Line 2873  sub _tree_construction_main ($) {
2873    
2874                ## "after head" insertion mode                ## "after head" insertion mode
2875                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2876                $self->{insertion_mode} = 'in '.$token->{tag_name};                if ($token->{tag_name} eq 'body') {
2877                    $self->{insertion_mode} = IN_BODY_IM;
2878                  } elsif ($token->{tag_name} eq 'frameset') {
2879                    $self->{insertion_mode} = IN_FRAMESET_IM;
2880                  } else {
2881                    die "$0: tag name: $self->{tag_name}";
2882                  }
2883                !!!next-token;                !!!next-token;
2884                redo B;                redo B;
2885              } else {              } else {
2886                #                #
2887              }              }
2888    
2889              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2890                ## As if </noscript>                ## As if </noscript>
2891                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2892                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3669  sub _tree_construction_main ($) { Line 2896  sub _tree_construction_main ($) {
2896                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2897    
2898                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
2899              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2900                ## As if </head>                ## As if </head>
2901                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2902    
# Line 3679  sub _tree_construction_main ($) { Line 2906  sub _tree_construction_main ($) {
2906              ## "after head" insertion mode              ## "after head" insertion mode
2907              ## As if <body>              ## As if <body>
2908              !!!insert-element ('body');              !!!insert-element ('body');
2909              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
2910              ## reprocess              ## reprocess
2911              redo B;              redo B;
2912            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
2913              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2914                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2915                    ## As if <head>
2916                    !!!create-element ($self->{head_element}, 'head');
2917                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2918                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2919    
2920                    ## Reprocess in the "in head" insertion mode...
2921                    pop @{$self->{open_elements}};
2922                    $self->{insertion_mode} = AFTER_HEAD_IM;
2923                    !!!next-token;
2924                    redo B;
2925                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2926                  ## As if </noscript>                  ## As if </noscript>
2927                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2928                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
2929                                    
                 $self->{insertion_mode} = 'in head';  
2930                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
               }  
                 
               if ($self->{insertion_mode} eq 'in head') {  
2931                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2932                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
2933                    !!!next-token;
2934                    redo B;
2935                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2936                    pop @{$self->{open_elements}};
2937                    $self->{insertion_mode} = AFTER_HEAD_IM;
2938                  !!!next-token;                  !!!next-token;
2939                  redo B;                  redo B;
2940                } else {                } else {
2941                  #                  #
2942                }                }
2943              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2944                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2945                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2946                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2947                    !!!next-token;
2948                    redo B;
2949                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2950                    !!!parse-error (type => 'unmatched end tag:noscript');
2951                    ## Ignore the token ## ISSUE: An issue in the spec.
2952                  !!!next-token;                  !!!next-token;
2953                  redo B;                  redo B;
2954                } else {                } else {
# Line 3713  sub _tree_construction_main ($) { Line 2957  sub _tree_construction_main ($) {
2957              } elsif ({              } elsif ({
2958                        body => 1, html => 1,                        body => 1, html => 1,
2959                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2960                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2961                    ## As if <head>
2962                    !!!create-element ($self->{head_element}, 'head');
2963                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2964                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2965    
2966                    $self->{insertion_mode} = IN_HEAD_IM;
2967                    ## Reprocess in the "in head" insertion mode...
2968                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2969                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2970                  ## Ignore the token                  ## Ignore the token
2971                  !!!next-token;                  !!!next-token;
2972                  redo B;                  redo B;
               } else { # 'in head', 'after head'  
                 #  
2973                }                }
2974                  
2975                  #
2976              } elsif ({              } elsif ({
2977                        p => 1, br => 1,                        p => 1, br => 1,
2978                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2979                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2980                    ## As if <head>
2981                    !!!create-element ($self->{head_element}, 'head');
2982                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2983                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2984    
2985                    $self->{insertion_mode} = IN_HEAD_IM;
2986                    ## Reprocess in the "in head" insertion mode...
2987                  }
2988    
2989                #                #
2990              } else {              } else {
2991                if ($self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2992                    #
2993                  } else {
2994                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2995                  ## Ignore the token                  ## Ignore the token
2996                  !!!next-token;                  !!!next-token;
2997                  redo B;                  redo B;
               } else {  
                 #  
2998                }                }
2999              }              }
3000    
3001              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3002                ## As if </noscript>                ## As if </noscript>
3003                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3004                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3746  sub _tree_construction_main ($) { Line 3008  sub _tree_construction_main ($) {
3008                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3009    
3010                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3011              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3012                ## As if </head>                ## As if </head>
3013                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3014    
3015                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3016                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3017                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3018                  ## Ignore the token ## ISSUE: An issue in the spec.
3019                  !!!next-token;
3020                  redo B;
3021              }              }
3022    
3023              ## "after head" insertion mode              ## "after head" insertion mode
3024              ## As if <body>              ## As if <body>
3025              !!!insert-element ('body');              !!!insert-element ('body');
3026              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3027              ## reprocess              ## reprocess
3028              redo B;              redo B;
3029            } else {            } else {
# Line 3764  sub _tree_construction_main ($) { Line 3031  sub _tree_construction_main ($) {
3031            }            }
3032    
3033            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3034          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
3035                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
                  $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
3036              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3037              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3038                            
# Line 3775  sub _tree_construction_main ($) { Line 3040  sub _tree_construction_main ($) {
3040    
3041              !!!next-token;              !!!next-token;
3042              redo B;              redo B;
3043            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3044              if ({              if ({
3045                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3046                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3047                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3048                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3049                  ## have an element in table scope                  ## have an element in table scope
3050                  my $tn;                  my $tn;
3051                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3803  sub _tree_construction_main ($) { Line 3068  sub _tree_construction_main ($) {
3068                                    
3069                  ## Close the cell                  ## Close the cell
3070                  !!!back-token; # <?>                  !!!back-token; # <?>
3071                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3072                  redo B;                  redo B;
3073                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3074                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3075                                    
3076                  ## As if </caption>                  ## As if </caption>
# Line 3836  sub _tree_construction_main ($) { Line 3101  sub _tree_construction_main ($) {
3101                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3102                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3103                    !!!back-token; # <?>                    !!!back-token; # <?>
3104                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3105                    !!!back-token;                    !!!back-token;
3106                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3107                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3108                    redo B;                    redo B;
3109                  }                  }
# Line 3851  sub _tree_construction_main ($) { Line 3116  sub _tree_construction_main ($) {
3116                                    
3117                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3118                                    
3119                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3120                                    
3121                  ## reprocess                  ## reprocess
3122                  redo B;                  redo B;
# Line 3861  sub _tree_construction_main ($) { Line 3126  sub _tree_construction_main ($) {
3126              } else {              } else {
3127                #                #
3128              }              }
3129            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3130              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3131                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3132                  ## have an element in table scope                  ## have an element in table scope
3133                  my $i;                  my $i;
3134                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3893  sub _tree_construction_main ($) { Line 3158  sub _tree_construction_main ($) {
3158                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3159                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3160                    !!!back-token;                    !!!back-token;
3161                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3162                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3163                    redo B;                    redo B;
3164                  }                  }
# Line 3906  sub _tree_construction_main ($) { Line 3171  sub _tree_construction_main ($) {
3171                                    
3172                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3173                                    
3174                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3175                                    
3176                  !!!next-token;                  !!!next-token;
3177                  redo B;                  redo B;
3178                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3179                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3180                  ## Ignore the token                  ## Ignore the token
3181                  !!!next-token;                  !!!next-token;
# Line 3919  sub _tree_construction_main ($) { Line 3184  sub _tree_construction_main ($) {
3184                  #                  #
3185                }                }
3186              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3187                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3188                  ## have a table element in table scope                  ## have a table element in table scope
3189                  my $i;                  my $i;
3190                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3947  sub _tree_construction_main ($) { Line 3212  sub _tree_construction_main ($) {
3212                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3213                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3214                    !!!back-token;                    !!!back-token;
3215                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3216                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3217                    redo B;                    redo B;
3218                  }                  }
# Line 3960  sub _tree_construction_main ($) { Line 3225  sub _tree_construction_main ($) {
3225                                    
3226                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3227                                    
3228                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3229                                    
3230                  !!!next-token;                  !!!next-token;
3231                  redo B;                  redo B;
3232                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3233                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3234                  ## Ignore the token                  ## Ignore the token
3235                  !!!next-token;                  !!!next-token;
# Line 3976  sub _tree_construction_main ($) { Line 3241  sub _tree_construction_main ($) {
3241                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3242                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3243                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3244                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3245                ## have an element in table scope                ## have an element in table scope
3246                my $i;                my $i;
3247                my $tn;                my $tn;
# Line 4004  sub _tree_construction_main ($) { Line 3269  sub _tree_construction_main ($) {
3269    
3270                ## Close the cell                ## Close the cell
3271                !!!back-token; # </?>                !!!back-token; # </?>
3272                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3273                redo B;                redo B;
3274              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3275                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3276                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3277    
3278                ## As if </caption>                ## As if </caption>
# Line 4038  sub _tree_construction_main ($) { Line 3303  sub _tree_construction_main ($) {
3303                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3304                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3305                  !!!back-token; # </table>                  !!!back-token; # </table>
3306                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3307                  !!!back-token;                  !!!back-token;
3308                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3309                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3310                  redo B;                  redo B;
3311                }                }
# Line 4053  sub _tree_construction_main ($) { Line 3318  sub _tree_construction_main ($) {
3318    
3319                $clear_up_to_marker->();                $clear_up_to_marker->();
3320    
3321                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3322    
3323                ## reprocess                ## reprocess
3324                redo B;                redo B;
3325              } elsif ({              } elsif ({
3326                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3327                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3328                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3329                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3330                  ## Ignore the token                  ## Ignore the token
3331                  !!!next-token;                  !!!next-token;
# Line 4073  sub _tree_construction_main ($) { Line 3337  sub _tree_construction_main ($) {
3337                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3338                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3339                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3340                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3341                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3342                ## Ignore the token                ## Ignore the token
3343                !!!next-token;                !!!next-token;
# Line 4081  sub _tree_construction_main ($) { Line 3345  sub _tree_construction_main ($) {
3345              } else {              } else {
3346                #                #
3347              }              }
3348            } else {        } else {
3349              #          die "$0: $token->{type}: Unknown token type";
3350            }        }
3351              
3352            $in_body->($insert_to_current);        $insert = $insert_to_current;
3353            redo B;        #
3354          } elsif ($self->{insertion_mode} eq 'in row' or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3355                   $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.  
3356              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3357                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3358                                
# Line 4148  sub _tree_construction_main ($) { Line 3409  sub _tree_construction_main ($) {
3409                            
3410              !!!next-token;              !!!next-token;
3411              redo B;              redo B;
3412            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3413              if ({              if ({
3414                   tr => ($self->{insertion_mode} ne 'in row'),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3415                   th => 1, td => 1,                   th => 1, td => 1,
3416                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3417                if ($self->{insertion_mode} eq 'in table') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3418                  ## Clear back to table context                  ## Clear back to table context
3419                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3420                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
# Line 4162  sub _tree_construction_main ($) { Line 3423  sub _tree_construction_main ($) {
3423                  }                  }
3424                                    
3425                  !!!insert-element ('tbody');                  !!!insert-element ('tbody');
3426                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3427                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3428                }                }
3429    
3430                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3431                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
3432                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
3433                  }                  }
# Line 4179  sub _tree_construction_main ($) { Line 3440  sub _tree_construction_main ($) {
3440                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3441                  }                  }
3442                                    
3443                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3444                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3445                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3446                    !!!next-token;                    !!!next-token;
# Line 4199  sub _tree_construction_main ($) { Line 3460  sub _tree_construction_main ($) {
3460                }                }
3461                                
3462                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3463                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3464    
3465                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3466                                
# Line 4208  sub _tree_construction_main ($) { Line 3469  sub _tree_construction_main ($) {
3469              } elsif ({              } elsif ({
3470                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3471                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3472                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3473                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3474                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3475                  ## As if </tr>                  ## As if </tr>
3476                  ## have an element in table scope                  ## have an element in table scope
3477                  my $i;                  my $i;
# Line 4241  sub _tree_construction_main ($) { Line 3502  sub _tree_construction_main ($) {
3502                  }                  }
3503                                    
3504                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3505                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3506                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3507                    ## reprocess                    ## reprocess
3508                    redo B;                    redo B;
# Line 4250  sub _tree_construction_main ($) { Line 3511  sub _tree_construction_main ($) {
3511                  }                  }
3512                }                }
3513    
3514                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3515                  ## have an element in table scope                  ## have an element in table scope
3516                  my $i;                  my $i;
3517                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4289  sub _tree_construction_main ($) { Line 3550  sub _tree_construction_main ($) {
3550                  ## nop by definition                  ## nop by definition
3551                                    
3552                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3553                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3554                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
3555                }                }
3556    
# Line 4302  sub _tree_construction_main ($) { Line 3563  sub _tree_construction_main ($) {
3563                  }                  }
3564                                    
3565                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup');
3566                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3567                  ## reprocess                  ## reprocess
3568                  redo B;                  redo B;
3569                } elsif ({                } elsif ({
# Line 4322  sub _tree_construction_main ($) { Line 3583  sub _tree_construction_main ($) {
3583                                    
3584                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3585                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
3586                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
3587                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
3588                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
3589                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
3590                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
3591                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
3592                  !!!next-token;                  !!!next-token;
3593                  redo B;                  redo B;
# Line 4334  sub _tree_construction_main ($) { Line 3595  sub _tree_construction_main ($) {
3595                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3596                }                }
3597              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3598                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3599    
3600                ## As if </table>                ## As if </table>
# Line 4365  sub _tree_construction_main ($) { Line 3625  sub _tree_construction_main ($) {
3625                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3626                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3627                  !!!back-token; # <table>                  !!!back-token; # <table>
3628                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3629                  !!!back-token;                  !!!back-token;
3630                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3631                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3632                  redo B;                  redo B;
3633                }                }
# Line 4382  sub _tree_construction_main ($) { Line 3642  sub _tree_construction_main ($) {
3642    
3643                ## reprocess                ## reprocess
3644                redo B;                redo B;
3645              } else {          } else {
3646                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3647              }  
3648            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3649              #
3650            }
3651          } elsif ($token->{type} == END_TAG_TOKEN) {
3652              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3653                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
3654                ## have an element in table scope                ## have an element in table scope
3655                my $i;                my $i;
3656                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4417  sub _tree_construction_main ($) { Line 3680  sub _tree_construction_main ($) {
3680                }                }
3681    
3682                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3683                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3684                !!!next-token;                !!!next-token;
3685                redo B;                redo B;
3686              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3687                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3688                  ## As if </tr>                  ## As if </tr>
3689                  ## have an element in table scope                  ## have an element in table scope
3690                  my $i;                  my $i;
# Line 4452  sub _tree_construction_main ($) { Line 3715  sub _tree_construction_main ($) {
3715                  }                  }
3716                                    
3717                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3718                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3719                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3720                }                }
3721    
3722                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3723                  ## have an element in table scope                  ## have an element in table scope
3724                  my $i;                  my $i;
3725                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4495  sub _tree_construction_main ($) { Line 3758  sub _tree_construction_main ($) {
3758                  ## nop by definition                  ## nop by definition
3759                                    
3760                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3761                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3762                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
3763                }                }
3764    
# Line 4526  sub _tree_construction_main ($) { Line 3789  sub _tree_construction_main ($) {
3789                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3790                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3791                  !!!back-token;                  !!!back-token;
3792                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3793                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3794                  redo B;                  redo B;
3795                }                }
# Line 4544  sub _tree_construction_main ($) { Line 3807  sub _tree_construction_main ($) {
3807              } elsif ({              } elsif ({
3808                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3809                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3810                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
3811                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
3812                  ## have an element in table scope                  ## have an element in table scope
3813                  my $i;                  my $i;
3814                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4597  sub _tree_construction_main ($) { Line 3859  sub _tree_construction_main ($) {
3859                  }                  }
3860                                    
3861                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3862                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3863                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3864                }                }
3865    
# Line 4630  sub _tree_construction_main ($) { Line 3892  sub _tree_construction_main ($) {
3892                }                }
3893    
3894                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3895                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3896                !!!next-token;                !!!next-token;
3897                redo B;                redo B;
3898              } elsif ({              } elsif ({
3899                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3900                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3901                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3902                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3903                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3904                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3905                ## Ignore the token                ## Ignore the token
3906                !!!next-token;                !!!next-token;
3907                redo B;                redo B;
3908              } else {          } else {
3909                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
3910    
3911            !!!parse-error (type => 'in table:'.$token->{tag_name});            $insert = $insert_to_foster;
3912            $in_body->($insert_to_foster);            #
3913            redo B;          }
3914          } elsif ($self->{insertion_mode} eq 'in column group') {        } else {
3915            if ($token->{type} eq 'character') {          die "$0: $token->{type}: Unknown token type";
3916          }
3917        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3918              if ($token->{type} == CHARACTER_TOKEN) {
3919              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3920                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3921                unless (length $token->{data}) {                unless (length $token->{data}) {
# Line 4664  sub _tree_construction_main ($) { Line 3925  sub _tree_construction_main ($) {
3925              }              }
3926                            
3927              #              #
3928            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3929              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
3930                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3931                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
# Line 4673  sub _tree_construction_main ($) { Line 3934  sub _tree_construction_main ($) {
3934              } else {              } else {
3935                #                #
3936              }              }
3937            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3938              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
3939                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3940                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
# Line 4682  sub _tree_construction_main ($) { Line 3943  sub _tree_construction_main ($) {
3943                  redo B;                  redo B;
3944                } else {                } else {
3945                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
3946                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3947                  !!!next-token;                  !!!next-token;
3948                  redo B;                              redo B;            
3949                }                }
# Line 4706  sub _tree_construction_main ($) { Line 3967  sub _tree_construction_main ($) {
3967              redo B;              redo B;
3968            } else {            } else {
3969              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
3970              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
3971              ## reprocess              ## reprocess
3972              redo B;              redo B;
3973            }            }
3974          } elsif ($self->{insertion_mode} eq 'in select') {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3975            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
3976              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3977              !!!next-token;          !!!next-token;
3978              redo B;          redo B;
3979            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3980              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
3981                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
3982                  ## As if </option>                  ## As if </option>
# Line 4768  sub _tree_construction_main ($) { Line 4029  sub _tree_construction_main ($) {
4029    
4030                !!!next-token;                !!!next-token;
4031                redo B;                redo B;
4032              } else {          } else {
4033                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4034              }            ## Ignore the token
4035            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4036              redo B;
4037            }
4038          } elsif ($token->{type} == END_TAG_TOKEN) {
4039              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4040                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4041                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4873  sub _tree_construction_main ($) { Line 4137  sub _tree_construction_main ($) {
4137    
4138                ## reprocess                ## reprocess
4139                redo B;                redo B;
4140              } else {          } else {
4141                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4142            ## Ignore the token            ## Ignore the token
4143            !!!next-token;            !!!next-token;
4144            redo B;            redo B;
4145          } elsif ($self->{insertion_mode} eq 'after body') {          }
4146            if ($token->{type} eq 'character') {        } else {
4147              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4148                my $data = $1;        }
4149                ## As if in body      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4150                $reconstruct_active_formatting_elements->($insert_to_current);        if ($token->{type} == CHARACTER_TOKEN) {
4151            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4152              my $data = $1;
4153              ## As if in body
4154              $reconstruct_active_formatting_elements->($insert_to_current);
4155                                
4156                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4157              
4158              unless (length $token->{data}) {
4159                !!!next-token;
4160                redo B;
4161              }
4162            }
4163            
4164            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4165              !!!parse-error (type => 'after html:#character');
4166    
4167                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4168                  !!!next-token;          }
4169                  redo B;          
4170                }          ## "after body" insertion mode
4171              }          !!!parse-error (type => 'after body:#character');
4172                
4173              #          $self->{insertion_mode} = IN_BODY_IM;
4174              !!!parse-error (type => 'after body:#character');          ## reprocess
4175            } elsif ($token->{type} eq 'start tag') {          redo B;
4176              !!!parse-error (type => 'after body:'.$token->{tag_name});        } elsif ($token->{type} == START_TAG_TOKEN) {
4177              #          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4178            } elsif ($token->{type} eq 'end tag') {            !!!parse-error (type => 'after html:'.$token->{tag_name});
4179              if ($token->{tag_name} eq 'html') {            
4180                if (defined $self->{inner_html_node}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4181                  !!!parse-error (type => 'unmatched end tag:html');          }
4182                  ## Ignore the token  
4183                  !!!next-token;          ## "after body" insertion mode
4184                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
4185                } else {  
4186                  $previous_insertion_mode = $self->{insertion_mode};          $self->{insertion_mode} = IN_BODY_IM;
4187                  $self->{insertion_mode} = 'trailing end';          ## reprocess
4188                  !!!next-token;          redo B;
4189                  redo B;        } elsif ($token->{type} == END_TAG_TOKEN) {
4190                }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4191              } else {            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4192                !!!parse-error (type => 'after body:/'.$token->{tag_name});            
4193              }            $self->{insertion_mode} = AFTER_BODY_IM;
4194              ## Reprocess in the "main" phase, "after body" insertion mode...
4195            }
4196    
4197            ## "after body" insertion mode
4198            if ($token->{tag_name} eq 'html') {
4199              if (defined $self->{inner_html_node}) {
4200                !!!parse-error (type => 'unmatched end tag:html');
4201                ## Ignore the token
4202                !!!next-token;
4203                redo B;
4204            } else {            } else {
4205              die "$0: $token->{type}: Unknown token type";              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4206                !!!next-token;
4207                redo B;
4208            }            }
4209            } else {
4210              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4211    
4212            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4213            ## reprocess            ## reprocess
4214            redo B;            redo B;
4215      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4216        if ($token->{type} eq 'character') {        } else {
4217            die "$0: $token->{type}: Unknown token type";
4218          }
4219        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4220          if ($token->{type} == CHARACTER_TOKEN) {
4221          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4222            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4223              
4224            unless (length $token->{data}) {            unless (length $token->{data}) {
4225              !!!next-token;              !!!next-token;
4226              redo B;              redo B;
4227            }            }
4228          }          }
4229            
4230            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4231              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4232                !!!parse-error (type => 'in frameset:#character');
4233              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4234                !!!parse-error (type => 'after frameset:#character');
4235              } else { # "after html frameset"
4236                !!!parse-error (type => 'after html:#character');
4237    
4238                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4239                ## Reprocess in the "main" phase, "after frameset"...
4240                !!!parse-error (type => 'after frameset:#character');
4241              }
4242              
4243              ## Ignore the token.
4244              if (length $token->{data}) {
4245                ## reprocess the rest of characters
4246              } else {
4247                !!!next-token;
4248              }
4249              redo B;
4250            }
4251            
4252            die qq[$0: Character "$token->{data}"];
4253          } elsif ($token->{type} == START_TAG_TOKEN) {
4254            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4255              !!!parse-error (type => 'after html:'.$token->{tag_name});
4256    
4257          !!!parse-error (type => 'in frameset:#character');            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4258          ## Ignore the token            ## Process in the "main" phase, "after frameset" insertion mode...
4259          !!!next-token;          }
4260          redo B;  
4261        } elsif ($token->{type} eq 'start tag') {          if ($token->{tag_name} eq 'frameset' and
4262          if ($token->{tag_name} eq 'frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4263            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4264            !!!next-token;            !!!next-token;
4265            redo B;            redo B;
4266          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
4267                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4268            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4269            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4270            !!!next-token;            !!!next-token;
# Line 4957  sub _tree_construction_main ($) { Line 4274  sub _tree_construction_main ($) {
4274            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4275            redo B;            redo B;
4276          } else {          } else {
4277            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4278                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4279              } else {
4280                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4281              }
4282            ## Ignore the token            ## Ignore the token
4283            !!!next-token;            !!!next-token;
4284            redo B;            redo B;
4285          }          }
4286        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4287          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4288              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4289    
4290              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4291              ## Process in the "main" phase, "after frameset" insertion mode...
4292            }
4293    
4294            if ($token->{tag_name} eq 'frameset' and
4295                $self->{insertion_mode} == IN_FRAMESET_IM) {
4296            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4297                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4298              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 4976  sub _tree_construction_main ($) { Line 4305  sub _tree_construction_main ($) {
4305    
4306            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4307                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4308              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4309            }            }
4310            redo B;            redo B;
4311            } elsif ($token->{tag_name} eq 'html' and
4312                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4313              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4314              !!!next-token;
4315              redo B;
4316          } else {          } else {
4317            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4318                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4319              } else {
4320                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4321              }
4322            ## Ignore the token            ## Ignore the token
4323            !!!next-token;            !!!next-token;
4324            redo B;            redo B;
# Line 4988  sub _tree_construction_main ($) { Line 4326  sub _tree_construction_main ($) {
4326        } else {        } else {
4327          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4328        }        }
     } 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);  
4329    
4330                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4331                  !!!next-token;      } else {
4332                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4333                }      }
             }  
4334    
4335              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
4336                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
4337          if ($token->{tag_name} eq 'script') {
4338            ## NOTE: This is an "as if in head" code clone
4339            $script_start_tag->($insert);
4340            redo B;
4341          } elsif ($token->{tag_name} eq 'style') {
4342            ## NOTE: This is an "as if in head" code clone
4343            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4344            redo B;
4345          } elsif ({
4346                    base => 1, link => 1,
4347                   }->{$token->{tag_name}}) {
4348            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4349            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4350            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4351            !!!next-token;
4352            redo B;
4353          } elsif ($token->{tag_name} eq 'meta') {
4354            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4355            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4356            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4357    
4358                ## Ignore the token.          unless ($self->{confident}) {
4359                if (length $token->{data}) {            my $charset;
4360                  ## reprocess the rest of characters            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4361                } else {              $charset = $token->{attributes}->{charset}->{value};
4362                  !!!next-token;            }
4363                }            if ($token->{attributes}->{'http-equiv'}) {
4364                redo B;              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4365              }              if ($token->{attributes}->{'http-equiv'}->{value}
4366                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4367                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4368                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4369                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4370                } ## TODO: And if supported
4371              }
4372              ## TODO: Change the encoding
4373            }
4374    
4375          die qq[$0: Character "$token->{data}"];          !!!next-token;
4376        } elsif ($token->{type} eq 'start tag') {          redo B;
4377          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
4378            ## NOTE: As if in body.          !!!parse-error (type => 'in body:title');
4379            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);          ## NOTE: This is an "as if in head" code clone
4380            redo B;          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4381          } else {            if (defined $self->{head_element}) {
4382            !!!parse-error (type => 'after frameset:'.$token->{tag_name});              $self->{head_element}->append_child ($_[0]);
4383              } else {
4384                $insert->($_[0]);
4385              }
4386            });
4387            redo B;
4388          } elsif ($token->{tag_name} eq 'body') {
4389            !!!parse-error (type => 'in body:body');
4390                  
4391            if (@{$self->{open_elements}} == 1 or
4392                $self->{open_elements}->[1]->[1] ne 'body') {
4393            ## Ignore the token            ## Ignore the token
4394            } else {
4395              my $body_el = $self->{open_elements}->[1]->[0];
4396              for my $attr_name (keys %{$token->{attributes}}) {
4397                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4398                  $body_el->set_attribute_ns
4399                    (undef, [undef, $attr_name],
4400                     $token->{attributes}->{$attr_name}->{value});
4401                }
4402              }
4403            }
4404            !!!next-token;
4405            redo B;
4406          } elsif ({
4407                    address => 1, blockquote => 1, center => 1, dir => 1,
4408                    div => 1, dl => 1, fieldset => 1, listing => 1,
4409                    menu => 1, ol => 1, p => 1, ul => 1,
4410                    pre => 1,
4411                   }->{$token->{tag_name}}) {
4412            ## has a p element in scope
4413            INSCOPE: for (reverse @{$self->{open_elements}}) {
4414              if ($_->[1] eq 'p') {
4415                !!!back-token;
4416                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4417                redo B;
4418              } elsif ({
4419                        table => 1, caption => 1, td => 1, th => 1,
4420                        button => 1, marquee => 1, object => 1, html => 1,
4421                       }->{$_->[1]}) {
4422                last INSCOPE;
4423              }
4424            } # INSCOPE
4425              
4426            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4427            if ($token->{tag_name} eq 'pre') {
4428              !!!next-token;
4429              if ($token->{type} == CHARACTER_TOKEN) {
4430                $token->{data} =~ s/^\x0A//;
4431                unless (length $token->{data}) {
4432                  !!!next-token;
4433                }
4434              }
4435            } else {
4436            !!!next-token;            !!!next-token;
           redo B;  
4437          }          }
4438        } elsif ($token->{type} eq 'end tag') {          redo B;
4439          if ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'form') {
4440            $previous_insertion_mode = $self->{insertion_mode};          if (defined $self->{form_element}) {
4441            $self->{insertion_mode} = 'trailing end';            !!!parse-error (type => 'in form:form');
4442              ## Ignore the token
4443            !!!next-token;            !!!next-token;
4444            redo B;            redo B;
4445          } else {          } else {
4446            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            ## has a p element in scope
4447            ## Ignore the token            INSCOPE: for (reverse @{$self->{open_elements}}) {
4448                if ($_->[1] eq 'p') {
4449                  !!!back-token;
4450                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4451                  redo B;
4452                } elsif ({
4453                          table => 1, caption => 1, td => 1, th => 1,
4454                          button => 1, marquee => 1, object => 1, html => 1,
4455                         }->{$_->[1]}) {
4456                  last INSCOPE;
4457                }
4458              } # INSCOPE
4459                
4460              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4461              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4462            !!!next-token;            !!!next-token;
4463            redo B;            redo B;
4464          }          }
4465        } else {        } elsif ($token->{tag_name} eq 'li') {
4466          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
4467        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
4468              if ($_->[1] eq 'p') {
4469                !!!back-token;
4470                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4471                redo B;
4472              } elsif ({
4473                        table => 1, caption => 1, td => 1, th => 1,
4474                        button => 1, marquee => 1, object => 1, html => 1,
4475                       }->{$_->[1]}) {
4476                last INSCOPE;
4477              }
4478            } # INSCOPE
4479              
4480            ## Step 1
4481            my $i = -1;
4482            my $node = $self->{open_elements}->[$i];
4483            LI: {
4484              ## Step 2
4485              if ($node->[1] eq 'li') {
4486                if ($i != -1) {
4487                  !!!parse-error (type => 'end tag missing:'.
4488                                  $self->{open_elements}->[-1]->[1]);
4489                }
4490                splice @{$self->{open_elements}}, $i;
4491                last LI;
4492              }
4493              
4494              ## Step 3
4495              if (not $formatting_category->{$node->[1]} and
4496                  #not $phrasing_category->{$node->[1]} and
4497                  ($special_category->{$node->[1]} or
4498                   $scoping_category->{$node->[1]}) and
4499                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4500                last LI;
4501              }
4502              
4503              ## Step 4
4504              $i--;
4505              $node = $self->{open_elements}->[$i];
4506              redo LI;
4507            } # LI
4508              
4509            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4510            !!!next-token;
4511            redo B;
4512          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4513            ## has a p element in scope
4514            INSCOPE: for (reverse @{$self->{open_elements}}) {
4515              if ($_->[1] eq 'p') {
4516                !!!back-token;
4517                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4518                redo B;
4519              } elsif ({
4520                        table => 1, caption => 1, td => 1, th => 1,
4521                        button => 1, marquee => 1, object => 1, html => 1,
4522                       }->{$_->[1]}) {
4523                last INSCOPE;
4524              }
4525            } # INSCOPE
4526              
4527            ## Step 1
4528            my $i = -1;
4529            my $node = $self->{open_elements}->[$i];
4530            LI: {
4531              ## Step 2
4532              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4533                if ($i != -1) {
4534                  !!!parse-error (type => 'end tag missing:'.
4535                                  $self->{open_elements}->[-1]->[1]);
4536                }
4537                splice @{$self->{open_elements}}, $i;
4538                last LI;
4539              }
4540              
4541              ## Step 3
4542              if (not $formatting_category->{$node->[1]} and
4543                  #not $phrasing_category->{$node->[1]} and
4544                  ($special_category->{$node->[1]} or
4545                   $scoping_category->{$node->[1]}) and
4546                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4547                last LI;
4548              }
4549              
4550              ## Step 4
4551              $i--;
4552              $node = $self->{open_elements}->[$i];
4553              redo LI;
4554            } # LI
4555              
4556            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4557            !!!next-token;
4558            redo B;
4559          } elsif ($token->{tag_name} eq 'plaintext') {
4560            ## has a p element in scope
4561            INSCOPE: for (reverse @{$self->{open_elements}}) {
4562              if ($_->[1] eq 'p') {
4563                !!!back-token;
4564                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4565                redo B;
4566              } elsif ({
4567                        table => 1, caption => 1, td => 1, th => 1,
4568                        button => 1, marquee => 1, object => 1, html => 1,
4569                       }->{$_->[1]}) {
4570                last INSCOPE;
4571              }
4572            } # INSCOPE
4573              
4574            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4575              
4576            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4577              
4578            !!!next-token;
4579            redo B;
4580          } elsif ({
4581                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4582                   }->{$token->{tag_name}}) {
4583            ## has a p element in scope
4584            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4585              my $node = $self->{open_elements}->[$_];
4586              if ($node->[1] eq 'p') {
4587                !!!back-token;
4588                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4589                redo B;
4590              } elsif ({
4591                        table => 1, caption => 1, td => 1, th => 1,
4592                        button => 1, marquee => 1, object => 1, html => 1,
4593                       }->{$node->[1]}) {
4594                last INSCOPE;
4595              }
4596            } # INSCOPE
4597              
4598            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4599            ## has an element in scope
4600            #my $i;
4601            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4602            #  my $node = $self->{open_elements}->[$_];
4603            #  if ({
4604            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4605            #      }->{$node->[1]}) {
4606            #    $i = $_;
4607            #    last INSCOPE;
4608            #  } elsif ({
4609            #            table => 1, caption => 1, td => 1, th => 1,
4610            #            button => 1, marquee => 1, object => 1, html => 1,
4611            #           }->{$node->[1]}) {
4612            #    last INSCOPE;
4613            #  }
4614            #} # INSCOPE
4615            #  
4616            #if (defined $i) {
4617            #  !!! parse-error (type => 'in hn:hn');
4618            #  splice @{$self->{open_elements}}, $i;
4619            #}
4620              
4621            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4622              
4623            !!!next-token;
4624            redo B;
4625          } elsif ($token->{tag_name} eq 'a') {
4626            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4627              my $node = $active_formatting_elements->[$i];
4628              if ($node->[1] eq 'a') {
4629                !!!parse-error (type => 'in a:a');
4630                
4631                !!!back-token;
4632                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4633                $formatting_end_tag->($token->{tag_name});
4634                
4635                AFE2: for (reverse 0..$#$active_formatting_elements) {
4636                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4637                    splice @$active_formatting_elements, $_, 1;
4638                    last AFE2;
4639                  }
4640                } # AFE2
4641                OE: for (reverse 0..$#{$self->{open_elements}}) {
4642                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4643                    splice @{$self->{open_elements}}, $_, 1;
4644                    last OE;
4645                  }
4646                } # OE
4647                last AFE;
4648              } elsif ($node->[0] eq '#marker') {
4649                last AFE;
4650              }
4651            } # AFE
4652              
4653            $reconstruct_active_formatting_elements->($insert_to_current);
4654    
4655        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4656      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
4657        ## states in the main stage is preserved yet # MUST  
4658                  !!!next-token;
4659        if ($token->{type} eq 'character') {          redo B;
4660          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
4661            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
4662            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
4663            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
4664            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4665            ## end phase is either "after body" or "after frameset".          $reconstruct_active_formatting_elements->($insert_to_current);
4666            $reconstruct_active_formatting_elements->($insert_to_current);          
4667            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4668            push @$active_formatting_elements, $self->{open_elements}->[-1];
4669            
4670            !!!next-token;
4671            redo B;
4672          } elsif ($token->{tag_name} eq 'nobr') {
4673            $reconstruct_active_formatting_elements->($insert_to_current);
4674    
4675            ## has a |nobr| element in scope
4676            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4677              my $node = $self->{open_elements}->[$_];
4678              if ($node->[1] eq 'nobr') {
4679                !!!parse-error (type => 'in nobr:nobr');
4680                !!!back-token;
4681                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4682                redo B;
4683              } elsif ({
4684                        table => 1, caption => 1, td => 1, th => 1,
4685                        button => 1, marquee => 1, object => 1, html => 1,
4686                       }->{$node->[1]}) {
4687                last INSCOPE;
4688              }
4689            } # INSCOPE
4690            
4691            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4692            push @$active_formatting_elements, $self->{open_elements}->[-1];
4693            
4694            !!!next-token;
4695            redo B;
4696          } elsif ($token->{tag_name} eq 'button') {
4697            ## has a button element in scope
4698            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4699              my $node = $self->{open_elements}->[$_];
4700              if ($node->[1] eq 'button') {
4701                !!!parse-error (type => 'in button:button');
4702                !!!back-token;
4703                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4704                redo B;
4705              } elsif ({
4706                        table => 1, caption => 1, td => 1, th => 1,
4707                        button => 1, marquee => 1, object => 1, html => 1,
4708                       }->{$node->[1]}) {
4709                last INSCOPE;
4710              }
4711            } # INSCOPE
4712                        
4713            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
4714                        
4715            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4716            push @$active_formatting_elements, ['#marker', ''];
4717    
4718            !!!next-token;
4719            redo B;
4720          } elsif ($token->{tag_name} eq 'marquee' or
4721                   $token->{tag_name} eq 'object') {
4722            $reconstruct_active_formatting_elements->($insert_to_current);
4723            
4724            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4725            push @$active_formatting_elements, ['#marker', ''];
4726            
4727            !!!next-token;
4728            redo B;
4729          } elsif ($token->{tag_name} eq 'xmp') {
4730            $reconstruct_active_formatting_elements->($insert_to_current);
4731            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4732            redo B;
4733          } elsif ($token->{tag_name} eq 'table') {
4734            ## has a p element in scope
4735            INSCOPE: for (reverse @{$self->{open_elements}}) {
4736              if ($_->[1] eq 'p') {
4737                !!!back-token;
4738                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4739                redo B;
4740              } elsif ({
4741                        table => 1, caption => 1, td => 1, th => 1,
4742                        button => 1, marquee => 1, object => 1, html => 1,
4743                       }->{$_->[1]}) {
4744                last INSCOPE;
4745              }
4746            } # INSCOPE
4747              
4748            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4749              
4750            $self->{insertion_mode} = IN_TABLE_IM;
4751              
4752            !!!next-token;
4753            redo B;
4754          } elsif ({
4755                    area => 1, basefont => 1, bgsound => 1, br => 1,
4756                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4757                    image => 1,
4758                   }->{$token->{tag_name}}) {
4759            if ($token->{tag_name} eq 'image') {
4760              !!!parse-error (type => 'image');
4761              $token->{tag_name} = 'img';
4762            }
4763    
4764            ## NOTE: There is an "as if <br>" code clone.
4765            $reconstruct_active_formatting_elements->($insert_to_current);
4766            
4767            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4768            pop @{$self->{open_elements}};
4769            
4770            !!!next-token;
4771            redo B;
4772          } elsif ($token->{tag_name} eq 'hr') {
4773            ## has a p element in scope
4774            INSCOPE: for (reverse @{$self->{open_elements}}) {
4775              if ($_->[1] eq 'p') {
4776                !!!back-token;
4777                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4778                redo B;
4779              } elsif ({
4780                        table => 1, caption => 1, td => 1, th => 1,
4781                        button => 1, marquee => 1, object => 1, html => 1,
4782                       }->{$_->[1]}) {
4783                last INSCOPE;
4784              }
4785            } # INSCOPE
4786              
4787            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4788            pop @{$self->{open_elements}};
4789              
4790            !!!next-token;
4791            redo B;
4792          } elsif ($token->{tag_name} eq 'input') {
4793            $reconstruct_active_formatting_elements->($insert_to_current);
4794            
4795            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4796            ## TODO: associate with $self->{form_element} if defined
4797            pop @{$self->{open_elements}};
4798            
4799            !!!next-token;
4800            redo B;
4801          } elsif ($token->{tag_name} eq 'isindex') {
4802            !!!parse-error (type => 'isindex');
4803            
4804            if (defined $self->{form_element}) {
4805              ## Ignore the token
4806              !!!next-token;
4807              redo B;
4808            } else {
4809              my $at = $token->{attributes};
4810              my $form_attrs;
4811              $form_attrs->{action} = $at->{action} if $at->{action};
4812              my $prompt_attr = $at->{prompt};
4813              $at->{name} = {name => 'name', value => 'isindex'};
4814              delete $at->{action};
4815              delete $at->{prompt};
4816              my @tokens = (
4817                            {type => START_TAG_TOKEN, tag_name => 'form',
4818                             attributes => $form_attrs},
4819                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4820                            {type => START_TAG_TOKEN, tag_name => 'p'},
4821                            {type => START_TAG_TOKEN, tag_name => 'label'},
4822                           );
4823              if ($prompt_attr) {
4824                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4825              } else {
4826                push @tokens, {type => CHARACTER_TOKEN,
4827                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4828                ## TODO: make this configurable
4829              }
4830              push @tokens,
4831                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4832                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4833                            {type => END_TAG_TOKEN, tag_name => 'label'},
4834                            {type => END_TAG_TOKEN, tag_name => 'p'},
4835                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4836                            {type => END_TAG_TOKEN, tag_name => 'form'};
4837              $token = shift @tokens;
4838              !!!back-token (@tokens);
4839              redo B;
4840            }
4841          } elsif ($token->{tag_name} eq 'textarea') {
4842            my $tag_name = $token->{tag_name};
4843            my $el;
4844            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4845            
4846            ## TODO: $self->{form_element} if defined
4847            $self->{content_model} = RCDATA_CONTENT_MODEL;
4848            delete $self->{escape}; # MUST
4849            
4850            $insert->($el);
4851            
4852            my $text = '';
4853            !!!next-token;
4854            if ($token->{type} == CHARACTER_TOKEN) {
4855              $token->{data} =~ s/^\x0A//;
4856            unless (length $token->{data}) {            unless (length $token->{data}) {
4857              !!!next-token;              !!!next-token;
             redo B;  
4858            }            }
4859          }          }
4860            while ($token->{type} == CHARACTER_TOKEN) {
4861              $text .= $token->{data};
4862              !!!next-token;
4863            }
4864            if (length $text) {
4865              $el->manakai_append_text ($text);
4866            }
4867            
4868            $self->{content_model} = PCDATA_CONTENT_MODEL;
4869            
4870            if ($token->{type} == END_TAG_TOKEN and
4871                $token->{tag_name} eq $tag_name) {
4872              ## Ignore the token
4873            } else {
4874              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4875            }
4876            !!!next-token;
4877            redo B;
4878          } elsif ({
4879                    iframe => 1,
4880                    noembed => 1,
4881                    noframes => 1,
4882                    noscript => 0, ## TODO: 1 if scripting is enabled
4883                   }->{$token->{tag_name}}) {
4884            ## NOTE: There is an "as if in body" code clone.
4885            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4886            redo B;
4887          } elsif ($token->{tag_name} eq 'select') {
4888            $reconstruct_active_formatting_elements->($insert_to_current);
4889            
4890            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4891            
4892            $self->{insertion_mode} = IN_SELECT_IM;
4893            !!!next-token;
4894            redo B;
4895          } elsif ({
4896                    caption => 1, col => 1, colgroup => 1, frame => 1,
4897                    frameset => 1, head => 1, option => 1, optgroup => 1,
4898                    tbody => 1, td => 1, tfoot => 1, th => 1,
4899                    thead => 1, tr => 1,
4900                   }->{$token->{tag_name}}) {
4901            !!!parse-error (type => 'in body:'.$token->{tag_name});
4902            ## Ignore the token
4903            !!!next-token;
4904            redo B;
4905            
4906            ## ISSUE: An issue on HTML5 new elements in the spec.
4907          } else {
4908            $reconstruct_active_formatting_elements->($insert_to_current);
4909            
4910            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4911            
4912            !!!next-token;
4913            redo B;
4914          }
4915        } elsif ($token->{type} == END_TAG_TOKEN) {
4916          if ($token->{tag_name} eq 'body') {
4917            if (@{$self->{open_elements}} > 1 and
4918                $self->{open_elements}->[1]->[1] eq 'body') {
4919              for (@{$self->{open_elements}}) {
4920                unless ({
4921                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4922                           th => 1, tr => 1, body => 1, html => 1,
4923                         tbody => 1, tfoot => 1, thead => 1,
4924                        }->{$_->[1]}) {
4925                  !!!parse-error (type => 'not closed:'.$_->[1]);
4926                }
4927              }
4928    
4929          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
4930          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
4931          ## reprocess            redo B;
4932            } else {
4933              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4934              ## Ignore the token
4935              !!!next-token;
4936              redo B;
4937            }
4938          } elsif ($token->{tag_name} eq 'html') {
4939            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4940              ## ISSUE: There is an issue in the spec.
4941              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4942                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4943              }
4944              $self->{insertion_mode} = AFTER_BODY_IM;
4945              ## reprocess
4946              redo B;
4947            } else {
4948              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4949              ## Ignore the token
4950              !!!next-token;
4951              redo B;
4952            }
4953          } elsif ({
4954                    address => 1, blockquote => 1, center => 1, dir => 1,
4955                    div => 1, dl => 1, fieldset => 1, listing => 1,
4956                    menu => 1, ol => 1, pre => 1, ul => 1,
4957                    p => 1,
4958                    dd => 1, dt => 1, li => 1,
4959                    button => 1, marquee => 1, object => 1,
4960                   }->{$token->{tag_name}}) {
4961            ## has an element in scope
4962            my $i;
4963            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4964              my $node = $self->{open_elements}->[$_];
4965              if ($node->[1] eq $token->{tag_name}) {
4966                ## generate implied end tags
4967                if ({
4968                     dd => ($token->{tag_name} ne 'dd'),
4969                     dt => ($token->{tag_name} ne 'dt'),
4970                     li => ($token->{tag_name} ne 'li'),
4971                     p => ($token->{tag_name} ne 'p'),
4972                     td => 1, th => 1, tr => 1,
4973                     tbody => 1, tfoot=> 1, thead => 1,
4974                    }->{$self->{open_elements}->[-1]->[1]}) {
4975                  !!!back-token;
4976                  $token = {type => END_TAG_TOKEN,
4977                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4978                  redo B;
4979                }
4980                $i = $_;
4981                last INSCOPE unless $token->{tag_name} eq 'p';
4982              } elsif ({
4983                        table => 1, caption => 1, td => 1, th => 1,
4984                        button => 1, marquee => 1, object => 1, html => 1,
4985                       }->{$node->[1]}) {
4986                last INSCOPE;
4987              }
4988            } # INSCOPE
4989            
4990            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4991              if (defined $i) {
4992                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4993              } else {
4994                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4995              }
4996            }
4997            
4998            if (defined $i) {
4999              splice @{$self->{open_elements}}, $i;
5000            } elsif ($token->{tag_name} eq 'p') {
5001              ## As if <p>, then reprocess the current token
5002              my $el;
5003              !!!create-element ($el, 'p');
5004              $insert->($el);
5005            }
5006            $clear_up_to_marker->()
5007              if {
5008                button => 1, marquee => 1, object => 1,
5009              }->{$token->{tag_name}};
5010            !!!next-token;
5011          redo B;          redo B;
5012        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'form') {
5013          !!!parse-error (type => 'after html:'.$token->{tag_name});          ## has an element in scope
5014          $self->{insertion_mode} = $previous_insertion_mode;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5015          ## reprocess            my $node = $self->{open_elements}->[$_];
5016              if ($node->[1] eq $token->{tag_name}) {
5017                ## generate implied end tags
5018                if ({
5019                     dd => 1, dt => 1, li => 1, p => 1,
5020                     td => 1, th => 1, tr => 1,
5021                     tbody => 1, tfoot=> 1, thead => 1,
5022                    }->{$self->{open_elements}->[-1]->[1]}) {
5023                  !!!back-token;
5024                  $token = {type => END_TAG_TOKEN,
5025                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5026                  redo B;
5027                }
5028                last INSCOPE;
5029              } elsif ({
5030                        table => 1, caption => 1, td => 1, th => 1,
5031                        button => 1, marquee => 1, object => 1, html => 1,
5032                       }->{$node->[1]}) {
5033                last INSCOPE;
5034              }
5035            } # INSCOPE
5036            
5037            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5038              pop @{$self->{open_elements}};
5039            } else {
5040              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5041            }
5042    
5043            undef $self->{form_element};
5044            !!!next-token;
5045          redo B;          redo B;
5046        } elsif ($token->{type} eq 'end tag') {        } elsif ({
5047          !!!parse-error (type => 'after html:/'.$token->{tag_name});                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5048          $self->{insertion_mode} = $previous_insertion_mode;                 }->{$token->{tag_name}}) {
5049          ## reprocess          ## has an element in scope
5050            my $i;
5051            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5052              my $node = $self->{open_elements}->[$_];
5053              if ({
5054                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5055                  }->{$node->[1]}) {
5056                ## generate implied end tags
5057                if ({
5058                     dd => 1, dt => 1, li => 1, p => 1,
5059                     td => 1, th => 1, tr => 1,
5060                     tbody => 1, tfoot=> 1, thead => 1,
5061                    }->{$self->{open_elements}->[-1]->[1]}) {
5062                  !!!back-token;
5063                  $token = {type => END_TAG_TOKEN,
5064                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5065                  redo B;
5066                }
5067                $i = $_;
5068                last INSCOPE;
5069              } elsif ({
5070                        table => 1, caption => 1, td => 1, th => 1,
5071                        button => 1, marquee => 1, object => 1, html => 1,
5072                       }->{$node->[1]}) {
5073                last INSCOPE;
5074              }
5075            } # INSCOPE
5076            
5077            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5078              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5079            }
5080            
5081            splice @{$self->{open_elements}}, $i if defined $i;
5082            !!!next-token;
5083          redo B;          redo B;
5084          } elsif ({
5085                    a => 1,
5086                    b => 1, big => 1, em => 1, font => 1, i => 1,
5087                    nobr => 1, s => 1, small => 1, strile => 1,
5088                    strong => 1, tt => 1, u => 1,
5089                   }->{$token->{tag_name}}) {
5090            $formatting_end_tag->($token->{tag_name});
5091            redo B;
5092          } elsif ($token->{tag_name} eq 'br') {
5093            !!!parse-error (type => 'unmatched end tag:br');
5094    
5095            ## As if <br>
5096            $reconstruct_active_formatting_elements->($insert_to_current);
5097            
5098            my $el;
5099            !!!create-element ($el, 'br');
5100            $insert->($el);
5101            
5102            ## Ignore the token.
5103            !!!next-token;
5104            redo B;
5105          } elsif ({
5106                    caption => 1, col => 1, colgroup => 1, frame => 1,
5107                    frameset => 1, head => 1, option => 1, optgroup => 1,
5108                    tbody => 1, td => 1, tfoot => 1, th => 1,
5109                    thead => 1, tr => 1,
5110                    area => 1, basefont => 1, bgsound => 1,
5111                    embed => 1, hr => 1, iframe => 1, image => 1,
5112                    img => 1, input => 1, isindex => 1, noembed => 1,
5113                    noframes => 1, param => 1, select => 1, spacer => 1,
5114                    table => 1, textarea => 1, wbr => 1,
5115                    noscript => 0, ## TODO: if scripting is enabled
5116                   }->{$token->{tag_name}}) {
5117            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5118            ## Ignore the token
5119            !!!next-token;
5120            redo B;
5121            
5122            ## ISSUE: Issue on HTML5 new elements in spec
5123            
5124        } else {        } else {
5125          die "$0: $token->{type}: Unknown token";          ## Step 1
5126            my $node_i = -1;
5127            my $node = $self->{open_elements}->[$node_i];
5128    
5129            ## Step 2
5130            S2: {
5131              if ($node->[1] eq $token->{tag_name}) {
5132                ## Step 1
5133                ## generate implied end tags
5134                if ({
5135                     dd => 1, dt => 1, li => 1, p => 1,
5136                     td => 1, th => 1, tr => 1,
5137                     tbody => 1, tfoot => 1, thead => 1,
5138                    }->{$self->{open_elements}->[-1]->[1]}) {
5139                  !!!back-token;
5140                  $token = {type => END_TAG_TOKEN,
5141                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5142                  redo B;
5143                }
5144            
5145                ## Step 2
5146                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5147                  ## NOTE: <x><y></x>
5148                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5149                }
5150                
5151                ## Step 3
5152                splice @{$self->{open_elements}}, $node_i;
5153    
5154                !!!next-token;
5155                last S2;
5156              } else {
5157                ## Step 3
5158                if (not $formatting_category->{$node->[1]} and
5159                    #not $phrasing_category->{$node->[1]} and
5160                    ($special_category->{$node->[1]} or
5161                     $scoping_category->{$node->[1]})) {
5162                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5163                  ## Ignore the token
5164                  !!!next-token;
5165                  last S2;
5166                }
5167              }
5168              
5169              ## Step 4
5170              $node_i--;
5171              $node = $self->{open_elements}->[$node_i];
5172              
5173              ## Step 5;
5174              redo S2;
5175            } # S2
5176            redo B;
5177        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
5178      }      }
5179        redo B;
5180    } # B    } # B
5181    
5182      ## NOTE: The "trailing end" phase in HTML5 is split into
5183      ## two insertion modes: "after html body" and "after html frameset".
5184      ## NOTE: States in the main stage is preserved while
5185      ## the parser stays in the trailing end phase. # MUST
5186    
5187    ## Stop parsing # MUST    ## Stop parsing # MUST
5188        
5189    ## TODO: script stuffs    ## TODO: script stuffs

Legend:
Removed from v.1.49  
changed lines
  Added in v.1.60

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24