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

Legend:
Removed from v.1.36  
changed lines
  Added in v.1.62

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24