/[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.43 by wakaba, Sat Jul 21 07:21:44 2007 UTC revision 1.56 by wakaba, Sat Aug 11 07:19:18 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 DOCTYPE_TOKEN () { 1 }
163    sub COMMENT_TOKEN () { 2 }
164    sub START_TAG_TOKEN () { 3 }
165    sub END_TAG_TOKEN () { 4 }
166    sub END_OF_FILE_TOKEN () { 5 }
167    sub CHARACTER_TOKEN () { 6 }
168    
169    sub AFTER_HTML_IMS () { 0b100 }
170    sub HEAD_IMS ()       { 0b1000 }
171    sub BODY_IMS ()       { 0b10000 }
172    sub BODY_TABLE_IMS () { 0b100000 }
173    sub TABLE_IMS ()      { 0b1000000 }
174    sub ROW_IMS ()        { 0b10000000 }
175    sub BODY_AFTER_IMS () { 0b100000000 }
176    sub FRAME_IMS ()      { 0b1000000000 }
177    
178    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
179    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
180    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
181    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
182    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
183    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
184    sub IN_BODY_IM () { BODY_IMS }
185    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
186    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
187    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
188    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
189    sub IN_TABLE_IM () { TABLE_IMS }
190    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
191    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
192    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
193    sub IN_SELECT_IM () { 0b01 }
194    sub IN_COLUMN_GROUP_IM () { 0b10 }
195    
196  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
197    
198  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
# Line 177  sub _initialize_tokenizer ($) { Line 211  sub _initialize_tokenizer ($) {
211  } # _initialize_tokenizer  } # _initialize_tokenizer
212    
213  ## A token has:  ## A token has:
214  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
215  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
216  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
217  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
218  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
219  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
220  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
221  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
222    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
223    
224  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
225    
# Line 243  sub _get_next_token ($) { Line 278  sub _get_next_token ($) {
278                    
279          #          #
280        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
281          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
282          last A; ## TODO: ok?          last A; ## TODO: ok?
283        }        }
284        # Anything else        # Anything else
285        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
286                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
287        ## Stay in the data state        ## Stay in the data state
288        !!!next-input-character;        !!!next-input-character;
# Line 264  sub _get_next_token ($) { Line 299  sub _get_next_token ($) {
299        # next-input-character is already done        # next-input-character is already done
300    
301        unless (defined $token) {        unless (defined $token) {
302          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
303        } else {        } else {
304          !!!emit ($token);          !!!emit ($token);
305        }        }
# Line 280  sub _get_next_token ($) { Line 315  sub _get_next_token ($) {
315            ## reconsume            ## reconsume
316            $self->{state} = 'data';            $self->{state} = 'data';
317    
318            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
319    
320            redo A;            redo A;
321          }          }
# Line 296  sub _get_next_token ($) { Line 331  sub _get_next_token ($) {
331          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
332                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
333            $self->{current_token}            $self->{current_token}
334              = {type => 'start tag',              = {type => START_TAG_TOKEN,
335                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
336            $self->{state} = 'tag name';            $self->{state} = 'tag name';
337            !!!next-input-character;            !!!next-input-character;
338            redo A;            redo A;
339          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
340                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
341            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
342                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
343            $self->{state} = 'tag name';            $self->{state} = 'tag name';
344            !!!next-input-character;            !!!next-input-character;
# Line 313  sub _get_next_token ($) { Line 348  sub _get_next_token ($) {
348            $self->{state} = 'data';            $self->{state} = 'data';
349            !!!next-input-character;            !!!next-input-character;
350    
351            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
352    
353            redo A;            redo A;
354          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
# Line 326  sub _get_next_token ($) { Line 361  sub _get_next_token ($) {
361            $self->{state} = 'data';            $self->{state} = 'data';
362            ## reconsume            ## reconsume
363    
364            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
365    
366            redo A;            redo A;
367          }          }
# Line 350  sub _get_next_token ($) { Line 385  sub _get_next_token ($) {
385                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
386                $self->{state} = 'data';                $self->{state} = 'data';
387    
388                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
389        
390                redo A;                redo A;
391              }              }
# Line 368  sub _get_next_token ($) { Line 403  sub _get_next_token ($) {
403              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
404              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
405              $self->{state} = 'data';              $self->{state} = 'data';
406              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
407              redo A;              redo A;
408            } else {            } else {
409              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 379  sub _get_next_token ($) { Line 414  sub _get_next_token ($) {
414            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
415            # next-input-character is already done            # next-input-character is already done
416            $self->{state} = 'data';            $self->{state} = 'data';
417            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
418            redo A;            redo A;
419          }          }
420        }        }
421                
422        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
423            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
424          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
425                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
426          $self->{state} = 'tag name';          $self->{state} = 'tag name';
427          !!!next-input-character;          !!!next-input-character;
428          redo A;          redo A;
429        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
430                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
431          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
432                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
433          $self->{state} = 'tag name';          $self->{state} = 'tag name';
434          !!!next-input-character;          !!!next-input-character;
# Line 408  sub _get_next_token ($) { Line 443  sub _get_next_token ($) {
443          $self->{state} = 'data';          $self->{state} = 'data';
444          # reconsume          # reconsume
445    
446          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
447    
448          redo A;          redo A;
449        } else {        } else {
# Line 427  sub _get_next_token ($) { Line 462  sub _get_next_token ($) {
462          !!!next-input-character;          !!!next-input-character;
463          redo A;          redo A;
464        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
465          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
466            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
467                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
468            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
469          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
470            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
471            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
472              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 454  sub _get_next_token ($) { Line 489  sub _get_next_token ($) {
489          redo A;          redo A;
490        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
491          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
492          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
493            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
494                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
495            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
496          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
497            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
498            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
499              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 475  sub _get_next_token ($) { Line 510  sub _get_next_token ($) {
510        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
511          !!!next-input-character;          !!!next-input-character;
512          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
513              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
514              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
515            # permitted slash            # permitted slash
516            #            #
# Line 502  sub _get_next_token ($) { Line 537  sub _get_next_token ($) {
537          !!!next-input-character;          !!!next-input-character;
538          redo A;          redo A;
539        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
540          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
541            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
542                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
543            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
544          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
545            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
546            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
547              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 530  sub _get_next_token ($) { Line 565  sub _get_next_token ($) {
565        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
566          !!!next-input-character;          !!!next-input-character;
567          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
568              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
569              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
570            # permitted slash            # permitted slash
571            #            #
# Line 542  sub _get_next_token ($) { Line 577  sub _get_next_token ($) {
577          redo A;          redo A;
578        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
579          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
580          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
581            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
582                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
583            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
584          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
585            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
586            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
587              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 595  sub _get_next_token ($) { Line 630  sub _get_next_token ($) {
630          redo A;          redo A;
631        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
632          $before_leave->();          $before_leave->();
633          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
634            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
635                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
636            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
637          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
638            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
639            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
640              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 623  sub _get_next_token ($) { Line 658  sub _get_next_token ($) {
658          $before_leave->();          $before_leave->();
659          !!!next-input-character;          !!!next-input-character;
660          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
661              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
662              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
663            # permitted slash            # permitted slash
664            #            #
# Line 636  sub _get_next_token ($) { Line 671  sub _get_next_token ($) {
671        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
672          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
673          $before_leave->();          $before_leave->();
674          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
675            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
676                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
677            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
678          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
679            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
680            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
681              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 674  sub _get_next_token ($) { Line 709  sub _get_next_token ($) {
709          !!!next-input-character;          !!!next-input-character;
710          redo A;          redo A;
711        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
712          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
713            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
714                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
715            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
716          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
717            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
718            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
719              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 702  sub _get_next_token ($) { Line 737  sub _get_next_token ($) {
737        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
738          !!!next-input-character;          !!!next-input-character;
739          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
740              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
741              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
742            # permitted slash            # permitted slash
743            #            #
# Line 715  sub _get_next_token ($) { Line 750  sub _get_next_token ($) {
750          redo A;          redo A;
751        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
752          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
753          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
754            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
755                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
756            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
757          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
758            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
759            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
760              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 762  sub _get_next_token ($) { Line 797  sub _get_next_token ($) {
797          !!!next-input-character;          !!!next-input-character;
798          redo A;          redo A;
799        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
800          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
801            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
802                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
803            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
804          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
805            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
806            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
807              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 782  sub _get_next_token ($) { Line 817  sub _get_next_token ($) {
817          redo A;          redo A;
818        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
819          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
820          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
821            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
822                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
823            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
824          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
825            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
826            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
827              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 818  sub _get_next_token ($) { Line 853  sub _get_next_token ($) {
853          redo A;          redo A;
854        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
855          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
856          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
857            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
858                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
859            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
860          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
861            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
862            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
863              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 854  sub _get_next_token ($) { Line 889  sub _get_next_token ($) {
889          redo A;          redo A;
890        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
891          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
892          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
893            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
894                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
895            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
896          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
897            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
898            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
899              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 893  sub _get_next_token ($) { Line 928  sub _get_next_token ($) {
928          !!!next-input-character;          !!!next-input-character;
929          redo A;          redo A;
930        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
931          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
932            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
933                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
934            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
935          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
936            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
937            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
938              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 913  sub _get_next_token ($) { Line 948  sub _get_next_token ($) {
948          redo A;          redo A;
949        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
950          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
951          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
952            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
953                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
954            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
955          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
956            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
957            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
958              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 953  sub _get_next_token ($) { Line 988  sub _get_next_token ($) {
988      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} eq 'bogus comment') {
989        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
990                
991        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
992    
993        BC: {        BC: {
994          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
# Line 986  sub _get_next_token ($) { Line 1021  sub _get_next_token ($) {
1021          !!!next-input-character;          !!!next-input-character;
1022          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1023          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1024            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1025            $self->{state} = 'comment start';            $self->{state} = 'comment start';
1026            !!!next-input-character;            !!!next-input-character;
1027            redo A;            redo A;
# Line 1189  sub _get_next_token ($) { Line 1224  sub _get_next_token ($) {
1224          $self->{state} = 'data';          $self->{state} = 'data';
1225          !!!next-input-character;          !!!next-input-character;
1226    
1227          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1228    
1229          redo A;          redo A;
1230        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1197  sub _get_next_token ($) { Line 1232  sub _get_next_token ($) {
1232          $self->{state} = 'data';          $self->{state} = 'data';
1233          ## reconsume          ## reconsume
1234    
1235          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1236    
1237          redo A;          redo A;
1238        } else {        } else {
1239          $self->{current_token}          $self->{current_token}
1240              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1241                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1242                 correct => 1};                 correct => 1};
1243  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1705  sub _tokenize_attempt_to_consume_an_enti
1705            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1706          }          }
1707    
1708          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code};
1709        } # X        } # X
1710      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1711               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1740  sub _tokenize_attempt_to_consume_an_enti
1740          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1741        }        }
1742                
1743        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1744      } else {      } else {
1745        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1746        !!!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 1788  sub _tokenize_attempt_to_consume_an_enti
1788      }      }
1789            
1790      if ($match > 0) {      if ($match > 0) {
1791        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value};
1792      } elsif ($match < 0) {      } elsif ($match < 0) {
1793        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1794        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1795          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1796        } else {        } else {
1797          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value};
1798        }        }
1799      } else {      } else {
1800        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1801        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1802        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1803      }      }
1804    } else {    } else {
1805      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 1841  sub _construct_tree ($) {
1841        
1842    !!!next-token;    !!!next-token;
1843    
1844    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1845    undef $self->{form_element};    undef $self->{form_element};
1846    undef $self->{head_element};    undef $self->{head_element};
1847    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 1855  sub _construct_tree ($) {
1855  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1856    my $self = shift;    my $self = shift;
1857    INITIAL: {    INITIAL: {
1858      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1859        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1860        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
1861        ## language.        ## language.
# Line 1947  sub _tree_construction_initial ($) { Line 1982  sub _tree_construction_initial ($) {
1982        !!!next-token;        !!!next-token;
1983        return;        return;
1984      } elsif ({      } elsif ({
1985                'start tag' => 1,                START_TAG_TOKEN, 1,
1986                'end tag' => 1,                END_TAG_TOKEN, 1,
1987                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
1988               }->{$token->{type}}) {               }->{$token->{type}}) {
1989        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
1990        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
1991        ## Go to the root element phase        ## Go to the root element phase
1992        ## reprocess        ## reprocess
1993        return;        return;
1994      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
1995        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1996          ## Ignore the token          ## Ignore the token
1997    
# Line 1972  sub _tree_construction_initial ($) { Line 2007  sub _tree_construction_initial ($) {
2007        ## Go to the root element phase        ## Go to the root element phase
2008        ## reprocess        ## reprocess
2009        return;        return;
2010      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2011        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2012        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2013                
# Line 1980  sub _tree_construction_initial ($) { Line 2015  sub _tree_construction_initial ($) {
2015        !!!next-token;        !!!next-token;
2016        redo INITIAL;        redo INITIAL;
2017      } else {      } else {
2018        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2019      }      }
2020    } # INITIAL    } # INITIAL
2021  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2024  sub _tree_construction_root_element ($)
2024    my $self = shift;    my $self = shift;
2025        
2026    B: {    B: {
2027        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2028          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2029          ## Ignore the token          ## Ignore the token
2030          ## Stay in the phase          ## Stay in the phase
2031          !!!next-token;          !!!next-token;
2032          redo B;          redo B;
2033        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2034          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2035          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2036          ## Stay in the phase          ## Stay in the phase
2037          !!!next-token;          !!!next-token;
2038          redo B;          redo B;
2039        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2040          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2041            ## Ignore the token.            ## Ignore the token.
2042    
# Line 2013  sub _tree_construction_root_element ($) Line 2048  sub _tree_construction_root_element ($)
2048          }          }
2049          #          #
2050        } elsif ({        } elsif ({
2051                  'start tag' => 1,                  START_TAG_TOKEN, 1,
2052                  'end tag' => 1,                  END_TAG_TOKEN, 1,
2053                  'end-of-file' => 1,                  END_OF_FILE_TOKEN, 1,
2054                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2055          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2056          #          #
2057        } else {        } else {
2058          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2059        }        }
2060        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2061        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
# Line 2062  sub _reset_insertion_mode ($) { Line 2097  sub _reset_insertion_mode ($) {
2097            
2098        ## Step 4..13        ## Step 4..13
2099        my $new_mode = {        my $new_mode = {
2100                        select => 'in select',                        select => IN_SELECT_IM,
2101                        td => 'in cell',                        td => IN_CELL_IM,
2102                        th => 'in cell',                        th => IN_CELL_IM,
2103                        tr => 'in row',                        tr => IN_ROW_IM,
2104                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2105                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2106                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2107                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2108                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2109                        table => 'in table',                        table => IN_TABLE_IM,
2110                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2111                        body => 'in body',                        body => IN_BODY_IM,
2112                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2113                       }->{$node->[1]};                       }->{$node->[1]};
2114        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2115                
2116        ## Step 14        ## Step 14
2117        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2118          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2119            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2120          } else {          } else {
2121            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2122          }          }
2123          return;          return;
2124        }        }
2125                
2126        ## Step 15        ## Step 15
2127        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2128                
2129        ## Step 16        ## Step 16
2130        $i--;        $i--;
# Line 2103  sub _reset_insertion_mode ($) { Line 2138  sub _reset_insertion_mode ($) {
2138  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2139    my $self = shift;    my $self = shift;
2140    
   my $previous_insertion_mode;  
   
2141    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2142    
2143    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2205  sub _tree_construction_main ($) { Line 2238  sub _tree_construction_main ($) {
2238      ## Step 4      ## Step 4
2239      my $text = '';      my $text = '';
2240      !!!next-token;      !!!next-token;
2241      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2242        $text .= $token->{data};        $text .= $token->{data};
2243        !!!next-token;        !!!next-token;
2244      }      }
# Line 2220  sub _tree_construction_main ($) { Line 2253  sub _tree_construction_main ($) {
2253      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2254    
2255      ## Step 7      ## Step 7
2256      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) {
2257        ## Ignore the token        ## Ignore the token
2258      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2259        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2243  sub _tree_construction_main ($) { Line 2276  sub _tree_construction_main ($) {
2276            
2277      my $text = '';      my $text = '';
2278      !!!next-token;      !!!next-token;
2279      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2280        $text .= $token->{data};        $text .= $token->{data};
2281        !!!next-token;        !!!next-token;
2282      } # 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 2286  sub _tree_construction_main ($) {
2286                                
2287      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2288    
2289      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2290          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2291        ## Ignore the token        ## Ignore the token
2292      } else {      } else {
# Line 2496  sub _tree_construction_main ($) { Line 2529  sub _tree_construction_main ($) {
2529                         }                         }
2530    }; # $insert_to_foster    }; # $insert_to_foster
2531    
2532    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}}) {  
         $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  
2533    
2534    B: {    B: {
2535      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2536        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2537        ## Ignore the token        ## Ignore the token
2538        ## Stay in the phase        ## Stay in the phase
2539        !!!next-token;        !!!next-token;
2540        redo B;        redo B;
2541      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2542        if ($token->{insertion_mode} ne 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2543            #
2544          } else {
2545          ## Generate implied end tags          ## Generate implied end tags
2546          if ({          if ({
2547               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,
2548               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2549              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2550            !!!back-token;            !!!back-token;
2551            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2552            redo B;            redo B;
2553          }          }
2554                    
# Line 3374  sub _tree_construction_main ($) { Line 2566  sub _tree_construction_main ($) {
2566    
2567        ## Stop parsing        ## Stop parsing
2568        last B;        last B;
2569      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2570               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2571        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2572          ## Turn into the main phase          ## Turn into the main phase
2573          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2574          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_BODY_IM;
2575          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2576            ## Turn into the main phase
2577            !!!parse-error (type => 'after html:html');
2578            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2579        }        }
2580    
2581  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 3397  sub _tree_construction_main ($) { Line 2593  sub _tree_construction_main ($) {
2593        }        }
2594        !!!next-token;        !!!next-token;
2595        redo B;        redo B;
2596      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2597        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2598        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2599          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2600        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2601          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2602        } else {        } else {
2603          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2604        }        }
2605        !!!next-token;        !!!next-token;
2606        redo B;        redo B;
2607      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2608            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
2609              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2610                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2611                unless (length $token->{data}) {            unless (length $token->{data}) {
2612                  !!!next-token;              !!!next-token;
2613                  redo B;              redo B;
2614                }            }
2615              }          }
2616              ## As if <head>  
2617              !!!create-element ($self->{head_element}, 'head');          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2618              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if <head>
2619              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!create-element ($self->{head_element}, 'head');
2620              $self->{insertion_mode} = 'in head';            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2621              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2622    
2623              ## Reprocess in the "in head" insertion mode...
2624              pop @{$self->{open_elements}};
2625    
2626              ## Reprocess in the "after head" insertion mode...
2627            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2628              ## As if </noscript>
2629              pop @{$self->{open_elements}};
2630              !!!parse-error (type => 'in noscript:#character');
2631              
2632              ## Reprocess in the "in head" insertion mode...
2633              ## As if </head>
2634              pop @{$self->{open_elements}};
2635    
2636              ## Reprocess in the "after head" insertion mode...
2637            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2638              pop @{$self->{open_elements}};
2639    
2640              ## Reprocess in the "after head" insertion mode...
2641            }
2642    
2643                ## "after head" insertion mode
2644                ## As if <body>
2645                !!!insert-element ('body');
2646                $self->{insertion_mode} = IN_BODY_IM;
2647              ## reprocess              ## reprocess
2648              redo B;              redo B;
2649            } 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';  
2650              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2651                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2652              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2653              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2654              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2655              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2656              #  ## reprocess                  !!!next-token;
2657              } else {                  redo B;
2658                ## reprocess                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2659              }                  #
2660              redo B;                } else {
2661            } elsif ($token->{type} eq 'end tag') {                  !!!parse-error (type => 'in head:head'); # or in head noscript
2662              if ({                  ## Ignore the token
2663                   head => 1, body => 1, html => 1,                  !!!next-token;
2664                   p => 1, br => 1,                  redo B;
2665                  }->{$token->{tag_name}}) {                }
2666                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2667                ## As if <head>                ## As if <head>
2668                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2669                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2670                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2671                $self->{insertion_mode} = 'in head';  
2672                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2673                redo B;                ## Reprocess in the "in head" insertion mode...
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
2674              }              }
2675            } else {  
2676              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
2677            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2678          } elsif ($self->{insertion_mode} eq 'in head' or                  ## As if </noscript>
2679                   $self->{insertion_mode} eq 'in head noscript' or                  pop @{$self->{open_elements}};
2680                   $self->{insertion_mode} eq 'after head') {                  !!!parse-error (type => 'in noscript:base');
2681            if ($token->{type} eq 'character') {                
2682              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  $self->{insertion_mode} = IN_HEAD_IM;
2683                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  ## Reprocess in the "in head" insertion mode...
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
2684                }                }
2685              }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
2686                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2687                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2688                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2689                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2690                }                }
2691                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2692                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2693                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2694                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2695                  !!!next-token;
2696                  redo B;
2697                } elsif ($token->{tag_name} eq 'link') {
2698                  ## NOTE: There is a "as if in head" code clone.
2699                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2700                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2701                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2702                  }
2703                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2704                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2705                  pop @{$self->{open_elements}}
2706                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2707                !!!next-token;                !!!next-token;
2708                redo B;                redo B;
2709              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2710                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2711                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2712                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2713                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2714                }                }
# Line 3518  sub _tree_construction_main ($) { Line 2734  sub _tree_construction_main ($) {
2734    
2735                ## TODO: Extracting |charset| from |meta|.                ## TODO: Extracting |charset| from |meta|.
2736                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2737                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2738                !!!next-token;                !!!next-token;
2739                redo B;                redo B;
2740              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
2741                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2742                ## NOTE: There is a "as if in head" code clone.                  ## As if </noscript>
2743                if ($self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
2744                    !!!parse-error (type => 'in noscript:title');
2745                  
2746                    $self->{insertion_mode} = IN_HEAD_IM;
2747                    ## Reprocess in the "in head" insertion mode...
2748                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2749                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2750                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2751                }                }
2752    
2753                  ## NOTE: There is a "as if in head" code clone.
2754                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
2755                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
2756                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2757                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2758                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2759                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2760                redo B;                redo B;
2761              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2762                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2763                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2764                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2765                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2766                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2767                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2768                }                }
2769                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2770                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2771                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2772                redo B;                redo B;
2773              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2774                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2775                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2776                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2777                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2778                  !!!next-token;                  !!!next-token;
2779                  redo B;                  redo B;
2780                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2781                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
2782                  ## Ignore the token                  ## Ignore the token
2783                  !!!next-token;                  !!!next-token;
# Line 3562  sub _tree_construction_main ($) { Line 2785  sub _tree_construction_main ($) {
2785                } else {                } else {
2786                  #                  #
2787                }                }
2788              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
2789                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2790                !!!parse-error (type => 'in head:head'); # or in head noscript                  ## As if </noscript>
2791                ## Ignore the token                  pop @{$self->{open_elements}};
2792                !!!next-token;                  !!!parse-error (type => 'in noscript:script');
2793                redo B;                
2794              } elsif ($self->{insertion_mode} ne 'in head noscript' and                  $self->{insertion_mode} = IN_HEAD_IM;
2795                       $token->{tag_name} eq 'script') {                  ## Reprocess in the "in head" insertion mode...
2796                if ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2797                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2798                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2799                }                }
2800    
2801                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2802                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
2803                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2804                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
2805                redo B;                redo B;
2806              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
2807                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
2808                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2809                $self->{insertion_mode} = 'in frameset';                  ## As if </noscript>
2810                    pop @{$self->{open_elements}};
2811                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2812                    
2813                    ## Reprocess in the "in head" insertion mode...
2814                    ## As if </head>
2815                    pop @{$self->{open_elements}};
2816                    
2817                    ## Reprocess in the "after head" insertion mode...
2818                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2819                    pop @{$self->{open_elements}};
2820                    
2821                    ## Reprocess in the "after head" insertion mode...
2822                  }
2823    
2824                  ## "after head" insertion mode
2825                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2826                  if ($token->{tag_name} eq 'body') {
2827                    $self->{insertion_mode} = IN_BODY_IM;
2828                  } elsif ($token->{tag_name} eq 'frameset') {
2829                    $self->{insertion_mode} = IN_FRAMESET_IM;
2830                  } else {
2831                    die "$0: tag name: $self->{tag_name}";
2832                  }
2833                !!!next-token;                !!!next-token;
2834                redo B;                redo B;
2835              } else {              } else {
2836                #                #
2837              }              }
2838            } elsif ($token->{type} eq 'end tag') {  
2839              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2840                  $token->{tag_name} eq 'head') {                ## As if </noscript>
2841                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2842                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2843                !!!next-token;                
2844                redo B;                ## Reprocess in the "in head" insertion mode...
2845              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
2846                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2847                $self->{insertion_mode} = 'in head';  
2848                !!!next-token;                ## Reprocess in the "after head" insertion mode...
2849                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2850              } elsif ($self->{insertion_mode} eq 'in head' and                ## As if </head>
2851                       {                pop @{$self->{open_elements}};
2852    
2853                  ## Reprocess in the "after head" insertion mode...
2854                }
2855    
2856                ## "after head" insertion mode
2857                ## As if <body>
2858                !!!insert-element ('body');
2859                $self->{insertion_mode} = IN_BODY_IM;
2860                ## reprocess
2861                redo B;
2862              } elsif ($token->{type} == END_TAG_TOKEN) {
2863                if ($token->{tag_name} eq 'head') {
2864                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2865                    ## As if <head>
2866                    !!!create-element ($self->{head_element}, 'head');
2867                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2868                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2869    
2870                    ## Reprocess in the "in head" insertion mode...
2871                    pop @{$self->{open_elements}};
2872                    $self->{insertion_mode} = AFTER_HEAD_IM;
2873                    !!!next-token;
2874                    redo B;
2875                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2876                    ## As if </noscript>
2877                    pop @{$self->{open_elements}};
2878                    !!!parse-error (type => 'in noscript:script');
2879                    
2880                    ## Reprocess in the "in head" insertion mode...
2881                    pop @{$self->{open_elements}};
2882                    $self->{insertion_mode} = AFTER_HEAD_IM;
2883                    !!!next-token;
2884                    redo B;
2885                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2886                    pop @{$self->{open_elements}};
2887                    $self->{insertion_mode} = AFTER_HEAD_IM;
2888                    !!!next-token;
2889                    redo B;
2890                  } else {
2891                    #
2892                  }
2893                } elsif ($token->{tag_name} eq 'noscript') {
2894                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2895                    pop @{$self->{open_elements}};
2896                    $self->{insertion_mode} = IN_HEAD_IM;
2897                    !!!next-token;
2898                    redo B;
2899                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2900                    !!!parse-error (type => 'unmatched end tag:noscript');
2901                    ## Ignore the token ## ISSUE: An issue in the spec.
2902                    !!!next-token;
2903                    redo B;
2904                  } else {
2905                    #
2906                  }
2907                } elsif ({
2908                        body => 1, html => 1,                        body => 1, html => 1,
                       p => 1, br => 1,  
2909                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2910                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2911                    ## As if <head>
2912                    !!!create-element ($self->{head_element}, 'head');
2913                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2914                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2915    
2916                    $self->{insertion_mode} = IN_HEAD_IM;
2917                    ## Reprocess in the "in head" insertion mode...
2918                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2919                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2920                    ## Ignore the token
2921                    !!!next-token;
2922                    redo B;
2923                  }
2924                  
2925                #                #
2926              } elsif ($self->{insertion_mode} eq 'in head noscript' and              } elsif ({
                      {  
2927                        p => 1, br => 1,                        p => 1, br => 1,
2928                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2929                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2930                    ## As if <head>
2931                    !!!create-element ($self->{head_element}, 'head');
2932                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2933                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2934    
2935                    $self->{insertion_mode} = IN_HEAD_IM;
2936                    ## Reprocess in the "in head" insertion mode...
2937                  }
2938    
2939                #                #
2940              } elsif ($self->{insertion_mode} ne 'after head') {              } else {
2941                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2942                    #
2943                  } else {
2944                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2945                    ## Ignore the token
2946                    !!!next-token;
2947                    redo B;
2948                  }
2949                }
2950    
2951                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2952                  ## As if </noscript>
2953                  pop @{$self->{open_elements}};
2954                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2955                  
2956                  ## Reprocess in the "in head" insertion mode...
2957                  ## As if </head>
2958                  pop @{$self->{open_elements}};
2959    
2960                  ## Reprocess in the "after head" insertion mode...
2961                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2962                  ## As if </head>
2963                  pop @{$self->{open_elements}};
2964    
2965                  ## Reprocess in the "after head" insertion mode...
2966                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2967                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2968                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
2969                !!!next-token;                !!!next-token;
2970                redo B;                redo B;
             } else {  
               #  
2971              }              }
           } else {  
             #  
           }  
