/[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.25 by wakaba, Sun Jun 24 05:12:11 2007 UTC revision 1.60 by wakaba, Sun Oct 14 09:21:46 2007 UTC
# Line 7  our $VERSION=do{my @r=(q$Revision$=~/\d+ Line 7  our $VERSION=do{my @r=(q$Revision$=~/\d+
7  ## doc.write ('');  ## doc.write ('');
8  ## alert (doc.compatMode);  ## alert (doc.compatMode);
9    
10    ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT
11    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
12    ## is not yet clear.
13    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
14    ## "{U+FEFF}..." in GB18030?
15    
16  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
17    base => 1,    base => 1,
18    link => 1,    link => 1,
# Line 144  sub new ($) { Line 150  sub new ($) {
150    return $self;    return $self;
151  } # new  } # new
152    
153    sub CM_ENTITY () { 0b001 } # & markup in data
154    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
155    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
156    
157    sub PLAINTEXT_CONTENT_MODEL () { 0 }
158    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
159    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162    sub DATA_STATE () { 0 }
163    sub ENTITY_DATA_STATE () { 1 }
164    sub TAG_OPEN_STATE () { 2 }
165    sub CLOSE_TAG_OPEN_STATE () { 3 }
166    sub TAG_NAME_STATE () { 4 }
167    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
168    sub ATTRIBUTE_NAME_STATE () { 6 }
169    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
170    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
171    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
172    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
173    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
174    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
175    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
176    sub COMMENT_START_STATE () { 14 }
177    sub COMMENT_START_DASH_STATE () { 15 }
178    sub COMMENT_STATE () { 16 }
179    sub COMMENT_END_STATE () { 17 }
180    sub COMMENT_END_DASH_STATE () { 18 }
181    sub BOGUS_COMMENT_STATE () { 19 }
182    sub DOCTYPE_STATE () { 20 }
183    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
184    sub DOCTYPE_NAME_STATE () { 22 }
185    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
186    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
187    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
188    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
189    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
190    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
191    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
192    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
193    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
194    sub BOGUS_DOCTYPE_STATE () { 32 }
195    
196    sub DOCTYPE_TOKEN () { 1 }
197    sub COMMENT_TOKEN () { 2 }
198    sub START_TAG_TOKEN () { 3 }
199    sub END_TAG_TOKEN () { 4 }
200    sub END_OF_FILE_TOKEN () { 5 }
201    sub CHARACTER_TOKEN () { 6 }
202    
203    sub AFTER_HTML_IMS () { 0b100 }
204    sub HEAD_IMS ()       { 0b1000 }
205    sub BODY_IMS ()       { 0b10000 }
206    sub BODY_TABLE_IMS () { 0b100000 }
207    sub TABLE_IMS ()      { 0b1000000 }
208    sub ROW_IMS ()        { 0b10000000 }
209    sub BODY_AFTER_IMS () { 0b100000000 }
210    sub FRAME_IMS ()      { 0b1000000000 }
211    
212    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
213    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
214    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
215    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
216    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
217    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
218    sub IN_BODY_IM () { BODY_IMS }
219    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
220    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
221    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
222    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
223    sub IN_TABLE_IM () { TABLE_IMS }
224    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
225    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
226    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
227    sub IN_SELECT_IM () { 0b01 }
228    sub IN_COLUMN_GROUP_IM () { 0b10 }
229    
230  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
231    
232  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
233    my $self = shift;    my $self = shift;
234    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
235    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
236    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
237    undef $self->{current_attribute};    undef $self->{current_attribute};
238    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 162  sub _initialize_tokenizer ($) { Line 245  sub _initialize_tokenizer ($) {
245  } # _initialize_tokenizer  } # _initialize_tokenizer
246    
247  ## A token has:  ## A token has:
248  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
249  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
250  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
251  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
252  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
253  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
254  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
255  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
256    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
257    
258  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
259    
# Line 179  sub _initialize_tokenizer ($) { Line 263  sub _initialize_tokenizer ($) {
263  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
264  ## and removed from the list.  ## and removed from the list.
265    
266    ## NOTE: HTML5 "Writing HTML documents" section, applied to
267    ## documents and not to user agents and conformance checkers,
268    ## contains some requirements that are not detected by the
269    ## parsing algorithm:
270    ## - Some requirements on character encoding declarations. ## TODO
271    ## - "Elements MUST NOT contain content that their content model disallows."
272    ##   ... Some are parse error, some are not (will be reported by c.c.).
273    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
274    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
275    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
276    
277    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
278    ## be detected by the HTML5 parsing algorithm:
279    ## - Text,
280    
281  sub _get_next_token ($) {  sub _get_next_token ($) {
282    my $self = shift;    my $self = shift;
283    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 186  sub _get_next_token ($) { Line 285  sub _get_next_token ($) {
285    }    }
286    
287    A: {    A: {
288      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
289        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
290          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
291              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
292            !!!next-input-character;            !!!next-input-character;
293            redo A;            redo A;
294          } else {          } else {
295            #            #
296          }          }
297        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
298          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
299            unless ($self->{escape}) {            unless ($self->{escape}) {
300              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
301                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 210  sub _get_next_token ($) { Line 307  sub _get_next_token ($) {
307                    
308          #          #
309        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
310          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
311              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
312               not $self->{escape})) {               not $self->{escape})) {
313            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
314            !!!next-input-character;            !!!next-input-character;
315            redo A;            redo A;
316          } else {          } else {
# Line 222  sub _get_next_token ($) { Line 318  sub _get_next_token ($) {
318          }          }
319        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
320          if ($self->{escape} and          if ($self->{escape} and
321              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
322            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
323                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
324              delete $self->{escape};              delete $self->{escape};
# Line 232  sub _get_next_token ($) { Line 327  sub _get_next_token ($) {
327                    
328          #          #
329        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
330          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
331          last A; ## TODO: ok?          last A; ## TODO: ok?
332        }        }
333        # Anything else        # Anything else
334        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
335                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
336        ## Stay in the data state        ## Stay in the data state
337        !!!next-input-character;        !!!next-input-character;
# Line 244  sub _get_next_token ($) { Line 339  sub _get_next_token ($) {
339        !!!emit ($token);        !!!emit ($token);
340    
341        redo A;        redo A;
342      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
343        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
344                
345        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
346    
347        $self->{state} = 'data';        $self->{state} = DATA_STATE;
348        # next-input-character is already done        # next-input-character is already done
349    
350        unless (defined $token) {        unless (defined $token) {
351          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
352        } else {        } else {
353          !!!emit ($token);          !!!emit ($token);
354        }        }
355    
356        redo A;        redo A;
357      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
358        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
359          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
360            !!!next-input-character;            !!!next-input-character;
361            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
362            redo A;            redo A;
363          } else {          } else {
364            ## reconsume            ## reconsume
365            $self->{state} = 'data';            $self->{state} = DATA_STATE;
366    
367            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
368    
369            redo A;            redo A;
370          }          }
371        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
372          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
373            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
374            !!!next-input-character;            !!!next-input-character;
375            redo A;            redo A;
376          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
377            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
378            !!!next-input-character;            !!!next-input-character;
379            redo A;            redo A;
380          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
381                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
382            $self->{current_token}            $self->{current_token}
383              = {type => 'start tag',              = {type => START_TAG_TOKEN,
384                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
385            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
386            !!!next-input-character;            !!!next-input-character;
387            redo A;            redo A;
388          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
389                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
390            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
391                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
392            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
393            !!!next-input-character;            !!!next-input-character;
394            redo A;            redo A;
395          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
396            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
397            $self->{state} = 'data';            $self->{state} = DATA_STATE;
398            !!!next-input-character;            !!!next-input-character;
399    
400            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
401    
402            redo A;            redo A;
403          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
404            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
405            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
406            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
407            redo A;            redo A;
408          } else {          } else {
409            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
410            $self->{state} = 'data';            $self->{state} = DATA_STATE;
411            ## reconsume            ## reconsume
412    
413            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
414    
415            redo A;            redo A;
416          }          }
417        } else {        } else {
418          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
419        }        }
420      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
421        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
422          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
423              ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
424            my @next_char;            my @next_char;
425            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
426              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_input_character};
# Line 338  sub _get_next_token ($) { Line 432  sub _get_next_token ($) {
432              } else {              } else {
433                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
434                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
435                $self->{state} = 'data';                $self->{state} = DATA_STATE;
436    
437                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
438        
439                redo A;                redo A;
440              }              }
# Line 357  sub _get_next_token ($) { Line 451  sub _get_next_token ($) {
451                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
452              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
453              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
454              $self->{state} = 'data';              $self->{state} = DATA_STATE;
455              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
456              redo A;              redo A;
457            } else {            } else {
458              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 368  sub _get_next_token ($) { Line 462  sub _get_next_token ($) {
462          } else {          } else {
463            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
464            # next-input-character is already done            # next-input-character is already done
465            $self->{state} = 'data';            $self->{state} = DATA_STATE;
466            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
467            redo A;            redo A;
468          }          }
469        }        }
470                
471        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
472            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
473          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
474                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
475          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
476          !!!next-input-character;          !!!next-input-character;
477          redo A;          redo A;
478        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
479                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
480          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
481                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
482          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
483          !!!next-input-character;          !!!next-input-character;
484          redo A;          redo A;
485        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
486          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
487          $self->{state} = 'data';          $self->{state} = DATA_STATE;
488          !!!next-input-character;          !!!next-input-character;
489          redo A;          redo A;
490        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
491          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
492          $self->{state} = 'data';          $self->{state} = DATA_STATE;
493          # reconsume          # reconsume
494    
495          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
496    
497          redo A;          redo A;
498        } else {        } else {
499          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
500          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
501          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
502          redo A;          redo A;
503        }        }
504      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
505        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
506            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
507            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
508            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
509            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
510          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
511          !!!next-input-character;          !!!next-input-character;
512          redo A;          redo A;
513        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
514          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
515              $self->{current_token}->{first_start_tag}
516                  = not defined $self->{last_emitted_start_tag_name};
517            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
518          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
519            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
520            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
521              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
522            }            }
523          } else {          } else {
524            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
525          }          }
526          $self->{state} = 'data';          $self->{state} = DATA_STATE;
527          !!!next-input-character;          !!!next-input-character;
528    
529          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 442  sub _get_next_token ($) { Line 538  sub _get_next_token ($) {
538          redo A;          redo A;
539        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
540          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
541          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
542              $self->{current_token}->{first_start_tag}
543                  = not defined $self->{last_emitted_start_tag_name};
544            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
545          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
546            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
547            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
548              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
549            }            }
550          } else {          } else {
551            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
552          }          }
553          $self->{state} = 'data';          $self->{state} = DATA_STATE;
554          # reconsume          # reconsume
555    
556          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 461  sub _get_next_token ($) { Line 559  sub _get_next_token ($) {
559        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
560          !!!next-input-character;          !!!next-input-character;
561          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
562              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
563              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
564            # permitted slash            # permitted slash
565            #            #
566          } else {          } else {
567            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
568          }          }
569          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
570          # next-input-character is already done          # next-input-character is already done
571          redo A;          redo A;
572        } else {        } else {
# Line 478  sub _get_next_token ($) { Line 576  sub _get_next_token ($) {
576          !!!next-input-character;          !!!next-input-character;
577          redo A;          redo A;
578        }        }
579      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
580        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
581            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
582            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 488  sub _get_next_token ($) { Line 586  sub _get_next_token ($) {
586          !!!next-input-character;          !!!next-input-character;
587          redo A;          redo A;
588        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
589          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
590              $self->{current_token}->{first_start_tag}
591                  = not defined $self->{last_emitted_start_tag_name};
592            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
593          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
594            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
595            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
596              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
597            }            }
598          } else {          } else {
599            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
600          }          }
601          $self->{state} = 'data';          $self->{state} = DATA_STATE;
602          !!!next-input-character;          !!!next-input-character;
603    
604          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 508  sub _get_next_token ($) { Line 608  sub _get_next_token ($) {
608                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
609          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
610                                value => ''};                                value => ''};
611          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
612          !!!next-input-character;          !!!next-input-character;
613          redo A;          redo A;
614        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
615          !!!next-input-character;          !!!next-input-character;
616          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
617              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
618              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
619            # permitted slash            # permitted slash
620            #            #
# Line 526  sub _get_next_token ($) { Line 626  sub _get_next_token ($) {
626          redo A;          redo A;
627        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
628          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
629          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
630              $self->{current_token}->{first_start_tag}
631                  = not defined $self->{last_emitted_start_tag_name};
632            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
633          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
634            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
635            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
636              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
637            }            }
638          } else {          } else {
639            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
640          }          }
641          $self->{state} = 'data';          $self->{state} = DATA_STATE;
642          # reconsume          # reconsume
643    
644          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 545  sub _get_next_token ($) { Line 647  sub _get_next_token ($) {
647        } else {        } else {
648          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
649                                value => ''};                                value => ''};
650          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
651          !!!next-input-character;          !!!next-input-character;
652          redo A;          redo A;
653        }        }
654      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
655        my $before_leave = sub {        my $before_leave = sub {
656          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
657              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
658            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
659            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
660          } else {          } else {
661            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 567  sub _get_next_token ($) { Line 669  sub _get_next_token ($) {
669            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
670            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
671          $before_leave->();          $before_leave->();
672          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
673          !!!next-input-character;          !!!next-input-character;
674          redo A;          redo A;
675        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
676          $before_leave->();          $before_leave->();
677          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
678          !!!next-input-character;          !!!next-input-character;
679          redo A;          redo A;
680        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
681          $before_leave->();          $before_leave->();
682          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
683              $self->{current_token}->{first_start_tag}
684                  = not defined $self->{last_emitted_start_tag_name};
685            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
686          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
687            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
688            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
689              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
690            }            }
691          } else {          } else {
692            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
693          }          }
694          $self->{state} = 'data';          $self->{state} = DATA_STATE;
695          !!!next-input-character;          !!!next-input-character;
696    
697          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 603  sub _get_next_token ($) { Line 707  sub _get_next_token ($) {
707          $before_leave->();          $before_leave->();
708          !!!next-input-character;          !!!next-input-character;
709          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
710              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
711              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
712            # permitted slash            # permitted slash
713            #            #
714          } else {          } else {
715            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
716          }          }
717          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
718          # next-input-character is already done          # next-input-character is already done
719          redo A;          redo A;
720        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
721          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
722          $before_leave->();          $before_leave->();
723          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
724              $self->{current_token}->{first_start_tag}
725                  = not defined $self->{last_emitted_start_tag_name};
726            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
727          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
728            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
729            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
730              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
731            }            }
732          } else {          } else {
733            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
734          }          }
735          $self->{state} = 'data';          $self->{state} = DATA_STATE;
736          # reconsume          # reconsume
737    
738          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 638  sub _get_next_token ($) { Line 744  sub _get_next_token ($) {
744          !!!next-input-character;          !!!next-input-character;
745          redo A;          redo A;
746        }        }
747      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
748        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
749            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
750            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 648  sub _get_next_token ($) { Line 754  sub _get_next_token ($) {
754          !!!next-input-character;          !!!next-input-character;
755          redo A;          redo A;
756        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
757          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
758          !!!next-input-character;          !!!next-input-character;
759          redo A;          redo A;
760        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
761          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
762              $self->{current_token}->{first_start_tag}
763                  = not defined $self->{last_emitted_start_tag_name};
764            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
765          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
766            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
767            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
768              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
769            }            }
770          } else {          } else {
771            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
772          }          }
773          $self->{state} = 'data';          $self->{state} = DATA_STATE;
774          !!!next-input-character;          !!!next-input-character;
775    
776          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 672  sub _get_next_token ($) { Line 780  sub _get_next_token ($) {
780                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
781          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
782                                value => ''};                                value => ''};
783          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
784          !!!next-input-character;          !!!next-input-character;
785          redo A;          redo A;
786        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
787          !!!next-input-character;          !!!next-input-character;
788          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
789              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
790              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
791            # permitted slash            # permitted slash
792            #            #
793          } else {          } else {
794            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
795              ## TODO: Different error type for <aa / bb> than <aa/>
796          }          }
797          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
798          # next-input-character is already done          # next-input-character is already done
799          redo A;          redo A;
800        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
801          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
802          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
803              $self->{current_token}->{first_start_tag}
804                  = not defined $self->{last_emitted_start_tag_name};
805            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
806          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
807            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
808            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
809              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
810            }            }
811          } else {          } else {
812            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
813          }          }
814          $self->{state} = 'data';          $self->{state} = DATA_STATE;
815          # reconsume          # reconsume
816    
817          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 709  sub _get_next_token ($) { Line 820  sub _get_next_token ($) {
820        } else {        } else {
821          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
822                                value => ''};                                value => ''};
823          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
824          !!!next-input-character;          !!!next-input-character;
825          redo A;                  redo A;        
826        }        }
827      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
828        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
829            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
830            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 723  sub _get_next_token ($) { Line 834  sub _get_next_token ($) {
834          !!!next-input-character;          !!!next-input-character;
835          redo A;          redo A;
836        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
837          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
838          !!!next-input-character;          !!!next-input-character;
839          redo A;          redo A;
840        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
841          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
842          ## reconsume          ## reconsume
843          redo A;          redo A;
844        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
845          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
846          !!!next-input-character;          !!!next-input-character;
847          redo A;          redo A;
848        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
849          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
850              $self->{current_token}->{first_start_tag}
851                  = not defined $self->{last_emitted_start_tag_name};
852            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
853          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
854            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
855            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
856              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
857            }            }
858          } else {          } else {
859            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
860          }          }
861          $self->{state} = 'data';          $self->{state} = DATA_STATE;
862          !!!next-input-character;          !!!next-input-character;
863    
864          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 753  sub _get_next_token ($) { Line 866  sub _get_next_token ($) {
866          redo A;          redo A;
867        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
868          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
869          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
870              $self->{current_token}->{first_start_tag}
871                  = not defined $self->{last_emitted_start_tag_name};
872            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
873          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
874            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
875            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
876              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
877            }            }
878          } else {          } else {
879            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
880          }          }
881          $self->{state} = 'data';          $self->{state} = DATA_STATE;
882          ## reconsume          ## reconsume
883    
884          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 771  sub _get_next_token ($) { Line 886  sub _get_next_token ($) {
886          redo A;          redo A;
887        } else {        } else {
888          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
889          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
890          !!!next-input-character;          !!!next-input-character;
891          redo A;          redo A;
892        }        }
893      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
894        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
895          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
896          !!!next-input-character;          !!!next-input-character;
897          redo A;          redo A;
898        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
899          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
900          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
901          !!!next-input-character;          !!!next-input-character;
902          redo A;          redo A;
903        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
904          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
905          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
906              $self->{current_token}->{first_start_tag}
907                  = not defined $self->{last_emitted_start_tag_name};
908            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
909          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
910            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
911            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
912              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
913            }            }
914          } else {          } else {
915            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
916          }          }
917          $self->{state} = 'data';          $self->{state} = DATA_STATE;
918          ## reconsume          ## reconsume
919    
920          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 809  sub _get_next_token ($) { Line 926  sub _get_next_token ($) {
926          !!!next-input-character;          !!!next-input-character;
927          redo A;          redo A;
928        }        }
929      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
930        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
931          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
932          !!!next-input-character;          !!!next-input-character;
933          redo A;          redo A;
934        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
935          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
936          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
937          !!!next-input-character;          !!!next-input-character;
938          redo A;          redo A;
939        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
940          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
941          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
942              $self->{current_token}->{first_start_tag}
943                  = not defined $self->{last_emitted_start_tag_name};
944            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
945          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
946            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
947            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
948              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
949            }            }
950          } else {          } else {
951            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
952          }          }
953          $self->{state} = 'data';          $self->{state} = DATA_STATE;
954          ## reconsume          ## reconsume
955    
956          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 843  sub _get_next_token ($) { Line 962  sub _get_next_token ($) {
962          !!!next-input-character;          !!!next-input-character;
963          redo A;          redo A;
964        }        }
965      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
966        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
967            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
968            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
969            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
970            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
971          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
972          !!!next-input-character;          !!!next-input-character;
973          redo A;          redo A;
974        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
975          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
976          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
977          !!!next-input-character;          !!!next-input-character;
978          redo A;          redo A;
979        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
980          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
981              $self->{current_token}->{first_start_tag}
982                  = not defined $self->{last_emitted_start_tag_name};
983            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
984          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
985            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
986            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
987              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
988            }            }
989          } else {          } else {
990            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
991          }          }
992          $self->{state} = 'data';          $self->{state} = DATA_STATE;
993          !!!next-input-character;          !!!next-input-character;
994    
995          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 876  sub _get_next_token ($) { Line 997  sub _get_next_token ($) {
997          redo A;          redo A;
998        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
999          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1000          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1001              $self->{current_token}->{first_start_tag}
1002                  = not defined $self->{last_emitted_start_tag_name};
1003            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1004          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1005            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1006            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1007              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1008            }            }
1009          } else {          } else {
1010            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1011          }          }
1012          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1013          ## reconsume          ## reconsume
1014    
1015          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 898  sub _get_next_token ($) { Line 1021  sub _get_next_token ($) {
1021          !!!next-input-character;          !!!next-input-character;
1022          redo A;          redo A;
1023        }        }
1024      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1025        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1026    
1027        unless (defined $token) {        unless (defined $token) {
1028          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 911  sub _get_next_token ($) { Line 1034  sub _get_next_token ($) {
1034        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1035        # next-input-character is already done        # next-input-character is already done
1036        redo A;        redo A;
1037      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1038        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1039                
1040        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1041    
1042        BC: {        BC: {
1043          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1044            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1045            !!!next-input-character;            !!!next-input-character;
1046    
1047            !!!emit ($token);            !!!emit ($token);
1048    
1049            redo A;            redo A;
1050          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1051            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1052            ## reconsume            ## reconsume
1053    
1054            !!!emit ($token);            !!!emit ($token);
# Line 937  sub _get_next_token ($) { Line 1060  sub _get_next_token ($) {
1060            redo BC;            redo BC;
1061          }          }
1062        } # BC        } # BC
1063      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1064        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1065    
1066        my @next_char;        my @next_char;
# Line 947  sub _get_next_token ($) { Line 1070  sub _get_next_token ($) {
1070          !!!next-input-character;          !!!next-input-character;
1071          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1072          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1073            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1074            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1075            !!!next-input-character;            !!!next-input-character;
1076            redo A;            redo A;
1077          }          }
# Line 979  sub _get_next_token ($) { Line 1102  sub _get_next_token ($) {
1102                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1103                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1104                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1105                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1106                      !!!next-input-character;                      !!!next-input-character;
1107                      redo A;                      redo A;
1108                    }                    }
# Line 990  sub _get_next_token ($) { Line 1113  sub _get_next_token ($) {
1113          }          }
1114        }        }
1115    
1116        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1117        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1118        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1119        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1120        redo A;        redo A;
1121                
1122        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1123        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1124      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1125        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1126          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1127          !!!next-input-character;          !!!next-input-character;
1128          redo A;          redo A;
1129        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1130          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1131          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1132          !!!next-input-character;          !!!next-input-character;
1133    
1134          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1013  sub _get_next_token ($) { Line 1136  sub _get_next_token ($) {
1136          redo A;          redo A;
1137        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1138          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1139          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1140          ## reconsume          ## reconsume
1141    
1142          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1022  sub _get_next_token ($) { Line 1145  sub _get_next_token ($) {
1145        } else {        } else {
1146          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1147              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1148          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1149          !!!next-input-character;          !!!next-input-character;
1150          redo A;          redo A;
1151        }        }
1152      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1153        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1154          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1155          !!!next-input-character;          !!!next-input-character;
1156          redo A;          redo A;
1157        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1158          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1159          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1160          !!!next-input-character;          !!!next-input-character;
1161    
1162          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1041  sub _get_next_token ($) { Line 1164  sub _get_next_token ($) {
1164          redo A;          redo A;
1165        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1166          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1167          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1168          ## reconsume          ## reconsume
1169    
1170          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1049  sub _get_next_token ($) { Line 1172  sub _get_next_token ($) {
1172          redo A;          redo A;
1173        } else {        } else {
1174          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1175              .= chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1176          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1177          !!!next-input-character;          !!!next-input-character;
1178          redo A;          redo A;
1179        }        }
1180      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1181        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1182          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1183          !!!next-input-character;          !!!next-input-character;
1184          redo A;          redo A;
1185        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1186          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1187          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1188          ## reconsume          ## reconsume
1189    
1190          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1073  sub _get_next_token ($) { Line 1196  sub _get_next_token ($) {
1196          !!!next-input-character;          !!!next-input-character;
1197          redo A;          redo A;
1198        }        }
1199      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1200        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1201          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1202          !!!next-input-character;          !!!next-input-character;
1203          redo A;          redo A;
1204        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1205          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1206          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1207          ## reconsume          ## reconsume
1208    
1209          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1088  sub _get_next_token ($) { Line 1211  sub _get_next_token ($) {
1211          redo A;          redo A;
1212        } else {        } else {
1213          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1214          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        }        }
1218      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1219        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1220          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1221          !!!next-input-character;          !!!next-input-character;
1222    
1223          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1108  sub _get_next_token ($) { Line 1231  sub _get_next_token ($) {
1231          redo A;          redo A;
1232        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1233          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1234          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1235          ## reconsume          ## reconsume
1236    
1237          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1117  sub _get_next_token ($) { Line 1240  sub _get_next_token ($) {
1240        } else {        } else {
1241          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1242          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1243          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1244          !!!next-input-character;          !!!next-input-character;
1245          redo A;          redo A;
1246        }        }
1247      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1248        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1249            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1250            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1251            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1252            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1253          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1254          !!!next-input-character;          !!!next-input-character;
1255          redo A;          redo A;
1256        } else {        } else {
1257          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1258          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1259          ## reconsume          ## reconsume
1260          redo A;          redo A;
1261        }        }
1262      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1263        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1264            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1265            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1147  sub _get_next_token ($) { Line 1270  sub _get_next_token ($) {
1270          redo A;          redo A;
1271        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1272          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1273          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1274          !!!next-input-character;          !!!next-input-character;
1275    
1276          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1277    
1278          redo A;          redo A;
1279        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1280          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1281          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1282          ## reconsume          ## reconsume
1283    
1284          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1285    
1286          redo A;          redo A;
1287        } else {        } else {
1288          $self->{current_token}          $self->{current_token}
1289              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1290                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1291                 correct => 1};                 correct => 1};
1292  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1293          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1294          !!!next-input-character;          !!!next-input-character;
1295          redo A;          redo A;
1296        }        }
1297      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1298  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1299        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1300            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1301            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1302            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1303            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1304          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1305          !!!next-input-character;          !!!next-input-character;
1306          redo A;          redo A;
1307        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1308          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1309          !!!next-input-character;          !!!next-input-character;
1310    
1311          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1190  sub _get_next_token ($) { Line 1313  sub _get_next_token ($) {
1313          redo A;          redo A;
1314        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1315          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1316          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1317          ## reconsume          ## reconsume
1318    
1319          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1204  sub _get_next_token ($) { Line 1327  sub _get_next_token ($) {
1327          !!!next-input-character;          !!!next-input-character;
1328          redo A;          redo A;
1329        }        }
1330      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1331        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1332            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1333            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1214  sub _get_next_token ($) { Line 1337  sub _get_next_token ($) {
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
1339        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1340          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1341          !!!next-input-character;          !!!next-input-character;
1342    
1343          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1222  sub _get_next_token ($) { Line 1345  sub _get_next_token ($) {
1345          redo A;          redo A;
1346        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1347          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1348          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1349          ## reconsume          ## reconsume
1350    
1351          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1246  sub _get_next_token ($) { Line 1369  sub _get_next_token ($) {
1369                  !!!next-input-character;                  !!!next-input-character;
1370                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1371                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1372                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1373                    !!!next-input-character;                    !!!next-input-character;
1374                    redo A;                    redo A;
1375                  }                  }
# Line 1273  sub _get_next_token ($) { Line 1396  sub _get_next_token ($) {
1396                  !!!next-input-character;                  !!!next-input-character;
1397                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1398                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1399                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1400                    !!!next-input-character;                    !!!next-input-character;
1401                    redo A;                    redo A;
1402                  }                  }
# Line 1289  sub _get_next_token ($) { Line 1412  sub _get_next_token ($) {
1412        }        }
1413    
1414        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1415        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1416        # next-input-character is already done        # next-input-character is already done
1417        redo A;        redo A;
1418      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1419        if ({        if ({
1420              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1421              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1302  sub _get_next_token ($) { Line 1425  sub _get_next_token ($) {
1425          redo A;          redo A;
1426        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1427          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1428          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1429          !!!next-input-character;          !!!next-input-character;
1430          redo A;          redo A;
1431        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1432          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1433          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1434          !!!next-input-character;          !!!next-input-character;
1435          redo A;          redo A;
1436        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1437          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1438    
1439          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1440          !!!next-input-character;          !!!next-input-character;
1441    
1442          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1323  sub _get_next_token ($) { Line 1446  sub _get_next_token ($) {
1446        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1447          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1448    
1449          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1450          ## reconsume          ## reconsume
1451    
1452          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1332  sub _get_next_token ($) { Line 1455  sub _get_next_token ($) {
1455          redo A;          redo A;
1456        } else {        } else {
1457          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1458          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1459          !!!next-input-character;          !!!next-input-character;
1460          redo A;          redo A;
1461        }        }
1462      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1463        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1464          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1465          !!!next-input-character;          !!!next-input-character;
1466          redo A;          redo A;
1467        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1468          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1469    
1470          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1471          ## reconsume          ## reconsume
1472    
1473          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1358  sub _get_next_token ($) { Line 1481  sub _get_next_token ($) {
1481          !!!next-input-character;          !!!next-input-character;
1482          redo A;          redo A;
1483        }        }
1484      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1485        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1486          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1487          !!!next-input-character;          !!!next-input-character;
1488          redo A;          redo A;
1489        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1490          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1491    
1492          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1493          ## reconsume          ## reconsume
1494    
1495          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1380  sub _get_next_token ($) { Line 1503  sub _get_next_token ($) {
1503          !!!next-input-character;          !!!next-input-character;
1504          redo A;          redo A;
1505        }        }
1506      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1507        if ({        if ({
1508              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1509              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1390  sub _get_next_token ($) { Line 1513  sub _get_next_token ($) {
1513          redo A;          redo A;
1514        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1515          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1516          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1517          !!!next-input-character;          !!!next-input-character;
1518          redo A;          redo A;
1519        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1520          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1521          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1522          !!!next-input-character;          !!!next-input-character;
1523          redo A;          redo A;
1524        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1525          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1526          !!!next-input-character;          !!!next-input-character;
1527    
1528          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1408  sub _get_next_token ($) { Line 1531  sub _get_next_token ($) {
1531        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1532          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1533    
1534          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1535          ## recomsume          ## reconsume
1536    
1537          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1538          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1417  sub _get_next_token ($) { Line 1540  sub _get_next_token ($) {
1540          redo A;          redo A;
1541        } else {        } else {
1542          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1543          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1544          !!!next-input-character;          !!!next-input-character;
1545          redo A;          redo A;
1546        }        }
1547      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1548        if ({        if ({
1549              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1550              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1431  sub _get_next_token ($) { Line 1554  sub _get_next_token ($) {
1554          redo A;          redo A;
1555        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1556          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1557          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1558          !!!next-input-character;          !!!next-input-character;
1559          redo A;          redo A;
1560        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1561          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1562          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1563          !!!next-input-character;          !!!next-input-character;
1564          redo A;          redo A;
1565        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1566          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1567          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1568          !!!next-input-character;          !!!next-input-character;
1569    
1570          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1451  sub _get_next_token ($) { Line 1574  sub _get_next_token ($) {
1574        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1575          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1576    
1577          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1578          ## recomsume          ## reconsume
1579    
1580          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1581          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1582    
1583          redo A;          redo A;
1584        } else {        } else {
1585          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after SYSTEM');
1586          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1587          !!!next-input-character;          !!!next-input-character;
1588          redo A;          redo A;
1589        }        }
1590      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1591        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1592          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1593          !!!next-input-character;          !!!next-input-character;
1594          redo A;          redo A;
1595        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1596          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1597    
1598          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1599          ## reconsume          ## reconsume
1600    
1601          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1486  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609          !!!next-input-character;          !!!next-input-character;
1610          redo A;          redo A;
1611        }        }
1612      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1613        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1614          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1615          !!!next-input-character;          !!!next-input-character;
1616          redo A;          redo A;
1617        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1618          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1619    
1620          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1621          ## reconsume          ## reconsume
1622    
1623          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1508  sub _get_next_token ($) { Line 1631  sub _get_next_token ($) {
1631          !!!next-input-character;          !!!next-input-character;
1632          redo A;          redo A;
1633        }        }
1634      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1635        if ({        if ({
1636              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1637              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1517  sub _get_next_token ($) { Line 1640  sub _get_next_token ($) {
1640          !!!next-input-character;          !!!next-input-character;
1641          redo A;          redo A;
1642        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1644          !!!next-input-character;          !!!next-input-character;
1645    
1646          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1526  sub _get_next_token ($) { Line 1649  sub _get_next_token ($) {
1649        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1650          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1651    
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          ## recomsume          ## reconsume
1654    
1655          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1656          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1535  sub _get_next_token ($) { Line 1658  sub _get_next_token ($) {
1658          redo A;          redo A;
1659        } else {        } else {
1660          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1661          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        }        }
1665      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1666        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1667          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1668          !!!next-input-character;          !!!next-input-character;
1669    
1670          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1550  sub _get_next_token ($) { Line 1673  sub _get_next_token ($) {
1673          redo A;          redo A;
1674        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1675          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1676          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1677          ## reconsume          ## reconsume
1678    
1679          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1570  sub _get_next_token ($) { Line 1693  sub _get_next_token ($) {
1693    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1694  } # _get_next_token  } # _get_next_token
1695    
1696  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1697    my $self = shift;    my ($self, $in_attr) = @_;
1698    
1699    if ({    if ({
1700         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
# Line 1584  sub _tokenize_attempt_to_consume_an_enti Line 1707  sub _tokenize_attempt_to_consume_an_enti
1707      !!!next-input-character;      !!!next-input-character;
1708      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1709          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1710        my $num;        my $code;
1711        X: {        X: {
1712          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1713          !!!next-input-character;          !!!next-input-character;
1714          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1715              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1716            $num ||= 0;            $code ||= 0;
1717            $num *= 0x10;            $code *= 0x10;
1718            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1719            redo X;            redo X;
1720          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1721                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1722            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1723            $num ||= 0;            $code *= 0x10;
1724            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1725            redo X;            redo X;
1726          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1727                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1728            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1729            $num ||= 0;            $code *= 0x10;
1730            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1731            redo X;            redo X;
1732          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1733            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1734              !!!back-next-input-character ($x_char, $self->{next_input_character});
1735            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1736            return undef;            return undef;
1737          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1738            !!!next-input-character;            !!!next-input-character;
# Line 1619  sub _tokenize_attempt_to_consume_an_enti Line 1740  sub _tokenize_attempt_to_consume_an_enti
1740            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1741          }          }
1742    
1743          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1744          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1745          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1746            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1747            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1748          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1749            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1750            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1751              $code = 0x000A;
1752            } elsif (0x80 <= $code and $code <= 0x9F) {
1753              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1754              $code = $c1_entity_char->{$code};
1755          }          }
1756    
1757          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code};
1758        } # X        } # X
1759      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1760               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1650  sub _tokenize_attempt_to_consume_an_enti Line 1775  sub _tokenize_attempt_to_consume_an_enti
1775          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1776        }        }
1777    
1778        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1779        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1780          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1781          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1782            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1783            $code = 0xFFFD;
1784          } elsif ($code == 0x000D) {
1785            !!!parse-error (type => 'CR character reference');
1786            $code = 0x000A;
1787        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1788          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1789          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1790        }        }
1791                
1792        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1793      } else {      } else {
1794        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1795        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1674  sub _tokenize_attempt_to_consume_an_enti Line 1804  sub _tokenize_attempt_to_consume_an_enti
1804      !!!next-input-character;      !!!next-input-character;
1805    
1806      my $value = $entity_name;      my $value = $entity_name;
1807      my $match;      my $match = 0;
1808      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1809      our $EntityChar;      our $EntityChar;
1810    
# Line 1689  sub _tokenize_attempt_to_consume_an_enti Line 1819  sub _tokenize_attempt_to_consume_an_enti
1819              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_input_character} == 0x003B)) { # ;
1820        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1821        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
         $value = $EntityChar->{$entity_name};  
1822          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_input_character} == 0x003B) { # ;
1823              $value = $EntityChar->{$entity_name};
1824            $match = 1;            $match = 1;
1825            !!!next-input-character;            !!!next-input-character;
1826            last;            last;
1827          } else {          } else {
1828              $value = $EntityChar->{$entity_name};
1829            $match = -1;            $match = -1;
1830              !!!next-input-character;
1831          }          }
1832        } else {        } else {
1833          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1834            $match *= 2;
1835            !!!next-input-character;
1836        }        }
       !!!next-input-character;  
1837      }      }
1838            
1839      if ($match > 0) {      if ($match > 0) {
1840        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value};
1841      } elsif ($match < 0) {      } elsif ($match < 0) {
1842        !!!parse-error (type => 'refc');        !!!parse-error (type => 'no refc');
1843        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1844            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1845          } else {
1846            return {type => CHARACTER_TOKEN, data => $value};
1847          }
1848      } else {      } else {
1849        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1850        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1851        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1852      }      }
1853    } else {    } else {
1854      ## no characters are consumed      ## no characters are consumed
# Line 1754  sub _construct_tree ($) { Line 1890  sub _construct_tree ($) {
1890        
1891    !!!next-token;    !!!next-token;
1892    
1893    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1894    undef $self->{form_element};    undef $self->{form_element};
1895    undef $self->{head_element};    undef $self->{head_element};
1896    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1768  sub _construct_tree ($) { Line 1904  sub _construct_tree ($) {
1904  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1905    my $self = shift;    my $self = shift;
1906    INITIAL: {    INITIAL: {
1907      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1908        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1909        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
1910        ## language.        ## language.
# Line 1895  sub _tree_construction_initial ($) { Line 2031  sub _tree_construction_initial ($) {
2031        !!!next-token;        !!!next-token;
2032        return;        return;
2033      } elsif ({      } elsif ({
2034                'start tag' => 1,                START_TAG_TOKEN, 1,
2035                'end tag' => 1,                END_TAG_TOKEN, 1,
2036                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2037               }->{$token->{type}}) {               }->{$token->{type}}) {
2038        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2039        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2040        ## Go to the root element phase        ## Go to the root element phase
2041        ## reprocess        ## reprocess
2042        return;        return;
2043      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2044        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2045          ## Ignore the token          ## Ignore the token
2046    
2047          unless (length $token->{data}) {          unless (length $token->{data}) {
2048            ## Stay in the phase            ## Stay in the phase
2049            !!!next-token;            !!!next-token;
# Line 1919  sub _tree_construction_initial ($) { Line 2056  sub _tree_construction_initial ($) {
2056        ## Go to the root element phase        ## Go to the root element phase
2057        ## reprocess        ## reprocess
2058        return;        return;
2059      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2060        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2061        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2062                
# Line 1927  sub _tree_construction_initial ($) { Line 2064  sub _tree_construction_initial ($) {
2064        !!!next-token;        !!!next-token;
2065        redo INITIAL;        redo INITIAL;
2066      } else {      } else {
2067        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2068      }      }
2069    } # INITIAL    } # INITIAL
2070  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1936  sub _tree_construction_root_element ($) Line 2073  sub _tree_construction_root_element ($)
2073    my $self = shift;    my $self = shift;
2074        
2075    B: {    B: {
2076        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2077          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2078          ## Ignore the token          ## Ignore the token
2079          ## Stay in the phase          ## Stay in the phase
2080          !!!next-token;          !!!next-token;
2081          redo B;          redo B;
2082        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2083          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2084          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2085          ## Stay in the phase          ## Stay in the phase
2086          !!!next-token;          !!!next-token;
2087          redo B;          redo B;
2088        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2089          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2090            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2091            ## ISSUE: DOM3 Core does not allow Document > Text  
2092            unless (length $token->{data}) {            unless (length $token->{data}) {
2093              ## Stay in the phase              ## Stay in the phase
2094              !!!next-token;              !!!next-token;
# Line 1960  sub _tree_construction_root_element ($) Line 2097  sub _tree_construction_root_element ($)
2097          }          }
2098          #          #
2099        } elsif ({        } elsif ({
2100                  'start tag' => 1,                  START_TAG_TOKEN, 1,
2101                  'end tag' => 1,                  END_TAG_TOKEN, 1,
2102                  'end-of-file' => 1,                  END_OF_FILE_TOKEN, 1,
2103                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2104          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2105          #          #
2106        } else {        } else {
2107          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2108        }        }
2109          ## TODO: application cache selection algorithm
2110        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2111        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2112        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2113        ## reprocess        ## reprocess
2114        #redo B;        #redo B;
2115        return;        return; ## Go to the main phase.
2116    } # B    } # B
2117  } # _tree_construction_root_element  } # _tree_construction_root_element
2118    
# Line 1991  sub _reset_insertion_mode ($) { Line 2128  sub _reset_insertion_mode ($) {
2128            
2129      ## Step 3      ## Step 3
2130      S3: {      S3: {
2131        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2132        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2133          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2134              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2135            #        ## The second "if" is in the scope of the first "if"!?
2136          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2137            $node = $self->{inner_html_node};          $last = 1;
2138            if (defined $self->{inner_html_node}) {
2139              if ($self->{inner_html_node}->[1] eq 'td' or
2140                  $self->{inner_html_node}->[1] eq 'th') {
2141                #
2142              } else {
2143                $node = $self->{inner_html_node};
2144              }
2145          }          }
2146        }        }
2147            
2148        ## Step 4..13        ## Step 4..13
2149        my $new_mode = {        my $new_mode = {
2150                        select => 'in select',                        select => IN_SELECT_IM,
2151                        td => 'in cell',                        td => IN_CELL_IM,
2152                        th => 'in cell',                        th => IN_CELL_IM,
2153                        tr => 'in row',                        tr => IN_ROW_IM,
2154                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2155                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2156                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2157                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2158                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2159                        table => 'in table',                        table => IN_TABLE_IM,
2160                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2161                        body => 'in body',                        body => IN_BODY_IM,
2162                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2163                       }->{$node->[1]};                       }->{$node->[1]};
2164        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2165                
2166        ## Step 14        ## Step 14
2167        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2168          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2169            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2170          } else {          } else {
2171            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2172          }          }
2173          return;          return;
2174        }        }
2175                
2176        ## Step 15        ## Step 15
2177        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2178                
2179        ## Step 16        ## Step 16
2180        $i--;        $i--;
# Line 2044  sub _reset_insertion_mode ($) { Line 2188  sub _reset_insertion_mode ($) {
2188  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2189    my $self = shift;    my $self = shift;
2190    
   my $phase = 'main';  
   
2191    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2192    
2193    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2140  sub _tree_construction_main ($) { Line 2282  sub _tree_construction_main ($) {
2282      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2283    
2284      ## Step 3      ## Step 3
2285      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2286      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2287    
2288      ## Step 4      ## Step 4
2289      my $text = '';      my $text = '';
2290      !!!next-token;      !!!next-token;
2291      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2292        $text .= $token->{data};        $text .= $token->{data};
2293        !!!next-token;        !!!next-token;
2294      }      }
# Line 2158  sub _tree_construction_main ($) { Line 2300  sub _tree_construction_main ($) {
2300      }      }
2301    
2302      ## Step 6      ## Step 6
2303      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2304    
2305      ## Step 7      ## Step 7
2306      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2307        ## Ignore the token        ## Ignore the token
2308        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2309          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2310        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2311          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2312      } else {      } else {
2313        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2314      }      }
2315      !!!next-token;      !!!next-token;
2316    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2175  sub _tree_construction_main ($) { Line 2321  sub _tree_construction_main ($) {
2321      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2322      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2323    
2324      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2325      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2326            
2327      my $text = '';      my $text = '';
2328      !!!next-token;      !!!next-token;
2329      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2330        $text .= $token->{data};        $text .= $token->{data};
2331        !!!next-token;        !!!next-token;
2332      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2188  sub _tree_construction_main ($) { Line 2334  sub _tree_construction_main ($) {
2334        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2335      }      }
2336                                
2337      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2338    
2339      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2340          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2341        ## Ignore the token        ## Ignore the token
2342      } else {      } else {
# Line 2433  sub _tree_construction_main ($) { Line 2579  sub _tree_construction_main ($) {
2579                         }                         }
2580    }; # $insert_to_foster    }; # $insert_to_foster
2581    
2582    my $in_body = sub {    my $insert;
2583      my $insert = shift;  
2584      if ($token->{type} eq 'start tag') {    B: {
2585        if ($token->{type} == DOCTYPE_TOKEN) {
2586          !!!parse-error (type => 'DOCTYPE in the middle');
2587          ## Ignore the token
2588          ## Stay in the phase
2589          !!!next-token;
2590          redo B;
2591        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2592          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2593            #
2594          } else {
2595            ## Generate implied end tags
2596            if ({
2597                 dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2598                 tbody => 1, tfoot=> 1, thead => 1,
2599                }->{$self->{open_elements}->[-1]->[1]}) {
2600              !!!back-token;
2601              $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2602              redo B;
2603            }
2604            
2605            if (@{$self->{open_elements}} > 2 or
2606                (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2607              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2608            } elsif (defined $self->{inner_html_node} and
2609                     @{$self->{open_elements}} > 1 and
2610                     $self->{open_elements}->[1]->[1] ne 'body') {
2611              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2612            }
2613    
2614            ## ISSUE: There is an issue in the spec.
2615          }
2616    
2617          ## Stop parsing
2618          last B;
2619        } elsif ($token->{type} == START_TAG_TOKEN and
2620                 $token->{tag_name} eq 'html') {
2621          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2622            ## Turn into the main phase
2623            !!!parse-error (type => 'after html:html');
2624            $self->{insertion_mode} = AFTER_BODY_IM;
2625          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2626            ## Turn into the main phase
2627            !!!parse-error (type => 'after html:html');
2628            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2629          }
2630    
2631    ## ISSUE: "aa<html>" is not a parse error.
2632    ## ISSUE: "<html>" in fragment is not a parse error.
2633          unless ($token->{first_start_tag}) {
2634            !!!parse-error (type => 'not first start tag');
2635          }
2636          my $top_el = $self->{open_elements}->[0]->[0];
2637          for my $attr_name (keys %{$token->{attributes}}) {
2638            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2639              $top_el->set_attribute_ns
2640                (undef, [undef, $attr_name],
2641                 $token->{attributes}->{$attr_name}->{value});
2642            }
2643          }
2644          !!!next-token;
2645          redo B;
2646        } elsif ($token->{type} == COMMENT_TOKEN) {
2647          my $comment = $self->{document}->create_comment ($token->{data});
2648          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2649            $self->{document}->append_child ($comment);
2650          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2651            $self->{open_elements}->[0]->[0]->append_child ($comment);
2652          } else {
2653            $self->{open_elements}->[-1]->[0]->append_child ($comment);
2654          }
2655          !!!next-token;
2656          redo B;
2657        } elsif ($self->{insertion_mode} & HEAD_IMS) {
2658          if ($token->{type} == CHARACTER_TOKEN) {
2659            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2660              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2661              unless (length $token->{data}) {
2662                !!!next-token;
2663                redo B;
2664              }
2665            }
2666    
2667            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2668              ## As if <head>
2669              !!!create-element ($self->{head_element}, 'head');
2670              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2671              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2672    
2673              ## Reprocess in the "in head" insertion mode...
2674              pop @{$self->{open_elements}};
2675    
2676              ## Reprocess in the "after head" insertion mode...
2677            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2678              ## As if </noscript>
2679              pop @{$self->{open_elements}};
2680              !!!parse-error (type => 'in noscript:#character');
2681              
2682              ## Reprocess in the "in head" insertion mode...
2683              ## As if </head>
2684              pop @{$self->{open_elements}};
2685    
2686              ## Reprocess in the "after head" insertion mode...
2687            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2688              pop @{$self->{open_elements}};
2689    
2690              ## Reprocess in the "after head" insertion mode...
2691            }
2692    
2693                ## "after head" insertion mode
2694                ## As if <body>
2695                !!!insert-element ('body');
2696                $self->{insertion_mode} = IN_BODY_IM;
2697                ## reprocess
2698                redo B;
2699              } elsif ($token->{type} == START_TAG_TOKEN) {
2700                if ($token->{tag_name} eq 'head') {
2701                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2702                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2703                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2704                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2705                    $self->{insertion_mode} = IN_HEAD_IM;
2706                    !!!next-token;
2707                    redo B;
2708                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2709                    #
2710                  } else {
2711                    !!!parse-error (type => 'in head:head'); # or in head noscript
2712                    ## Ignore the token
2713                    !!!next-token;
2714                    redo B;
2715                  }
2716                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2717                  ## As if <head>
2718                  !!!create-element ($self->{head_element}, 'head');
2719                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2720                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2721    
2722                  $self->{insertion_mode} = IN_HEAD_IM;
2723                  ## Reprocess in the "in head" insertion mode...
2724                }
2725    
2726                if ($token->{tag_name} eq 'base') {
2727                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2728                    ## As if </noscript>
2729                    pop @{$self->{open_elements}};
2730                    !!!parse-error (type => 'in noscript:base');
2731                  
2732                    $self->{insertion_mode} = IN_HEAD_IM;
2733                    ## Reprocess in the "in head" insertion mode...
2734                  }
2735    
2736                  ## NOTE: There is a "as if in head" code clone.
2737                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2738                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2739                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2740                  }
2741                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2742                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2743                  pop @{$self->{open_elements}}
2744                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2745                  !!!next-token;
2746                  redo B;
2747                } elsif ($token->{tag_name} eq 'link') {
2748                  ## NOTE: There is a "as if in head" code clone.
2749                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2750                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2751                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2752                  }
2753                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2754                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2755                  pop @{$self->{open_elements}}
2756                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2757                  !!!next-token;
2758                  redo B;
2759                } elsif ($token->{tag_name} eq 'meta') {
2760                  ## NOTE: There is a "as if in head" code clone.
2761                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2762                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2763                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2764                  }
2765                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2766                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2767    
2768                  unless ($self->{confident}) {
2769                    my $charset;
2770                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2771                      $charset = $token->{attributes}->{charset}->{value};
2772                    }
2773                    if ($token->{attributes}->{'http-equiv'}) {
2774                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2775                      if ($token->{attributes}->{'http-equiv'}->{value}
2776                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2777                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2778                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2779                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2780                      } ## TODO: And if supported
2781                    }
2782                    ## TODO: Change the encoding
2783                  }
2784    
2785                  ## TODO: Extracting |charset| from |meta|.
2786                  pop @{$self->{open_elements}}
2787                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2788                  !!!next-token;
2789                  redo B;
2790                } elsif ($token->{tag_name} eq 'title') {
2791                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2792                    ## As if </noscript>
2793                    pop @{$self->{open_elements}};
2794                    !!!parse-error (type => 'in noscript:title');
2795                  
2796                    $self->{insertion_mode} = IN_HEAD_IM;
2797                    ## Reprocess in the "in head" insertion mode...
2798                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2799                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2800                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2801                  }
2802    
2803                  ## NOTE: There is a "as if in head" code clone.
2804                  my $parent = defined $self->{head_element} ? $self->{head_element}
2805                      : $self->{open_elements}->[-1]->[0];
2806                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2807                                  sub { $parent->append_child ($_[0]) });
2808                  pop @{$self->{open_elements}}
2809                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2810                  redo B;
2811                } elsif ($token->{tag_name} eq 'style') {
2812                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2813                  ## insertion mode IN_HEAD_IM)
2814                  ## NOTE: There is a "as if in head" code clone.
2815                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2816                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2817                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2818                  }
2819                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2820                  pop @{$self->{open_elements}}
2821                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2822                  redo B;
2823                } elsif ($token->{tag_name} eq 'noscript') {
2824                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2825                    ## NOTE: and scripting is disalbed
2826                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2827                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2828                    !!!next-token;
2829                    redo B;
2830                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2831                    !!!parse-error (type => 'in noscript:noscript');
2832                    ## Ignore the token
2833                    !!!next-token;
2834                    redo B;
2835                  } else {
2836                    #
2837                  }
2838                } elsif ($token->{tag_name} eq 'script') {
2839                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2840                    ## As if </noscript>
2841                    pop @{$self->{open_elements}};
2842                    !!!parse-error (type => 'in noscript:script');
2843                  
2844                    $self->{insertion_mode} = IN_HEAD_IM;
2845                    ## Reprocess in the "in head" insertion mode...
2846                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2847                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2848                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2849                  }
2850    
2851                  ## NOTE: There is a "as if in head" code clone.
2852                  $script_start_tag->($insert_to_current);
2853                  pop @{$self->{open_elements}}
2854                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2855                  redo B;
2856                } elsif ($token->{tag_name} eq 'body' or
2857                         $token->{tag_name} eq 'frameset') {
2858                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2859                    ## As if </noscript>
2860                    pop @{$self->{open_elements}};
2861                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2862                    
2863                    ## Reprocess in the "in head" insertion mode...
2864                    ## As if </head>
2865                    pop @{$self->{open_elements}};
2866                    
2867                    ## Reprocess in the "after head" insertion mode...
2868                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2869                    pop @{$self->{open_elements}};
2870                    
2871                    ## Reprocess in the "after head" insertion mode...
2872                  }
2873    
2874                  ## "after head" insertion mode
2875                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2876                  if ($token->{tag_name} eq 'body') {
2877                    $self->{insertion_mode} = IN_BODY_IM;
2878                  } elsif ($token->{tag_name} eq 'frameset') {
2879                    $self->{insertion_mode} = IN_FRAMESET_IM;
2880                  } else {
2881                    die "$0: tag name: $self->{tag_name}";
2882                  }
2883                  !!!next-token;
2884                  redo B;
2885                } else {
2886                  #
2887                }
2888    
2889                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2890                  ## As if </noscript>
2891                  pop @{$self->{open_elements}};
2892                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2893                  
2894                  ## Reprocess in the "in head" insertion mode...
2895                  ## As if </head>
2896                  pop @{$self->{open_elements}};
2897    
2898                  ## Reprocess in the "after head" insertion mode...
2899                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2900                  ## As if </head>
2901                  pop @{$self->{open_elements}};
2902    
2903                  ## Reprocess in the "after head" insertion mode...
2904                }
2905    
2906                ## "after head" insertion mode
2907                ## As if <body>
2908                !!!insert-element ('body');
2909                $self->{insertion_mode} = IN_BODY_IM;
2910                ## reprocess
2911                redo B;
2912              } elsif ($token->{type} == END_TAG_TOKEN) {
2913                if ($token->{tag_name} eq 'head') {
2914                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2915                    ## As if <head>
2916                    !!!create-element ($self->{head_element}, 'head');
2917                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2918                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2919    
2920                    ## Reprocess in the "in head" insertion mode...
2921                    pop @{$self->{open_elements}};
2922                    $self->{insertion_mode} = AFTER_HEAD_IM;
2923                    !!!next-token;
2924                    redo B;
2925                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2926                    ## As if </noscript>
2927                    pop @{$self->{open_elements}};
2928                    !!!parse-error (type => 'in noscript:script');
2929                    
2930                    ## Reprocess in the "in head" insertion mode...
2931                    pop @{$self->{open_elements}};
2932                    $self->{insertion_mode} = AFTER_HEAD_IM;
2933                    !!!next-token;
2934                    redo B;
2935                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2936                    pop @{$self->{open_elements}};
2937                    $self->{insertion_mode} = AFTER_HEAD_IM;
2938                    !!!next-token;
2939                    redo B;
2940                  } else {
2941                    #
2942                  }
2943                } elsif ($token->{tag_name} eq 'noscript') {
2944                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2945                    pop @{$self->{open_elements}};
2946                    $self->{insertion_mode} = IN_HEAD_IM;
2947                    !!!next-token;
2948                    redo B;
2949                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2950                    !!!parse-error (type => 'unmatched end tag:noscript');
2951                    ## Ignore the token ## ISSUE: An issue in the spec.
2952                    !!!next-token;
2953                    redo B;
2954                  } else {
2955                    #
2956                  }
2957                } elsif ({
2958                          body => 1, html => 1,
2959                         }->{$token->{tag_name}}) {
2960                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2961                    ## As if <head>
2962                    !!!create-element ($self->{head_element}, 'head');
2963                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2964                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2965    
2966                    $self->{insertion_mode} = IN_HEAD_IM;
2967                    ## Reprocess in the "in head" insertion mode...
2968                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2969                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2970                    ## Ignore the token
2971                    !!!next-token;
2972                    redo B;
2973                  }
2974                  
2975                  #
2976                } elsif ({
2977                          p => 1, br => 1,
2978                         }->{$token->{tag_name}}) {
2979                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2980                    ## As if <head>
2981                    !!!create-element ($self->{head_element}, 'head');
2982                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2983                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2984    
2985                    $self->{insertion_mode} = IN_HEAD_IM;
2986                    ## Reprocess in the "in head" insertion mode...
2987                  }
2988    
2989                  #
2990                } else {
2991                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2992                    #
2993                  } else {
2994                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2995                    ## Ignore the token
2996                    !!!next-token;
2997                    redo B;
2998                  }
2999                }
3000    
3001                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3002                  ## As if </noscript>
3003                  pop @{$self->{open_elements}};
3004                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3005                  
3006                  ## Reprocess in the "in head" insertion mode...
3007                  ## As if </head>
3008                  pop @{$self->{open_elements}};
3009    
3010                  ## Reprocess in the "after head" insertion mode...
3011                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3012                  ## As if </head>
3013                  pop @{$self->{open_elements}};
3014    
3015                  ## Reprocess in the "after head" insertion mode...
3016                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3017                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3018                  ## Ignore the token ## ISSUE: An issue in the spec.
3019                  !!!next-token;
3020                  redo B;
3021                }
3022    
3023                ## "after head" insertion mode
3024                ## As if <body>
3025                !!!insert-element ('body');
3026                $self->{insertion_mode} = IN_BODY_IM;
3027                ## reprocess
3028                redo B;
3029              } else {
3030                die "$0: $token->{type}: Unknown token type";
3031              }
3032    
3033              ## ISSUE: An issue in the spec.
3034        } elsif ($self->{insertion_mode} & BODY_IMS) {
3035              if ($token->{type} == CHARACTER_TOKEN) {
3036                ## NOTE: There is a code clone of "character in body".
3037                $reconstruct_active_formatting_elements->($insert_to_current);
3038                
3039                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3040    
3041                !!!next-token;
3042                redo B;
3043              } elsif ($token->{type} == START_TAG_TOKEN) {
3044                if ({
3045                     caption => 1, col => 1, colgroup => 1, tbody => 1,
3046                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3047                    }->{$token->{tag_name}}) {
3048                  if ($self->{insertion_mode} == IN_CELL_IM) {
3049                    ## have an element in table scope
3050                    my $tn;
3051                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3052                      my $node = $self->{open_elements}->[$_];
3053                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3054                        $tn = $node->[1];
3055                        last INSCOPE;
3056                      } elsif ({
3057                                table => 1, html => 1,
3058                               }->{$node->[1]}) {
3059                        last INSCOPE;
3060                      }
3061                    } # INSCOPE
3062                      unless (defined $tn) {
3063                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3064                        ## Ignore the token
3065                        !!!next-token;
3066                        redo B;
3067                      }
3068                    
3069                    ## Close the cell
3070                    !!!back-token; # <?>
3071                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3072                    redo B;
3073                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3074                    !!!parse-error (type => 'not closed:caption');
3075                    
3076                    ## As if </caption>
3077                    ## have a table element in table scope
3078                    my $i;
3079                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3080                      my $node = $self->{open_elements}->[$_];
3081                      if ($node->[1] eq 'caption') {
3082                        $i = $_;
3083                        last INSCOPE;
3084                      } elsif ({
3085                                table => 1, html => 1,
3086                               }->{$node->[1]}) {
3087                        last INSCOPE;
3088                      }
3089                    } # INSCOPE
3090                      unless (defined $i) {
3091                        !!!parse-error (type => 'unmatched end tag:caption');
3092                        ## Ignore the token
3093                        !!!next-token;
3094                        redo B;
3095                      }
3096                    
3097                    ## generate implied end tags
3098                    if ({
3099                         dd => 1, dt => 1, li => 1, p => 1,
3100                         td => 1, th => 1, tr => 1,
3101                         tbody => 1, tfoot=> 1, thead => 1,
3102                        }->{$self->{open_elements}->[-1]->[1]}) {
3103                      !!!back-token; # <?>
3104                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3105                      !!!back-token;
3106                      $token = {type => END_TAG_TOKEN,
3107                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3108                      redo B;
3109                    }
3110    
3111                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3112                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3113                    }
3114                    
3115                    splice @{$self->{open_elements}}, $i;
3116                    
3117                    $clear_up_to_marker->();
3118                    
3119                    $self->{insertion_mode} = IN_TABLE_IM;
3120                    
3121                    ## reprocess
3122                    redo B;
3123                  } else {
3124                    #
3125                  }
3126                } else {
3127                  #
3128                }
3129              } elsif ($token->{type} == END_TAG_TOKEN) {
3130                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3131                  if ($self->{insertion_mode} == IN_CELL_IM) {
3132                    ## have an element in table scope
3133                    my $i;
3134                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3135                      my $node = $self->{open_elements}->[$_];
3136                      if ($node->[1] eq $token->{tag_name}) {
3137                        $i = $_;
3138                        last INSCOPE;
3139                      } elsif ({
3140                                table => 1, html => 1,
3141                               }->{$node->[1]}) {
3142                        last INSCOPE;
3143                      }
3144                    } # INSCOPE
3145                      unless (defined $i) {
3146                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3147                        ## Ignore the token
3148                        !!!next-token;
3149                        redo B;
3150                      }
3151                    
3152                    ## generate implied end tags
3153                    if ({
3154                         dd => 1, dt => 1, li => 1, p => 1,
3155                         td => ($token->{tag_name} eq 'th'),
3156                         th => ($token->{tag_name} eq 'td'),
3157                         tr => 1,
3158                         tbody => 1, tfoot=> 1, thead => 1,
3159                        }->{$self->{open_elements}->[-1]->[1]}) {
3160                      !!!back-token;
3161                      $token = {type => END_TAG_TOKEN,
3162                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3163                      redo B;
3164                    }
3165                    
3166                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3167                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3168                    }
3169                    
3170                    splice @{$self->{open_elements}}, $i;
3171                    
3172                    $clear_up_to_marker->();
3173                    
3174                    $self->{insertion_mode} = IN_ROW_IM;
3175                    
3176                    !!!next-token;
3177                    redo B;
3178                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3179                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3180                    ## Ignore the token
3181                    !!!next-token;
3182                    redo B;
3183                  } else {
3184                    #
3185                  }
3186                } elsif ($token->{tag_name} eq 'caption') {
3187                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3188                    ## have a table element in table scope
3189                    my $i;
3190                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3191                      my $node = $self->{open_elements}->[$_];
3192                      if ($node->[1] eq $token->{tag_name}) {
3193                        $i = $_;
3194                        last INSCOPE;
3195                      } elsif ({
3196                                table => 1, html => 1,
3197                               }->{$node->[1]}) {
3198                        last INSCOPE;
3199                      }
3200                    } # INSCOPE
3201                      unless (defined $i) {
3202                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3203                        ## Ignore the token
3204                        !!!next-token;
3205                        redo B;
3206                      }
3207                    
3208                    ## generate implied end tags
3209                    if ({
3210                         dd => 1, dt => 1, li => 1, p => 1,
3211                         td => 1, th => 1, tr => 1,
3212                         tbody => 1, tfoot=> 1, thead => 1,
3213                        }->{$self->{open_elements}->[-1]->[1]}) {
3214                      !!!back-token;
3215                      $token = {type => END_TAG_TOKEN,
3216                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3217                      redo B;
3218                    }
3219                    
3220                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3221                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3222                    }
3223                    
3224                    splice @{$self->{open_elements}}, $i;
3225                    
3226                    $clear_up_to_marker->();
3227                    
3228                    $self->{insertion_mode} = IN_TABLE_IM;
3229                    
3230                    !!!next-token;
3231                    redo B;
3232                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3233                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3234                    ## Ignore the token
3235                    !!!next-token;
3236                    redo B;
3237                  } else {
3238                    #
3239                  }
3240                } elsif ({
3241                          table => 1, tbody => 1, tfoot => 1,
3242                          thead => 1, tr => 1,
3243                         }->{$token->{tag_name}} and
3244                         $self->{insertion_mode} == IN_CELL_IM) {
3245                  ## have an element in table scope
3246                  my $i;
3247                  my $tn;
3248                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3249                    my $node = $self->{open_elements}->[$_];
3250                    if ($node->[1] eq $token->{tag_name}) {
3251                      $i = $_;
3252                      last INSCOPE;
3253                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3254                      $tn = $node->[1];
3255                      ## NOTE: There is exactly one |td| or |th| element
3256                      ## in scope in the stack of open elements by definition.
3257                    } elsif ({
3258                              table => 1, html => 1,
3259                             }->{$node->[1]}) {
3260                      last INSCOPE;
3261                    }
3262                  } # INSCOPE
3263                  unless (defined $i) {
3264                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3265                    ## Ignore the token
3266                    !!!next-token;
3267                    redo B;
3268                  }
3269    
3270                  ## Close the cell
3271                  !!!back-token; # </?>
3272                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3273                  redo B;
3274                } elsif ($token->{tag_name} eq 'table' and
3275                         $self->{insertion_mode} == IN_CAPTION_IM) {
3276                  !!!parse-error (type => 'not closed:caption');
3277    
3278                  ## As if </caption>
3279                  ## have a table element in table scope
3280                  my $i;
3281                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3282                    my $node = $self->{open_elements}->[$_];
3283                    if ($node->[1] eq 'caption') {
3284                      $i = $_;
3285                      last INSCOPE;
3286                    } elsif ({
3287                              table => 1, html => 1,
3288                             }->{$node->[1]}) {
3289                      last INSCOPE;
3290                    }
3291                  } # INSCOPE
3292                  unless (defined $i) {
3293                    !!!parse-error (type => 'unmatched end tag:caption');
3294                    ## Ignore the token
3295                    !!!next-token;
3296                    redo B;
3297                  }
3298                  
3299                  ## generate implied end tags
3300                  if ({
3301                       dd => 1, dt => 1, li => 1, p => 1,
3302                       td => 1, th => 1, tr => 1,
3303                       tbody => 1, tfoot=> 1, thead => 1,
3304                      }->{$self->{open_elements}->[-1]->[1]}) {
3305                    !!!back-token; # </table>
3306                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3307                    !!!back-token;
3308                    $token = {type => END_TAG_TOKEN,
3309                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3310                    redo B;
3311                  }
3312    
3313                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3314                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3315                  }
3316    
3317                  splice @{$self->{open_elements}}, $i;
3318    
3319                  $clear_up_to_marker->();
3320    
3321                  $self->{insertion_mode} = IN_TABLE_IM;
3322    
3323                  ## reprocess
3324                  redo B;
3325                } elsif ({
3326                          body => 1, col => 1, colgroup => 1, html => 1,
3327                         }->{$token->{tag_name}}) {
3328                  if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3329                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3330                    ## Ignore the token
3331                    !!!next-token;
3332                    redo B;
3333                  } else {
3334                    #
3335                  }
3336                } elsif ({
3337                          tbody => 1, tfoot => 1,
3338                          thead => 1, tr => 1,
3339                         }->{$token->{tag_name}} and
3340                         $self->{insertion_mode} == IN_CAPTION_IM) {
3341                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3342                  ## Ignore the token
3343                  !!!next-token;
3344                  redo B;
3345                } else {
3346                  #
3347                }
3348          } else {
3349            die "$0: $token->{type}: Unknown token type";
3350          }
3351    
3352          $insert = $insert_to_current;
3353          #
3354        } elsif ($self->{insertion_mode} & TABLE_IMS) {
3355          if ($token->{type} == CHARACTER_TOKEN) {
3356                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3357                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3358                  
3359                  unless (length $token->{data}) {
3360                    !!!next-token;
3361                    redo B;
3362                  }
3363                }
3364    
3365                !!!parse-error (type => 'in table:#character');
3366    
3367                ## As if in body, but insert into foster parent element
3368                ## ISSUE: Spec says that "whenever a node would be inserted
3369                ## into the current node" while characters might not be
3370                ## result in a new Text node.
3371                $reconstruct_active_formatting_elements->($insert_to_foster);
3372                
3373                if ({
3374                     table => 1, tbody => 1, tfoot => 1,
3375                     thead => 1, tr => 1,
3376                    }->{$self->{open_elements}->[-1]->[1]}) {
3377                  # MUST
3378                  my $foster_parent_element;
3379                  my $next_sibling;
3380                  my $prev_sibling;
3381                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3382                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3383                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3384                      if (defined $parent and $parent->node_type == 1) {
3385                        $foster_parent_element = $parent;
3386                        $next_sibling = $self->{open_elements}->[$_]->[0];
3387                        $prev_sibling = $next_sibling->previous_sibling;
3388                      } else {
3389                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3390                        $prev_sibling = $foster_parent_element->last_child;
3391                      }
3392                      last OE;
3393                    }
3394                  } # OE
3395                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3396                  $prev_sibling = $foster_parent_element->last_child
3397                    unless defined $foster_parent_element;
3398                  if (defined $prev_sibling and
3399                      $prev_sibling->node_type == 3) {
3400                    $prev_sibling->manakai_append_text ($token->{data});
3401                  } else {
3402                    $foster_parent_element->insert_before
3403                      ($self->{document}->create_text_node ($token->{data}),
3404                       $next_sibling);
3405                  }
3406                } else {
3407                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3408                }
3409                
3410                !!!next-token;
3411                redo B;
3412          } elsif ($token->{type} == START_TAG_TOKEN) {
3413                if ({
3414                     tr => ($self->{insertion_mode} != IN_ROW_IM),
3415                     th => 1, td => 1,
3416                    }->{$token->{tag_name}}) {
3417                  if ($self->{insertion_mode} == IN_TABLE_IM) {
3418                    ## Clear back to table context
3419                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3420                           $self->{open_elements}->[-1]->[1] ne 'html') {
3421                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3422                      pop @{$self->{open_elements}};
3423                    }
3424                    
3425                    !!!insert-element ('tbody');
3426                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3427                    ## reprocess in the "in table body" insertion mode...
3428                  }
3429    
3430                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3431                    unless ($token->{tag_name} eq 'tr') {
3432                      !!!parse-error (type => 'missing start tag:tr');
3433                    }
3434                    
3435                    ## Clear back to table body context
3436                    while (not {
3437                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3438                    }->{$self->{open_elements}->[-1]->[1]}) {
3439                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3440                      pop @{$self->{open_elements}};
3441                    }
3442                    
3443                    $self->{insertion_mode} = IN_ROW_IM;
3444                    if ($token->{tag_name} eq 'tr') {
3445                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3446                      !!!next-token;
3447                      redo B;
3448                    } else {
3449                      !!!insert-element ('tr');
3450                      ## reprocess in the "in row" insertion mode
3451                    }
3452                  }
3453    
3454                  ## Clear back to table row context
3455                  while (not {
3456                    tr => 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                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3463                  $self->{insertion_mode} = IN_CELL_IM;
3464    
3465                  push @$active_formatting_elements, ['#marker', ''];
3466                  
3467                  !!!next-token;
3468                  redo B;
3469                } elsif ({
3470                          caption => 1, col => 1, colgroup => 1,
3471                          tbody => 1, tfoot => 1, thead => 1,
3472                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3473                         }->{$token->{tag_name}}) {
3474                  if ($self->{insertion_mode} == IN_ROW_IM) {
3475                    ## As if </tr>
3476                    ## have an element in table scope
3477                    my $i;
3478                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3479                      my $node = $self->{open_elements}->[$_];
3480                      if ($node->[1] eq 'tr') {
3481                        $i = $_;
3482                        last INSCOPE;
3483                      } elsif ({
3484                                table => 1, html => 1,
3485                               }->{$node->[1]}) {
3486                        last INSCOPE;
3487                      }
3488                    } # INSCOPE
3489                    unless (defined $i) {
3490                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3491                      ## Ignore the token
3492                      !!!next-token;
3493                      redo B;
3494                    }
3495                    
3496                    ## Clear back to table row context
3497                    while (not {
3498                      tr => 1, html => 1,
3499                    }->{$self->{open_elements}->[-1]->[1]}) {
3500                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3501                      pop @{$self->{open_elements}};
3502                    }
3503                    
3504                    pop @{$self->{open_elements}}; # tr
3505                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3506                    if ($token->{tag_name} eq 'tr') {
3507                      ## reprocess
3508                      redo B;
3509                    } else {
3510                      ## reprocess in the "in table body" insertion mode...
3511                    }
3512                  }
3513    
3514                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3515                    ## have an element in table scope
3516                    my $i;
3517                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3518                      my $node = $self->{open_elements}->[$_];
3519                      if ({
3520                           tbody => 1, thead => 1, tfoot => 1,
3521                          }->{$node->[1]}) {
3522                        $i = $_;
3523                        last INSCOPE;
3524                      } elsif ({
3525                                table => 1, html => 1,
3526                               }->{$node->[1]}) {
3527                        last INSCOPE;
3528                      }
3529                    } # INSCOPE
3530                    unless (defined $i) {
3531                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3532                      ## Ignore the token
3533                      !!!next-token;
3534                      redo B;
3535                    }
3536    
3537                    ## Clear back to table body context
3538                    while (not {
3539                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3540                    }->{$self->{open_elements}->[-1]->[1]}) {
3541                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3542                      pop @{$self->{open_elements}};
3543                    }
3544                    
3545                    ## As if <{current node}>
3546                    ## have an element in table scope
3547                    ## true by definition
3548                    
3549                    ## Clear back to table body context
3550                    ## nop by definition
3551                    
3552                    pop @{$self->{open_elements}};
3553                    $self->{insertion_mode} = IN_TABLE_IM;
3554                    ## reprocess in "in table" insertion mode...
3555                  }
3556    
3557                  if ($token->{tag_name} eq 'col') {
3558                    ## Clear back to table context
3559                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3560                           $self->{open_elements}->[-1]->[1] ne 'html') {
3561                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3562                      pop @{$self->{open_elements}};
3563                    }
3564                    
3565                    !!!insert-element ('colgroup');
3566                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3567                    ## reprocess
3568                    redo B;
3569                  } elsif ({
3570                            caption => 1,
3571                            colgroup => 1,
3572                            tbody => 1, tfoot => 1, thead => 1,
3573                           }->{$token->{tag_name}}) {
3574                    ## Clear back to table context
3575                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3576                           $self->{open_elements}->[-1]->[1] ne 'html') {
3577                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3578                      pop @{$self->{open_elements}};
3579                    }
3580                    
3581                    push @$active_formatting_elements, ['#marker', '']
3582                        if $token->{tag_name} eq 'caption';
3583                    
3584                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3585                    $self->{insertion_mode} = {
3586                                               caption => IN_CAPTION_IM,
3587                                               colgroup => IN_COLUMN_GROUP_IM,
3588                                               tbody => IN_TABLE_BODY_IM,
3589                                               tfoot => IN_TABLE_BODY_IM,
3590                                               thead => IN_TABLE_BODY_IM,
3591                                              }->{$token->{tag_name}};
3592                    !!!next-token;
3593                    redo B;
3594                  } else {
3595                    die "$0: in table: <>: $token->{tag_name}";
3596                  }
3597                } elsif ($token->{tag_name} eq 'table') {
3598                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3599    
3600                  ## As if </table>
3601                  ## have a table element in table scope
3602                  my $i;
3603                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3604                    my $node = $self->{open_elements}->[$_];
3605                    if ($node->[1] eq 'table') {
3606                      $i = $_;
3607                      last INSCOPE;
3608                    } elsif ({
3609                              table => 1, html => 1,
3610                             }->{$node->[1]}) {
3611                      last INSCOPE;
3612                    }
3613                  } # INSCOPE
3614                  unless (defined $i) {
3615                    !!!parse-error (type => 'unmatched end tag:table');
3616                    ## Ignore tokens </table><table>
3617                    !!!next-token;
3618                    redo B;
3619                  }
3620                  
3621                  ## generate implied end tags
3622                  if ({
3623                       dd => 1, dt => 1, li => 1, p => 1,
3624                       td => 1, th => 1, tr => 1,
3625                       tbody => 1, tfoot=> 1, thead => 1,
3626                      }->{$self->{open_elements}->[-1]->[1]}) {
3627                    !!!back-token; # <table>
3628                    $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3629                    !!!back-token;
3630                    $token = {type => END_TAG_TOKEN,
3631                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3632                    redo B;
3633                  }
3634    
3635                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3636                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3637                  }
3638    
3639                  splice @{$self->{open_elements}}, $i;
3640    
3641                  $self->_reset_insertion_mode;
3642    
3643                  ## reprocess
3644                  redo B;
3645            } else {
3646              !!!parse-error (type => 'in table:'.$token->{tag_name});
3647    
3648              $insert = $insert_to_foster;
3649              #
3650            }
3651          } elsif ($token->{type} == END_TAG_TOKEN) {
3652                if ($token->{tag_name} eq 'tr' and
3653                    $self->{insertion_mode} == IN_ROW_IM) {
3654                  ## have an element in table scope
3655                  my $i;
3656                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3657                    my $node = $self->{open_elements}->[$_];
3658                    if ($node->[1] eq $token->{tag_name}) {
3659                      $i = $_;
3660                      last INSCOPE;
3661                    } elsif ({
3662                              table => 1, html => 1,
3663                             }->{$node->[1]}) {
3664                      last INSCOPE;
3665                    }
3666                  } # INSCOPE
3667                  unless (defined $i) {
3668                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3669                    ## Ignore the token
3670                    !!!next-token;
3671                    redo B;
3672                  }
3673    
3674                  ## Clear back to table row context
3675                  while (not {
3676                    tr => 1, html => 1,
3677                  }->{$self->{open_elements}->[-1]->[1]}) {
3678                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3679                    pop @{$self->{open_elements}};
3680                  }
3681    
3682                  pop @{$self->{open_elements}}; # tr
3683                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3684                  !!!next-token;
3685                  redo B;
3686                } elsif ($token->{tag_name} eq 'table') {
3687                  if ($self->{insertion_mode} == IN_ROW_IM) {
3688                    ## As if </tr>
3689                    ## have an element in table scope
3690                    my $i;
3691                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3692                      my $node = $self->{open_elements}->[$_];
3693                      if ($node->[1] eq 'tr') {
3694                        $i = $_;
3695                        last INSCOPE;
3696                      } elsif ({
3697                                table => 1, html => 1,
3698                               }->{$node->[1]}) {
3699                        last INSCOPE;
3700                      }
3701                    } # INSCOPE
3702                    unless (defined $i) {
3703                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3704                      ## Ignore the token
3705                      !!!next-token;
3706                      redo B;
3707                    }
3708                    
3709                    ## Clear back to table row context
3710                    while (not {
3711                      tr => 1, html => 1,
3712                    }->{$self->{open_elements}->[-1]->[1]}) {
3713                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3714                      pop @{$self->{open_elements}};
3715                    }
3716                    
3717                    pop @{$self->{open_elements}}; # tr
3718                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3719                    ## reprocess in the "in table body" insertion mode...
3720                  }
3721    
3722                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3723                    ## have an element in table scope
3724                    my $i;
3725                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3726                      my $node = $self->{open_elements}->[$_];
3727                      if ({
3728                           tbody => 1, thead => 1, tfoot => 1,
3729                          }->{$node->[1]}) {
3730                        $i = $_;
3731                        last INSCOPE;
3732                      } elsif ({
3733                                table => 1, html => 1,
3734                               }->{$node->[1]}) {
3735                        last INSCOPE;
3736                      }
3737                    } # INSCOPE
3738                    unless (defined $i) {
3739                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3740                      ## Ignore the token
3741                      !!!next-token;
3742                      redo B;
3743                    }
3744                    
3745                    ## Clear back to table body context
3746                    while (not {
3747                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3748                    }->{$self->{open_elements}->[-1]->[1]}) {
3749                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3750                      pop @{$self->{open_elements}};
3751                    }
3752                    
3753                    ## As if <{current node}>
3754                    ## have an element in table scope
3755                    ## true by definition
3756                    
3757                    ## Clear back to table body context
3758                    ## nop by definition
3759                    
3760                    pop @{$self->{open_elements}};
3761                    $self->{insertion_mode} = IN_TABLE_IM;
3762                    ## reprocess in the "in table" insertion mode...
3763                  }
3764    
3765                  ## have a table element in table scope
3766                  my $i;
3767                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3768                    my $node = $self->{open_elements}->[$_];
3769                    if ($node->[1] eq $token->{tag_name}) {
3770                      $i = $_;
3771                      last INSCOPE;
3772                    } elsif ({
3773                              table => 1, html => 1,
3774                             }->{$node->[1]}) {
3775                      last INSCOPE;
3776                    }
3777                  } # INSCOPE
3778                  unless (defined $i) {
3779                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3780                    ## Ignore the token
3781                    !!!next-token;
3782                    redo B;
3783                  }
3784    
3785                  ## generate implied end tags
3786                  if ({
3787                       dd => 1, dt => 1, li => 1, p => 1,
3788                       td => 1, th => 1, tr => 1,
3789                       tbody => 1, tfoot=> 1, thead => 1,
3790                      }->{$self->{open_elements}->[-1]->[1]}) {
3791                    !!!back-token;
3792                    $token = {type => END_TAG_TOKEN,
3793                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3794                    redo B;
3795                  }
3796                  
3797                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3798                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3799                  }
3800                    
3801                  splice @{$self->{open_elements}}, $i;
3802                  
3803                  $self->_reset_insertion_mode;
3804                  
3805                  !!!next-token;
3806                  redo B;
3807                } elsif ({
3808                          tbody => 1, tfoot => 1, thead => 1,
3809                         }->{$token->{tag_name}} and
3810                         $self->{insertion_mode} & ROW_IMS) {
3811                  if ($self->{insertion_mode} == IN_ROW_IM) {
3812                    ## have an element in table scope
3813                    my $i;
3814                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3815                      my $node = $self->{open_elements}->[$_];
3816                      if ($node->[1] eq $token->{tag_name}) {
3817                        $i = $_;
3818                        last INSCOPE;
3819                      } elsif ({
3820                                table => 1, html => 1,
3821                               }->{$node->[1]}) {
3822                        last INSCOPE;
3823                      }
3824                    } # INSCOPE
3825                      unless (defined $i) {
3826                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3827                        ## Ignore the token
3828                        !!!next-token;
3829                        redo B;
3830                      }
3831                    
3832                    ## As if </tr>
3833                    ## have an element in table scope
3834                    my $i;
3835                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3836                      my $node = $self->{open_elements}->[$_];
3837                      if ($node->[1] eq 'tr') {
3838                        $i = $_;
3839                        last INSCOPE;
3840                      } elsif ({
3841                                table => 1, html => 1,
3842                               }->{$node->[1]}) {
3843                        last INSCOPE;
3844                      }
3845                    } # INSCOPE
3846                      unless (defined $i) {
3847                        !!!parse-error (type => 'unmatched end tag:tr');
3848                        ## Ignore the token
3849                        !!!next-token;
3850                        redo B;
3851                      }
3852                    
3853                    ## Clear back to table row context
3854                    while (not {
3855                      tr => 1, html => 1,
3856                    }->{$self->{open_elements}->[-1]->[1]}) {
3857                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3858                      pop @{$self->{open_elements}};
3859                    }
3860                    
3861                    pop @{$self->{open_elements}}; # tr
3862                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3863                    ## reprocess in the "in table body" insertion mode...
3864                  }
3865    
3866                  ## have an element in table scope
3867                  my $i;
3868                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3869                    my $node = $self->{open_elements}->[$_];
3870                    if ($node->[1] eq $token->{tag_name}) {
3871                      $i = $_;
3872                      last INSCOPE;
3873                    } elsif ({
3874                              table => 1, html => 1,
3875                             }->{$node->[1]}) {
3876                      last INSCOPE;
3877                    }
3878                  } # INSCOPE
3879                  unless (defined $i) {
3880                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3881                    ## Ignore the token
3882                    !!!next-token;
3883                    redo B;
3884                  }
3885    
3886                  ## Clear back to table body context
3887                  while (not {
3888                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3889                  }->{$self->{open_elements}->[-1]->[1]}) {
3890                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3891                    pop @{$self->{open_elements}};
3892                  }
3893    
3894                  pop @{$self->{open_elements}};
3895                  $self->{insertion_mode} = IN_TABLE_IM;
3896                  !!!next-token;
3897                  redo B;
3898                } elsif ({
3899                          body => 1, caption => 1, col => 1, colgroup => 1,
3900                          html => 1, td => 1, th => 1,
3901                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3902                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3903                         }->{$token->{tag_name}}) {
3904                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3905                  ## Ignore the token
3906                  !!!next-token;
3907                  redo B;
3908            } else {
3909              !!!parse-error (type => 'in table:/'.$token->{tag_name});
3910    
3911              $insert = $insert_to_foster;
3912              #
3913            }
3914          } else {
3915            die "$0: $token->{type}: Unknown token type";
3916          }
3917        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3918              if ($token->{type} == CHARACTER_TOKEN) {
3919                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3920                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3921                  unless (length $token->{data}) {
3922                    !!!next-token;
3923                    redo B;
3924                  }
3925                }
3926                
3927                #
3928              } elsif ($token->{type} == START_TAG_TOKEN) {
3929                if ($token->{tag_name} eq 'col') {
3930                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3931                  pop @{$self->{open_elements}};
3932                  !!!next-token;
3933                  redo B;
3934                } else {
3935                  #
3936                }
3937              } elsif ($token->{type} == END_TAG_TOKEN) {
3938                if ($token->{tag_name} eq 'colgroup') {
3939                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
3940                    !!!parse-error (type => 'unmatched end tag:colgroup');
3941                    ## Ignore the token
3942                    !!!next-token;
3943                    redo B;
3944                  } else {
3945                    pop @{$self->{open_elements}}; # colgroup
3946                    $self->{insertion_mode} = IN_TABLE_IM;
3947                    !!!next-token;
3948                    redo B;            
3949                  }
3950                } elsif ($token->{tag_name} eq 'col') {
3951                  !!!parse-error (type => 'unmatched end tag:col');
3952                  ## Ignore the token
3953                  !!!next-token;
3954                  redo B;
3955                } else {
3956                  #
3957                }
3958              } else {
3959                #
3960              }
3961    
3962              ## As if </colgroup>
3963              if ($self->{open_elements}->[-1]->[1] eq 'html') {
3964                !!!parse-error (type => 'unmatched end tag:colgroup');
3965                ## Ignore the token
3966                !!!next-token;
3967                redo B;
3968              } else {
3969                pop @{$self->{open_elements}}; # colgroup
3970                $self->{insertion_mode} = IN_TABLE_IM;
3971                ## reprocess
3972                redo B;
3973              }
3974        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3975          if ($token->{type} == CHARACTER_TOKEN) {
3976            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3977            !!!next-token;
3978            redo B;
3979          } elsif ($token->{type} == START_TAG_TOKEN) {
3980                if ($token->{tag_name} eq 'option') {
3981                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3982                    ## As if </option>
3983                    pop @{$self->{open_elements}};
3984                  }
3985    
3986                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3987                  !!!next-token;
3988                  redo B;
3989                } elsif ($token->{tag_name} eq 'optgroup') {
3990                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3991                    ## As if </option>
3992                    pop @{$self->{open_elements}};
3993                  }
3994    
3995                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3996                    ## As if </optgroup>
3997                    pop @{$self->{open_elements}};
3998                  }
3999    
4000                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4001                  !!!next-token;
4002                  redo B;
4003                } elsif ($token->{tag_name} eq 'select') {
4004                  !!!parse-error (type => 'not closed:select');
4005                  ## As if </select> instead
4006                  ## have an element in table scope
4007                  my $i;
4008                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4009                    my $node = $self->{open_elements}->[$_];
4010                    if ($node->[1] eq $token->{tag_name}) {
4011                      $i = $_;
4012                      last INSCOPE;
4013                    } elsif ({
4014                              table => 1, html => 1,
4015                             }->{$node->[1]}) {
4016                      last INSCOPE;
4017                    }
4018                  } # INSCOPE
4019                  unless (defined $i) {
4020                    !!!parse-error (type => 'unmatched end tag:select');
4021                    ## Ignore the token
4022                    !!!next-token;
4023                    redo B;
4024                  }
4025                  
4026                  splice @{$self->{open_elements}}, $i;
4027    
4028                  $self->_reset_insertion_mode;
4029    
4030                  !!!next-token;
4031                  redo B;
4032            } else {
4033              !!!parse-error (type => 'in select:'.$token->{tag_name});
4034              ## Ignore the token
4035              !!!next-token;
4036              redo B;
4037            }
4038          } elsif ($token->{type} == END_TAG_TOKEN) {
4039                if ($token->{tag_name} eq 'optgroup') {
4040                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
4041                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4042                    ## As if </option>
4043                    splice @{$self->{open_elements}}, -2;
4044                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4045                    pop @{$self->{open_elements}};
4046                  } else {
4047                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4048                    ## Ignore the token
4049                  }
4050                  !!!next-token;
4051                  redo B;
4052                } elsif ($token->{tag_name} eq 'option') {
4053                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4054                    pop @{$self->{open_elements}};
4055                  } else {
4056                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4057                    ## Ignore the token
4058                  }
4059                  !!!next-token;
4060                  redo B;
4061                } elsif ($token->{tag_name} eq 'select') {
4062                  ## have an element in table scope
4063                  my $i;
4064                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4065                    my $node = $self->{open_elements}->[$_];
4066                    if ($node->[1] eq $token->{tag_name}) {
4067                      $i = $_;
4068                      last INSCOPE;
4069                    } elsif ({
4070                              table => 1, html => 1,
4071                             }->{$node->[1]}) {
4072                      last INSCOPE;
4073                    }
4074                  } # INSCOPE
4075                  unless (defined $i) {
4076                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4077                    ## Ignore the token
4078                    !!!next-token;
4079                    redo B;
4080                  }
4081                  
4082                  splice @{$self->{open_elements}}, $i;
4083    
4084                  $self->_reset_insertion_mode;
4085    
4086                  !!!next-token;
4087                  redo B;
4088                } elsif ({
4089                          caption => 1, table => 1, tbody => 1,
4090                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4091                         }->{$token->{tag_name}}) {
4092                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4093                  
4094                  ## have an element in table scope
4095                  my $i;
4096                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4097                    my $node = $self->{open_elements}->[$_];
4098                    if ($node->[1] eq $token->{tag_name}) {
4099                      $i = $_;
4100                      last INSCOPE;
4101                    } elsif ({
4102                              table => 1, html => 1,
4103                             }->{$node->[1]}) {
4104                      last INSCOPE;
4105                    }
4106                  } # INSCOPE
4107                  unless (defined $i) {
4108                    ## Ignore the token
4109                    !!!next-token;
4110                    redo B;
4111                  }
4112                  
4113                  ## As if </select>
4114                  ## have an element in table scope
4115                  undef $i;
4116                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4117                    my $node = $self->{open_elements}->[$_];
4118                    if ($node->[1] eq 'select') {
4119                      $i = $_;
4120                      last INSCOPE;
4121                    } elsif ({
4122                              table => 1, html => 1,
4123                             }->{$node->[1]}) {
4124                      last INSCOPE;
4125                    }
4126                  } # INSCOPE
4127                  unless (defined $i) {
4128                    !!!parse-error (type => 'unmatched end tag:select');
4129                    ## Ignore the </select> token
4130                    !!!next-token; ## TODO: ok?
4131                    redo B;
4132                  }
4133                  
4134                  splice @{$self->{open_elements}}, $i;
4135    
4136                  $self->_reset_insertion_mode;
4137    
4138                  ## reprocess
4139                  redo B;
4140            } else {
4141              !!!parse-error (type => 'in select:/'.$token->{tag_name});
4142              ## Ignore the token
4143              !!!next-token;
4144              redo B;
4145            }
4146          } else {
4147            die "$0: $token->{type}: Unknown token type";
4148          }
4149        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4150          if ($token->{type} == CHARACTER_TOKEN) {
4151            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4152              my $data = $1;
4153              ## As if in body
4154              $reconstruct_active_formatting_elements->($insert_to_current);
4155                  
4156              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4157              
4158              unless (length $token->{data}) {
4159                !!!next-token;
4160                redo B;
4161              }
4162            }
4163            
4164            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4165              !!!parse-error (type => 'after html:#character');
4166    
4167              ## Reprocess in the "main" phase, "after body" insertion mode...
4168            }
4169            
4170            ## "after body" insertion mode
4171            !!!parse-error (type => 'after body:#character');
4172    
4173            $self->{insertion_mode} = IN_BODY_IM;
4174            ## reprocess
4175            redo B;
4176          } elsif ($token->{type} == START_TAG_TOKEN) {
4177            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4178              !!!parse-error (type => 'after html:'.$token->{tag_name});
4179              
4180              ## Reprocess in the "main" phase, "after body" insertion mode...
4181            }
4182    
4183            ## "after body" insertion mode
4184            !!!parse-error (type => 'after body:'.$token->{tag_name});
4185    
4186            $self->{insertion_mode} = IN_BODY_IM;
4187            ## reprocess
4188            redo B;
4189          } elsif ($token->{type} == END_TAG_TOKEN) {
4190            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4191              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4192              
4193              $self->{insertion_mode} = AFTER_BODY_IM;
4194              ## Reprocess in the "main" phase, "after body" insertion mode...
4195            }
4196    
4197            ## "after body" insertion mode
4198            if ($token->{tag_name} eq 'html') {
4199              if (defined $self->{inner_html_node}) {
4200                !!!parse-error (type => 'unmatched end tag:html');
4201                ## Ignore the token
4202                !!!next-token;
4203                redo B;
4204              } else {
4205                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4206                !!!next-token;
4207                redo B;
4208              }
4209            } else {
4210              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4211    
4212              $self->{insertion_mode} = IN_BODY_IM;
4213              ## reprocess
4214              redo B;
4215            }
4216          } else {
4217            die "$0: $token->{type}: Unknown token type";
4218          }
4219        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4220          if ($token->{type} == CHARACTER_TOKEN) {
4221            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4222              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4223              
4224              unless (length $token->{data}) {
4225                !!!next-token;
4226                redo B;
4227              }
4228            }
4229            
4230            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4231              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4232                !!!parse-error (type => 'in frameset:#character');
4233              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4234                !!!parse-error (type => 'after frameset:#character');
4235              } else { # "after html frameset"
4236                !!!parse-error (type => 'after html:#character');
4237    
4238                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4239                ## Reprocess in the "main" phase, "after frameset"...
4240                !!!parse-error (type => 'after frameset:#character');
4241              }
4242              
4243              ## Ignore the token.
4244              if (length $token->{data}) {
4245                ## reprocess the rest of characters
4246              } else {
4247                !!!next-token;
4248              }
4249              redo B;
4250            }
4251            
4252            die qq[$0: Character "$token->{data}"];
4253          } elsif ($token->{type} == START_TAG_TOKEN) {
4254            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4255              !!!parse-error (type => 'after html:'.$token->{tag_name});
4256    
4257              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4258              ## Process in the "main" phase, "after frameset" insertion mode...
4259            }
4260    
4261            if ($token->{tag_name} eq 'frameset' and
4262                $self->{insertion_mode} == IN_FRAMESET_IM) {
4263              !!!insert-element ($token->{tag_name}, $token->{attributes});
4264              !!!next-token;
4265              redo B;
4266            } elsif ($token->{tag_name} eq 'frame' and
4267                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4268              !!!insert-element ($token->{tag_name}, $token->{attributes});
4269              pop @{$self->{open_elements}};
4270              !!!next-token;
4271              redo B;
4272            } elsif ($token->{tag_name} eq 'noframes') {
4273              ## NOTE: As if in body.
4274              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4275              redo B;
4276            } else {
4277              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4278                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4279              } else {
4280                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4281              }
4282              ## Ignore the token
4283              !!!next-token;
4284              redo B;
4285            }
4286          } elsif ($token->{type} == END_TAG_TOKEN) {
4287            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4288              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4289    
4290              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4291              ## Process in the "main" phase, "after frameset" insertion mode...
4292            }
4293    
4294            if ($token->{tag_name} eq 'frameset' and
4295                $self->{insertion_mode} == IN_FRAMESET_IM) {
4296              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4297                  @{$self->{open_elements}} == 1) {
4298                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4299                ## Ignore the token
4300                !!!next-token;
4301              } else {
4302                pop @{$self->{open_elements}};
4303                !!!next-token;
4304              }
4305    
4306              if (not defined $self->{inner_html_node} and
4307                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4308                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4309              }
4310              redo B;
4311            } elsif ($token->{tag_name} eq 'html' and
4312                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4313              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4314              !!!next-token;
4315              redo B;
4316            } else {
4317              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4318                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4319              } else {
4320                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4321              }
4322              ## Ignore the token
4323              !!!next-token;
4324              redo B;
4325            }
4326          } else {
4327            die "$0: $token->{type}: Unknown token type";
4328          }
4329    
4330          ## ISSUE: An issue in spec here
4331        } else {
4332          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4333        }
4334    
4335        ## "in body" insertion mode
4336        if ($token->{type} == START_TAG_TOKEN) {
4337        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4338          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4339          $script_start_tag->($insert);          $script_start_tag->($insert);
4340          return;          redo B;
4341        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
4342          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4343          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4344          return;          redo B;
4345        } elsif ({        } elsif ({
4346                  base => 1, link => 1, meta => 1,                  base => 1, link => 1,
4347                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4348          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
4349          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4350          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4351          !!!next-token;          !!!next-token;
4352          return;          redo B;
4353          } elsif ($token->{tag_name} eq 'meta') {
4354            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4355            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4356            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4357    
4358            unless ($self->{confident}) {
4359              my $charset;
4360              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4361                $charset = $token->{attributes}->{charset}->{value};
4362              }
4363              if ($token->{attributes}->{'http-equiv'}) {
4364                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4365                if ($token->{attributes}->{'http-equiv'}->{value}
4366                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4367                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4368                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4369                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4370                } ## TODO: And if supported
4371              }
4372              ## TODO: Change the encoding
4373            }
4374    
4375            !!!next-token;
4376            redo B;
4377        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
4378          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
4379          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4380          $parse_rcdata->('RCDATA', $insert);          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4381          return;            if (defined $self->{head_element}) {
4382                $self->{head_element}->append_child ($_[0]);
4383              } else {
4384                $insert->($_[0]);
4385              }
4386            });
4387            redo B;
4388        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4389          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
4390                                
# Line 2474  sub _tree_construction_main ($) { Line 4402  sub _tree_construction_main ($) {
4402            }            }
4403          }          }
4404          !!!next-token;          !!!next-token;
4405          return;          redo B;
4406        } elsif ({        } elsif ({
4407                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
4408                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 2485  sub _tree_construction_main ($) { Line 4413  sub _tree_construction_main ($) {
4413          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4414            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4415              !!!back-token;              !!!back-token;
4416              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4417              return;              redo B;
4418            } elsif ({            } elsif ({
4419                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4420                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2498  sub _tree_construction_main ($) { Line 4426  sub _tree_construction_main ($) {
4426          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4427          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4428            !!!next-token;            !!!next-token;
4429            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4430              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4431              unless (length $token->{data}) {              unless (length $token->{data}) {
4432                !!!next-token;                !!!next-token;
# Line 2507  sub _tree_construction_main ($) { Line 4435  sub _tree_construction_main ($) {
4435          } else {          } else {
4436            !!!next-token;            !!!next-token;
4437          }          }
4438          return;          redo B;
4439        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4440          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4441            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
4442            ## Ignore the token            ## Ignore the token
4443            !!!next-token;            !!!next-token;
4444            return;            redo B;
4445          } else {          } else {
4446            ## has a p element in scope            ## has a p element in scope
4447            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4448              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4449                !!!back-token;                !!!back-token;
4450                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4451                return;                redo B;
4452              } elsif ({              } elsif ({
4453                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
4454                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
# Line 2532  sub _tree_construction_main ($) { Line 4460  sub _tree_construction_main ($) {
4460            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4461            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
4462            !!!next-token;            !!!next-token;
4463            return;            redo B;
4464          }          }
4465        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4466          ## has a p element in scope          ## has a p element in scope
4467          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4468            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4469              !!!back-token;              !!!back-token;
4470              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4471              return;              redo B;
4472            } elsif ({            } elsif ({
4473                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4474                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2558  sub _tree_construction_main ($) { Line 4486  sub _tree_construction_main ($) {
4486              if ($i != -1) {              if ($i != -1) {
4487                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
4488                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
4489              }              }
4490              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
4491              last LI;              last LI;
# Line 2581  sub _tree_construction_main ($) { Line 4508  sub _tree_construction_main ($) {
4508                        
4509          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4510          !!!next-token;          !!!next-token;
4511          return;          redo B;
4512        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4513          ## has a p element in scope          ## has a p element in scope
4514          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4515            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4516              !!!back-token;              !!!back-token;
4517              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4518              return;              redo B;
4519            } elsif ({            } elsif ({
4520                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4521                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2606  sub _tree_construction_main ($) { Line 4533  sub _tree_construction_main ($) {
4533              if ($i != -1) {              if ($i != -1) {
4534                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
4535                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
4536              }              }
4537              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
4538              last LI;              last LI;
# Line 2629  sub _tree_construction_main ($) { Line 4555  sub _tree_construction_main ($) {
4555                        
4556          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4557          !!!next-token;          !!!next-token;
4558          return;          redo B;
4559        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
4560          ## has a p element in scope          ## has a p element in scope
4561          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4562            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4563              !!!back-token;              !!!back-token;
4564              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4565              return;              redo B;
4566            } elsif ({            } elsif ({
4567                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4568                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2647  sub _tree_construction_main ($) { Line 4573  sub _tree_construction_main ($) {
4573                        
4574          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4575                        
4576          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4577                        
4578          !!!next-token;          !!!next-token;
4579          return;          redo B;
4580        } elsif ({        } elsif ({
4581                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4582                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2659  sub _tree_construction_main ($) { Line 4585  sub _tree_construction_main ($) {
4585            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4586            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4587              !!!back-token;              !!!back-token;
4588              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4589              return;              redo B;
4590            } elsif ({            } elsif ({
4591                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4592                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2695  sub _tree_construction_main ($) { Line 4621  sub _tree_construction_main ($) {
4621          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4622                        
4623          !!!next-token;          !!!next-token;
4624          return;          redo B;
4625        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4626          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4627            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 2703  sub _tree_construction_main ($) { Line 4629  sub _tree_construction_main ($) {
4629              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4630                            
4631              !!!back-token;              !!!back-token;
4632              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4633              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4634                            
4635              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 2730  sub _tree_construction_main ($) { Line 4656  sub _tree_construction_main ($) {
4656          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4657    
4658          !!!next-token;          !!!next-token;
4659          return;          redo B;
4660        } elsif ({        } elsif ({
4661                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
4662                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
# Line 2742  sub _tree_construction_main ($) { Line 4668  sub _tree_construction_main ($) {
4668          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4669                    
4670          !!!next-token;          !!!next-token;
4671          return;          redo B;
4672        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4673          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4674    
# Line 2750  sub _tree_construction_main ($) { Line 4676  sub _tree_construction_main ($) {
4676          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4677            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4678            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4679                !!!parse-error (type => 'in nobr:nobr');
4680              !!!back-token;              !!!back-token;
4681              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4682              return;              redo B;
4683            } elsif ({            } elsif ({
4684                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4685                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2765  sub _tree_construction_main ($) { Line 4692  sub _tree_construction_main ($) {
4692          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4693                    
4694          !!!next-token;          !!!next-token;
4695          return;          redo B;
4696        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4697          ## has a button element in scope          ## has a button element in scope
4698          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2773  sub _tree_construction_main ($) { Line 4700  sub _tree_construction_main ($) {
4700            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4701              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4702              !!!back-token;              !!!back-token;
4703              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4704              return;              redo B;
4705            } elsif ({            } elsif ({
4706                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4707                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2789  sub _tree_construction_main ($) { Line 4716  sub _tree_construction_main ($) {
4716          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4717    
4718          !!!next-token;          !!!next-token;
4719          return;          redo B;
4720        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
4721                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
4722          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2798  sub _tree_construction_main ($) { Line 4725  sub _tree_construction_main ($) {
4725          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4726                    
4727          !!!next-token;          !!!next-token;
4728          return;          redo B;
4729        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
4730          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4731          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4732          return;          redo B;
4733        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
4734          ## has a p element in scope          ## has a p element in scope
4735          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4736            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4737              !!!back-token;              !!!back-token;
4738              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4739              return;              redo B;
4740            } elsif ({            } elsif ({
4741                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4742                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2820  sub _tree_construction_main ($) { Line 4747  sub _tree_construction_main ($) {
4747                        
4748          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4749                        
4750          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
4751                        
4752          !!!next-token;          !!!next-token;
4753          return;          redo B;
4754        } elsif ({        } elsif ({
4755                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
4756                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 2833  sub _tree_construction_main ($) { Line 4760  sub _tree_construction_main ($) {
4760            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
4761            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
4762          }          }
4763            
4764            ## NOTE: There is an "as if <br>" code clone.
4765          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4766                    
4767          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4768          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4769                    
4770          !!!next-token;          !!!next-token;
4771          return;          redo B;
4772        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
4773          ## has a p element in scope          ## has a p element in scope
4774          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4775            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4776              !!!back-token;              !!!back-token;
4777              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4778              return;              redo B;
4779            } elsif ({            } elsif ({
4780                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4781                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2860  sub _tree_construction_main ($) { Line 4788  sub _tree_construction_main ($) {
4788          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4789                        
4790          !!!next-token;          !!!next-token;
4791          return;          redo B;
4792        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
4793          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4794                    
# Line 2869  sub _tree_construction_main ($) { Line 4797  sub _tree_construction_main ($) {
4797          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4798                    
4799          !!!next-token;          !!!next-token;
4800          return;          redo B;
4801        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
4802          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
4803                    
4804          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4805            ## Ignore the token            ## Ignore the token
4806            !!!next-token;            !!!next-token;
4807            return;            redo B;
4808          } else {          } else {
4809            my $at = $token->{attributes};            my $at = $token->{attributes};
4810            my $form_attrs;            my $form_attrs;
# Line 2886  sub _tree_construction_main ($) { Line 4814  sub _tree_construction_main ($) {
4814            delete $at->{action};            delete $at->{action};
4815            delete $at->{prompt};            delete $at->{prompt};
4816            my @tokens = (            my @tokens = (
4817                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
4818                           attributes => $form_attrs},                           attributes => $form_attrs},
4819                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4820                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
4821                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
4822                         );                         );
4823            if ($prompt_attr) {            if ($prompt_attr) {
4824              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4825            } else {            } else {
4826              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
4827                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4828              ## TODO: make this configurable              ## TODO: make this configurable
4829            }            }
4830            push @tokens,            push @tokens,
4831                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4832                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4833                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
4834                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
4835                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4836                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
4837            $token = shift @tokens;            $token = shift @tokens;
4838            !!!back-token (@tokens);            !!!back-token (@tokens);
4839            return;            redo B;
4840          }          }
4841        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
4842          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
# Line 2916  sub _tree_construction_main ($) { Line 4844  sub _tree_construction_main ($) {
4844          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4845                    
4846          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
4847          $self->{content_model_flag} = 'RCDATA';          $self->{content_model} = RCDATA_CONTENT_MODEL;
4848          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
4849                    
4850          $insert->($el);          $insert->($el);
4851                    
4852          my $text = '';          my $text = '';
4853          !!!next-token;          !!!next-token;
4854          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
4855            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
4856            unless (length $token->{data}) {            unless (length $token->{data}) {
4857              !!!next-token;              !!!next-token;
4858            }            }
4859          }          }
4860          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
4861            $text .= $token->{data};            $text .= $token->{data};
4862            !!!next-token;            !!!next-token;
4863          }          }
# Line 2937  sub _tree_construction_main ($) { Line 4865  sub _tree_construction_main ($) {
4865            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
4866          }          }
4867                    
4868          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
4869                    
4870          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
4871              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
4872            ## Ignore the token            ## Ignore the token
4873          } else {          } else {
4874            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4875          }          }
4876          !!!next-token;          !!!next-token;
4877          return;          redo B;
4878        } elsif ({        } elsif ({
4879                  iframe => 1,                  iframe => 1,
4880                  noembed => 1,                  noembed => 1,
4881                  noframes => 1,                  noframes => 1,
4882                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
4883                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4884          $parse_rcdata->('CDATA', $insert);          ## NOTE: There is an "as if in body" code clone.
4885          return;          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4886            redo B;
4887        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
4888          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4889                    
4890          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4891                    
4892          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
4893          !!!next-token;          !!!next-token;
4894          return;          redo B;
4895        } elsif ({        } elsif ({
4896                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
4897                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 2972  sub _tree_construction_main ($) { Line 4901  sub _tree_construction_main ($) {
4901          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
4902          ## Ignore the token          ## Ignore the token
4903          !!!next-token;          !!!next-token;
4904          return;          redo B;
4905                    
4906          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
4907        } else {        } else {
# Line 2981  sub _tree_construction_main ($) { Line 4910  sub _tree_construction_main ($) {
4910          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4911                    
4912          !!!next-token;          !!!next-token;
4913          return;          redo B;
4914        }        }
4915      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
4916        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
4917          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
4918              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 2991  sub _tree_construction_main ($) { Line 4920  sub _tree_construction_main ($) {
4920              unless ({              unless ({
4921                         dd => 1, dt => 1, li => 1, p => 1, td => 1,                         dd => 1, dt => 1, li => 1, p => 1, td => 1,
4922                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
4923                         tbody => 1, tfoot => 1, thead => 1,
4924                      }->{$_->[1]}) {                      }->{$_->[1]}) {
4925                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
4926              }              }
4927            }            }
4928    
4929            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4930            !!!next-token;            !!!next-token;
4931            return;            redo B;
4932          } else {          } else {
4933            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4934            ## Ignore the token            ## Ignore the token
4935            !!!next-token;            !!!next-token;
4936            return;            redo B;
4937          }          }
4938        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
4939          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3011  sub _tree_construction_main ($) { Line 4941  sub _tree_construction_main ($) {
4941            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
4942              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4943            }            }
4944            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4945            ## reprocess            ## reprocess
4946            return;            redo B;
4947          } else {          } else {
4948            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4949            ## Ignore the token            ## Ignore the token
4950            !!!next-token;            !!!next-token;
4951            return;            redo B;
4952          }          }
4953        } elsif ({        } elsif ({
4954                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 3040  sub _tree_construction_main ($) { Line 4970  sub _tree_construction_main ($) {
4970                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
4971                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
4972                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
4973                     tbody => 1, tfoot=> 1, thead => 1,
4974                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4975                !!!back-token;                !!!back-token;
4976                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
4977                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4978                return;                redo B;
4979              }              }
4980              $i = $_;              $i = $_;
4981              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
# Line 3057  sub _tree_construction_main ($) { Line 4988  sub _tree_construction_main ($) {
4988          } # INSCOPE          } # INSCOPE
4989                    
4990          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4991            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            if (defined $i) {
4992                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4993              } else {
4994                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4995              }
4996          }          }
4997                    
4998          splice @{$self->{open_elements}}, $i if defined $i;          if (defined $i) {
4999              splice @{$self->{open_elements}}, $i;
5000            } elsif ($token->{tag_name} eq 'p') {
5001              ## As if <p>, then reprocess the current token
5002              my $el;
5003              !!!create-element ($el, 'p');
5004              $insert->($el);
5005            }
5006          $clear_up_to_marker->()          $clear_up_to_marker->()
5007            if {            if {
5008              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
5009            }->{$token->{tag_name}};            }->{$token->{tag_name}};
5010          !!!next-token;          !!!next-token;
5011          return;          redo B;
5012        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5013          ## has an element in scope          ## has an element in scope
5014          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3076  sub _tree_construction_main ($) { Line 5018  sub _tree_construction_main ($) {
5018              if ({              if ({
5019                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5020                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5021                     tbody => 1, tfoot=> 1, thead => 1,
5022                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5023                !!!back-token;                !!!back-token;
5024                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5025                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5026                return;                redo B;
5027              }              }
5028              last INSCOPE;              last INSCOPE;
5029            } elsif ({            } elsif ({
# Line 3094  sub _tree_construction_main ($) { Line 5037  sub _tree_construction_main ($) {
5037          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5038            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5039          } else {          } else {
5040            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5041          }          }
5042    
5043          undef $self->{form_element};          undef $self->{form_element};
5044          !!!next-token;          !!!next-token;
5045          return;          redo B;
5046        } elsif ({        } elsif ({
5047                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5048                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 3114  sub _tree_construction_main ($) { Line 5057  sub _tree_construction_main ($) {
5057              if ({              if ({
5058                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5059                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5060                     tbody => 1, tfoot=> 1, thead => 1,
5061                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5062                !!!back-token;                !!!back-token;
5063                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5064                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5065                return;                redo B;
5066              }              }
5067              $i = $_;              $i = $_;
5068              last INSCOPE;              last INSCOPE;
# Line 3131  sub _tree_construction_main ($) { Line 5075  sub _tree_construction_main ($) {
5075          } # INSCOPE          } # INSCOPE
5076                    
5077          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5078            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5079          }          }
5080                    
5081          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
5082          !!!next-token;          !!!next-token;
5083          return;          redo B;
5084        } elsif ({        } elsif ({
5085                  a => 1,                  a => 1,
5086                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 3144  sub _tree_construction_main ($) { Line 5088  sub _tree_construction_main ($) {
5088                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5089                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5090          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
5091  ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>          redo B;
5092          return;        } elsif ($token->{tag_name} eq 'br') {
5093            !!!parse-error (type => 'unmatched end tag:br');
5094    
5095            ## As if <br>
5096            $reconstruct_active_formatting_elements->($insert_to_current);
5097            
5098            my $el;
5099            !!!create-element ($el, 'br');
5100            $insert->($el);
5101            
5102            ## Ignore the token.
5103            !!!next-token;
5104            redo B;
5105        } elsif ({        } elsif ({
5106                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5107                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
5108                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5109                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5110                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
5111                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
5112                  img => 1, input => 1, isindex => 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
5113                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
# Line 3161  sub _tree_construction_main ($) { Line 5117  sub _tree_construction_main ($) {
5117          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5118          ## Ignore the token          ## Ignore the token
5119          !!!next-token;          !!!next-token;
5120          return;          redo B;
5121                    
5122          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
5123                    
# Line 3178  sub _tree_construction_main ($) { Line 5134  sub _tree_construction_main ($) {
5134              if ({              if ({
5135                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5136                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5137                     tbody => 1, tfoot => 1, thead => 1,
5138                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5139                !!!back-token;                !!!back-token;
5140                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5141                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5142                return;                redo B;
5143              }              }
5144                    
5145              ## Step 2              ## Step 2
5146              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5147                  ## NOTE: <x><y></x>
5148                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5149              }              }
5150                            
# Line 3215  sub _tree_construction_main ($) { Line 5173  sub _tree_construction_main ($) {
5173            ## Step 5;            ## Step 5;
5174            redo S2;            redo S2;
5175          } # S2          } # S2
5176          return;          redo B;
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($phase eq 'main') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'in html:#DOCTYPE');  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## Stop parsing  
         last B;  
   
         ## ISSUE: There is an issue in the spec.  
       } else {  
         if ($self->{insertion_mode} eq 'before head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             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';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({head => 1, body => 1, html => 1}->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head' or  
                  $self->{insertion_mode} eq 'in head noscript' or  
                  $self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'title' and  
                      $self->{insertion_mode} eq 'in head') {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               $parse_rcdata->('RCDATA', $insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and  
               ## insertion mode 'in head')  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               $parse_rcdata->('CDATA', $insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'noscript') {  
               if ($self->{insertion_mode} eq 'in head') {  
                 ## NOTE: and scripting is disalbed  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 $self->{insertion_mode} = 'in head noscript';  
                 !!!next-token;  
                 redo B;  
               } elsif ($self->{insertion_mode} eq 'in head noscript') {  
                 !!!parse-error (type => 'noscript in noscript');  
                 ## Ignore the token  
                 redo B;  
               } else {  
                 #  
               }  
             } elsif ($token->{tag_name} eq 'head' and  
                      $self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'in head:head'); # or in head noscript  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} ne 'in head noscript' and  
                      $token->{tag_name} eq 'script') {  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               ## NOTE: There is a "as if in head" code clone.  
               $script_start_tag->($insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               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;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($self->{insertion_mode} eq 'in head' and  
                 $token->{tag_name} eq 'head') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                 $token->{tag_name} eq 'noscript') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      ($token->{tag_name} eq 'body' or  
                       $token->{tag_name} eq 'html')) {  
               #  
             } 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 {  
             #  
           }  
   
           ## As if </head> or </noscript> or <body>  
           if ($self->{insertion_mode} eq 'in head') {  
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } 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});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!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]);  
                 pop @{$self->{open_elements}};  
               }  
   
               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;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 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]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!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]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $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;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 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]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           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});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!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]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $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  
               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;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 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]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!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]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               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 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
   
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!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 ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!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}};  
               }  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!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,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!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;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } 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';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## 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:tr');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## 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 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});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 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  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## 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;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 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});  
               ## Ignore the token  
               !!!next-token;  
               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;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 ## As if </optgroup>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               !!!parse-error (type => 'not closed:select');  
               ## As if </select> instead  
               ## 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:select');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option' and  
                   $self->{open_elements}->[-2]->[1] eq 'optgroup') {  
                 ## As if </option>  
                 splice @{$self->{open_elements}}, -2;  
               } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## 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;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, table => 1, tbody => 1,  
                       tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after body') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           }  
   
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'in frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'in frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } 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 ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'after frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
   
           ## ISSUE: An issue in spec there  
         } else {  
           die "$0: $self->{insertion_mode}: Unknown insertion mode";  
         }  
       }  
     } elsif ($phase eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           my $data = $1;  
           ## As if in the main phase.  
           ## NOTE: The insertion mode in the main phase  
           ## just before the phase has been changed to the trailing  
           ## end phase is either "after body" or "after frameset".  
           $reconstruct_active_formatting_elements->($insert_to_current)  
             if $phase eq 'main';  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
           }  
         }  
   
         !!!parse-error (type => 'after html:#character');  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.$token->{tag_name});  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
5177        }        }
5178      }      }
5179        redo B;
5180    } # B    } # B
5181    
5182      ## NOTE: The "trailing end" phase in HTML5 is split into
5183      ## two insertion modes: "after html body" and "after html frameset".
5184      ## NOTE: States in the main stage is preserved while
5185      ## the parser stays in the trailing end phase. # MUST
5186    
5187    ## Stop parsing # MUST    ## Stop parsing # MUST
5188        
5189    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5167  sub set_inner_html ($$$) { Line 5268  sub set_inner_html ($$$) {
5268    
5269      ## Step 2      ## Step 2
5270      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5271      $p->{content_model_flag} = {      $p->{content_model} = {
5272        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5273        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5274        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5275        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5276        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5277        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5278        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5279        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5280        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5281        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5282      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5283         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5284            unless defined $p->{content_model};
5285            ## ISSUE: What is "the name of the element"? local name?
5286    
5287      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5288    
# Line 5279  sub get_inner_html ($$$) { Line 5382  sub get_inner_html ($$$) {
5382            
5383      my $nt = $child->node_type;      my $nt = $child->node_type;
5384      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5385        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5386        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5387          ## NOTE: Non-HTML case:
5388        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5389    
5390        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5391        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5392          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5393          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5394          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5395          ## escape          ## escape
# Line 5310  sub get_inner_html ($$$) { Line 5413  sub get_inner_html ($$$) {
5413        if (not $in_cdata and {        if (not $in_cdata and {
5414          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5415          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5416            plaintext => 1,
5417        }->{$tag_name}) {        }->{$tag_name}) {
5418          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5419          $in_cdata = 1;          $in_cdata = 1;

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24