2972    
2973            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
2974            if ($self->{insertion_mode} eq 'in head') {              ## As if <body>
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
2975              !!!insert-element ('body');              !!!insert-element ('body');
2976              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
2977                ## reprocess
2978                redo B;
2979              } else {
2980                die "$0: $token->{type}: Unknown token type";
2981            }            }
           ## reprocess  
           redo B;  
2982    
2983            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
2984          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
2985                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
                  $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
2986              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
2987              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
2988                            
# Line 3657  sub _tree_construction_main ($) { Line 2990  sub _tree_construction_main ($) {
2990    
2991              !!!next-token;              !!!next-token;
2992              redo B;              redo B;
2993            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2994              if ({              if ({
2995                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
2996                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2997                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
2998                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
2999                  ## have an element in table scope                  ## have an element in table scope
3000                  my $tn;                  my $tn;
3001                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3685  sub _tree_construction_main ($) { Line 3018  sub _tree_construction_main ($) {
3018                                    
3019                  ## Close the cell                  ## Close the cell
3020                  !!!back-token; # <?>                  !!!back-token; # <?>
3021                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3022                  redo B;                  redo B;
3023                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3024                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3025                                    
3026                  ## As if </caption>                  ## As if </caption>
# Line 3718  sub _tree_construction_main ($) { Line 3051  sub _tree_construction_main ($) {
3051                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3052                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3053                    !!!back-token; # <?>                    !!!back-token; # <?>
3054                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3055                    !!!back-token;                    !!!back-token;
3056                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3057                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3058                    redo B;                    redo B;
3059                  }                  }
# Line 3733  sub _tree_construction_main ($) { Line 3066  sub _tree_construction_main ($) {
3066                                    
3067                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3068                                    
3069                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3070                                    
3071                  ## reprocess                  ## reprocess
3072                  redo B;                  redo B;
# Line 3743  sub _tree_construction_main ($) { Line 3076  sub _tree_construction_main ($) {
3076              } else {              } else {
3077                #                #
3078              }              }
3079            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3080              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3081                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3082                  ## have an element in table scope                  ## have an element in table scope
3083                  my $i;                  my $i;
3084                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3775  sub _tree_construction_main ($) { Line 3108  sub _tree_construction_main ($) {
3108                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3109                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3110                    !!!back-token;                    !!!back-token;
3111                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3112                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3113                    redo B;                    redo B;
3114                  }                  }
# Line 3788  sub _tree_construction_main ($) { Line 3121  sub _tree_construction_main ($) {
3121                                    
3122                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3123                                    
3124                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3125                                    
3126                  !!!next-token;                  !!!next-token;
3127                  redo B;                  redo B;
3128                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3129                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3130                  ## Ignore the token                  ## Ignore the token
3131                  !!!next-token;                  !!!next-token;
# Line 3801  sub _tree_construction_main ($) { Line 3134  sub _tree_construction_main ($) {
3134                  #                  #
3135                }                }
3136              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3137                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3138                  ## have a table element in table scope                  ## have a table element in table scope
3139                  my $i;                  my $i;
3140                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3829  sub _tree_construction_main ($) { Line 3162  sub _tree_construction_main ($) {
3162                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3163                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3164                    !!!back-token;                    !!!back-token;
3165                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3166                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3167                    redo B;                    redo B;
3168                  }                  }
# Line 3842  sub _tree_construction_main ($) { Line 3175  sub _tree_construction_main ($) {
3175                                    
3176                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3177                                    
3178                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3179                                    
3180                  !!!next-token;                  !!!next-token;
3181                  redo B;                  redo B;
3182                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3183                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3184                  ## Ignore the token                  ## Ignore the token
3185                  !!!next-token;                  !!!next-token;
# Line 3858  sub _tree_construction_main ($) { Line 3191  sub _tree_construction_main ($) {
3191                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3192                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3193                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3194                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3195                ## have an element in table scope                ## have an element in table scope
3196                my $i;                my $i;
3197                my $tn;                my $tn;
# Line 3886  sub _tree_construction_main ($) { Line 3219  sub _tree_construction_main ($) {
3219    
3220                ## Close the cell                ## Close the cell
3221                !!!back-token; # </?>                !!!back-token; # </?>
3222                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3223                redo B;                redo B;
3224              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3225                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3226                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3227    
3228                ## As if </caption>                ## As if </caption>
# Line 3920  sub _tree_construction_main ($) { Line 3253  sub _tree_construction_main ($) {
3253                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3254                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3255                  !!!back-token; # </table>                  !!!back-token; # </table>
3256                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3257                  !!!back-token;                  !!!back-token;
3258                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3259                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3260                  redo B;                  redo B;
3261                }                }
# Line 3935  sub _tree_construction_main ($) { Line 3268  sub _tree_construction_main ($) {
3268    
3269                $clear_up_to_marker->();                $clear_up_to_marker->();
3270    
3271                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3272    
3273                ## reprocess                ## reprocess
3274                redo B;                redo B;
3275              } elsif ({              } elsif ({
3276                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3277                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3278                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3279                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3280                  ## Ignore the token                  ## Ignore the token
3281                  !!!next-token;                  !!!next-token;
# Line 3955  sub _tree_construction_main ($) { Line 3287  sub _tree_construction_main ($) {
3287                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3288                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3289                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3290                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3291                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3292                ## Ignore the token                ## Ignore the token
3293                !!!next-token;                !!!next-token;
# Line 3963  sub _tree_construction_main ($) { Line 3295  sub _tree_construction_main ($) {
3295              } else {              } else {
3296                #                #
3297              }              }
3298            } else {        } else {
3299              #          die "$0: $token->{type}: Unknown token type";
3300            }        }
3301              
3302            $in_body->($insert_to_current);        $insert = $insert_to_current;
3303            redo B;        #
3304          } elsif ($self->{insertion_mode} eq 'in table') {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3305            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
3306              ## NOTE: There are "character in table" code clones.              ## NOTE: There are "character in table" code clones.
3307              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3308                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
# Line 4028  sub _tree_construction_main ($) { Line 3360  sub _tree_construction_main ($) {
3360                            
3361              !!!next-token;              !!!next-token;
3362              redo B;              redo B;
3363            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3364              if ({              if ({
3365                   caption => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3366                   colgroup => 1,                   th => 1, td => 1,
                  tbody => 1, tfoot => 1, thead => 1,  
3367                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3368                ## Clear back to table context                if ($self->{insertion_mode} == IN_TABLE_IM) {
3369                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## Clear back to table context
3370                       $self->{open_elements}->[-1]->[1] ne 'html') {                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3371                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                         $self->{open_elements}->[-1]->[1] ne 'html') {
3372                  pop @{$self->{open_elements}};                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3373                      pop @{$self->{open_elements}};
3374                    }
3375                    
3376                    !!!insert-element ('tbody');
3377                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3378                    ## reprocess in the "in table body" insertion mode...
3379                }                }
3380    
3381                push @$active_formatting_elements, ['#marker', '']                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3382                  if $token->{tag_name} eq 'caption';                  unless ($token->{tag_name} eq 'tr') {
3383                      !!!parse-error (type => 'missing start tag:tr');
3384                    }
3385                    
3386                    ## Clear back to table body context
3387                    while (not {
3388                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3389                    }->{$self->{open_elements}->[-1]->[1]}) {
3390                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3391                      pop @{$self->{open_elements}};
3392                    }
3393                    
3394                    $self->{insertion_mode} = IN_ROW_IM;
3395                    if ($token->{tag_name} eq 'tr') {
3396                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3397                      !!!next-token;
3398                      redo B;
3399                    } else {
3400                      !!!insert-element ('tr');
3401                      ## reprocess in the "in row" insertion mode
3402                    }
3403                  }
3404    
3405                  ## Clear back to table row context
3406                  while (not {
3407                    tr => 1, html => 1,
3408                  }->{$self->{open_elements}->[-1]->[1]}) {
3409                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3410                    pop @{$self->{open_elements}};
3411                  }
3412                  
3413                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3414                $self->{insertion_mode} = {                $self->{insertion_mode} = IN_CELL_IM;
3415                                   caption => 'in caption',  
3416                                   colgroup => 'in column group',                push @$active_formatting_elements, ['#marker', ''];
3417                                   tbody => 'in table body',                
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
3418                !!!next-token;                !!!next-token;
3419                redo B;                redo B;
3420              } elsif ({              } elsif ({
3421                        col => 1,                        caption => 1, col => 1, colgroup => 1,
3422                        td => 1, th => 1, tr => 1,                        tbody => 1, tfoot => 1, thead => 1,
3423                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3424                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3425                ## Clear back to table context                if ($self->{insertion_mode} == IN_ROW_IM) {
3426                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if </tr>
3427                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## have an element in table scope
3428                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  my $i;
3429                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3430                      my $node = $self->{open_elements}->[$_];
3431                      if ($node->[1] eq 'tr') {
3432                        $i = $_;
3433                        last INSCOPE;
3434                      } elsif ({
3435                                table => 1, html => 1,
3436                               }->{$node->[1]}) {
3437                        last INSCOPE;
3438                      }
3439                    } # INSCOPE
3440                    unless (defined $i) {
3441                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3442                      ## Ignore the token
3443                      !!!next-token;
3444                      redo B;
3445                    }
3446                    
3447                    ## Clear back to table row context
3448                    while (not {
3449                      tr => 1, html => 1,
3450                    }->{$self->{open_elements}->[-1]->[1]}) {
3451                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3452                      pop @{$self->{open_elements}};
3453                    }
3454                    
3455                    pop @{$self->{open_elements}}; # tr
3456                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3457                    if ($token->{tag_name} eq 'tr') {
3458                      ## reprocess
3459                      redo B;
3460                    } else {
3461                      ## reprocess in the "in table body" insertion mode...
3462                    }
3463                  }
3464    
3465                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3466                    ## have an element in table scope
3467                    my $i;
3468                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3469                      my $node = $self->{open_elements}->[$_];
3470                      if ({
3471                           tbody => 1, thead => 1, tfoot => 1,
3472                          }->{$node->[1]}) {
3473                        $i = $_;
3474                        last INSCOPE;
3475                      } elsif ({
3476                                table => 1, html => 1,
3477                               }->{$node->[1]}) {
3478                        last INSCOPE;
3479                      }
3480                    } # INSCOPE
3481                    unless (defined $i) {
3482                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3483                      ## Ignore the token
3484                      !!!next-token;
3485                      redo B;
3486                    }
3487    
3488                    ## Clear back to table body context
3489                    while (not {
3490                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3491                    }->{$self->{open_elements}->[-1]->[1]}) {
3492                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3493                      pop @{$self->{open_elements}};
3494                    }
3495                    
3496                    ## As if <{current node}>
3497                    ## have an element in table scope
3498                    ## true by definition
3499                    
3500                    ## Clear back to table body context
3501                    ## nop by definition
3502                    
3503                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3504                    $self->{insertion_mode} = IN_TABLE_IM;
3505                    ## reprocess in "in table" insertion mode...
3506                }                }
3507    
3508                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                if ($token->{tag_name} eq 'col') {
3509                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  ## Clear back to table context
3510                  ? 'in column group' : 'in table body';                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3511                ## reprocess                         $self->{open_elements}->[-1]->[1] ne 'html') {
3512                redo B;                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3513                      pop @{$self->{open_elements}};
3514                    }
3515                    
3516                    !!!insert-element ('colgroup');
3517                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3518                    ## reprocess
3519                    redo B;
3520                  } elsif ({
3521                            caption => 1,
3522                            colgroup => 1,
3523                            tbody => 1, tfoot => 1, thead => 1,
3524                           }->{$token->{tag_name}}) {
3525                    ## Clear back to table context
3526                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3527                           $self->{open_elements}->[-1]->[1] ne 'html') {
3528                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3529                      pop @{$self->{open_elements}};
3530                    }
3531                    
3532                    push @$active_formatting_elements, ['#marker', '']
3533                        if $token->{tag_name} eq 'caption';
3534                    
3535                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3536                    $self->{insertion_mode} = {
3537                                               caption => IN_CAPTION_IM,
3538                                               colgroup => IN_COLUMN_GROUP_IM,
3539                                               tbody => IN_TABLE_BODY_IM,
3540                                               tfoot => IN_TABLE_BODY_IM,
3541                                               thead => IN_TABLE_BODY_IM,
3542                                              }->{$token->{tag_name}};
3543                    !!!next-token;
3544                    redo B;
3545                  } else {
3546                    die "$0: in table: <>: $token->{tag_name}";
3547                  }
3548              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3549                ## NOTE: There are code clones for this "table in table"                ## NOTE: There are code clones for this "table in table"
3550                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
# Line 4102  sub _tree_construction_main ($) { Line 3577  sub _tree_construction_main ($) {
3577                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3578                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3579                  !!!back-token; # <table>                  !!!back-token; # <table>
3580                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3581                  !!!back-token;                  !!!back-token;
3582                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3583                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3584                  redo B;                  redo B;
3585                }                }
# Line 4122  sub _tree_construction_main ($) { Line 3597  sub _tree_construction_main ($) {
3597              } else {              } else {
3598                #                #
3599              }              }
3600            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3601              if ($token->{tag_name} eq 'table') {              if ($token->{tag_name} eq 'tr' and
3602                ## have a table element in table scope                  $self->{insertion_mode} == IN_ROW_IM) {
3603                  ## have an element in table scope
3604                my $i;                my $i;
3605                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3606                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
# Line 4143  sub _tree_construction_main ($) { Line 3619  sub _tree_construction_main ($) {
3619                  !!!next-token;                  !!!next-token;
3620                  redo B;                  redo B;
3621                }                }
                 
               ## 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  
                 redo B;  
               }  
3622    
3623                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## Clear back to table row context
3624                  while (not {
3625                    tr => 1, html => 1,
3626                  }->{$self->{open_elements}->[-1]->[1]}) {
3627                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3628                    pop @{$self->{open_elements}};
3629                }                }
3630    
3631                splice @{$self->{open_elements}}, $i;                pop @{$self->{open_elements}}; # tr
3632                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           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;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
3633                !!!next-token;                !!!next-token;
3634                redo B;                redo B;
3635              } else {              } elsif ($token->{tag_name} eq 'table') {
3636                #                if ($self->{insertion_mode} == IN_ROW_IM) {
3637              }                  ## As if </tr>
3638            } else {                  ## have an element in table scope
3639              #                  my $i;
3640            }                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3641                      my $node = $self->{open_elements}->[$_];
3642            ## As if </colgroup>                    if ($node->[1] eq 'tr') {
3643            if ($self->{open_elements}->[-1]->[1] eq 'html') {                      $i = $_;
3644              !!!parse-error (type => 'unmatched end tag:colgroup');                      last INSCOPE;
3645              ## Ignore the token                    } elsif ({
3646              !!!next-token;                              table => 1, html => 1,
3647              redo B;                             }->{$node->[1]}) {
3648            } else {                      last INSCOPE;
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             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;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
   
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
3649                    }                    }
3650                    last OE;                  } # INSCOPE
3651                    unless (defined $i) {
3652                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3653                      ## Ignore the token
3654                      !!!next-token;
3655                      redo B;
3656                  }                  }
3657                } # OE                  
3658                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table row context
3659                $prev_sibling = $foster_parent_element->last_child                  while (not {
3660                  unless defined $foster_parent_element;                    tr => 1, html => 1,
3661                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3662                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3663                  $prev_sibling->manakai_append_text ($token->{data});                    pop @{$self->{open_elements}};
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3664                  }                  }
3665                } # INSCOPE                  
3666                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3667                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3668                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3669                }                }
3670    
3671                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3672                while (not {                  ## have an element in table scope
3673                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3674                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3675                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3676                      if ({
3677                           tbody => 1, thead => 1, tfoot => 1,
3678                          }->{$node->[1]}) {
3679                        $i = $_;
3680                        last INSCOPE;
3681                      } elsif ({
3682                                table => 1, html => 1,
3683                               }->{$node->[1]}) {
3684                        last INSCOPE;
3685                      }
3686                    } # INSCOPE
3687                    unless (defined $i) {
3688                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3689                      ## Ignore the token
3690                      !!!next-token;
3691                      redo B;
3692                    }
3693                    
3694                    ## Clear back to table body context
3695                    while (not {
3696                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3697                    }->{$self->{open_elements}->[-1]->[1]}) {
3698                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3699                      pop @{$self->{open_elements}};
3700                    }
3701                    
3702                    ## As if <{current node}>
3703                    ## have an element in table scope
3704                    ## true by definition
3705                    
3706                    ## Clear back to table body context
3707                    ## nop by definition
3708                    
3709                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3710                    $self->{insertion_mode} = IN_TABLE_IM;
3711                    ## reprocess in the "in table" insertion mode...
3712                }                }
3713    
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
3714                ## have a table element in table scope                ## have a table element in table scope
3715                my $i;                my $i;
3716                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3717                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3718                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3719                    $i = $_;                    $i = $_;
3720                    last INSCOPE;                    last INSCOPE;
3721                  } elsif ({                  } elsif ({
# Line 4391  sub _tree_construction_main ($) { Line 3725  sub _tree_construction_main ($) {
3725                  }                  }
3726                } # INSCOPE                } # INSCOPE
3727                unless (defined $i) {                unless (defined $i) {
3728                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3729                  ## Ignore tokens </table><table>                  ## Ignore the token
3730                  !!!next-token;                  !!!next-token;
3731                  redo B;                  redo B;
3732                }                }
3733                  
3734                ## generate implied end tags                ## generate implied end tags
3735                if ({                if ({
3736                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3737                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3738                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3739                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3740                  !!!back-token;                  !!!back-token;
3741                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3742                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3743                  redo B;                  redo B;
3744                }                }
3745                  
3746                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3747                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3748                }                }
3749                    
3750                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3751                  
3752                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3753                  
3754                ## reprocess                !!!next-token;
3755                redo B;                redo B;
3756              } else {              } elsif ({
3757                #                        tbody => 1, tfoot => 1, thead => 1,
3758              }                       }->{$token->{tag_name}} and
3759            } elsif ($token->{type} eq 'end tag') {                       $self->{insertion_mode} & ROW_IMS) {
3760              if ({                if ($self->{insertion_mode} == IN_ROW_IM) {
3761                   tbody => 1, tfoot => 1, thead => 1,                  ## have an element in table scope
3762                  }->{$token->{tag_name}}) {                  my $i;
3763                ## have an element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3764                my $i;                    my $node = $self->{open_elements}->[$_];
3765                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq $token->{tag_name}) {
3766                  my $node = $self->{open_elements}->[$_];                      $i = $_;
3767                  if ($node->[1] eq $token->{tag_name}) {                      last INSCOPE;
3768                    $i = $_;                    } elsif ({
3769                    last INSCOPE;                              table => 1, html => 1,
3770                  } elsif ({                             }->{$node->[1]}) {
3771                            table => 1, html => 1,                      last INSCOPE;
3772                           }->{$node->[1]}) {                    }
3773                    last INSCOPE;                  } # INSCOPE
3774                      unless (defined $i) {
3775                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3776                        ## Ignore the token
3777                        !!!next-token;
3778                        redo B;
3779                      }
3780                    
3781                    ## As if </tr>
3782                    ## have an element in table scope
3783                    my $i;
3784                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3785                      my $node = $self->{open_elements}->[$_];
3786                      if ($node->[1] eq 'tr') {
3787                        $i = $_;
3788                        last INSCOPE;
3789                      } elsif ({
3790                                table => 1, html => 1,
3791                               }->{$node->[1]}) {
3792                        last INSCOPE;
3793                      }
3794                    } # INSCOPE
3795                      unless (defined $i) {
3796                        !!!parse-error (type => 'unmatched end tag:tr');
3797                        ## Ignore the token
3798                        !!!next-token;
3799                        redo B;
3800                      }
3801                    
3802                    ## Clear back to table row context
3803                    while (not {
3804                      tr => 1, html => 1,
3805                    }->{$self->{open_elements}->[-1]->[1]}) {
3806                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3807                      pop @{$self->{open_elements}};
3808                  }                  }
3809                } # INSCOPE                  
3810                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3811                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3812                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
3813                }                }
3814    
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
3815                ## have an element in table scope                ## have an element in table scope
3816                my $i;                my $i;
3817                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3818                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3819                  if ({                  if ($node->[1] eq $token->{tag_name}) {
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
3820                    $i = $_;                    $i = $_;
3821                    last INSCOPE;                    last INSCOPE;
3822                  } elsif ({                  } elsif ({
# Line 4491  sub _tree_construction_main ($) { Line 3840  sub _tree_construction_main ($) {
3840                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3841                }                }
3842    
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
3843                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3844                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3845                ## reprocess                !!!next-token;
3846                redo B;                redo B;
3847              } elsif ({              } elsif ({
3848                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3849                        html => 1, td => 1, th => 1, tr => 1,                        html => 1, td => 1, th => 1,
3850                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3851                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3852                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3853                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3854                ## Ignore the token                ## Ignore the token
# Line 4514  sub _tree_construction_main ($) { Line 3858  sub _tree_construction_main ($) {
3858                #                #
3859              }              }
3860            } else {            } else {
3861              #              die "$0: $token->{type}: Unknown token type";
3862            }            }
3863              
3864            ## As if in table        !!!parse-error (type => 'in table:'.$token->{tag_name});
3865            !!!parse-error (type => 'in table:'.$token->{tag_name});  
3866            $in_body->($insert_to_foster);        $insert = $insert_to_foster;
3867            redo B;        #
3868          } elsif ($self->{insertion_mode} eq 'in row') {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3869            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
             ## NOTE: This is a "character in table" code clone.  
3870              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3871                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
                 
3872                unless (length $token->{data}) {                unless (length $token->{data}) {
3873                  !!!next-token;                  !!!next-token;
3874                  redo B;                  redo B;
3875                }                }
3876              }              }
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
3877                            
3878              !!!next-token;              #
3879              redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
3880            } elsif ($token->{type} eq 'start tag') {              if ($token->{tag_name} eq 'col') {
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
3881                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3882                $self->{insertion_mode} = 'in cell';                pop @{$self->{open_elements}};
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
3883                !!!next-token;                !!!next-token;
3884                redo B;                redo B;
3885              } elsif ({              } else {
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3886                #                #
3887              }              }
3888            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3889              if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'colgroup') {
3890                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3891                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
3892                  ## Ignore the token                  ## Ignore the token
3893                  !!!next-token;                  !!!next-token;
3894                  redo B;                  redo B;
3895                }                } else {
3896                    pop @{$self->{open_elements}}; # colgroup
3897                ## As if </tr>                  $self->{insertion_mode} = IN_TABLE_IM;
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
3898                  !!!next-token;                  !!!next-token;
3899                  redo B;                  redo B;            
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
3900                }                }
3901                } elsif ($token->{tag_name} eq 'col') {
3902                pop @{$self->{open_elements}}; # tr                !!!parse-error (type => 'unmatched end tag:col');
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
3903                ## Ignore the token                ## Ignore the token
3904                !!!next-token;                !!!next-token;
3905                redo B;                redo B;
3906              } else {              } else {
3907                #                #
3908              }              }
3909            } else {            } else {
3910              #              #
3911            }            }
3912    
3913            ## As if in table            ## As if </colgroup>
3914            !!!parse-error (type => 'in table:'.$token->{tag_name});            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3915            $in_body->($insert_to_foster);              !!!parse-error (type => 'unmatched end tag:colgroup');
3916            redo B;              ## Ignore the token
3917          } elsif ($self->{insertion_mode} eq 'in select') {              !!!next-token;
3918            if ($token->{type} eq 'character') {              redo B;
3919              } else {
3920                pop @{$self->{open_elements}}; # colgroup
3921                $self->{insertion_mode} = IN_TABLE_IM;
3922                ## reprocess
3923                redo B;
3924              }
3925        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3926              if ($token->{type} == CHARACTER_TOKEN) {
3927              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3928              !!!next-token;              !!!next-token;
3929              redo B;              redo B;
3930            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3931              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
3932                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
3933                  ## As if </option>                  ## As if </option>
# Line 4891  sub _tree_construction_main ($) { Line 3983  sub _tree_construction_main ($) {
3983              } else {              } else {
3984                #                #
3985              }              }
3986            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3987              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
3988                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
3989                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 5004  sub _tree_construction_main ($) { Line 4096  sub _tree_construction_main ($) {
4096            ## Ignore the token            ## Ignore the token
4097            !!!next-token;            !!!next-token;
4098            redo B;            redo B;
4099          } elsif ($self->{insertion_mode} eq 'after body') {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4100            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
4101              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4102                my $data = $1;            my $data = $1;
4103                ## As if in body            ## As if in body
4104                $reconstruct_active_formatting_elements->($insert_to_current);            $reconstruct_active_formatting_elements->($insert_to_current);
4105                                
4106                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4107              
4108              unless (length $token->{data}) {
4109                !!!next-token;
4110                redo B;
4111              }
4112            }
4113            
4114            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4115              !!!parse-error (type => 'after html:#character');
4116    
4117                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4118                  !!!next-token;          }
4119                  redo B;          
4120                }          ## "after body" insertion mode
4121              }          !!!parse-error (type => 'after body:#character');
4122                
4123              #          $self->{insertion_mode} = IN_BODY_IM;
4124              !!!parse-error (type => 'after body:#character');          ## reprocess
4125            } elsif ($token->{type} eq 'start tag') {          redo B;
4126              !!!parse-error (type => 'after body:'.$token->{tag_name});        } elsif ($token->{type} == START_TAG_TOKEN) {
4127              #          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4128            } elsif ($token->{type} eq 'end tag') {            !!!parse-error (type => 'after html:'.$token->{tag_name});
4129              if ($token->{tag_name} eq 'html') {            
4130                if (defined $self->{inner_html_node}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4131                  !!!parse-error (type => 'unmatched end tag:html');          }
4132                  ## Ignore the token  
4133                  !!!next-token;          ## "after body" insertion mode
4134                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
4135                } else {  
4136                  $previous_insertion_mode = $self->{insertion_mode};          $self->{insertion_mode} = IN_BODY_IM;
4137                  $self->{insertion_mode} = 'trailing end';          ## reprocess
4138                  !!!next-token;          redo B;
4139                  redo B;        } elsif ($token->{type} == END_TAG_TOKEN) {
4140                }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4141              } else {            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4142                !!!parse-error (type => 'after body:/'.$token->{tag_name});            
4143              }            $self->{insertion_mode} = AFTER_BODY_IM;
4144              ## Reprocess in the "main" phase, "after body" insertion mode...
4145            }
4146    
4147            ## "after body" insertion mode
4148            if ($token->{tag_name} eq 'html') {
4149              if (defined $self->{inner_html_node}) {
4150                !!!parse-error (type => 'unmatched end tag:html');
4151                ## Ignore the token
4152                !!!next-token;
4153                redo B;
4154            } else {            } else {
4155              die "$0: $token->{type}: Unknown token type";              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4156                !!!next-token;
4157                redo B;
4158            }            }
4159            } else {
4160              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4161    
4162            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4163            ## reprocess            ## reprocess
4164            redo B;            redo B;
4165      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4166        if ($token->{type} eq 'character') {        } else {
4167            die "$0: $token->{type}: Unknown token type";
4168          }
4169        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4170          if ($token->{type} == CHARACTER_TOKEN) {
4171          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4172            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4173              
4174            unless (length $token->{data}) {            unless (length $token->{data}) {
4175              !!!next-token;              !!!next-token;
4176              redo B;              redo B;
4177            }            }
4178          }          }
4179            
4180            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4181              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4182                !!!parse-error (type => 'in frameset:#character');
4183              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4184                !!!parse-error (type => 'after frameset:#character');
4185              } else { # "after html frameset"
4186                !!!parse-error (type => 'after html:#character');
4187    
4188                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4189                ## Reprocess in the "main" phase, "after frameset"...
4190                !!!parse-error (type => 'after frameset:#character');
4191              }
4192              
4193              ## Ignore the token.
4194              if (length $token->{data}) {
4195                ## reprocess the rest of characters
4196              } else {
4197                !!!next-token;
4198              }
4199              redo B;
4200            }
4201            
4202            die qq[$0: Character "$token->{data}"];
4203          } elsif ($token->{type} == START_TAG_TOKEN) {
4204            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4205              !!!parse-error (type => 'after html:'.$token->{tag_name});
4206    
4207          !!!parse-error (type => 'in frameset:#character');            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4208          ## Ignore the token            ## Process in the "main" phase, "after frameset" insertion mode...
4209          !!!next-token;          }
4210          redo B;  
4211        } elsif ($token->{type} eq 'start tag') {          if ($token->{tag_name} eq 'frameset' and
4212          if ($token->{tag_name} eq 'frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4213            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4214            !!!next-token;            !!!next-token;
4215            redo B;            redo B;
4216          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
4217                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4218            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4219            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4220            !!!next-token;            !!!next-token;
4221            redo B;            redo B;
4222          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
4223            $in_body->($insert_to_current);            ## NOTE: As if in body.
4224              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4225            redo B;            redo B;
4226          } else {          } else {
4227            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4228                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4229              } else {
4230                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4231              }
4232            ## Ignore the token            ## Ignore the token
4233            !!!next-token;            !!!next-token;
4234            redo B;            redo B;
4235          }          }
4236        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4237          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4238              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4239    
4240              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4241              ## Process in the "main" phase, "after frameset" insertion mode...
4242            }
4243    
4244            if ($token->{tag_name} eq 'frameset' and
4245                $self->{insertion_mode} == IN_FRAMESET_IM) {
4246            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4247                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4248              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 5095  sub _tree_construction_main ($) { Line 4255  sub _tree_construction_main ($) {
4255    
4256            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4257                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4258              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4259            }            }
4260            redo B;            redo B;
4261            } elsif ($token->{tag_name} eq 'html' and
4262                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4263              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4264              !!!next-token;
4265              redo B;
4266          } else {          } else {
4267            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4268                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4269              } else {
4270                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4271              }
4272            ## Ignore the token            ## Ignore the token
4273            !!!next-token;            !!!next-token;
4274            redo B;            redo B;
# Line 5107  sub _tree_construction_main ($) { Line 4276  sub _tree_construction_main ($) {
4276        } else {        } else {
4277          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4278        }        }
     } 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);  
4279    
4280                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4281                  !!!next-token;      } else {
4282                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4283                }      }
             }  
4284    
4285              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
4286                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
4287          if ($token->{tag_name} eq 'script') {
4288            ## NOTE: This is an "as if in head" code clone
4289            $script_start_tag->($insert);
4290            redo B;
4291          } elsif ($token->{tag_name} eq 'style') {
4292            ## NOTE: This is an "as if in head" code clone
4293            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4294            redo B;
4295          } elsif ({
4296                    base => 1, link => 1,
4297                   }->{$token->{tag_name}}) {
4298            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4299            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4300            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4301            !!!next-token;
4302            redo B;
4303          } elsif ($token->{tag_name} eq 'meta') {
4304            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4305            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4306            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4307    
4308                ## Ignore the token.          unless ($self->{confident}) {
4309                if (length $token->{data}) {            my $charset;
4310                  ## reprocess the rest of characters            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4311                } else {              $charset = $token->{attributes}->{charset}->{value};
4312                  !!!next-token;            }
4313                }            if ($token->{attributes}->{'http-equiv'}) {
4314                redo B;              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4315              }              if ($token->{attributes}->{'http-equiv'}->{value}
4316                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4317                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4318                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4319                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4320                } ## TODO: And if supported
4321              }
4322              ## TODO: Change the encoding
4323            }
4324    
4325          die qq[$0: Character "$token->{data}"];          !!!next-token;
4326        } elsif ($token->{type} eq 'start tag') {          redo B;
4327          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
4328            $in_body->($insert_to_current);          !!!parse-error (type => 'in body:title');
4329            redo B;          ## NOTE: This is an "as if in head" code clone
4330          } else {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4331            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            if (defined $self->{head_element}) {
4332                $self->{head_element}->append_child ($_[0]);
4333              } else {
4334                $insert->($_[0]);
4335              }
4336            });
4337            redo B;
4338          } elsif ($token->{tag_name} eq 'body') {
4339            !!!parse-error (type => 'in body:body');
4340                  
4341            if (@{$self->{open_elements}} == 1 or
4342                $self->{open_elements}->[1]->[1] ne 'body') {
4343            ## Ignore the token            ## Ignore the token
4344            } else {
4345              my $body_el = $self->{open_elements}->[1]->[0];
4346              for my $attr_name (keys %{$token->{attributes}}) {
4347                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4348                  $body_el->set_attribute_ns
4349                    (undef, [undef, $attr_name],
4350                     $token->{attributes}->{$attr_name}->{value});
4351                }
4352              }
4353            }
4354            !!!next-token;
4355            redo B;
4356          } elsif ({
4357                    address => 1, blockquote => 1, center => 1, dir => 1,
4358                    div => 1, dl => 1, fieldset => 1, listing => 1,
4359                    menu => 1, ol => 1, p => 1, ul => 1,
4360                    pre => 1,
4361                   }->{$token->{tag_name}}) {
4362            ## has a p element in scope
4363            INSCOPE: for (reverse @{$self->{open_elements}}) {
4364              if ($_->[1] eq 'p') {
4365                !!!back-token;
4366                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4367                redo B;
4368              } elsif ({
4369                        table => 1, caption => 1, td => 1, th => 1,
4370                        button => 1, marquee => 1, object => 1, html => 1,
4371                       }->{$_->[1]}) {
4372                last INSCOPE;
4373              }
4374            } # INSCOPE
4375              
4376            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4377            if ($token->{tag_name} eq 'pre') {
4378              !!!next-token;
4379              if ($token->{type} == CHARACTER_TOKEN) {
4380                $token->{data} =~ s/^\x0A//;
4381                unless (length $token->{data}) {
4382                  !!!next-token;
4383                }
4384              }
4385            } else {
4386            !!!next-token;            !!!next-token;
           redo B;  
4387          }          }
4388        } elsif ($token->{type} eq 'end tag') {          redo B;
4389          if ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'form') {
4390            $previous_insertion_mode = $self->{insertion_mode};          if (defined $self->{form_element}) {
4391            $self->{insertion_mode} = 'trailing end';            !!!parse-error (type => 'in form:form');
4392              ## Ignore the token
4393            !!!next-token;            !!!next-token;
4394            redo B;            redo B;
4395          } else {          } else {
4396            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            ## has a p element in scope
4397            ## Ignore the token            INSCOPE: for (reverse @{$self->{open_elements}}) {
4398                if ($_->[1] eq 'p') {
4399                  !!!back-token;
4400                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4401                  redo B;
4402                } elsif ({
4403                          table => 1, caption => 1, td => 1, th => 1,
4404                          button => 1, marquee => 1, object => 1, html => 1,
4405                         }->{$_->[1]}) {
4406                  last INSCOPE;
4407                }
4408              } # INSCOPE
4409                
4410              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4411              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4412            !!!next-token;            !!!next-token;
4413            redo B;            redo B;
4414          }          }
4415        } else {        } elsif ($token->{tag_name} eq 'li') {
4416          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
4417        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
4418              if ($_->[1] eq 'p') {
4419                !!!back-token;
4420                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4421                redo B;
4422              } elsif ({
4423                        table => 1, caption => 1, td => 1, th => 1,
4424                        button => 1, marquee => 1, object => 1, html => 1,
4425                       }->{$_->[1]}) {
4426                last INSCOPE;
4427              }
4428            } # INSCOPE
4429              
4430            ## Step 1
4431            my $i = -1;
4432            my $node = $self->{open_elements}->[$i];
4433            LI: {
4434              ## Step 2
4435              if ($node->[1] eq 'li') {
4436                if ($i != -1) {
4437                  !!!parse-error (type => 'end tag missing:'.
4438                                  $self->{open_elements}->[-1]->[1]);
4439                }
4440                splice @{$self->{open_elements}}, $i;
4441                last LI;
4442              }
4443              
4444              ## Step 3
4445              if (not $formatting_category->{$node->[1]} and
4446                  #not $phrasing_category->{$node->[1]} and
4447                  ($special_category->{$node->[1]} or
4448                   $scoping_category->{$node->[1]}) and
4449                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4450                last LI;
4451              }
4452              
4453              ## Step 4
4454              $i--;
4455              $node = $self->{open_elements}->[$i];
4456              redo LI;
4457            } # LI
4458              
4459            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4460            !!!next-token;
4461            redo B;
4462          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4463            ## has a p element in scope
4464            INSCOPE: for (reverse @{$self->{open_elements}}) {
4465              if ($_->[1] eq 'p') {
4466                !!!back-token;
4467                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4468                redo B;
4469              } elsif ({
4470                        table => 1, caption => 1, td => 1, th => 1,
4471                        button => 1, marquee => 1, object => 1, html => 1,
4472                       }->{$_->[1]}) {
4473                last INSCOPE;
4474              }
4475            } # INSCOPE
4476              
4477            ## Step 1
4478            my $i = -1;
4479            my $node = $self->{open_elements}->[$i];
4480            LI: {
4481              ## Step 2
4482              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4483                if ($i != -1) {
4484                  !!!parse-error (type => 'end tag missing:'.
4485                                  $self->{open_elements}->[-1]->[1]);
4486                }
4487                splice @{$self->{open_elements}}, $i;
4488                last LI;
4489              }
4490              
4491              ## Step 3
4492              if (not $formatting_category->{$node->[1]} and
4493                  #not $phrasing_category->{$node->[1]} and
4494                  ($special_category->{$node->[1]} or
4495                   $scoping_category->{$node->[1]}) and
4496                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4497                last LI;
4498              }
4499              
4500              ## Step 4
4501              $i--;
4502              $node = $self->{open_elements}->[$i];
4503              redo LI;
4504            } # LI
4505              
4506            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4507            !!!next-token;
4508            redo B;
4509          } elsif ($token->{tag_name} eq 'plaintext') {
4510            ## has a p element in scope
4511            INSCOPE: for (reverse @{$self->{open_elements}}) {
4512              if ($_->[1] eq 'p') {
4513                !!!back-token;
4514                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4515                redo B;
4516              } elsif ({
4517                        table => 1, caption => 1, td => 1, th => 1,
4518                        button => 1, marquee => 1, object => 1, html => 1,
4519                       }->{$_->[1]}) {
4520                last INSCOPE;
4521              }
4522            } # INSCOPE
4523              
4524            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4525              
4526            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4527              
4528            !!!next-token;
4529            redo B;
4530          } elsif ({
4531                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4532                   }->{$token->{tag_name}}) {
4533            ## has a p element in scope
4534            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4535              my $node = $self->{open_elements}->[$_];
4536              if ($node->[1] eq 'p') {
4537                !!!back-token;
4538                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4539                redo B;
4540              } elsif ({
4541                        table => 1, caption => 1, td => 1, th => 1,
4542                        button => 1, marquee => 1, object => 1, html => 1,
4543                       }->{$node->[1]}) {
4544                last INSCOPE;
4545              }
4546            } # INSCOPE
4547              
4548            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4549            ## has an element in scope
4550            #my $i;
4551            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4552            #  my $node = $self->{open_elements}->[$_];
4553            #  if ({
4554            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4555            #      }->{$node->[1]}) {
4556            #    $i = $_;
4557            #    last INSCOPE;
4558            #  } elsif ({
4559            #            table => 1, caption => 1, td => 1, th => 1,
4560            #            button => 1, marquee => 1, object => 1, html => 1,
4561            #           }->{$node->[1]}) {
4562            #    last INSCOPE;
4563            #  }
4564            #} # INSCOPE
4565            #  
4566            #if (defined $i) {
4567            #  !!! parse-error (type => 'in hn:hn');
4568            #  splice @{$self->{open_elements}}, $i;
4569            #}
4570              
4571            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4572              
4573            !!!next-token;
4574            redo B;
4575          } elsif ($token->{tag_name} eq 'a') {
4576            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4577              my $node = $active_formatting_elements->[$i];
4578              if ($node->[1] eq 'a') {
4579                !!!parse-error (type => 'in a:a');
4580                
4581                !!!back-token;
4582                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4583                $formatting_end_tag->($token->{tag_name});
4584                
4585                AFE2: for (reverse 0..$#$active_formatting_elements) {
4586                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4587                    splice @$active_formatting_elements, $_, 1;
4588                    last AFE2;
4589                  }
4590                } # AFE2
4591                OE: for (reverse 0..$#{$self->{open_elements}}) {
4592                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4593                    splice @{$self->{open_elements}}, $_, 1;
4594                    last OE;
4595                  }
4596                } # OE
4597                last AFE;
4598              } elsif ($node->[0] eq '#marker') {
4599                last AFE;
4600              }
4601            } # AFE
4602              
4603            $reconstruct_active_formatting_elements->($insert_to_current);
4604    
4605        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4606      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
4607        ## states in the main stage is preserved yet # MUST  
4608                  !!!next-token;
4609        if ($token->{type} eq 'character') {          redo B;
4610          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
4611            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
4612            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
4613            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
4614            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4615            ## end phase is either "after body" or "after frameset".          $reconstruct_active_formatting_elements->($insert_to_current);
4616            $reconstruct_active_formatting_elements->($insert_to_current);          
4617            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4618            push @$active_formatting_elements, $self->{open_elements}->[-1];
4619            
4620            !!!next-token;
4621            redo B;
4622          } elsif ($token->{tag_name} eq 'nobr') {
4623            $reconstruct_active_formatting_elements->($insert_to_current);
4624    
4625            ## has a |nobr| element in scope
4626            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4627              my $node = $self->{open_elements}->[$_];
4628              if ($node->[1] eq 'nobr') {
4629                !!!parse-error (type => 'not closed:nobr');
4630                !!!back-token;
4631                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4632                redo B;
4633              } elsif ({
4634                        table => 1, caption => 1, td => 1, th => 1,
4635                        button => 1, marquee => 1, object => 1, html => 1,
4636                       }->{$node->[1]}) {
4637                last INSCOPE;
4638              }
4639            } # INSCOPE
4640            
4641            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4642            push @$active_formatting_elements, $self->{open_elements}->[-1];
4643            
4644            !!!next-token;
4645            redo B;
4646          } elsif ($token->{tag_name} eq 'button') {
4647            ## has a button element in scope
4648            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4649              my $node = $self->{open_elements}->[$_];
4650              if ($node->[1] eq 'button') {
4651                !!!parse-error (type => 'in button:button');
4652                !!!back-token;
4653                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4654                redo B;
4655              } elsif ({
4656                        table => 1, caption => 1, td => 1, th => 1,
4657                        button => 1, marquee => 1, object => 1, html => 1,
4658                       }->{$node->[1]}) {
4659                last INSCOPE;
4660              }
4661            } # INSCOPE
4662              
4663            $reconstruct_active_formatting_elements->($insert_to_current);
4664              
4665            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4666            push @$active_formatting_elements, ['#marker', ''];
4667    
4668            !!!next-token;
4669            redo B;
4670          } elsif ($token->{tag_name} eq 'marquee' or
4671                   $token->{tag_name} eq 'object') {
4672            $reconstruct_active_formatting_elements->($insert_to_current);
4673            
4674            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4675            push @$active_formatting_elements, ['#marker', ''];
4676            
4677            !!!next-token;
4678            redo B;
4679          } elsif ($token->{tag_name} eq 'xmp') {
4680            $reconstruct_active_formatting_elements->($insert_to_current);
4681            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4682            redo B;
4683          } elsif ($token->{tag_name} eq 'table') {
4684            ## has a p element in scope
4685            INSCOPE: for (reverse @{$self->{open_elements}}) {
4686              if ($_->[1] eq 'p') {
4687                !!!back-token;
4688                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4689                redo B;
4690              } elsif ({
4691                        table => 1, caption => 1, td => 1, th => 1,
4692                        button => 1, marquee => 1, object => 1, html => 1,
4693                       }->{$_->[1]}) {
4694                last INSCOPE;
4695              }
4696            } # INSCOPE
4697                        
4698            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4699                        
4700            $self->{insertion_mode} = IN_TABLE_IM;
4701              
4702            !!!next-token;
4703            redo B;
4704          } elsif ({
4705                    area => 1, basefont => 1, bgsound => 1, br => 1,
4706                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4707                    image => 1,
4708                   }->{$token->{tag_name}}) {
4709            if ($token->{tag_name} eq 'image') {
4710              !!!parse-error (type => 'image');
4711              $token->{tag_name} = 'img';
4712            }
4713    
4714            ## NOTE: There is an "as if <br>" code clone.
4715            $reconstruct_active_formatting_elements->($insert_to_current);
4716            
4717            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4718            pop @{$self->{open_elements}};
4719            
4720            !!!next-token;
4721            redo B;
4722          } elsif ($token->{tag_name} eq 'hr') {
4723            ## has a p element in scope
4724            INSCOPE: for (reverse @{$self->{open_elements}}) {
4725              if ($_->[1] eq 'p') {
4726                !!!back-token;
4727                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4728                redo B;
4729              } elsif ({
4730                        table => 1, caption => 1, td => 1, th => 1,
4731                        button => 1, marquee => 1, object => 1, html => 1,
4732                       }->{$_->[1]}) {
4733                last INSCOPE;
4734              }
4735            } # INSCOPE
4736              
4737            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4738            pop @{$self->{open_elements}};
4739              
4740            !!!next-token;
4741            redo B;
4742          } elsif ($token->{tag_name} eq 'input') {
4743            $reconstruct_active_formatting_elements->($insert_to_current);
4744            
4745            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4746            ## TODO: associate with $self->{form_element} if defined
4747            pop @{$self->{open_elements}};
4748            
4749            !!!next-token;
4750            redo B;
4751          } elsif ($token->{tag_name} eq 'isindex') {
4752            !!!parse-error (type => 'isindex');
4753            
4754            if (defined $self->{form_element}) {
4755              ## Ignore the token
4756              !!!next-token;
4757              redo B;
4758            } else {
4759              my $at = $token->{attributes};
4760              my $form_attrs;
4761              $form_attrs->{action} = $at->{action} if $at->{action};
4762              my $prompt_attr = $at->{prompt};
4763              $at->{name} = {name => 'name', value => 'isindex'};
4764              delete $at->{action};
4765              delete $at->{prompt};
4766              my @tokens = (
4767                            {type => START_TAG_TOKEN, tag_name => 'form',
4768                             attributes => $form_attrs},
4769                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4770                            {type => START_TAG_TOKEN, tag_name => 'p'},
4771                            {type => START_TAG_TOKEN, tag_name => 'label'},
4772                           );
4773              if ($prompt_attr) {
4774                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4775              } else {
4776                push @tokens, {type => CHARACTER_TOKEN,
4777                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4778                ## TODO: make this configurable
4779              }
4780              push @tokens,
4781                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4782                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4783                            {type => END_TAG_TOKEN, tag_name => 'label'},
4784                            {type => END_TAG_TOKEN, tag_name => 'p'},
4785                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4786                            {type => END_TAG_TOKEN, tag_name => 'form'};
4787              $token = shift @tokens;
4788              !!!back-token (@tokens);
4789              redo B;
4790            }
4791          } elsif ($token->{tag_name} eq 'textarea') {
4792            my $tag_name = $token->{tag_name};
4793            my $el;
4794            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4795            
4796            ## TODO: $self->{form_element} if defined
4797            $self->{content_model} = RCDATA_CONTENT_MODEL;
4798            delete $self->{escape}; # MUST
4799            
4800            $insert->($el);
4801            
4802            my $text = '';
4803            !!!next-token;
4804            if ($token->{type} == CHARACTER_TOKEN) {
4805              $token->{data} =~ s/^\x0A//;
4806            unless (length $token->{data}) {            unless (length $token->{data}) {
4807              !!!next-token;              !!!next-token;
             redo B;  
4808            }            }
4809          }          }
4810            while ($token->{type} == CHARACTER_TOKEN) {
4811              $text .= $token->{data};
4812              !!!next-token;
4813            }
4814            if (length $text) {
4815              $el->manakai_append_text ($text);
4816            }
4817            
4818            $self->{content_model} = PCDATA_CONTENT_MODEL;
4819            
4820            if ($token->{type} == END_TAG_TOKEN and
4821                $token->{tag_name} eq $tag_name) {
4822              ## Ignore the token
4823            } else {
4824              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4825            }
4826            !!!next-token;
4827            redo B;
4828          } elsif ({
4829                    iframe => 1,
4830                    noembed => 1,
4831                    noframes => 1,
4832                    noscript => 0, ## TODO: 1 if scripting is enabled
4833                   }->{$token->{tag_name}}) {
4834            ## NOTE: There are two "as if in body" code clones.
4835            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4836            redo B;
4837          } elsif ($token->{tag_name} eq 'select') {
4838            $reconstruct_active_formatting_elements->($insert_to_current);
4839            
4840            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4841            
4842            $self->{insertion_mode} = IN_SELECT_IM;
4843            !!!next-token;
4844            redo B;
4845          } elsif ({
4846                    caption => 1, col => 1, colgroup => 1, frame => 1,
4847                    frameset => 1, head => 1, option => 1, optgroup => 1,
4848                    tbody => 1, td => 1, tfoot => 1, th => 1,
4849                    thead => 1, tr => 1,
4850                   }->{$token->{tag_name}}) {
4851            !!!parse-error (type => 'in body:'.$token->{tag_name});
4852            ## Ignore the token
4853            !!!next-token;
4854            redo B;
4855            
4856            ## ISSUE: An issue on HTML5 new elements in the spec.
4857          } else {
4858            $reconstruct_active_formatting_elements->($insert_to_current);
4859            
4860            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4861            
4862            !!!next-token;
4863            redo B;
4864          }
4865        } elsif ($token->{type} == END_TAG_TOKEN) {
4866          if ($token->{tag_name} eq 'body') {
4867            if (@{$self->{open_elements}} > 1 and
4868                $self->{open_elements}->[1]->[1] eq 'body') {
4869              for (@{$self->{open_elements}}) {
4870                unless ({
4871                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4872                           th => 1, tr => 1, body => 1, html => 1,
4873                         tbody => 1, tfoot => 1, thead => 1,
4874                        }->{$_->[1]}) {
4875                  !!!parse-error (type => 'not closed:'.$_->[1]);
4876                }
4877              }
4878    
4879          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
4880          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
4881          ## reprocess            redo B;
4882            } else {
4883              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4884              ## Ignore the token
4885              !!!next-token;
4886              redo B;
4887            }
4888          } elsif ($token->{tag_name} eq 'html') {
4889            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4890              ## ISSUE: There is an issue in the spec.
4891              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4892                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4893              }
4894              $self->{insertion_mode} = AFTER_BODY_IM;
4895              ## reprocess
4896              redo B;
4897            } else {
4898              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4899              ## Ignore the token
4900              !!!next-token;
4901              redo B;
4902            }
4903          } elsif ({
4904                    address => 1, blockquote => 1, center => 1, dir => 1,
4905                    div => 1, dl => 1, fieldset => 1, listing => 1,
4906                    menu => 1, ol => 1, pre => 1, ul => 1,
4907                    p => 1,
4908                    dd => 1, dt => 1, li => 1,
4909                    button => 1, marquee => 1, object => 1,
4910                   }->{$token->{tag_name}}) {
4911            ## has an element in scope
4912            my $i;
4913            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4914              my $node = $self->{open_elements}->[$_];
4915              if ($node->[1] eq $token->{tag_name}) {
4916                ## generate implied end tags
4917                if ({
4918                     dd => ($token->{tag_name} ne 'dd'),
4919                     dt => ($token->{tag_name} ne 'dt'),
4920                     li => ($token->{tag_name} ne 'li'),
4921                     p => ($token->{tag_name} ne 'p'),
4922                     td => 1, th => 1, tr => 1,
4923                     tbody => 1, tfoot=> 1, thead => 1,
4924                    }->{$self->{open_elements}->[-1]->[1]}) {
4925                  !!!back-token;
4926                  $token = {type => END_TAG_TOKEN,
4927                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4928                  redo B;
4929                }
4930                $i = $_;
4931                last INSCOPE unless $token->{tag_name} eq 'p';
4932              } elsif ({
4933                        table => 1, caption => 1, td => 1, th => 1,
4934                        button => 1, marquee => 1, object => 1, html => 1,
4935                       }->{$node->[1]}) {
4936                last INSCOPE;
4937              }
4938            } # INSCOPE
4939            
4940            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4941              if (defined $i) {
4942                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4943              } else {
4944                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4945              }
4946            }
4947            
4948            if (defined $i) {
4949              splice @{$self->{open_elements}}, $i;
4950            } elsif ($token->{tag_name} eq 'p') {
4951              ## As if <p>, then reprocess the current token
4952              my $el;
4953              !!!create-element ($el, 'p');
4954              $insert->($el);
4955            }
4956            $clear_up_to_marker->()
4957              if {
4958                button => 1, marquee => 1, object => 1,
4959              }->{$token->{tag_name}};
4960            !!!next-token;
4961          redo B;          redo B;
4962        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'form') {
4963          !!!parse-error (type => 'after html:'.$token->{tag_name});          ## has an element in scope
4964          $self->{insertion_mode} = $previous_insertion_mode;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4965          ## reprocess            my $node = $self->{open_elements}->[$_];
4966              if ($node->[1] eq $token->{tag_name}) {
4967                ## generate implied end tags
4968                if ({
4969                     dd => 1, dt => 1, li => 1, p => 1,
4970                     td => 1, th => 1, tr => 1,
4971                     tbody => 1, tfoot=> 1, thead => 1,
4972                    }->{$self->{open_elements}->[-1]->[1]}) {
4973                  !!!back-token;
4974                  $token = {type => END_TAG_TOKEN,
4975                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4976                  redo B;
4977                }
4978                last INSCOPE;
4979              } elsif ({
4980                        table => 1, caption => 1, td => 1, th => 1,
4981                        button => 1, marquee => 1, object => 1, html => 1,
4982                       }->{$node->[1]}) {
4983                last INSCOPE;
4984              }
4985            } # INSCOPE
4986            
4987            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
4988              pop @{$self->{open_elements}};
4989            } else {
4990              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4991            }
4992    
4993            undef $self->{form_element};
4994            !!!next-token;
4995          redo B;          redo B;
4996        } elsif ($token->{type} eq 'end tag') {        } elsif ({
4997          !!!parse-error (type => 'after html:/'.$token->{tag_name});                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4998          $self->{insertion_mode} = $previous_insertion_mode;                 }->{$token->{tag_name}}) {
4999          ## reprocess          ## has an element in scope
5000            my $i;
5001            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5002              my $node = $self->{open_elements}->[$_];
5003              if ({
5004                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5005                  }->{$node->[1]}) {
5006                ## generate implied end tags
5007                if ({
5008                     dd => 1, dt => 1, li => 1, p => 1,
5009                     td => 1, th => 1, tr => 1,
5010                     tbody => 1, tfoot=> 1, thead => 1,
5011                    }->{$self->{open_elements}->[-1]->[1]}) {
5012                  !!!back-token;
5013                  $token = {type => END_TAG_TOKEN,
5014                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5015                  redo B;
5016                }
5017                $i = $_;
5018                last INSCOPE;
5019              } elsif ({
5020                        table => 1, caption => 1, td => 1, th => 1,
5021                        button => 1, marquee => 1, object => 1, html => 1,
5022                       }->{$node->[1]}) {
5023                last INSCOPE;
5024              }
5025            } # INSCOPE
5026            
5027            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5028              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5029            }
5030            
5031            splice @{$self->{open_elements}}, $i if defined $i;
5032            !!!next-token;
5033            redo B;
5034          } elsif ({
5035                    a => 1,
5036                    b => 1, big => 1, em => 1, font => 1, i => 1,
5037                    nobr => 1, s => 1, small => 1, strile => 1,
5038                    strong => 1, tt => 1, u => 1,
5039                   }->{$token->{tag_name}}) {
5040            $formatting_end_tag->($token->{tag_name});
5041            redo B;
5042          } elsif ($token->{tag_name} eq 'br') {
5043            !!!parse-error (type => 'unmatched end tag:br');
5044    
5045            ## As if <br>
5046            $reconstruct_active_formatting_elements->($insert_to_current);
5047            
5048            my $el;
5049            !!!create-element ($el, 'br');
5050            $insert->($el);
5051            
5052            ## Ignore the token.
5053            !!!next-token;
5054          redo B;          redo B;
5055          } elsif ({
5056                    caption => 1, col => 1, colgroup => 1, frame => 1,
5057                    frameset => 1, head => 1, option => 1, optgroup => 1,
5058                    tbody => 1, td => 1, tfoot => 1, th => 1,
5059                    thead => 1, tr => 1,
5060                    area => 1, basefont => 1, bgsound => 1,
5061                    embed => 1, hr => 1, iframe => 1, image => 1,
5062                    img => 1, input => 1, isindex => 1, noembed => 1,
5063                    noframes => 1, param => 1, select => 1, spacer => 1,
5064                    table => 1, textarea => 1, wbr => 1,
5065                    noscript => 0, ## TODO: if scripting is enabled
5066                   }->{$token->{tag_name}}) {
5067            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5068            ## Ignore the token
5069            !!!next-token;
5070            redo B;
5071            
5072            ## ISSUE: Issue on HTML5 new elements in spec
5073            
5074        } else {        } else {
5075          die "$0: $token->{type}: Unknown token";          ## Step 1
5076            my $node_i = -1;
5077            my $node = $self->{open_elements}->[$node_i];
5078    
5079            ## Step 2
5080            S2: {
5081              if ($node->[1] eq $token->{tag_name}) {
5082                ## Step 1
5083                ## generate implied end tags
5084                if ({
5085                     dd => 1, dt => 1, li => 1, p => 1,
5086                     td => 1, th => 1, tr => 1,
5087                     tbody => 1, tfoot => 1, thead => 1,
5088                    }->{$self->{open_elements}->[-1]->[1]}) {
5089                  !!!back-token;
5090                  $token = {type => END_TAG_TOKEN,
5091                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5092                  redo B;
5093                }
5094            
5095                ## Step 2
5096                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5097                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5098                }
5099                
5100                ## Step 3
5101                splice @{$self->{open_elements}}, $node_i;
5102    
5103                !!!next-token;
5104                last S2;
5105              } else {
5106                ## Step 3
5107                if (not $formatting_category->{$node->[1]} and
5108                    #not $phrasing_category->{$node->[1]} and
5109                    ($special_category->{$node->[1]} or
5110                     $scoping_category->{$node->[1]})) {
5111                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5112                  ## Ignore the token
5113                  !!!next-token;
5114                  last S2;
5115                }
5116              }
5117              
5118              ## Step 4
5119              $node_i--;
5120              $node = $self->{open_elements}->[$node_i];
5121              
5122              ## Step 5;
5123              redo S2;
5124            } # S2
5125            redo B;
5126        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
5127      }      }
5128        redo B;
5129    } # B    } # B
5130    
5131      ## NOTE: The "trailing end" phase in HTML5 is split into
5132      ## two insertion modes: "after html body" and "after html frameset".
5133      ## NOTE: States in the main stage is preserved while
5134      ## the parser stays in the trailing end phase. # MUST
5135    
5136    ## Stop parsing # MUST    ## Stop parsing # MUST
5137        
5138    ## TODO: script stuffs    ## TODO: script stuffs

Legend:
Removed from v.1.43  
changed lines
  Added in v.1.56

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24