/[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.10 by wakaba, Sat Jun 23 03:30:06 2007 UTC revision 1.53 by wakaba, Sat Jul 21 12:37:57 2007 UTC
# Line 2  package Whatpm::HTML; Line 2  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    
5  ## This is an early version of an HTML parser.  ## ISSUE:
6    ## var doc = implementation.createDocument (null, null, null);
7    ## doc.write ('');
8    ## 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,
# Line 18  my $permitted_slash_tag_name = { Line 27  my $permitted_slash_tag_name = {
27    input => 1,    input => 1,
28  };  };
29    
 my $entity_char = {  
   AElig => "\x{00C6}",  
   Aacute => "\x{00C1}",  
   Acirc => "\x{00C2}",  
   Agrave => "\x{00C0}",  
   Alpha => "\x{0391}",  
   Aring => "\x{00C5}",  
   Atilde => "\x{00C3}",  
   Auml => "\x{00C4}",  
   Beta => "\x{0392}",  
   Ccedil => "\x{00C7}",  
   Chi => "\x{03A7}",  
   Dagger => "\x{2021}",  
   Delta => "\x{0394}",  
   ETH => "\x{00D0}",  
   Eacute => "\x{00C9}",  
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 }; # $entity_char  
   
30  my $c1_entity_char = {  my $c1_entity_char = {
31    0x80 => 0x20AC,    0x80 => 0x20AC,
32    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 349  sub parse_string ($$$;$) { Line 96  sub parse_string ($$$;$) {
96    my $column = 0;    my $column = 0;
97    $self->{set_next_input_character} = sub {    $self->{set_next_input_character} = sub {
98      my $self = shift;      my $self = shift;
99    
100        pop @{$self->{prev_input_character}};
101        unshift @{$self->{prev_input_character}}, $self->{next_input_character};
102    
103      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_input_character} = -1 and return if $i >= length $$s;
104      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_input_character} = ord substr $$s, $i++, 1;
105      $column++;      $column++;
# Line 357  sub parse_string ($$$;$) { Line 108  sub parse_string ($$$;$) {
108        $line++;        $line++;
109        $column = 0;        $column = 0;
110      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_input_character} == 0x000D) { # CR
111        if ($i >= length $$s) {        $i++ if substr ($$s, $i, 1) eq "\x0A";
         #  
       } else {  
         my $next_char = ord substr $$s, $i++, 1;  
         if ($next_char == 0x000A) { # LF  
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
       }  
112        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_input_character} = 0x000A; # LF # MUST
113        $line++;        $line++;
114        $column = 0;        $column = 0;
# Line 374  sub parse_string ($$$;$) { Line 116  sub parse_string ($$$;$) {
116        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
117      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_input_character} == 0x0000) { # NULL
118        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
 ## TODO: test  
119        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
120      }      }
121    };    };
122      $self->{prev_input_character} = [-1, -1, -1];
123      $self->{next_input_character} = -1;
124    
125    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
126      my (%opt) = @_;      my (%opt) = @_;
# Line 407  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  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
163    
164  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
165    my $self = shift;    my $self = shift;
166    $self->{state} = 'data'; # MUST    $self->{state} = 'data'; # MUST
167    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
168    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
169    undef $self->{current_attribute};    undef $self->{current_attribute};
170    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 421  sub _initialize_tokenizer ($) { Line 173  sub _initialize_tokenizer ($) {
173    # $self->{next_input_character}    # $self->{next_input_character}
174    !!!next-input-character;    !!!next-input-character;
175    $self->{token} = [];    $self->{token} = [];
176      # $self->{escape}
177  } # _initialize_tokenizer  } # _initialize_tokenizer
178    
179  ## A token has:  ## A token has:
180  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',
181  ##       'character', or 'end-of-file'  ##       'character', or 'end-of-file'
182  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))
183      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{public_identifier} (DOCTYPE)
184  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE)
185    ##   ->{correct} == 1 or 0 (DOCTYPE)
186  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{attributes} isa HASH (start tag, end tag)
187  ##   ->{data} (comment, character)  ##   ->{data} (comment, character)
188    
 ## Macros  
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
   
189  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
190    
191  ## Before each step, UA MAY check to see if either one of the scripts in  ## Before each step, UA MAY check to see if either one of the scripts in
# Line 445  sub _initialize_tokenizer ($) { Line 194  sub _initialize_tokenizer ($) {
194  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
195  ## and removed from the list.  ## and removed from the list.
196    
 ## ISSUE: <http://html5.org/tools/web-apps-tracker?from=874&to=876>  
   
197  sub _get_next_token ($) {  sub _get_next_token ($) {
198    my $self = shift;    my $self = shift;
199    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 456  sub _get_next_token ($) { Line 203  sub _get_next_token ($) {
203    A: {    A: {
204      if ($self->{state} eq 'data') {      if ($self->{state} eq 'data') {
205        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
206          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
             $self->{content_model_flag} eq 'RCDATA') {  
207            $self->{state} = 'entity data';            $self->{state} = 'entity data';
208            !!!next-input-character;            !!!next-input-character;
209            redo A;            redo A;
210          } else {          } else {
211            #            #
212          }          }
213          } elsif ($self->{next_input_character} == 0x002D) { # -
214            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
215              unless ($self->{escape}) {
216                if ($self->{prev_input_character}->[0] == 0x002D and # -
217                    $self->{prev_input_character}->[1] == 0x0021 and # !
218                    $self->{prev_input_character}->[2] == 0x003C) { # <
219                  $self->{escape} = 1;
220                }
221              }
222            }
223            
224            #
225        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
226          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
227                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
228                 not $self->{escape})) {
229            $self->{state} = 'tag open';            $self->{state} = 'tag open';
230            !!!next-input-character;            !!!next-input-character;
231            redo A;            redo A;
232          } else {          } else {
233            #            #
234          }          }
235          } elsif ($self->{next_input_character} == 0x003E) { # >
236            if ($self->{escape} and
237                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
238              if ($self->{prev_input_character}->[0] == 0x002D and # -
239                  $self->{prev_input_character}->[1] == 0x002D) { # -
240                delete $self->{escape};
241              }
242            }
243            
244            #
245        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
246          !!!emit ({type => 'end-of-file'});          !!!emit ({type => 'end-of-file'});
247          last A; ## TODO: ok?          last A; ## TODO: ok?
# Line 488  sub _get_next_token ($) { Line 258  sub _get_next_token ($) {
258      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
259        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
260                
261        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
262    
263        $self->{state} = 'data';        $self->{state} = 'data';
264        # next-input-character is already done        # next-input-character is already done
# Line 501  sub _get_next_token ($) { Line 271  sub _get_next_token ($) {
271    
272        redo A;        redo A;
273      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} eq 'tag open') {
274        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
275          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
276            !!!next-input-character;            !!!next-input-character;
277            $self->{state} = 'close tag open';            $self->{state} = 'close tag open';
# Line 515  sub _get_next_token ($) { Line 284  sub _get_next_token ($) {
284    
285            redo A;            redo A;
286          }          }
287        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
288          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
289            $self->{state} = 'markup declaration open';            $self->{state} = 'markup declaration open';
290            !!!next-input-character;            !!!next-input-character;
# Line 562  sub _get_next_token ($) { Line 331  sub _get_next_token ($) {
331            redo A;            redo A;
332          }          }
333        } else {        } else {
334          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
335        }        }
336      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
337        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
338            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
339          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
340          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
341              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
342                push @next_char, $self->{next_input_character};
343                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
344                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
345                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
346                  !!!next-input-character;
347                  next TAGNAME;
348                } else {
349                  $self->{next_input_character} = shift @next_char; # reconsume
350                  !!!back-next-input-character (@next_char);
351                  $self->{state} = 'data';
352    
353                  !!!emit ({type => 'character', data => '</'});
354      
355                  redo A;
356                }
357              }
358            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
359            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
360            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
361            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
362              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
363              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
364            } else {                    $self->{next_input_character} == 0x0020 or # SP
365              !!!parse-error (type => 'unmatched end tag');                    $self->{next_input_character} == 0x003E or # >
366                      $self->{next_input_character} == 0x002F or # /
367                      $self->{next_input_character} == -1) {
368              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
369              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
370              $self->{state} = 'data';              $self->{state} = 'data';
   
371              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => 'character', data => '</'});
   
372              redo A;              redo A;
373              } else {
374                $self->{next_input_character} = shift @next_char;
375                !!!back-next-input-character (@next_char);
376                # and consume...
377            }            }
378          }          } else {
379          push @next_char, $self->{next_input_character};            ## No start tag token has ever been emitted
380                  # next-input-character is already done
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error (type => 'unmatched end tag');  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
381            $self->{state} = 'data';            $self->{state} = 'data';
   
382            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => 'character', data => '</'});
   
383            redo A;            redo A;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
384          }          }
385        }        }
386                
# Line 656  sub _get_next_token ($) { Line 428  sub _get_next_token ($) {
428          redo A;          redo A;
429        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
430          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
431              $self->{current_token}->{first_start_tag}
432                  = not defined $self->{last_emitted_start_tag_name};
433            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
434          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
435            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
436            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
437              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
438            }            }
# Line 669  sub _get_next_token ($) { Line 443  sub _get_next_token ($) {
443          !!!next-input-character;          !!!next-input-character;
444    
445          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
446    
447          redo A;          redo A;
448        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 679  sub _get_next_token ($) { Line 452  sub _get_next_token ($) {
452          ## Stay in this state          ## Stay in this state
453          !!!next-input-character;          !!!next-input-character;
454          redo A;          redo A;
455        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
456          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
457          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
458              $self->{current_token}->{first_start_tag}
459                  = not defined $self->{last_emitted_start_tag_name};
460            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
461          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
462            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
463            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
464              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
465            }            }
# Line 696  sub _get_next_token ($) { Line 470  sub _get_next_token ($) {
470          # reconsume          # reconsume
471    
472          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
473    
474          redo A;          redo A;
475        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
# Line 730  sub _get_next_token ($) { Line 503  sub _get_next_token ($) {
503          redo A;          redo A;
504        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
505          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
506              $self->{current_token}->{first_start_tag}
507                  = not defined $self->{last_emitted_start_tag_name};
508            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
509          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
510            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
511            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
512              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
513            }            }
# Line 743  sub _get_next_token ($) { Line 518  sub _get_next_token ($) {
518          !!!next-input-character;          !!!next-input-character;
519    
520          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
521    
522          redo A;          redo A;
523        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 766  sub _get_next_token ($) { Line 540  sub _get_next_token ($) {
540          ## Stay in the state          ## Stay in the state
541          # next-input-character is already done          # next-input-character is already done
542          redo A;          redo A;
543        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
544          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
545          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
546              $self->{current_token}->{first_start_tag}
547                  = not defined $self->{last_emitted_start_tag_name};
548            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
549          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
550            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
551            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
552              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
553            }            }
# Line 783  sub _get_next_token ($) { Line 558  sub _get_next_token ($) {
558          # reconsume          # reconsume
559    
560          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
561    
562          redo A;          redo A;
563        } else {        } else {
# Line 797  sub _get_next_token ($) { Line 571  sub _get_next_token ($) {
571        my $before_leave = sub {        my $before_leave = sub {
572          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
573              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
574            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
575            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
576          } else {          } else {
577            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 822  sub _get_next_token ($) { Line 596  sub _get_next_token ($) {
596        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
597          $before_leave->();          $before_leave->();
598          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
599              $self->{current_token}->{first_start_tag}
600                  = not defined $self->{last_emitted_start_tag_name};
601            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
602          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
603            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
604            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
605              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
606            }            }
# Line 835  sub _get_next_token ($) { Line 611  sub _get_next_token ($) {
611          !!!next-input-character;          !!!next-input-character;
612    
613          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
614    
615          redo A;          redo A;
616        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 858  sub _get_next_token ($) { Line 633  sub _get_next_token ($) {
633          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
634          # next-input-character is already done          # next-input-character is already done
635          redo A;          redo A;
636        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
637          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
638          $before_leave->();          $before_leave->();
639          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
640              $self->{current_token}->{first_start_tag}
641                  = not defined $self->{last_emitted_start_tag_name};
642            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
643          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
644            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
645            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
646              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
647            }            }
# Line 876  sub _get_next_token ($) { Line 652  sub _get_next_token ($) {
652          # reconsume          # reconsume
653    
654          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
655    
656          redo A;          redo A;
657        } else {        } else {
# Line 900  sub _get_next_token ($) { Line 675  sub _get_next_token ($) {
675          redo A;          redo A;
676        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
677          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
678              $self->{current_token}->{first_start_tag}
679                  = not defined $self->{last_emitted_start_tag_name};
680            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
681          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
682            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
683            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
684              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
685            }            }
# Line 913  sub _get_next_token ($) { Line 690  sub _get_next_token ($) {
690          !!!next-input-character;          !!!next-input-character;
691    
692          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
693    
694          redo A;          redo A;
695        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 932  sub _get_next_token ($) { Line 708  sub _get_next_token ($) {
708            #            #
709          } else {          } else {
710            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
711              ## TODO: Different error type for <aa / bb> than <aa/>
712          }          }
713          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
714          # next-input-character is already done          # next-input-character is already done
715          redo A;          redo A;
716        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
717          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
718          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
719              $self->{current_token}->{first_start_tag}
720                  = not defined $self->{last_emitted_start_tag_name};
721            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
722          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
723            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
724            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
725              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
726            }            }
# Line 953  sub _get_next_token ($) { Line 731  sub _get_next_token ($) {
731          # reconsume          # reconsume
732    
733          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
734    
735          redo A;          redo A;
736        } else {        } else {
# Line 986  sub _get_next_token ($) { Line 763  sub _get_next_token ($) {
763          redo A;          redo A;
764        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
765          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
766              $self->{current_token}->{first_start_tag}
767                  = not defined $self->{last_emitted_start_tag_name};
768            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
769          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
770            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
771            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
772              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
773            }            }
# Line 999  sub _get_next_token ($) { Line 778  sub _get_next_token ($) {
778          !!!next-input-character;          !!!next-input-character;
779    
780          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
781    
782          redo A;          redo A;
783        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
784          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
785          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
786              $self->{current_token}->{first_start_tag}
787                  = not defined $self->{last_emitted_start_tag_name};
788            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
789          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
790            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
791            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
792              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
793            }            }
# Line 1019  sub _get_next_token ($) { Line 798  sub _get_next_token ($) {
798          ## reconsume          ## reconsume
799    
800          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
801    
802          redo A;          redo A;
803        } else {        } else {
# Line 1041  sub _get_next_token ($) { Line 819  sub _get_next_token ($) {
819        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
820          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
821          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
822              $self->{current_token}->{first_start_tag}
823                  = not defined $self->{last_emitted_start_tag_name};
824            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
825          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
826            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
827            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
828              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
829            }            }
# Line 1054  sub _get_next_token ($) { Line 834  sub _get_next_token ($) {
834          ## reconsume          ## reconsume
835    
836          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
837    
838          redo A;          redo A;
839        } else {        } else {
# Line 1076  sub _get_next_token ($) { Line 855  sub _get_next_token ($) {
855        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
856          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
857          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
858              $self->{current_token}->{first_start_tag}
859                  = not defined $self->{last_emitted_start_tag_name};
860            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
861          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
862            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
863            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
864              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
865            }            }
# Line 1089  sub _get_next_token ($) { Line 870  sub _get_next_token ($) {
870          ## reconsume          ## reconsume
871    
872          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
873    
874          redo A;          redo A;
875        } else {        } else {
# Line 1114  sub _get_next_token ($) { Line 894  sub _get_next_token ($) {
894          redo A;          redo A;
895        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
896          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
897              $self->{current_token}->{first_start_tag}
898                  = not defined $self->{last_emitted_start_tag_name};
899            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
900          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
901            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
902            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
903              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
904            }            }
# Line 1127  sub _get_next_token ($) { Line 909  sub _get_next_token ($) {
909          !!!next-input-character;          !!!next-input-character;
910    
911          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
912    
913          redo A;          redo A;
914        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
915          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
916          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
917              $self->{current_token}->{first_start_tag}
918                  = not defined $self->{last_emitted_start_tag_name};
919            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
920          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
921            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
922            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
923              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
924            }            }
# Line 1147  sub _get_next_token ($) { Line 929  sub _get_next_token ($) {
929          ## reconsume          ## reconsume
930    
931          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
932    
933          redo A;          redo A;
934        } else {        } else {
# Line 1157  sub _get_next_token ($) { Line 938  sub _get_next_token ($) {
938          redo A;          redo A;
939        }        }
940      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
941        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
942    
943        unless (defined $token) {        unless (defined $token) {
944          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 1206  sub _get_next_token ($) { Line 987  sub _get_next_token ($) {
987          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
988          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
989            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => 'comment', data => ''};
990            $self->{state} = 'comment';            $self->{state} = 'comment start';
991            !!!next-input-character;            !!!next-input-character;
992            redo A;            redo A;
993          }          }
# Line 1248  sub _get_next_token ($) { Line 1029  sub _get_next_token ($) {
1029          }          }
1030        }        }
1031    
1032        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1033        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1034        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1035        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1256  sub _get_next_token ($) { Line 1037  sub _get_next_token ($) {
1037                
1038        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1039        ## 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?
1040        } elsif ($self->{state} eq 'comment start') {
1041          if ($self->{next_input_character} == 0x002D) { # -
1042            $self->{state} = 'comment start dash';
1043            !!!next-input-character;
1044            redo A;
1045          } elsif ($self->{next_input_character} == 0x003E) { # >
1046            !!!parse-error (type => 'bogus comment');
1047            $self->{state} = 'data';
1048            !!!next-input-character;
1049    
1050            !!!emit ($self->{current_token}); # comment
1051    
1052            redo A;
1053          } elsif ($self->{next_input_character} == -1) {
1054            !!!parse-error (type => 'unclosed comment');
1055            $self->{state} = 'data';
1056            ## reconsume
1057    
1058            !!!emit ($self->{current_token}); # comment
1059    
1060            redo A;
1061          } else {
1062            $self->{current_token}->{data} # comment
1063                .= chr ($self->{next_input_character});
1064            $self->{state} = 'comment';
1065            !!!next-input-character;
1066            redo A;
1067          }
1068        } elsif ($self->{state} eq 'comment start dash') {
1069          if ($self->{next_input_character} == 0x002D) { # -
1070            $self->{state} = 'comment end';
1071            !!!next-input-character;
1072            redo A;
1073          } elsif ($self->{next_input_character} == 0x003E) { # >
1074            !!!parse-error (type => 'bogus comment');
1075            $self->{state} = 'data';
1076            !!!next-input-character;
1077    
1078            !!!emit ($self->{current_token}); # comment
1079    
1080            redo A;
1081          } elsif ($self->{next_input_character} == -1) {
1082            !!!parse-error (type => 'unclosed comment');
1083            $self->{state} = 'data';
1084            ## reconsume
1085    
1086            !!!emit ($self->{current_token}); # comment
1087    
1088            redo A;
1089          } else {
1090            $self->{current_token}->{data} # comment
1091                .= '-' . chr ($self->{next_input_character});
1092            $self->{state} = 'comment';
1093            !!!next-input-character;
1094            redo A;
1095          }
1096      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} eq 'comment') {
1097        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1098          $self->{state} = 'comment dash';          $self->{state} = 'comment end dash';
1099          !!!next-input-character;          !!!next-input-character;
1100          redo A;          redo A;
1101        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1267  sub _get_next_token ($) { Line 1104  sub _get_next_token ($) {
1104          ## reconsume          ## reconsume
1105    
1106          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1107    
1108          redo A;          redo A;
1109        } else {        } else {
# Line 1276  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112          !!!next-input-character;          !!!next-input-character;
1113          redo A;          redo A;
1114        }        }
1115      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} eq 'comment end dash') {
1116        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1117          $self->{state} = 'comment end';          $self->{state} = 'comment end';
1118          !!!next-input-character;          !!!next-input-character;
# Line 1287  sub _get_next_token ($) { Line 1123  sub _get_next_token ($) {
1123          ## reconsume          ## reconsume
1124    
1125          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1126    
1127          redo A;          redo A;
1128        } else {        } else {
# Line 1302  sub _get_next_token ($) { Line 1137  sub _get_next_token ($) {
1137          !!!next-input-character;          !!!next-input-character;
1138    
1139          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1140    
1141          redo A;          redo A;
1142        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1317  sub _get_next_token ($) { Line 1151  sub _get_next_token ($) {
1151          ## reconsume          ## reconsume
1152    
1153          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1154    
1155          redo A;          redo A;
1156        } else {        } else {
# Line 1351  sub _get_next_token ($) { Line 1184  sub _get_next_token ($) {
1184          ## Stay in the state          ## Stay in the state
1185          !!!next-input-character;          !!!next-input-character;
1186          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
1187        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1188          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1189          $self->{state} = 'data';          $self->{state} = 'data';
1190          !!!next-input-character;          !!!next-input-character;
1191    
1192          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1193    
1194          redo A;          redo A;
1195        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1373  sub _get_next_token ($) { Line 1197  sub _get_next_token ($) {
1197          $self->{state} = 'data';          $self->{state} = 'data';
1198          ## reconsume          ## reconsume
1199    
1200          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1201    
1202          redo A;          redo A;
1203        } else {        } else {
1204          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1205                            name => chr ($self->{next_input_character}),              = {type => 'DOCTYPE',
1206                            error => 1};                 name => chr ($self->{next_input_character}),
1207                   correct => 1};
1208  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1209          $self->{state} = 'DOCTYPE name';          $self->{state} = 'DOCTYPE name';
1210          !!!next-input-character;          !!!next-input-character;
1211          redo A;          redo A;
1212        }        }
1213      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} eq 'DOCTYPE name') {
1214    ## ISSUE: Redundant "First," in the spec.
1215        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1216            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1217            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1218            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1219            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1220          $self->{state} = 'after DOCTYPE name';          $self->{state} = 'after DOCTYPE name';
1221          !!!next-input-character;          !!!next-input-character;
1222          redo A;          redo A;
1223        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1224          $self->{state} = 'data';          $self->{state} = 'data';
1225          !!!next-input-character;          !!!next-input-character;
1226    
1227          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1228    
1229          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
         $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE  
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
1230        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1231          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1232          $self->{state} = 'data';          $self->{state} = 'data';
1233          ## reconsume          ## reconsume
1234    
1235          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1236          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1237    
1238          redo A;          redo A;
1239        } else {        } else {
1240          $self->{current_token}->{name}          $self->{current_token}->{name}
1241            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1242          ## Stay in the state          ## Stay in the state
1243          !!!next-input-character;          !!!next-input-character;
1244          redo A;          redo A;
# Line 1443  sub _get_next_token ($) { Line 1257  sub _get_next_token ($) {
1257          !!!next-input-character;          !!!next-input-character;
1258    
1259          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1260    
1261          redo A;          redo A;
1262        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1451  sub _get_next_token ($) { Line 1264  sub _get_next_token ($) {
1264          $self->{state} = 'data';          $self->{state} = 'data';
1265          ## reconsume          ## reconsume
1266    
1267            delete $self->{current_token}->{correct};
1268          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1269    
1270          redo A;          redo A;
1271          } elsif ($self->{next_input_character} == 0x0050 or # P
1272                   $self->{next_input_character} == 0x0070) { # p
1273            !!!next-input-character;
1274            if ($self->{next_input_character} == 0x0055 or # U
1275                $self->{next_input_character} == 0x0075) { # u
1276              !!!next-input-character;
1277              if ($self->{next_input_character} == 0x0042 or # B
1278                  $self->{next_input_character} == 0x0062) { # b
1279                !!!next-input-character;
1280                if ($self->{next_input_character} == 0x004C or # L
1281                    $self->{next_input_character} == 0x006C) { # l
1282                  !!!next-input-character;
1283                  if ($self->{next_input_character} == 0x0049 or # I
1284                      $self->{next_input_character} == 0x0069) { # i
1285                    !!!next-input-character;
1286                    if ($self->{next_input_character} == 0x0043 or # C
1287                        $self->{next_input_character} == 0x0063) { # c
1288                      $self->{state} = 'before DOCTYPE public identifier';
1289                      !!!next-input-character;
1290                      redo A;
1291                    }
1292                  }
1293                }
1294              }
1295            }
1296    
1297            #
1298          } elsif ($self->{next_input_character} == 0x0053 or # S
1299                   $self->{next_input_character} == 0x0073) { # s
1300            !!!next-input-character;
1301            if ($self->{next_input_character} == 0x0059 or # Y
1302                $self->{next_input_character} == 0x0079) { # y
1303              !!!next-input-character;
1304              if ($self->{next_input_character} == 0x0053 or # S
1305                  $self->{next_input_character} == 0x0073) { # s
1306                !!!next-input-character;
1307                if ($self->{next_input_character} == 0x0054 or # T
1308                    $self->{next_input_character} == 0x0074) { # t
1309                  !!!next-input-character;
1310                  if ($self->{next_input_character} == 0x0045 or # E
1311                      $self->{next_input_character} == 0x0065) { # e
1312                    !!!next-input-character;
1313                    if ($self->{next_input_character} == 0x004D or # M
1314                        $self->{next_input_character} == 0x006D) { # m
1315                      $self->{state} = 'before DOCTYPE system identifier';
1316                      !!!next-input-character;
1317                      redo A;
1318                    }
1319                  }
1320                }
1321              }
1322            }
1323    
1324            #
1325        } else {        } else {
1326          !!!parse-error (type => 'string after DOCTYPE name');          !!!next-input-character;
1327          $self->{current_token}->{error} = 1; # DOCTYPE          #
1328          }
1329    
1330          !!!parse-error (type => 'string after DOCTYPE name');
1331          $self->{state} = 'bogus DOCTYPE';
1332          # next-input-character is already done
1333          redo A;
1334        } elsif ($self->{state} eq 'before DOCTYPE public identifier') {
1335          if ({
1336                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1337                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1338              }->{$self->{next_input_character}}) {
1339            ## Stay in the state
1340            !!!next-input-character;
1341            redo A;
1342          } elsif ($self->{next_input_character} eq 0x0022) { # "
1343            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1344            $self->{state} = 'DOCTYPE public identifier (double-quoted)';
1345            !!!next-input-character;
1346            redo A;
1347          } elsif ($self->{next_input_character} eq 0x0027) { # '
1348            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1349            $self->{state} = 'DOCTYPE public identifier (single-quoted)';
1350            !!!next-input-character;
1351            redo A;
1352          } elsif ($self->{next_input_character} eq 0x003E) { # >
1353            !!!parse-error (type => 'no PUBLIC literal');
1354    
1355            $self->{state} = 'data';
1356            !!!next-input-character;
1357    
1358            delete $self->{current_token}->{correct};
1359            !!!emit ($self->{current_token}); # DOCTYPE
1360    
1361            redo A;
1362          } elsif ($self->{next_input_character} == -1) {
1363            !!!parse-error (type => 'unclosed DOCTYPE');
1364    
1365            $self->{state} = 'data';
1366            ## reconsume
1367    
1368            delete $self->{current_token}->{correct};
1369            !!!emit ($self->{current_token}); # DOCTYPE
1370    
1371            redo A;
1372          } else {
1373            !!!parse-error (type => 'string after PUBLIC');
1374            $self->{state} = 'bogus DOCTYPE';
1375            !!!next-input-character;
1376            redo A;
1377          }
1378        } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {
1379          if ($self->{next_input_character} == 0x0022) { # "
1380            $self->{state} = 'after DOCTYPE public identifier';
1381            !!!next-input-character;
1382            redo A;
1383          } elsif ($self->{next_input_character} == -1) {
1384            !!!parse-error (type => 'unclosed PUBLIC literal');
1385    
1386            $self->{state} = 'data';
1387            ## reconsume
1388    
1389            delete $self->{current_token}->{correct};
1390            !!!emit ($self->{current_token}); # DOCTYPE
1391    
1392            redo A;
1393          } else {
1394            $self->{current_token}->{public_identifier} # DOCTYPE
1395                .= chr $self->{next_input_character};
1396            ## Stay in the state
1397            !!!next-input-character;
1398            redo A;
1399          }
1400        } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {
1401          if ($self->{next_input_character} == 0x0027) { # '
1402            $self->{state} = 'after DOCTYPE public identifier';
1403            !!!next-input-character;
1404            redo A;
1405          } elsif ($self->{next_input_character} == -1) {
1406            !!!parse-error (type => 'unclosed PUBLIC literal');
1407    
1408            $self->{state} = 'data';
1409            ## reconsume
1410    
1411            delete $self->{current_token}->{correct};
1412            !!!emit ($self->{current_token}); # DOCTYPE
1413    
1414            redo A;
1415          } else {
1416            $self->{current_token}->{public_identifier} # DOCTYPE
1417                .= chr $self->{next_input_character};
1418            ## Stay in the state
1419            !!!next-input-character;
1420            redo A;
1421          }
1422        } elsif ($self->{state} eq 'after DOCTYPE public identifier') {
1423          if ({
1424                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1425                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1426              }->{$self->{next_input_character}}) {
1427            ## Stay in the state
1428            !!!next-input-character;
1429            redo A;
1430          } elsif ($self->{next_input_character} == 0x0022) { # "
1431            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1432            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1433            !!!next-input-character;
1434            redo A;
1435          } elsif ($self->{next_input_character} == 0x0027) { # '
1436            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1437            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1438            !!!next-input-character;
1439            redo A;
1440          } elsif ($self->{next_input_character} == 0x003E) { # >
1441            $self->{state} = 'data';
1442            !!!next-input-character;
1443    
1444            !!!emit ($self->{current_token}); # DOCTYPE
1445    
1446            redo A;
1447          } elsif ($self->{next_input_character} == -1) {
1448            !!!parse-error (type => 'unclosed DOCTYPE');
1449    
1450            $self->{state} = 'data';
1451            ## reconsume
1452    
1453            delete $self->{current_token}->{correct};
1454            !!!emit ($self->{current_token}); # DOCTYPE
1455    
1456            redo A;
1457          } else {
1458            !!!parse-error (type => 'string after PUBLIC literal');
1459            $self->{state} = 'bogus DOCTYPE';
1460            !!!next-input-character;
1461            redo A;
1462          }
1463        } elsif ($self->{state} eq 'before DOCTYPE system identifier') {
1464          if ({
1465                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1466                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1467              }->{$self->{next_input_character}}) {
1468            ## Stay in the state
1469            !!!next-input-character;
1470            redo A;
1471          } elsif ($self->{next_input_character} == 0x0022) { # "
1472            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1473            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1474            !!!next-input-character;
1475            redo A;
1476          } elsif ($self->{next_input_character} == 0x0027) { # '
1477            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1478            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1479            !!!next-input-character;
1480            redo A;
1481          } elsif ($self->{next_input_character} == 0x003E) { # >
1482            !!!parse-error (type => 'no SYSTEM literal');
1483            $self->{state} = 'data';
1484            !!!next-input-character;
1485    
1486            delete $self->{current_token}->{correct};
1487            !!!emit ($self->{current_token}); # DOCTYPE
1488    
1489            redo A;
1490          } elsif ($self->{next_input_character} == -1) {
1491            !!!parse-error (type => 'unclosed DOCTYPE');
1492    
1493            $self->{state} = 'data';
1494            ## reconsume
1495    
1496            delete $self->{current_token}->{correct};
1497            !!!emit ($self->{current_token}); # DOCTYPE
1498    
1499            redo A;
1500          } else {
1501            !!!parse-error (type => 'string after SYSTEM');
1502            $self->{state} = 'bogus DOCTYPE';
1503            !!!next-input-character;
1504            redo A;
1505          }
1506        } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {
1507          if ($self->{next_input_character} == 0x0022) { # "
1508            $self->{state} = 'after DOCTYPE system identifier';
1509            !!!next-input-character;
1510            redo A;
1511          } elsif ($self->{next_input_character} == -1) {
1512            !!!parse-error (type => 'unclosed SYSTEM literal');
1513    
1514            $self->{state} = 'data';
1515            ## reconsume
1516    
1517            delete $self->{current_token}->{correct};
1518            !!!emit ($self->{current_token}); # DOCTYPE
1519    
1520            redo A;
1521          } else {
1522            $self->{current_token}->{system_identifier} # DOCTYPE
1523                .= chr $self->{next_input_character};
1524            ## Stay in the state
1525            !!!next-input-character;
1526            redo A;
1527          }
1528        } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {
1529          if ($self->{next_input_character} == 0x0027) { # '
1530            $self->{state} = 'after DOCTYPE system identifier';
1531            !!!next-input-character;
1532            redo A;
1533          } elsif ($self->{next_input_character} == -1) {
1534            !!!parse-error (type => 'unclosed SYSTEM literal');
1535    
1536            $self->{state} = 'data';
1537            ## reconsume
1538    
1539            delete $self->{current_token}->{correct};
1540            !!!emit ($self->{current_token}); # DOCTYPE
1541    
1542            redo A;
1543          } else {
1544            $self->{current_token}->{system_identifier} # DOCTYPE
1545                .= chr $self->{next_input_character};
1546            ## Stay in the state
1547            !!!next-input-character;
1548            redo A;
1549          }
1550        } elsif ($self->{state} eq 'after DOCTYPE system identifier') {
1551          if ({
1552                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1553                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1554              }->{$self->{next_input_character}}) {
1555            ## Stay in the state
1556            !!!next-input-character;
1557            redo A;
1558          } elsif ($self->{next_input_character} == 0x003E) { # >
1559            $self->{state} = 'data';
1560            !!!next-input-character;
1561    
1562            !!!emit ($self->{current_token}); # DOCTYPE
1563    
1564            redo A;
1565          } elsif ($self->{next_input_character} == -1) {
1566            !!!parse-error (type => 'unclosed DOCTYPE');
1567    
1568            $self->{state} = 'data';
1569            ## reconsume
1570    
1571            delete $self->{current_token}->{correct};
1572            !!!emit ($self->{current_token}); # DOCTYPE
1573    
1574            redo A;
1575          } else {
1576            !!!parse-error (type => 'string after SYSTEM literal');
1577          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1578          !!!next-input-character;          !!!next-input-character;
1579          redo A;          redo A;
# Line 1467  sub _get_next_token ($) { Line 1583  sub _get_next_token ($) {
1583          $self->{state} = 'data';          $self->{state} = 'data';
1584          !!!next-input-character;          !!!next-input-character;
1585    
1586            delete $self->{current_token}->{correct};
1587          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1588    
1589          redo A;          redo A;
1590        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1476  sub _get_next_token ($) { Line 1592  sub _get_next_token ($) {
1592          $self->{state} = 'data';          $self->{state} = 'data';
1593          ## reconsume          ## reconsume
1594    
1595            delete $self->{current_token}->{correct};
1596          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1597    
1598          redo A;          redo A;
1599        } else {        } else {
# Line 1493  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1610  } # _get_next_token  } # _get_next_token
1611    
1612  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1613    my $self = shift;    my ($self, $in_attr) = @_;
1614      
1615    if ($self->{next_input_character} == 0x0023) { # #    if ({
1616           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1617           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1618          }->{$self->{next_input_character}}) {
1619        ## Don't consume
1620        ## No error
1621        return undef;
1622      } elsif ($self->{next_input_character} == 0x0023) { # #
1623      !!!next-input-character;      !!!next-input-character;
1624      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1625          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1626        my $num;        my $code;
1627        X: {        X: {
1628          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1629          !!!next-input-character;          !!!next-input-character;
1630          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1631              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1632            $num ||= 0;            $code ||= 0;
1633            $num *= 0x10;            $code *= 0x10;
1634            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1635            redo X;            redo X;
1636          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1637                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1638            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1639            $num ||= 0;            $code *= 0x10;
1640            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1641            redo X;            redo X;
1642          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1643                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1644            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1645            $num ||= 0;            $code *= 0x10;
1646            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1647            redo X;            redo X;
1648          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1649            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1650              !!!back-next-input-character ($x_char, $self->{next_input_character});
1651            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1652            return undef;            return undef;
1653          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1654            !!!next-input-character;            !!!next-input-character;
# Line 1535  sub _tokenize_attempt_to_consume_an_enti Line 1656  sub _tokenize_attempt_to_consume_an_enti
1656            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1657          }          }
1658    
1659          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1660          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1661          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1662            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1663            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1664          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1665            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1666            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1667              $code = 0x000A;
1668            } elsif (0x80 <= $code and $code <= 0x9F) {
1669              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1670              $code = $c1_entity_char->{$code};
1671          }          }
1672    
1673          return {type => 'character', data => chr $num};          return {type => 'character', data => chr $code};
1674        } # X        } # X
1675      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1676               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1566  sub _tokenize_attempt_to_consume_an_enti Line 1691  sub _tokenize_attempt_to_consume_an_enti
1691          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1692        }        }
1693    
1694        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1695        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1696          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1697          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1698            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1699            $code = 0xFFFD;
1700          } elsif ($code == 0x000D) {
1701            !!!parse-error (type => 'CR character reference');
1702            $code = 0x000A;
1703        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1704          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1705          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1706        }        }
1707                
# Line 1590  sub _tokenize_attempt_to_consume_an_enti Line 1720  sub _tokenize_attempt_to_consume_an_enti
1720      !!!next-input-character;      !!!next-input-character;
1721    
1722      my $value = $entity_name;      my $value = $entity_name;
1723      my $match;      my $match = 0;
1724        require Whatpm::_NamedEntityList;
1725        our $EntityChar;
1726    
1727      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1728             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1729             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1730               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1731              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1732               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1733              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1734               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1735                $self->{next_input_character} == 0x003B)) { # ;
1736        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1737        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1738          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1739          $match = 1;            $value = $EntityChar->{$entity_name};
1740              $match = 1;
1741              !!!next-input-character;
1742              last;
1743            } else {
1744              $value = $EntityChar->{$entity_name};
1745              $match = -1;
1746              !!!next-input-character;
1747            }
1748        } else {        } else {
1749          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1750            $match *= 2;
1751            !!!next-input-character;
1752        }        }
       !!!next-input-character;  
1753      }      }
1754            
1755      if ($match) {      if ($match > 0) {
1756        if ($self->{next_input_character} == 0x003B) { # ;        return {type => 'character', data => $value};
1757          !!!next-input-character;      } elsif ($match < 0) {
1758          !!!parse-error (type => 'no refc');
1759          if ($in_attr and $match < -1) {
1760            return {type => 'character', data => '&'.$entity_name};
1761        } else {        } else {
1762          !!!parse-error (type => 'refc');          return {type => 'character', data => $value};
1763        }        }
   
       return {type => 'character', data => $value};  
1764      } else {      } else {
1765        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1766        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1767        !!!back-token ({type => 'character', data => $value});        return {type => 'character', data => '&'.$value};
       return undef;  
1768      }      }
1769    } else {    } else {
1770      ## no characters are consumed      ## no characters are consumed
# Line 1637  sub _initialize_tree_constructor ($) { Line 1779  sub _initialize_tree_constructor ($) {
1779    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1780    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1781    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1782    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1783  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1784    
1785  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1677  sub _construct_tree ($) { Line 1819  sub _construct_tree ($) {
1819    
1820  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1821    my $self = shift;    my $self = shift;
1822    B: {    INITIAL: {
1823        if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} eq 'DOCTYPE') {
1824          if ($token->{error}) {        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1825            ## ISSUE: Spec currently left this case undefined.        ## error, switch to a conformance checking mode for another
1826            !!!parse-error (type => 'bogus DOCTYPE');        ## language.
1827          }        my $doctype_name = $token->{name};
1828          my $doctype = $self->{document}->create_document_type_definition        $doctype_name = '' unless defined $doctype_name;
1829            ($token->{name});        $doctype_name =~ tr/a-z/A-Z/;
1830          $self->{document}->append_child ($doctype);        if (not defined $token->{name} or # <!DOCTYPE>
1831          #$phase = 'root element';            defined $token->{public_identifier} or
1832          !!!next-token;            defined $token->{system_identifier}) {
1833          #redo B;          !!!parse-error (type => 'not HTML5');
1834          return;        } elsif ($doctype_name ne 'HTML') {
1835        } elsif ({          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1836                  comment => 1,          !!!parse-error (type => 'not HTML5');
1837                  'start tag' => 1,        }
1838                  'end tag' => 1,        
1839                  'end-of-file' => 1,        my $doctype = $self->{document}->create_document_type_definition
1840                 }->{$token->{type}}) {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1841          ## ISSUE: Spec currently left this case undefined.        $doctype->public_id ($token->{public_identifier})
1842          !!!parse-error (type => 'missing DOCTYPE');            if defined $token->{public_identifier};
1843          #$phase = 'root element';        $doctype->system_id ($token->{system_identifier})
1844          ## reprocess            if defined $token->{system_identifier};
1845          #redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
1846          return;        ## ISSUE: internalSubset = null??
1847        } elsif ($token->{type} eq 'character') {        $self->{document}->append_child ($doctype);
1848          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        
1849            $self->{document}->manakai_append_text ($1);        if (not $token->{correct} or $doctype_name ne 'HTML') {
1850            ## ISSUE: DOM3 Core does not allow Document > Text          $self->{document}->manakai_compat_mode ('quirks');
1851            unless (length $token->{data}) {        } elsif (defined $token->{public_identifier}) {
1852              ## Stay in the phase          my $pubid = $token->{public_identifier};
1853              !!!next-token;          $pubid =~ tr/a-z/A-z/;
1854              redo B;          if ({
1855              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1856              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1857              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1858              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1859              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1860              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1861              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1862              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1863              "-//IETF//DTD HTML 2.0//EN" => 1,
1864              "-//IETF//DTD HTML 2.1E//EN" => 1,
1865              "-//IETF//DTD HTML 3.0//EN" => 1,
1866              "-//IETF//DTD HTML 3.0//EN//" => 1,
1867              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1868              "-//IETF//DTD HTML 3.2//EN" => 1,
1869              "-//IETF//DTD HTML 3//EN" => 1,
1870              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1871              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1872              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1873              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1874              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1875              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1876              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1877              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1878              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1879              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1880              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1881              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1882              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1883              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1884              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1885              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1886              "-//IETF//DTD HTML STRICT//EN" => 1,
1887              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1888              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1889              "-//IETF//DTD HTML//EN" => 1,
1890              "-//IETF//DTD HTML//EN//2.0" => 1,
1891              "-//IETF//DTD HTML//EN//3.0" => 1,
1892              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1893              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1894              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1895              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1896              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1897              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1898              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1899              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1900              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1901              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1902              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1903              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1904              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1905              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1906              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1907              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1908              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1909              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1910              "-//W3C//DTD HTML 3.2//EN" => 1,
1911              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1912              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
1913              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
1914              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
1915              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
1916              "-//W3C//DTD W3 HTML//EN" => 1,
1917              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
1918              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
1919              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
1920              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
1921              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
1922              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
1923              "HTML" => 1,
1924            }->{$pubid}) {
1925              $self->{document}->manakai_compat_mode ('quirks');
1926            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
1927                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
1928              if (defined $token->{system_identifier}) {
1929                $self->{document}->manakai_compat_mode ('quirks');
1930              } else {
1931                $self->{document}->manakai_compat_mode ('limited quirks');
1932            }            }
1933            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
1934                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
1935              $self->{document}->manakai_compat_mode ('limited quirks');
1936            }
1937          }
1938          if (defined $token->{system_identifier}) {
1939            my $sysid = $token->{system_identifier};
1940            $sysid =~ tr/A-Z/a-z/;
1941            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1942              $self->{document}->manakai_compat_mode ('quirks');
1943            }
1944          }
1945          
1946          ## Go to the root element phase.
1947          !!!next-token;
1948          return;
1949        } elsif ({
1950                  'start tag' => 1,
1951                  'end tag' => 1,
1952                  'end-of-file' => 1,
1953                 }->{$token->{type}}) {
1954          !!!parse-error (type => 'no DOCTYPE');
1955          $self->{document}->manakai_compat_mode ('quirks');
1956          ## Go to the root element phase
1957          ## reprocess
1958          return;
1959        } elsif ($token->{type} eq 'character') {
1960          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1961            ## Ignore the token
1962    
1963            unless (length $token->{data}) {
1964              ## Stay in the phase
1965              !!!next-token;
1966              redo INITIAL;
1967          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
1968        }        }
1969      } # B  
1970          !!!parse-error (type => 'no DOCTYPE');
1971          $self->{document}->manakai_compat_mode ('quirks');
1972          ## Go to the root element phase
1973          ## reprocess
1974          return;
1975        } elsif ($token->{type} eq 'comment') {
1976          my $comment = $self->{document}->create_comment ($token->{data});
1977          $self->{document}->append_child ($comment);
1978          
1979          ## Stay in the phase.
1980          !!!next-token;
1981          redo INITIAL;
1982        } else {
1983          die "$0: $token->{type}: Unknown token";
1984        }
1985      } # INITIAL
1986  } # _tree_construction_initial  } # _tree_construction_initial
1987    
1988  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
# Line 1741  sub _tree_construction_root_element ($) Line 2002  sub _tree_construction_root_element ($)
2002          !!!next-token;          !!!next-token;
2003          redo B;          redo B;
2004        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'character') {
2005          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2006            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2007            ## ISSUE: DOM3 Core does not allow Document > Text  
2008            unless (length $token->{data}) {            unless (length $token->{data}) {
2009              ## Stay in the phase              ## Stay in the phase
2010              !!!next-token;              !!!next-token;
# Line 1764  sub _tree_construction_root_element ($) Line 2025  sub _tree_construction_root_element ($)
2025        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2026        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2027        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2028        ## reprocess        ## reprocess
2029        #redo B;        #redo B;
2030        return;        return; ## Go to the main phase.
2031    } # B    } # B
2032  } # _tree_construction_root_element  } # _tree_construction_root_element
2033    
# Line 1783  sub _reset_insertion_mode ($) { Line 2043  sub _reset_insertion_mode ($) {
2043            
2044      ## Step 3      ## Step 3
2045      S3: {      S3: {
2046        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2047        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2048          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2049              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2050            #        ## The second "if" is in the scope of the first "if"!?
2051          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2052            $node = $self->{inner_html_node};          $last = 1;
2053            if (defined $self->{inner_html_node}) {
2054              if ($self->{inner_html_node}->[1] eq 'td' or
2055                  $self->{inner_html_node}->[1] eq 'th') {
2056                #
2057              } else {
2058                $node = $self->{inner_html_node};
2059              }
2060          }          }
2061        }        }
2062            
# Line 1800  sub _reset_insertion_mode ($) { Line 2067  sub _reset_insertion_mode ($) {
2067                        th => 'in cell',                        th => 'in cell',
2068                        tr => 'in row',                        tr => 'in row',
2069                        tbody => 'in table body',                        tbody => 'in table body',
2070                        thead => 'in table head',                        thead => 'in table body',
2071                        tfoot => 'in table foot',                        tfoot => 'in table body',
2072                        caption => 'in caption',                        caption => 'in caption',
2073                        colgroup => 'in column group',                        colgroup => 'in column group',
2074                        table => 'in table',                        table => 'in table',
# Line 1836  sub _reset_insertion_mode ($) { Line 2103  sub _reset_insertion_mode ($) {
2103  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2104    my $self = shift;    my $self = shift;
2105    
   my $phase = 'main';  
   
2106    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2107    
2108    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1920  sub _tree_construction_main ($) { Line 2185  sub _tree_construction_main ($) {
2185      }      }
2186    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2187    
2188    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2189      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2190      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2191      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2192       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2193        ->append_child ($style_el);      my $el;
2194      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2195                  
2196        ## Step 2
2197        $insert->($el); # /context node/->append_child ($el)
2198    
2199        ## Step 3
2200        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2201        delete $self->{escape}; # MUST
2202    
2203        ## Step 4
2204      my $text = '';      my $text = '';
2205      !!!next-token;      !!!next-token;
2206      while ($token->{type} eq 'character') {      while ($token->{type} eq 'character') { # or until stop tokenizing
2207        $text .= $token->{data};        $text .= $token->{data};
2208        !!!next-token;        !!!next-token;
2209      } # stop if non-character token or tokenizer stops tokenising      }
2210    
2211        ## Step 5
2212      if (length $text) {      if (length $text) {
2213        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2214          $el->append_child ($text);
2215      }      }
2216        
2217      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2218                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2219      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2220        ## Step 7
2221        if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2222        ## Ignore the token        ## Ignore the token
2223      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2224        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2225        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2226          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2227        } else {
2228          die "$0: $content_model_flag in parse_rcdata";
2229      }      }
2230      !!!next-token;      !!!next-token;
2231    }; # $style_start_tag    }; # $parse_rcdata
2232    
2233    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2234        my $insert = $_[0];
2235      my $script_el;      my $script_el;
2236      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2237      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2238    
2239      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2240        delete $self->{escape}; # MUST
2241            
2242      my $text = '';      my $text = '';
2243      !!!next-token;      !!!next-token;
# Line 1966  sub _tree_construction_main ($) { Line 2249  sub _tree_construction_main ($) {
2249        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2250      }      }
2251                                
2252      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2253    
2254      if ($token->{type} eq 'end tag' and      if ($token->{type} eq 'end tag' and
2255          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
# Line 1982  sub _tree_construction_main ($) { Line 2265  sub _tree_construction_main ($) {
2265      } else {      } else {
2266        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2267        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2268          
2269        (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})        $insert->($script_el);
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
2270                
2271        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2272                
# Line 2178  sub _tree_construction_main ($) { Line 2460  sub _tree_construction_main ($) {
2460    }; # $formatting_end_tag    }; # $formatting_end_tag
2461    
2462    my $insert_to_current = sub {    my $insert_to_current = sub {
2463      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2464    }; # $insert_to_current    }; # $insert_to_current
2465    
2466    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2212  sub _tree_construction_main ($) { Line 2494  sub _tree_construction_main ($) {
2494                         }                         }
2495    }; # $insert_to_foster    }; # $insert_to_foster
2496    
2497    my $in_body = sub {    my $insert;
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         $script_start_tag->();  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         $style_start_tag->();  
         return;  
       } elsif ({  
                 base => 1, link => 1, meta => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## NOTE: This is an "as if in head" code clone  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
         if (defined $self->{head_element}) {  
           $self->{head_element}->append_child ($el);  
         } else {  
           $insert->($el);  
         }  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: There is an "as if in head" code clone  
         my $title_el;  
         !!!create-element ($title_el, 'title', $token->{attributes});  
         (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
           ->append_child ($title_el);  
         $self->{content_model_flag} = 'RCDATA';  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
         }  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
           
         $insert->($el);  
           
         my $text = '';  
         if ($token->{tag_name} eq 'textarea') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 form => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         undef $self->{form_element} if $token->{tag_name} eq 'form';  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
2498    
2499    B: {    B: {
2500      if ($phase eq 'main') {      if ($token->{type} eq 'DOCTYPE') {
2501        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
2502          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
2503          ## Ignore the token        ## Stay in the phase
2504          ## Stay in the phase        !!!next-token;
2505          !!!next-token;        redo B;
2506          redo B;      } elsif ($token->{type} eq 'end-of-file') {
2507        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} eq 'after html body' or
2508                 $token->{tag_name} eq 'html') {            $self->{insertion_mode} eq 'after html frameset') {
2509          ## TODO: unless it is the first start tag token, parse-error          #
2510          my $top_el = $self->{open_elements}->[0]->[0];        } else {
         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') {  
2511          ## Generate implied end tags          ## Generate implied end tags
2512          if ({          if ({
2513               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2514                 tbody => 1, tfoot=> 1, thead => 1,
2515              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2516            !!!back-token;            !!!back-token;
2517            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
# Line 3012  sub _tree_construction_main ($) { Line 2527  sub _tree_construction_main ($) {
2527            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2528          }          }
2529    
         ## Stop parsing  
         last B;  
   
2530          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
2531          }
2532    
2533          ## Stop parsing
2534          last B;
2535        } elsif ($token->{type} eq 'start tag' and
2536                 $token->{tag_name} eq 'html') {
2537          if ($self->{insertion_mode} eq 'after html body') {
2538            ## Turn into the main phase
2539            !!!parse-error (type => 'after html:html');
2540            $self->{insertion_mode} = 'after body';
2541          } elsif ($self->{insertion_mode} eq 'after html frameset') {
2542            ## Turn into the main phase
2543            !!!parse-error (type => 'after html:html');
2544            $self->{insertion_mode} = 'after frameset';
2545          }
2546    
2547    ## ISSUE: "aa<html>" is not a parse error.
2548    ## ISSUE: "<html>" in fragment is not a parse error.
2549          unless ($token->{first_start_tag}) {
2550            !!!parse-error (type => 'not first start tag');
2551          }
2552          my $top_el = $self->{open_elements}->[0]->[0];
2553          for my $attr_name (keys %{$token->{attributes}}) {
2554            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2555              $top_el->set_attribute_ns
2556                (undef, [undef, $attr_name],
2557                 $token->{attributes}->{$attr_name}->{value});
2558            }
2559          }
2560          !!!next-token;
2561          redo B;
2562        } elsif ($token->{type} eq 'comment') {
2563          my $comment = $self->{document}->create_comment ($token->{data});
2564          if ($self->{insertion_mode} eq 'after html body' or
2565              $self->{insertion_mode} eq 'after html frameset') {
2566            $self->{document}->append_child ($comment);
2567          } elsif ($self->{insertion_mode} eq 'after body') {
2568            $self->{open_elements}->[0]->[0]->append_child ($comment);
2569        } else {        } else {
2570            $self->{open_elements}->[-1]->[0]->append_child ($comment);
2571          }
2572          !!!next-token;
2573          redo B;
2574        } elsif ($self->{insertion_mode} eq 'in head' or
2575                 $self->{insertion_mode} eq 'in head noscript' or
2576                 $self->{insertion_mode} eq 'after head' or
2577                 $self->{insertion_mode} eq 'before head') {
2578          if ($token->{type} eq 'character') {
2579            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2580              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2581              unless (length $token->{data}) {
2582                !!!next-token;
2583                redo B;
2584              }
2585            }
2586    
2587          if ($self->{insertion_mode} eq 'before head') {          if ($self->{insertion_mode} eq 'before head') {
2588            if ($token->{type} eq 'character') {            ## As if <head>
2589              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            !!!create-element ($self->{head_element}, 'head');
2590                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2591                unless (length $token->{data}) {            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2592                  !!!next-token;  
2593                  redo B;            ## Reprocess in the "in head" insertion mode...
2594                }            pop @{$self->{open_elements}};
2595              }  
2596              ## As if <head>            ## Reprocess in the "after head" insertion mode...
2597              !!!create-element ($self->{head_element}, 'head');          } elsif ($self->{insertion_mode} eq 'in head noscript') {
2598              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if </noscript>
2599              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            pop @{$self->{open_elements}};
2600              $self->{insertion_mode} = 'in head';            !!!parse-error (type => 'in noscript:#character');
2601              
2602              ## Reprocess in the "in head" insertion mode...
2603              ## As if </head>
2604              pop @{$self->{open_elements}};
2605    
2606              ## Reprocess in the "after head" insertion mode...
2607            } elsif ($self->{insertion_mode} eq 'in head') {
2608              pop @{$self->{open_elements}};
2609    
2610              ## Reprocess in the "after head" insertion mode...
2611            }
2612    
2613                ## "after head" insertion mode
2614                ## As if <body>
2615                !!!insert-element ('body');
2616                $self->{insertion_mode} = 'in body';
2617              ## reprocess              ## reprocess
2618              redo B;              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;  
2619            } elsif ($token->{type} eq 'start tag') {            } 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';  
2620              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2621                !!!next-token;                if ($self->{insertion_mode} eq 'before head') {
2622              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2623              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2624              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2625              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = 'in head';
2626              #  ## reprocess                  !!!next-token;
2627              } else {                  redo B;
2628                ## reprocess                } elsif ($self->{insertion_mode} ne 'after head') {
2629              }                  !!!parse-error (type => 'in head:head'); # or in head noscript
2630              redo B;                  ## Ignore the token
2631            } elsif ($token->{type} eq 'end tag') {                  !!!next-token;
2632              if ($token->{tag_name} eq 'html') {                  redo B;
2633                  } else {
2634                    #
2635                  }
2636                } elsif ($self->{insertion_mode} eq 'before head') {
2637                ## As if <head>                ## As if <head>
2638                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2639                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2640                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2641    
2642                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = 'in head';
2643                ## reprocess                ## Reprocess in the "in head" insertion mode...
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
2644              }              }
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in 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 ($token->{tag_name} eq 'title') {  
               ## NOTE: There is an "as if in head" code clone  
               my $title_el;  
               !!!create-element ($title_el, 'title', $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($title_el);  
               $self->{content_model_flag} = 'RCDATA';  
2645    
2646                my $text = '';              if ($token->{tag_name} eq 'base') {
2647                !!!next-token;                if ($self->{insertion_mode} eq 'in head noscript') {
2648                while ($token->{type} eq 'character') {                  ## As if </noscript>
2649                  $text .= $token->{data};                  pop @{$self->{open_elements}};
2650                  !!!next-token;                  !!!parse-error (type => 'in noscript:base');
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
2651                                
2652                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = 'in head';
2653                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
               } else {  
                 !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
                 ## ISSUE: And ignore?  
2654                }                }
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
2655    
2656                  ## NOTE: There is a "as if in head" code clone.
2657                  if ($self->{insertion_mode} eq 'after head') {
2658                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2659                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2660                  }
2661                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2662                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2663                  pop @{$self->{open_elements}}
2664                      if $self->{insertion_mode} eq 'after head';
2665                !!!next-token;                !!!next-token;
2666                redo B;                redo B;
2667              } elsif ($token->{tag_name} eq 'head') {              } elsif ($token->{tag_name} eq 'link') {
2668                !!!parse-error (type => 'in head:head');                ## NOTE: There is a "as if in head" code clone.
2669                ## Ignore the token                if ($self->{insertion_mode} eq 'after head') {
2670                !!!next-token;                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2671                redo B;                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{open_elements}->[-1]->[1] eq 'head') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:head');  
2672                }                }
2673                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes});
2674                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2675                  pop @{$self->{open_elements}}
2676                      if $self->{insertion_mode} eq 'after head';
2677                !!!next-token;                !!!next-token;
2678                redo B;                redo B;
2679              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'meta') {
2680                #                ## NOTE: There is a "as if in head" code clone.
2681              } else {                if ($self->{insertion_mode} eq 'after head') {
2682                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2683                ## Ignore the token                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2684                  }
2685                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2686                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2687    
2688                  unless ($self->{confident}) {
2689                    my $charset;
2690                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2691                      $charset = $token->{attributes}->{charset}->{value};
2692                    }
2693                    if ($token->{attributes}->{'http-equiv'}) {
2694                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2695                      if ($token->{attributes}->{'http-equiv'}->{value}
2696                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2697                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2698                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2699                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2700                      } ## TODO: And if supported
2701                    }
2702                    ## TODO: Change the encoding
2703                  }
2704    
2705                  ## TODO: Extracting |charset| from |meta|.
2706                  pop @{$self->{open_elements}}
2707                      if $self->{insertion_mode} eq 'after head';
2708                !!!next-token;                !!!next-token;
2709                redo B;                redo B;
2710              }              } elsif ($token->{tag_name} eq 'title') {
2711            } else {                if ($self->{insertion_mode} eq 'in head noscript') {
2712              #                  ## As if </noscript>
2713            }                  pop @{$self->{open_elements}};
2714                    !!!parse-error (type => 'in noscript:title');
2715            if ($self->{open_elements}->[-1]->[1] eq 'head') {                
2716              ## As if </head>                  $self->{insertion_mode} = 'in head';
2717              pop @{$self->{open_elements}};                  ## Reprocess in the "in head" insertion mode...
2718            }                } elsif ($self->{insertion_mode} eq 'after head') {
2719            $self->{insertion_mode} = 'after head';                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2720            ## reprocess                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2721            redo B;                }
2722    
2723            ## ISSUE: An issue in the spec.                ## NOTE: There is a "as if in head" code clone.
2724          } elsif ($self->{insertion_mode} eq 'after head') {                my $parent = defined $self->{head_element} ? $self->{head_element}
2725            if ($token->{type} eq 'character') {                    : $self->{open_elements}->[-1]->[0];
2726              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2727                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                                sub { $parent->append_child ($_[0]) });
2728                unless (length $token->{data}) {                pop @{$self->{open_elements}}
2729                      if $self->{insertion_mode} eq 'after head';
2730                  redo B;
2731                } elsif ($token->{tag_name} eq 'style') {
2732                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2733                  ## insertion mode 'in head')
2734                  ## NOTE: There is a "as if in head" code clone.
2735                  if ($self->{insertion_mode} eq 'after head') {
2736                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2737                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2738                  }
2739                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2740                  pop @{$self->{open_elements}}
2741                      if $self->{insertion_mode} eq 'after head';
2742                  redo B;
2743                } elsif ($token->{tag_name} eq 'noscript') {
2744                  if ($self->{insertion_mode} eq 'in head') {
2745                    ## NOTE: and scripting is disalbed
2746                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2747                    $self->{insertion_mode} = 'in head noscript';
2748                  !!!next-token;                  !!!next-token;
2749                  redo B;                  redo B;
2750                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
2751                    !!!parse-error (type => 'in noscript:noscript');
2752                    ## Ignore the token
2753                    !!!next-token;
2754                    redo B;
2755                  } else {
2756                    #
2757                }                }
2758              }              } elsif ($token->{tag_name} eq 'script') {
2759                              if ($self->{insertion_mode} eq 'in head noscript') {
2760              #                  ## As if </noscript>
2761            } elsif ($token->{type} eq 'comment') {                  pop @{$self->{open_elements}};
2762              my $comment = $self->{document}->create_comment ($token->{data});                  !!!parse-error (type => 'in noscript:script');
2763              $self->{open_elements}->[-1]->[0]->append_child ($comment);                
2764              !!!next-token;                  $self->{insertion_mode} = 'in head';
2765              redo B;                  ## Reprocess in the "in head" insertion mode...
2766            } elsif ($token->{type} eq 'start tag') {                } elsif ($self->{insertion_mode} eq 'after head') {
2767              if ($token->{tag_name} eq 'body') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2768                !!!insert-element ('body', $token->{attributes});                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2769                $self->{insertion_mode} = 'in body';                }
2770                !!!next-token;  
2771                redo B;                ## NOTE: There is a "as if in head" code clone.
2772              } elsif ($token->{tag_name} eq 'frameset') {                $script_start_tag->($insert_to_current);
2773                !!!insert-element ('frameset', $token->{attributes});                pop @{$self->{open_elements}}
2774                $self->{insertion_mode} = 'in frameset';                    if $self->{insertion_mode} eq 'after head';
2775                  redo B;
2776                } elsif ($token->{tag_name} eq 'body' or
2777                         $token->{tag_name} eq 'frameset') {
2778                  if ($self->{insertion_mode} eq 'in head noscript') {
2779                    ## As if </noscript>
2780                    pop @{$self->{open_elements}};
2781                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2782                    
2783                    ## Reprocess in the "in head" insertion mode...
2784                    ## As if </head>
2785                    pop @{$self->{open_elements}};
2786                    
2787                    ## Reprocess in the "after head" insertion mode...
2788                  } elsif ($self->{insertion_mode} eq 'in head') {
2789                    pop @{$self->{open_elements}};
2790                    
2791                    ## Reprocess in the "after head" insertion mode...
2792                  }
2793    
2794                  ## "after head" insertion mode
2795                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2796                  $self->{insertion_mode} = 'in '.$token->{tag_name};
2797                !!!next-token;                !!!next-token;
2798                redo B;                redo B;
             } elsif ({  
                       base => 1, link => 1, meta => 1,  
                       script => 1, style => 1, title => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'after head:'.$token->{tag_name});  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
2799              } else {              } else {
2800                #                #
2801              }              }
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } 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});  
2802    
2803              !!!next-token;              if ($self->{insertion_mode} eq 'in head noscript') {
2804              redo B;                ## As if </noscript>
2805            } elsif ($token->{type} eq 'comment') {                pop @{$self->{open_elements}};
2806              ## NOTE: There is a code clone of "comment in body".                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
             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);  
2807                                
2808                unless (length $token->{data}) {                ## Reprocess in the "in head" insertion mode...
2809                  !!!next-token;                ## As if </head>
2810                  redo B;                pop @{$self->{open_elements}};
               }  
             }  
2811    
2812              !!!parse-error (type => 'in table:#character');                ## Reprocess in the "after head" insertion mode...
2813                } elsif ($self->{insertion_mode} eq 'in head') {
2814                  ## As if </head>
2815                  pop @{$self->{open_elements}};
2816    
2817              ## As if in body, but insert into foster parent element                ## Reprocess in the "after head" insertion mode...
             ## 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});  
2818              }              }
               
             !!!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}};  
               }  
2819    
2820                push @$active_formatting_elements, ['#marker', '']              ## "after head" insertion mode
2821                  if $token->{tag_name} eq 'caption';              ## As if <body>
2822                !!!insert-element ('body');
2823                $self->{insertion_mode} = 'in body';
2824                ## reprocess
2825                redo B;
2826              } elsif ($token->{type} eq 'end tag') {
2827                if ($token->{tag_name} eq 'head') {
2828                  if ($self->{insertion_mode} eq 'before head') {
2829                    ## As if <head>
2830                    !!!create-element ($self->{head_element}, 'head');
2831                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2832                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2833    
2834                !!!insert-element ($token->{tag_name}, $token->{attributes});                  ## Reprocess in the "in head" insertion mode...
2835                $self->{insertion_mode} = {                  pop @{$self->{open_elements}};
2836                                   caption => 'in caption',                  $self->{insertion_mode} = 'after head';
2837                                   colgroup => 'in column group',                  !!!next-token;
2838                                   tbody => 'in table body',                  redo B;
2839                                   tfoot => 'in table body',                } elsif ($self->{insertion_mode} eq 'in head noscript') {
2840                                   thead => 'in table body',                  ## As if </noscript>
2841                                  }->{$token->{tag_name}};                  pop @{$self->{open_elements}};
2842                !!!next-token;                  !!!parse-error (type => 'in noscript:script');
2843                redo B;                  
2844              } elsif ({                  ## Reprocess in the "in head" insertion mode...
                       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]);  
2845                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2846                    $self->{insertion_mode} = 'after head';
2847                    !!!next-token;
2848                    redo B;
2849                  } elsif ($self->{insertion_mode} eq 'in head') {
2850                    pop @{$self->{open_elements}};
2851                    $self->{insertion_mode} = 'after head';
2852                    !!!next-token;
2853                    redo B;
2854                  } else {
2855                    #
2856                }                }
2857                } elsif ($token->{tag_name} eq 'noscript') {
2858                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                if ($self->{insertion_mode} eq 'in head noscript') {
2859                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  pop @{$self->{open_elements}};
2860                  ? 'in column group' : 'in table body';                  $self->{insertion_mode} = 'in head';
               ## 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>  
2861                  !!!next-token;                  !!!next-token;
2862                  redo B;                  redo B;
2863                  } elsif ($self->{insertion_mode} eq 'before head') {
2864                    !!!parse-error (type => 'unmatched end tag:noscript');
2865                    ## Ignore the token ## ISSUE: An issue in the spec.
2866                    !!!next-token;
2867                    redo B;
2868                  } else {
2869                    #
2870                }                }
2871                              } elsif ({
2872                ## generate implied end tags                        body => 1, html => 1,
2873                if ({                       }->{$token->{tag_name}}) {
2874                     dd => 1, dt => 1, li => 1, p => 1,                if ($self->{insertion_mode} eq 'before head') {
2875                     td => 1, th => 1, tr => 1,                  ## As if <head>
2876                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!create-element ($self->{head_element}, 'head');
2877                  !!!back-token; # <table>                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2878                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2879                  !!!back-token;  
2880                  $token = {type => 'end tag',                  $self->{insertion_mode} = 'in head';
2881                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  ## Reprocess in the "in head" insertion mode...
2882                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
2883                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2884                    ## Ignore the token
2885                    !!!next-token;
2886                  redo B;                  redo B;
2887                }                }
2888                  
2889                  #
2890                } elsif ({
2891                          p => 1, br => 1,
2892                         }->{$token->{tag_name}}) {
2893                  if ($self->{insertion_mode} eq 'before head') {
2894                    ## As if <head>
2895                    !!!create-element ($self->{head_element}, 'head');
2896                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2897                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2898    
2899                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = 'in head';
2900                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
2901                }                }
2902    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
2903                #                #
2904              }              } else {
2905            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} ne 'after head') {
             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) {  
2906                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2907                  ## Ignore the token                  ## Ignore the token
2908                  !!!next-token;                  !!!next-token;
2909                  redo B;                  redo B;
2910                  } else {
2911                    #
2912                }                }
2913                              }
               ## 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]);  
               }  
2914    
2915                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} eq 'in head noscript') {
2916                  ## As if </noscript>
2917                  pop @{$self->{open_elements}};
2918                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2919                  
2920                  ## Reprocess in the "in head" insertion mode...
2921                  ## As if </head>
2922                  pop @{$self->{open_elements}};
2923    
2924                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
2925                } elsif ($self->{insertion_mode} eq 'in head') {
2926                  ## As if </head>
2927                  pop @{$self->{open_elements}};
2928    
2929                !!!next-token;                ## Reprocess in the "after head" insertion mode...
2930                redo B;              } elsif ($self->{insertion_mode} eq 'before head') {
             } 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}}) {  
2931                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2932                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
2933                !!!next-token;                !!!next-token;
2934                redo B;                redo B;
             } else {  
               #  
2935              }              }
2936    
2937                ## "after head" insertion mode
2938                ## As if <body>
2939                !!!insert-element ('body');
2940                $self->{insertion_mode} = 'in body';
2941                ## reprocess
2942                redo B;
2943            } else {            } else {
2944              #              die "$0: $token->{type}: Unknown token type";
2945            }            }
2946    
2947            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
2948            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} eq 'in body' or
2949            redo B;               $self->{insertion_mode} eq 'in cell' or
2950          } elsif ($self->{insertion_mode} eq 'in caption') {               $self->{insertion_mode} eq 'in caption') {
2951            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
2952              ## NOTE: This is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
2953              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
2954                            
2955              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2956    
2957              !!!next-token;              !!!next-token;
2958              redo B;              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;  
2959            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
2960              if ({              if ({
2961                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
2962                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2963                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
2964                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} eq 'in cell') {
2965                    ## have an element in table scope
2966                ## As if </caption>                  my $tn;
2967                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2968                my $i;                    my $node = $self->{open_elements}->[$_];
2969                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
2970                  my $node = $self->{open_elements}->[$_];                      $tn = $node->[1];
2971                  if ($node->[1] eq 'caption') {                      last INSCOPE;
2972                    $i = $_;                    } elsif ({
2973                    last INSCOPE;                              table => 1, html => 1,
2974                  } elsif ({                             }->{$node->[1]}) {
2975                            table => 1, html => 1,                      last INSCOPE;
2976                           }->{$node->[1]}) {                    }
2977                    last INSCOPE;                  } # INSCOPE
2978                  }                    unless (defined $tn) {
2979                } # INSCOPE                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2980                unless (defined $i) {                      ## Ignore the token
2981                  !!!parse-error (type => 'unmatched end tag:caption');                      !!!next-token;
2982                  ## Ignore the token                      redo B;
2983                  !!!next-token;                    }
2984                  redo B;                  
2985                }                  ## Close the cell
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
2986                  !!!back-token; # <?>                  !!!back-token; # <?>
2987                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => $tn};
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
2988                  redo B;                  redo B;
2989                }                } elsif ($self->{insertion_mode} eq 'in caption') {
2990                    !!!parse-error (type => 'not closed:caption');
2991                    
2992                    ## As if </caption>
2993                    ## have a table element in table scope
2994                    my $i;
2995                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2996                      my $node = $self->{open_elements}->[$_];
2997                      if ($node->[1] eq 'caption') {
2998                        $i = $_;
2999                        last INSCOPE;
3000                      } elsif ({
3001                                table => 1, html => 1,
3002                               }->{$node->[1]}) {
3003                        last INSCOPE;
3004                      }
3005                    } # INSCOPE
3006                      unless (defined $i) {
3007                        !!!parse-error (type => 'unmatched end tag:caption');
3008                        ## Ignore the token
3009                        !!!next-token;
3010                        redo B;
3011                      }
3012                    
3013                    ## generate implied end tags
3014                    if ({
3015                         dd => 1, dt => 1, li => 1, p => 1,
3016                         td => 1, th => 1, tr => 1,
3017                         tbody => 1, tfoot=> 1, thead => 1,
3018                        }->{$self->{open_elements}->[-1]->[1]}) {
3019                      !!!back-token; # <?>
3020                      $token = {type => 'end tag', tag_name => 'caption'};
3021                      !!!back-token;
3022                      $token = {type => 'end tag',
3023                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3024                      redo B;
3025                    }
3026    
3027                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3028                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3029                    }
3030                    
3031                    splice @{$self->{open_elements}}, $i;
3032                    
3033                    $clear_up_to_marker->();
3034                    
3035                    $self->{insertion_mode} = 'in table';
3036                    
3037                    ## reprocess
3038                    redo B;
3039                  } else {
3040                    #
3041                }                }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
3042              } else {              } else {
3043                #                #
3044              }              }
3045            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3046              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3047                ## have a table element in table scope                if ($self->{insertion_mode} eq 'in cell') {
3048                    ## have an element in table scope
3049                    my $i;
3050                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3051                      my $node = $self->{open_elements}->[$_];
3052                      if ($node->[1] eq $token->{tag_name}) {
3053                        $i = $_;
3054                        last INSCOPE;
3055                      } elsif ({
3056                                table => 1, html => 1,
3057                               }->{$node->[1]}) {
3058                        last INSCOPE;
3059                      }
3060                    } # INSCOPE
3061                      unless (defined $i) {
3062                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3063                        ## Ignore the token
3064                        !!!next-token;
3065                        redo B;
3066                      }
3067                    
3068                    ## generate implied end tags
3069                    if ({
3070                         dd => 1, dt => 1, li => 1, p => 1,
3071                         td => ($token->{tag_name} eq 'th'),
3072                         th => ($token->{tag_name} eq 'td'),
3073                         tr => 1,
3074                         tbody => 1, tfoot=> 1, thead => 1,
3075                        }->{$self->{open_elements}->[-1]->[1]}) {
3076                      !!!back-token;
3077                      $token = {type => 'end tag',
3078                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3079                      redo B;
3080                    }
3081                    
3082                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3083                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3084                    }
3085                    
3086                    splice @{$self->{open_elements}}, $i;
3087                    
3088                    $clear_up_to_marker->();
3089                    
3090                    $self->{insertion_mode} = 'in row';
3091                    
3092                    !!!next-token;
3093                    redo B;
3094                  } elsif ($self->{insertion_mode} eq 'in caption') {
3095                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3096                    ## Ignore the token
3097                    !!!next-token;
3098                    redo B;
3099                  } else {
3100                    #
3101                  }
3102                } elsif ($token->{tag_name} eq 'caption') {
3103                  if ($self->{insertion_mode} eq 'in caption') {
3104                    ## have a table element in table scope
3105                    my $i;
3106                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3107                      my $node = $self->{open_elements}->[$_];
3108                      if ($node->[1] eq $token->{tag_name}) {
3109                        $i = $_;
3110                        last INSCOPE;
3111                      } elsif ({
3112                                table => 1, html => 1,
3113                               }->{$node->[1]}) {
3114                        last INSCOPE;
3115                      }
3116                    } # INSCOPE
3117                      unless (defined $i) {
3118                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3119                        ## Ignore the token
3120                        !!!next-token;
3121                        redo B;
3122                      }
3123                    
3124                    ## generate implied end tags
3125                    if ({
3126                         dd => 1, dt => 1, li => 1, p => 1,
3127                         td => 1, th => 1, tr => 1,
3128                         tbody => 1, tfoot=> 1, thead => 1,
3129                        }->{$self->{open_elements}->[-1]->[1]}) {
3130                      !!!back-token;
3131                      $token = {type => 'end tag',
3132                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3133                      redo B;
3134                    }
3135                    
3136                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3137                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3138                    }
3139                    
3140                    splice @{$self->{open_elements}}, $i;
3141                    
3142                    $clear_up_to_marker->();
3143                    
3144                    $self->{insertion_mode} = 'in table';
3145                    
3146                    !!!next-token;
3147                    redo B;
3148                  } elsif ($self->{insertion_mode} eq 'in cell') {
3149                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3150                    ## Ignore the token
3151                    !!!next-token;
3152                    redo B;
3153                  } else {
3154                    #
3155                  }
3156                } elsif ({
3157                          table => 1, tbody => 1, tfoot => 1,
3158                          thead => 1, tr => 1,
3159                         }->{$token->{tag_name}} and
3160                         $self->{insertion_mode} eq 'in cell') {
3161                  ## have an element in table scope
3162                my $i;                my $i;
3163                  my $tn;
3164                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3165                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3166                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3167                    $i = $_;                    $i = $_;
3168                    last INSCOPE;                    last INSCOPE;
3169                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3170                      $tn = $node->[1];
3171                      ## NOTE: There is exactly one |td| or |th| element
3172                      ## in scope in the stack of open elements by definition.
3173                  } elsif ({                  } elsif ({
3174                            table => 1, html => 1,                            table => 1, html => 1,
3175                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3550  sub _tree_construction_main ($) { Line 3182  sub _tree_construction_main ($) {
3182                  !!!next-token;                  !!!next-token;
3183                  redo B;                  redo B;
3184                }                }
                 
               ## 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;  
3185    
3186                $clear_up_to_marker->();                ## Close the cell
3187                  !!!back-token; # </?>
3188                $self->{insertion_mode} = 'in table';                $token = {type => 'end tag', tag_name => $tn};
   
               !!!next-token;  
3189                redo B;                redo B;
3190              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3191                         $self->{insertion_mode} eq 'in caption') {
3192                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3193    
3194                ## As if </caption>                ## As if </caption>
# Line 3602  sub _tree_construction_main ($) { Line 3216  sub _tree_construction_main ($) {
3216                if ({                if ({
3217                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3218                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3219                       tbody => 1, tfoot=> 1, thead => 1,
3220                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3221                  !!!back-token; # </table>                  !!!back-token; # </table>
3222                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
# Line 3624  sub _tree_construction_main ($) { Line 3239  sub _tree_construction_main ($) {
3239                ## reprocess                ## reprocess
3240                redo B;                redo B;
3241              } elsif ({              } elsif ({
3242                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
3243                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3244                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} eq 'in cell' or
3245                ## Ignore the token                    $self->{insertion_mode} eq 'in caption') {
3246                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
             } 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');  
3247                  ## Ignore the token                  ## Ignore the token
3248                  !!!next-token;                  !!!next-token;
3249                  redo B;                  redo B;
3250                } else {                } else {
3251                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3252                }                }
3253              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3254                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3255                          thead => 1, tr => 1,
3256                         }->{$token->{tag_name}} and
3257                         $self->{insertion_mode} eq 'in caption') {
3258                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3259                ## Ignore the token                ## Ignore the token
3260                !!!next-token;                !!!next-token;
3261                redo B;                redo B;
3262              } else {              } else {
3263                #                #
3264              }              }
3265            } else {        } else {
3266              #          die "$0: $token->{type}: Unknown token type";
3267            }        }
3268    
3269            ## As if </colgroup>        $insert = $insert_to_current;
3270            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3271              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} eq 'in row' or
3272              ## Ignore the token               $self->{insertion_mode} eq 'in table body' or
3273              !!!next-token;               $self->{insertion_mode} eq 'in table') {
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
3274            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3275              ## NOTE: This is a "character in table" code clone.              ## NOTE: There are "character in table" code clones.
3276              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3277                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3278                                
# Line 3721  sub _tree_construction_main ($) { Line 3289  sub _tree_construction_main ($) {
3289              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3290              ## result in a new Text node.              ## result in a new Text node.
3291              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3292                
3293              if ({              if ({
3294                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3295                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3761  sub _tree_construction_main ($) { Line 3329  sub _tree_construction_main ($) {
3329                            
3330              !!!next-token;              !!!next-token;
3331              redo B;              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;  
3332            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3333              if ({              if ({
3334                   tr => 1,                   tr => ($self->{insertion_mode} ne 'in row'),
3335                   th => 1, td => 1,                   th => 1, td => 1,
3336                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3337                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} eq 'in table') {
3338                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3339                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3340                           $self->{open_elements}->[-1]->[1] ne 'html') {
3341                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3342                      pop @{$self->{open_elements}};
3343                    }
3344                    
3345                    !!!insert-element ('tbody');
3346                    $self->{insertion_mode} = 'in table body';
3347                    ## reprocess in the "in table body" insertion mode...
3348                }                }
3349    
3350                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
3351                    unless ($token->{tag_name} eq 'tr') {
3352                      !!!parse-error (type => 'missing start tag:tr');
3353                    }
3354                    
3355                    ## Clear back to table body context
3356                    while (not {
3357                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3358                    }->{$self->{open_elements}->[-1]->[1]}) {
3359                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3360                      pop @{$self->{open_elements}};
3361                    }
3362                    
3363                    $self->{insertion_mode} = 'in row';
3364                    if ($token->{tag_name} eq 'tr') {
3365                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3366                      !!!next-token;
3367                      redo B;
3368                    } else {
3369                      !!!insert-element ('tr');
3370                      ## reprocess in the "in row" insertion mode
3371                    }
3372                  }
3373    
3374                  ## Clear back to table row context
3375                while (not {                while (not {
3376                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3377                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3378                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3379                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3380                }                }
3381                                
3382                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3383                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = 'in cell';
3384                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3385                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3386                } else {                
3387                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3388                redo B;                redo B;
3389              } elsif ({              } elsif ({
3390                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3391                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3392                          tr => 1, # $self->{insertion_mode} eq 'in row'
3393                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3394                ## have an element in table scope                if ($self->{insertion_mode} eq 'in row') {
3395                my $i;                  ## As if </tr>
3396                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3397                  my $node = $self->{open_elements}->[$_];                  my $i;
3398                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3399                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3400                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3401                    $i = $_;                      $i = $_;
3402                    last INSCOPE;                      last INSCOPE;
3403                  } elsif ({                    } elsif ({
3404                            table => 1, html => 1,                              table => 1, html => 1,
3405                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3406                    last INSCOPE;                      last INSCOPE;
3407                      }
3408                    } # INSCOPE
3409                    unless (defined $i) {
3410                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3411                      ## Ignore the token
3412                      !!!next-token;
3413                      redo B;
3414                    }
3415                    
3416                    ## Clear back to table row context
3417                    while (not {
3418                      tr => 1, html => 1,
3419                    }->{$self->{open_elements}->[-1]->[1]}) {
3420                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3421                      pop @{$self->{open_elements}};
3422                    }
3423                    
3424                    pop @{$self->{open_elements}}; # tr
3425                    $self->{insertion_mode} = 'in table body';
3426                    if ($token->{tag_name} eq 'tr') {
3427                      ## reprocess
3428                      redo B;
3429                    } else {
3430                      ## reprocess in the "in table body" insertion mode...
3431                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3432                }                }
3433    
3434                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
3435                while (not {                  ## have an element in table scope
3436                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3437                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3438                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3439                      if ({
3440                           tbody => 1, thead => 1, tfoot => 1,
3441                          }->{$node->[1]}) {
3442                        $i = $_;
3443                        last INSCOPE;
3444                      } elsif ({
3445                                table => 1, html => 1,
3446                               }->{$node->[1]}) {
3447                        last INSCOPE;
3448                      }
3449                    } # INSCOPE
3450                    unless (defined $i) {
3451                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3452                      ## Ignore the token
3453                      !!!next-token;
3454                      redo B;
3455                    }
3456    
3457                    ## Clear back to table body context
3458                    while (not {
3459                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3460                    }->{$self->{open_elements}->[-1]->[1]}) {
3461                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3462                      pop @{$self->{open_elements}};
3463                    }
3464                    
3465                    ## As if <{current node}>
3466                    ## have an element in table scope
3467                    ## true by definition
3468                    
3469                    ## Clear back to table body context
3470                    ## nop by definition
3471                    
3472                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3473                    $self->{insertion_mode} = 'in table';
3474                    ## reprocess in "in table" insertion mode...
3475                }                }
3476    
3477                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3478                ## have an element in table scope                  ## Clear back to table context
3479                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3480                           $self->{open_elements}->[-1]->[1] ne 'html') {
3481                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3482                ## nop by definition                    pop @{$self->{open_elements}};
3483                    }
3484                pop @{$self->{open_elements}};                  
3485                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3486                ## reprocess                  $self->{insertion_mode} = 'in column group';
3487                redo B;                  ## reprocess
3488                    redo B;
3489                  } elsif ({
3490                            caption => 1,
3491                            colgroup => 1,
3492                            tbody => 1, tfoot => 1, thead => 1,
3493                           }->{$token->{tag_name}}) {
3494                    ## Clear back to table context
3495                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3496                           $self->{open_elements}->[-1]->[1] ne 'html') {
3497                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3498                      pop @{$self->{open_elements}};
3499                    }
3500                    
3501                    push @$active_formatting_elements, ['#marker', '']
3502                        if $token->{tag_name} eq 'caption';
3503                    
3504                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3505                    $self->{insertion_mode} = {
3506                                               caption => 'in caption',
3507                                               colgroup => 'in column group',
3508                                               tbody => 'in table body',
3509                                               tfoot => 'in table body',
3510                                               thead => 'in table body',
3511                                              }->{$token->{tag_name}};
3512                    !!!next-token;
3513                    redo B;
3514                  } else {
3515                    die "$0: in table: <>: $token->{tag_name}";
3516                  }
3517              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3518                ## NOTE: This is a code clone of "table in table"                ## NOTE: There are code clones for this "table in table"
3519                !!!parse-error (type => 'not closed:table');                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3520    
3521                ## As if </table>                ## As if </table>
3522                ## have a table element in table scope                ## have a table element in table scope
# Line 3867  sub _tree_construction_main ($) { Line 3543  sub _tree_construction_main ($) {
3543                if ({                if ({
3544                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3545                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3546                       tbody => 1, tfoot=> 1, thead => 1,
3547                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3548                  !!!back-token; # <table>                  !!!back-token; # <table>
3549                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 3882  sub _tree_construction_main ($) { Line 3559  sub _tree_construction_main ($) {
3559    
3560                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3561    
3562                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3563    
3564                ## reprocess                ## reprocess
3565                redo B;                redo B;
# Line 3890  sub _tree_construction_main ($) { Line 3567  sub _tree_construction_main ($) {
3567                #                #
3568              }              }
3569            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3570              if ({              if ($token->{tag_name} eq 'tr' and
3571                   tbody => 1, tfoot => 1, thead => 1,                  $self->{insertion_mode} eq 'in row') {
                 }->{$token->{tag_name}}) {  
3572                ## have an element in table scope                ## have an element in table scope
3573                my $i;                my $i;
3574                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3913  sub _tree_construction_main ($) { Line 3589  sub _tree_construction_main ($) {
3589                  redo B;                  redo B;
3590                }                }
3591    
3592                ## Clear back to table body context                ## Clear back to table row context
3593                while (not {                while (not {
3594                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3595                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3596                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3597                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3598                }                }
3599    
3600                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3601                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = 'in table body';
3602                !!!next-token;                !!!next-token;
3603                redo B;                redo B;
3604              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3605                ## have an element in table scope                if ($self->{insertion_mode} eq 'in row') {
3606                my $i;                  ## As if </tr>
3607                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3608                  my $node = $self->{open_elements}->[$_];                  my $i;
3609                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3610                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3611                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3612                    $i = $_;                      $i = $_;
3613                    last INSCOPE;                      last INSCOPE;
3614                  } elsif ({                    } elsif ({
3615                            table => 1, html => 1,                              table => 1, html => 1,
3616                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3617                    last INSCOPE;                      last INSCOPE;
3618                      }
3619                    } # INSCOPE
3620                    unless (defined $i) {
3621                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3622                      ## Ignore the token
3623                      !!!next-token;
3624                      redo B;
3625                  }                  }
3626                } # INSCOPE                  
3627                unless (defined $i) {                  ## Clear back to table row context
3628                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3629                  ## Ignore the token                    tr => 1, html => 1,
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
3630                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3631                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3632                my $foster_parent_element;                    pop @{$self->{open_elements}};
3633                my $next_sibling;                  }
3634                my $prev_sibling;                  
3635                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3636                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = 'in table body';
3637                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3638                    if (defined $parent and $parent->node_type == 1) {                }
3639                      $foster_parent_element = $parent;  
3640                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} eq 'in table body') {
3641                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3642                    } else {                  my $i;
3643                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3644                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
3645                      if ({
3646                           tbody => 1, thead => 1, tfoot => 1,
3647                          }->{$node->[1]}) {
3648                        $i = $_;
3649                        last INSCOPE;
3650                      } elsif ({
3651                                table => 1, html => 1,
3652                               }->{$node->[1]}) {
3653                        last INSCOPE;
3654                    }                    }
3655                    last OE;                  } # INSCOPE
3656                    unless (defined $i) {
3657                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3658                      ## Ignore the token
3659                      !!!next-token;
3660                      redo B;
3661                  }                  }
3662                } # OE                  
3663                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3664                $prev_sibling = $foster_parent_element->last_child                  while (not {
3665                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3666                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3667                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3668                  $prev_sibling->manakai_append_text ($token->{data});                    pop @{$self->{open_elements}};
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq '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;  
3669                  }                  }
3670                } # INSCOPE                  
3671                unless (defined $i) {                  ## As if <{current node}>
3672                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3673                  ## Ignore the token                  ## true by definition
3674                  !!!next-token;                  
3675                  redo B;                  ## Clear back to table body context
3676                }                  ## nop by definition
3677                    
               ## 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]);  
3678                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3679                    $self->{insertion_mode} = 'in table';
3680                    ## reprocess in the "in table" insertion mode...
3681                }                }
3682    
               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>  
3683                ## have a table element in table scope                ## have a table element in table scope
3684                my $i;                my $i;
3685                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3686                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3687                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3688                    $i = $_;                    $i = $_;
3689                    last INSCOPE;                    last INSCOPE;
3690                  } elsif ({                  } elsif ({
# Line 4125  sub _tree_construction_main ($) { Line 3694  sub _tree_construction_main ($) {
3694                  }                  }
3695                } # INSCOPE                } # INSCOPE
3696                unless (defined $i) {                unless (defined $i) {
3697                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3698                  ## Ignore tokens </table><table>                  ## Ignore the token
3699                  !!!next-token;                  !!!next-token;
3700                  redo B;                  redo B;
3701                }                }
3702                  
3703                ## generate implied end tags                ## generate implied end tags
3704                if ({                if ({
3705                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3706                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3707                       tbody => 1, tfoot=> 1, thead => 1,
3708                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3709                  !!!back-token;                  !!!back-token;
3710                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3711                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3712                  redo B;                  redo B;
3713                }                }
3714                  
3715                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3716                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3717                }                }
3718                    
3719                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3720                  
3721                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3722                  
               ## 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';  
3723                !!!next-token;                !!!next-token;
3724                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
3725              } elsif ({              } elsif ({
3726                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3727                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3728                ## have an element in table scope                       ($self->{insertion_mode} eq 'in row' or
3729                my $i;                        $self->{insertion_mode} eq 'in table body')) {
3730                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} eq 'in row') {
3731                  my $node = $self->{open_elements}->[$_];                  ## have an element in table scope
3732                  if ($node->[1] eq $token->{tag_name}) {                  my $i;
3733                    $i = $_;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3734                    last INSCOPE;                    my $node = $self->{open_elements}->[$_];
3735                  } elsif ({                    if ($node->[1] eq $token->{tag_name}) {
3736                            table => 1, html => 1,                      $i = $_;
3737                           }->{$node->[1]}) {                      last INSCOPE;
3738                    last INSCOPE;                    } elsif ({
3739                                table => 1, html => 1,
3740                               }->{$node->[1]}) {
3741                        last INSCOPE;
3742                      }
3743                    } # INSCOPE
3744                      unless (defined $i) {
3745                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3746                        ## Ignore the token
3747                        !!!next-token;
3748                        redo B;
3749                      }
3750                    
3751                    ## As if </tr>
3752                    ## have an element in table scope
3753                    my $i;
3754                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3755                      my $node = $self->{open_elements}->[$_];
3756                      if ($node->[1] eq 'tr') {
3757                        $i = $_;
3758                        last INSCOPE;
3759                      } elsif ({
3760                                table => 1, html => 1,
3761                               }->{$node->[1]}) {
3762                        last INSCOPE;
3763                      }
3764                    } # INSCOPE
3765                      unless (defined $i) {
3766                        !!!parse-error (type => 'unmatched end tag:tr');
3767                        ## Ignore the token
3768                        !!!next-token;
3769                        redo B;
3770                      }
3771                    
3772                    ## Clear back to table row context
3773                    while (not {
3774                      tr => 1, html => 1,
3775                    }->{$self->{open_elements}->[-1]->[1]}) {
3776                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3777                      pop @{$self->{open_elements}};
3778                  }                  }
3779                } # INSCOPE                  
3780                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3781                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = 'in table body';
3782                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3783                }                }
3784    
               ## As if </tr>  
3785                ## have an element in table scope                ## have an element in table scope
3786                my $i;                my $i;
3787                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3788                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3789                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3790                    $i = $_;                    $i = $_;
3791                    last INSCOPE;                    last INSCOPE;
3792                  } elsif ({                  } elsif ({
# Line 4263  sub _tree_construction_main ($) { Line 3796  sub _tree_construction_main ($) {
3796                  }                  }
3797                } # INSCOPE                } # INSCOPE
3798                unless (defined $i) {                unless (defined $i) {
3799                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3800                  ## Ignore the token                  ## Ignore the token
3801                  !!!next-token;                  !!!next-token;
3802                  redo B;                  redo B;
3803                }                }
3804    
3805                ## Clear back to table row context                ## Clear back to table body context
3806                while (not {                while (not {
3807                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
3808                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3809                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3810                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3811                }                }
3812    
3813                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
3814                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = 'in table';
3815                ## reprocess                !!!next-token;
3816                redo B;                redo B;
3817              } elsif ({              } elsif ({
3818                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3819                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3820                          tr => 1, # $self->{insertion_mode} eq 'in row'
3821                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'
3822                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3823                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3824                ## Ignore the token                ## Ignore the token
# Line 4293  sub _tree_construction_main ($) { Line 3828  sub _tree_construction_main ($) {
3828                #                #
3829              }              }
3830            } else {            } else {
3831              #              die "$0: $token->{type}: Unknown token type";
3832            }            }
3833    
3834            ## As if in table        !!!parse-error (type => 'in table:'.$token->{tag_name});
           !!!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});  
3835    
3836              !!!next-token;        $insert = $insert_to_foster;
3837              redo B;        #
3838            } elsif ($token->{type} eq 'comment') {      } elsif ($self->{insertion_mode} eq 'in column group') {
3839              ## NOTE: This is a code clone of "comment in body".            if ($token->{type} eq 'character') {
3840              my $comment = $self->{document}->create_comment ($token->{data});              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3841              $self->{open_elements}->[-1]->[0]->append_child ($comment);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3842              !!!next-token;                unless (length $token->{data}) {
             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  
3843                  !!!next-token;                  !!!next-token;
3844                  redo B;                  redo B;
3845                }                }
3846                }
3847                ## Close the cell              
3848                !!!back-token; # <?>              #
3849                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} eq 'start tag') {
3850                if ($token->{tag_name} eq 'col') {
3851                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3852                  pop @{$self->{open_elements}};
3853                  !!!next-token;
3854                redo B;                redo B;
3855              } else {              } else {
3856                #                #
3857              }              }
3858            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3859              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
3860                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3861                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
3862                  ## Ignore the token                  ## Ignore the token
3863                  !!!next-token;                  !!!next-token;
3864                  redo B;                  redo B;
3865                  } else {
3866                    pop @{$self->{open_elements}}; # colgroup
3867                    $self->{insertion_mode} = 'in table';
3868                    !!!next-token;
3869                    redo B;            
3870                }                }
3871                              } elsif ($token->{tag_name} eq 'col') {
3872                ## generate implied end tags                !!!parse-error (type => 'unmatched end tag:col');
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$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});  
3873                ## Ignore the token                ## Ignore the token
3874                !!!next-token;                !!!next-token;
3875                redo B;                redo B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
3876              } else {              } else {
3877                #                #
3878              }              }
3879            } else {            } else {
3880              #              #
3881            }            }
3882              
3883            $in_body->($insert_to_current);            ## As if </colgroup>
3884            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3885          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
3886            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
3887              !!!next-token;              !!!next-token;
3888              redo B;              redo B;
3889            } elsif ($token->{type} eq 'comment') {            } else {
3890              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
3891              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = 'in table';
3892                ## reprocess
3893                redo B;
3894              }
3895        } elsif ($self->{insertion_mode} eq 'in select') {
3896              if ($token->{type} eq 'character') {
3897                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3898              !!!next-token;              !!!next-token;
3899              redo B;              redo B;
3900            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 4624  sub _tree_construction_main ($) { Line 4066  sub _tree_construction_main ($) {
4066            ## Ignore the token            ## Ignore the token
4067            !!!next-token;            !!!next-token;
4068            redo B;            redo B;
4069          } elsif ($self->{insertion_mode} eq 'after body') {      } elsif ($self->{insertion_mode} eq 'after body' or
4070            if ($token->{type} eq 'character') {               $self->{insertion_mode} eq 'after html body') {
4071              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        if ($token->{type} eq 'character') {
4072                ## As if in body          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4073                $reconstruct_active_formatting_elements->($insert_to_current);            my $data = $1;
4074              ## As if in body
4075              $reconstruct_active_formatting_elements->($insert_to_current);
4076                                
4077                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4078              
4079              unless (length $token->{data}) {
4080                !!!next-token;
4081                redo B;
4082              }
4083            }
4084            
4085            if ($self->{insertion_mode} eq 'after html body') {
4086              !!!parse-error (type => 'after html:#character');
4087    
4088                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4089                  !!!next-token;          }
4090                  redo B;          
4091                }          ## "after body" insertion mode
4092              }          !!!parse-error (type => 'after body:#character');
4093                
4094              #          $self->{insertion_mode} = 'in body';
4095              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4096            } elsif ($token->{type} eq 'comment') {          redo B;
4097              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} eq 'start tag') {
4098              $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($self->{insertion_mode} eq 'after html body') {
4099              !!!parse-error (type => 'after html:'.$token->{tag_name});
4100              
4101              ## Reprocess in the "main" phase, "after body" insertion mode...
4102            }
4103    
4104            ## "after body" insertion mode
4105            !!!parse-error (type => 'after body:'.$token->{tag_name});
4106    
4107            $self->{insertion_mode} = 'in body';
4108            ## reprocess
4109            redo B;
4110          } elsif ($token->{type} eq 'end tag') {
4111            if ($self->{insertion_mode} eq 'after html body') {
4112              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4113              
4114              $self->{insertion_mode} = 'after body';
4115              ## Reprocess in the "main" phase, "after body" insertion mode...
4116            }
4117    
4118            ## "after body" insertion mode
4119            if ($token->{tag_name} eq 'html') {
4120              if (defined $self->{inner_html_node}) {
4121                !!!parse-error (type => 'unmatched end tag:html');
4122                ## Ignore the token
4123              !!!next-token;              !!!next-token;
4124              redo B;              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});  
             }  
4125            } else {            } else {
4126              !!!parse-error (type => 'after body:#'.$token->{type});              $self->{insertion_mode} = 'after html body';
4127                !!!next-token;
4128                redo B;
4129            }            }
4130            } else {
4131              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4132    
4133            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = 'in body';
4134            ## reprocess            ## reprocess
4135            redo B;            redo B;
4136          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4137            if ($token->{type} eq 'character') {        } else {
4138              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4139                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4140        } elsif ($self->{insertion_mode} eq 'in frameset' or
4141                unless (length $token->{data}) {               $self->{insertion_mode} eq 'after frameset' or
4142                  !!!next-token;               $self->{insertion_mode} eq 'after html frameset') {
4143                  redo B;        if ($token->{type} eq 'character') {
4144                }          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4145              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4146              
4147              #            unless (length $token->{data}) {
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
4148              !!!next-token;              !!!next-token;
4149              redo B;              redo B;
4150            } elsif ($token->{type} eq 'start tag') {            }
4151              if ($token->{tag_name} eq 'frameset') {          }
4152                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4153                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4154                redo B;            if ($self->{insertion_mode} eq 'in frameset') {
4155              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4156                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} eq 'after frameset') {
4157                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
4158                !!!next-token;            } else { # "after html frameset"
4159                redo B;              !!!parse-error (type => 'after html:#character');
4160              } elsif ($token->{tag_name} eq 'noframes') {  
4161                $in_body->($insert_to_current);              $self->{insertion_mode} = 'after frameset';
4162                redo B;              ## Reprocess in the "main" phase, "after frameset"...
4163              } else {              !!!parse-error (type => 'after frameset:#character');
               #  
             }  
           } 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 {  
             #  
4164            }            }
4165                        
4166            if (defined $token->{tag_name}) {            ## Ignore the token.
4167              if (length $token->{data}) {
4168                ## reprocess the rest of characters
4169              } else {
4170                !!!next-token;
4171              }
4172              redo B;
4173            }
4174            
4175            die qq[$0: Character "$token->{data}"];
4176          } elsif ($token->{type} eq 'start tag') {
4177            if ($self->{insertion_mode} eq 'after html frameset') {
4178              !!!parse-error (type => 'after html:'.$token->{tag_name});
4179    
4180              $self->{insertion_mode} = 'after frameset';
4181              ## Process in the "main" phase, "after frameset" insertion mode...
4182            }
4183    
4184            if ($token->{tag_name} eq 'frameset' and
4185                $self->{insertion_mode} eq 'in frameset') {
4186              !!!insert-element ($token->{tag_name}, $token->{attributes});
4187              !!!next-token;
4188              redo B;
4189            } elsif ($token->{tag_name} eq 'frame' and
4190                     $self->{insertion_mode} eq 'in frameset') {
4191              !!!insert-element ($token->{tag_name}, $token->{attributes});
4192              pop @{$self->{open_elements}};
4193              !!!next-token;
4194              redo B;
4195            } elsif ($token->{tag_name} eq 'noframes') {
4196              ## NOTE: As if in body.
4197              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4198              redo B;
4199            } else {
4200              if ($self->{insertion_mode} eq 'in frameset') {
4201              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4202            } else {            } else {
4203              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4204            }            }
4205            ## Ignore the token            ## Ignore the token
4206            !!!next-token;            !!!next-token;
4207            redo B;            redo B;
4208          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4209            if ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'end tag') {
4210              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} eq 'after html frameset') {
4211                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4212    
4213                unless (length $token->{data}) {            $self->{insertion_mode} = 'after frameset';
4214                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
4215                  redo B;          }
               }  
             }  
4216    
4217              #          if ($token->{tag_name} eq 'frameset' and
4218            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} eq 'in frameset') {
4219              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4220              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4221                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4222                ## Ignore the token
4223              !!!next-token;              !!!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 {  
               #  
             }  
4224            } else {            } else {
4225              #              pop @{$self->{open_elements}};
4226                !!!next-token;
4227            }            }
4228              
4229            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
4230              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4231                $self->{insertion_mode} = 'after frameset';
4232              }
4233              redo B;
4234            } elsif ($token->{tag_name} eq 'html' and
4235                     $self->{insertion_mode} eq 'after frameset') {
4236              $self->{insertion_mode} = 'after html frameset';
4237              !!!next-token;
4238              redo B;
4239            } else {
4240              if ($self->{insertion_mode} eq 'in frameset') {
4241                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4242            } else {            } else {
4243              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4244            }            }
4245            ## Ignore the token            ## Ignore the token
4246            !!!next-token;            !!!next-token;
4247            redo B;            redo B;
4248            }
4249          } else {
4250            die "$0: $token->{type}: Unknown token type";
4251          }
4252    
4253          ## ISSUE: An issue in spec here
4254        } else {
4255          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4256        }
4257    
4258        ## "in body" insertion mode
4259        if ($token->{type} eq 'start tag') {
4260          if ($token->{tag_name} eq 'script') {
4261            ## NOTE: This is an "as if in head" code clone
4262            $script_start_tag->($insert);
4263            redo B;
4264          } elsif ($token->{tag_name} eq 'style') {
4265            ## NOTE: This is an "as if in head" code clone
4266            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4267            redo B;
4268          } elsif ({
4269                    base => 1, link => 1,
4270                   }->{$token->{tag_name}}) {
4271            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4272            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4273            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4274            !!!next-token;
4275            redo B;
4276          } elsif ($token->{tag_name} eq 'meta') {
4277            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4278            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4279            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4280    
4281            unless ($self->{confident}) {
4282              my $charset;
4283              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4284                $charset = $token->{attributes}->{charset}->{value};
4285              }
4286              if ($token->{attributes}->{'http-equiv'}) {
4287                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4288                if ($token->{attributes}->{'http-equiv'}->{value}
4289                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4290                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4291                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4292                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4293                } ## TODO: And if supported
4294              }
4295              ## TODO: Change the encoding
4296            }
4297    
4298            ## ISSUE: An issue in spec there          !!!next-token;
4299            redo B;
4300          } elsif ($token->{tag_name} eq 'title') {
4301            !!!parse-error (type => 'in body:title');
4302            ## NOTE: This is an "as if in head" code clone
4303            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4304              if (defined $self->{head_element}) {
4305                $self->{head_element}->append_child ($_[0]);
4306              } else {
4307                $insert->($_[0]);
4308              }
4309            });
4310            redo B;
4311          } elsif ($token->{tag_name} eq 'body') {
4312            !!!parse-error (type => 'in body:body');
4313                  
4314            if (@{$self->{open_elements}} == 1 or
4315                $self->{open_elements}->[1]->[1] ne 'body') {
4316              ## Ignore the token
4317          } else {          } else {
4318            die "$0: $self->{insertion_mode}: Unknown insertion mode";            my $body_el = $self->{open_elements}->[1]->[0];
4319              for my $attr_name (keys %{$token->{attributes}}) {
4320                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4321                  $body_el->set_attribute_ns
4322                    (undef, [undef, $attr_name],
4323                     $token->{attributes}->{$attr_name}->{value});
4324                }
4325              }
4326          }          }
       }  
     } 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  
4327          !!!next-token;          !!!next-token;
4328          redo B;          redo B;
4329        } elsif ($token->{type} eq 'comment') {        } elsif ({
4330          my $comment = $self->{document}->create_comment ($token->{data});                  address => 1, blockquote => 1, center => 1, dir => 1,
4331          $self->{document}->append_child ($comment);                  div => 1, dl => 1, fieldset => 1, listing => 1,
4332                    menu => 1, ol => 1, p => 1, ul => 1,
4333                    pre => 1,
4334                   }->{$token->{tag_name}}) {
4335            ## has a p element in scope
4336            INSCOPE: for (reverse @{$self->{open_elements}}) {
4337              if ($_->[1] eq 'p') {
4338                !!!back-token;
4339                $token = {type => 'end tag', tag_name => 'p'};
4340                redo B;
4341              } elsif ({
4342                        table => 1, caption => 1, td => 1, th => 1,
4343                        button => 1, marquee => 1, object => 1, html => 1,
4344                       }->{$_->[1]}) {
4345                last INSCOPE;
4346              }
4347            } # INSCOPE
4348              
4349            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4350            if ($token->{tag_name} eq 'pre') {
4351              !!!next-token;
4352              if ($token->{type} eq 'character') {
4353                $token->{data} =~ s/^\x0A//;
4354                unless (length $token->{data}) {
4355                  !!!next-token;
4356                }
4357              }
4358            } else {
4359              !!!next-token;
4360            }
4361            redo B;
4362          } elsif ($token->{tag_name} eq 'form') {
4363            if (defined $self->{form_element}) {
4364              !!!parse-error (type => 'in form:form');
4365              ## Ignore the token
4366              !!!next-token;
4367              redo B;
4368            } else {
4369              ## has a p element in scope
4370              INSCOPE: for (reverse @{$self->{open_elements}}) {
4371                if ($_->[1] eq 'p') {
4372                  !!!back-token;
4373                  $token = {type => 'end tag', tag_name => 'p'};
4374                  redo B;
4375                } elsif ({
4376                          table => 1, caption => 1, td => 1, th => 1,
4377                          button => 1, marquee => 1, object => 1, html => 1,
4378                         }->{$_->[1]}) {
4379                  last INSCOPE;
4380                }
4381              } # INSCOPE
4382                
4383              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4384              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4385              !!!next-token;
4386              redo B;
4387            }
4388          } elsif ($token->{tag_name} eq 'li') {
4389            ## has a p element in scope
4390            INSCOPE: for (reverse @{$self->{open_elements}}) {
4391              if ($_->[1] eq 'p') {
4392                !!!back-token;
4393                $token = {type => 'end tag', tag_name => 'p'};
4394                redo B;
4395              } elsif ({
4396                        table => 1, caption => 1, td => 1, th => 1,
4397                        button => 1, marquee => 1, object => 1, html => 1,
4398                       }->{$_->[1]}) {
4399                last INSCOPE;
4400              }
4401            } # INSCOPE
4402              
4403            ## Step 1
4404            my $i = -1;
4405            my $node = $self->{open_elements}->[$i];
4406            LI: {
4407              ## Step 2
4408              if ($node->[1] eq 'li') {
4409                if ($i != -1) {
4410                  !!!parse-error (type => 'end tag missing:'.
4411                                  $self->{open_elements}->[-1]->[1]);
4412                }
4413                splice @{$self->{open_elements}}, $i;
4414                last LI;
4415              }
4416              
4417              ## Step 3
4418              if (not $formatting_category->{$node->[1]} and
4419                  #not $phrasing_category->{$node->[1]} and
4420                  ($special_category->{$node->[1]} or
4421                   $scoping_category->{$node->[1]}) and
4422                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4423                last LI;
4424              }
4425              
4426              ## Step 4
4427              $i--;
4428              $node = $self->{open_elements}->[$i];
4429              redo LI;
4430            } # LI
4431              
4432            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4433          !!!next-token;          !!!next-token;
4434          redo B;          redo B;
4435        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4436          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## has a p element in scope
4437            my $data = $1;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4438            ## As if in the main phase.            if ($_->[1] eq 'p') {
4439            ## NOTE: The insertion mode in the main phase              !!!back-token;
4440            ## just before the phase has been changed to the trailing              $token = {type => 'end tag', tag_name => 'p'};
4441            ## end phase is either "after body" or "after frameset".              redo B;
4442            $reconstruct_active_formatting_elements->($insert_to_current)            } elsif ({
4443              if $phase eq 'main';                      table => 1, caption => 1, td => 1, th => 1,
4444                        button => 1, marquee => 1, object => 1, html => 1,
4445                       }->{$_->[1]}) {
4446                last INSCOPE;
4447              }
4448            } # INSCOPE
4449                        
4450            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          ## Step 1
4451            my $i = -1;
4452            my $node = $self->{open_elements}->[$i];
4453            LI: {
4454              ## Step 2
4455              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4456                if ($i != -1) {
4457                  !!!parse-error (type => 'end tag missing:'.
4458                                  $self->{open_elements}->[-1]->[1]);
4459                }
4460                splice @{$self->{open_elements}}, $i;
4461                last LI;
4462              }
4463                        
4464              ## Step 3
4465              if (not $formatting_category->{$node->[1]} and
4466                  #not $phrasing_category->{$node->[1]} and
4467                  ($special_category->{$node->[1]} or
4468                   $scoping_category->{$node->[1]}) and
4469                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4470                last LI;
4471              }
4472              
4473              ## Step 4
4474              $i--;
4475              $node = $self->{open_elements}->[$i];
4476              redo LI;
4477            } # LI
4478              
4479            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4480            !!!next-token;
4481            redo B;
4482          } elsif ($token->{tag_name} eq 'plaintext') {
4483            ## has a p element in scope
4484            INSCOPE: for (reverse @{$self->{open_elements}}) {
4485              if ($_->[1] eq 'p') {
4486                !!!back-token;
4487                $token = {type => 'end tag', tag_name => 'p'};
4488                redo B;
4489              } elsif ({
4490                        table => 1, caption => 1, td => 1, th => 1,
4491                        button => 1, marquee => 1, object => 1, html => 1,
4492                       }->{$_->[1]}) {
4493                last INSCOPE;
4494              }
4495            } # INSCOPE
4496              
4497            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4498              
4499            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4500              
4501            !!!next-token;
4502            redo B;
4503          } elsif ({
4504                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4505                   }->{$token->{tag_name}}) {
4506            ## has a p element in scope
4507            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4508              my $node = $self->{open_elements}->[$_];
4509              if ($node->[1] eq 'p') {
4510                !!!back-token;
4511                $token = {type => 'end tag', tag_name => 'p'};
4512                redo B;
4513              } elsif ({
4514                        table => 1, caption => 1, td => 1, th => 1,
4515                        button => 1, marquee => 1, object => 1, html => 1,
4516                       }->{$node->[1]}) {
4517                last INSCOPE;
4518              }
4519            } # INSCOPE
4520              
4521            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4522            ## has an element in scope
4523            #my $i;
4524            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4525            #  my $node = $self->{open_elements}->[$_];
4526            #  if ({
4527            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4528            #      }->{$node->[1]}) {
4529            #    $i = $_;
4530            #    last INSCOPE;
4531            #  } elsif ({
4532            #            table => 1, caption => 1, td => 1, th => 1,
4533            #            button => 1, marquee => 1, object => 1, html => 1,
4534            #           }->{$node->[1]}) {
4535            #    last INSCOPE;
4536            #  }
4537            #} # INSCOPE
4538            #  
4539            #if (defined $i) {
4540            #  !!! parse-error (type => 'in hn:hn');
4541            #  splice @{$self->{open_elements}}, $i;
4542            #}
4543              
4544            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4545              
4546            !!!next-token;
4547            redo B;
4548          } elsif ($token->{tag_name} eq 'a') {
4549            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4550              my $node = $active_formatting_elements->[$i];
4551              if ($node->[1] eq 'a') {
4552                !!!parse-error (type => 'in a:a');
4553                
4554                !!!back-token;
4555                $token = {type => 'end tag', tag_name => 'a'};
4556                $formatting_end_tag->($token->{tag_name});
4557                
4558                AFE2: for (reverse 0..$#$active_formatting_elements) {
4559                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4560                    splice @$active_formatting_elements, $_, 1;
4561                    last AFE2;
4562                  }
4563                } # AFE2
4564                OE: for (reverse 0..$#{$self->{open_elements}}) {
4565                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4566                    splice @{$self->{open_elements}}, $_, 1;
4567                    last OE;
4568                  }
4569                } # OE
4570                last AFE;
4571              } elsif ($node->[0] eq '#marker') {
4572                last AFE;
4573              }
4574            } # AFE
4575              
4576            $reconstruct_active_formatting_elements->($insert_to_current);
4577    
4578            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4579            push @$active_formatting_elements, $self->{open_elements}->[-1];
4580    
4581            !!!next-token;
4582            redo B;
4583          } elsif ({
4584                    b => 1, big => 1, em => 1, font => 1, i => 1,
4585                    s => 1, small => 1, strile => 1,
4586                    strong => 1, tt => 1, u => 1,
4587                   }->{$token->{tag_name}}) {
4588            $reconstruct_active_formatting_elements->($insert_to_current);
4589            
4590            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4591            push @$active_formatting_elements, $self->{open_elements}->[-1];
4592            
4593            !!!next-token;
4594            redo B;
4595          } elsif ($token->{tag_name} eq 'nobr') {
4596            $reconstruct_active_formatting_elements->($insert_to_current);
4597    
4598            ## has a |nobr| element in scope
4599            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4600              my $node = $self->{open_elements}->[$_];
4601              if ($node->[1] eq 'nobr') {
4602                !!!parse-error (type => 'not closed:nobr');
4603                !!!back-token;
4604                $token = {type => 'end tag', tag_name => 'nobr'};
4605                redo B;
4606              } elsif ({
4607                        table => 1, caption => 1, td => 1, th => 1,
4608                        button => 1, marquee => 1, object => 1, html => 1,
4609                       }->{$node->[1]}) {
4610                last INSCOPE;
4611              }
4612            } # INSCOPE
4613            
4614            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4615            push @$active_formatting_elements, $self->{open_elements}->[-1];
4616            
4617            !!!next-token;
4618            redo B;
4619          } elsif ($token->{tag_name} eq 'button') {
4620            ## has a button element in scope
4621            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4622              my $node = $self->{open_elements}->[$_];
4623              if ($node->[1] eq 'button') {
4624                !!!parse-error (type => 'in button:button');
4625                !!!back-token;
4626                $token = {type => 'end tag', tag_name => 'button'};
4627                redo B;
4628              } elsif ({
4629                        table => 1, caption => 1, td => 1, th => 1,
4630                        button => 1, marquee => 1, object => 1, html => 1,
4631                       }->{$node->[1]}) {
4632                last INSCOPE;
4633              }
4634            } # INSCOPE
4635              
4636            $reconstruct_active_formatting_elements->($insert_to_current);
4637              
4638            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4639            push @$active_formatting_elements, ['#marker', ''];
4640    
4641            !!!next-token;
4642            redo B;
4643          } elsif ($token->{tag_name} eq 'marquee' or
4644                   $token->{tag_name} eq 'object') {
4645            $reconstruct_active_formatting_elements->($insert_to_current);
4646            
4647            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4648            push @$active_formatting_elements, ['#marker', ''];
4649            
4650            !!!next-token;
4651            redo B;
4652          } elsif ($token->{tag_name} eq 'xmp') {
4653            $reconstruct_active_formatting_elements->($insert_to_current);
4654            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4655            redo B;
4656          } elsif ($token->{tag_name} eq 'table') {
4657            ## has a p element in scope
4658            INSCOPE: for (reverse @{$self->{open_elements}}) {
4659              if ($_->[1] eq 'p') {
4660                !!!back-token;
4661                $token = {type => 'end tag', tag_name => 'p'};
4662                redo B;
4663              } elsif ({
4664                        table => 1, caption => 1, td => 1, th => 1,
4665                        button => 1, marquee => 1, object => 1, html => 1,
4666                       }->{$_->[1]}) {
4667                last INSCOPE;
4668              }
4669            } # INSCOPE
4670              
4671            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4672              
4673            $self->{insertion_mode} = 'in table';
4674              
4675            !!!next-token;
4676            redo B;
4677          } elsif ({
4678                    area => 1, basefont => 1, bgsound => 1, br => 1,
4679                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4680                    image => 1,
4681                   }->{$token->{tag_name}}) {
4682            if ($token->{tag_name} eq 'image') {
4683              !!!parse-error (type => 'image');
4684              $token->{tag_name} = 'img';
4685            }
4686    
4687            ## NOTE: There is an "as if <br>" code clone.
4688            $reconstruct_active_formatting_elements->($insert_to_current);
4689            
4690            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4691            pop @{$self->{open_elements}};
4692            
4693            !!!next-token;
4694            redo B;
4695          } elsif ($token->{tag_name} eq 'hr') {
4696            ## has a p element in scope
4697            INSCOPE: for (reverse @{$self->{open_elements}}) {
4698              if ($_->[1] eq 'p') {
4699                !!!back-token;
4700                $token = {type => 'end tag', tag_name => 'p'};
4701                redo B;
4702              } elsif ({
4703                        table => 1, caption => 1, td => 1, th => 1,
4704                        button => 1, marquee => 1, object => 1, html => 1,
4705                       }->{$_->[1]}) {
4706                last INSCOPE;
4707              }
4708            } # INSCOPE
4709              
4710            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4711            pop @{$self->{open_elements}};
4712              
4713            !!!next-token;
4714            redo B;
4715          } elsif ($token->{tag_name} eq 'input') {
4716            $reconstruct_active_formatting_elements->($insert_to_current);
4717            
4718            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4719            ## TODO: associate with $self->{form_element} if defined
4720            pop @{$self->{open_elements}};
4721            
4722            !!!next-token;
4723            redo B;
4724          } elsif ($token->{tag_name} eq 'isindex') {
4725            !!!parse-error (type => 'isindex');
4726            
4727            if (defined $self->{form_element}) {
4728              ## Ignore the token
4729              !!!next-token;
4730              redo B;
4731            } else {
4732              my $at = $token->{attributes};
4733              my $form_attrs;
4734              $form_attrs->{action} = $at->{action} if $at->{action};
4735              my $prompt_attr = $at->{prompt};
4736              $at->{name} = {name => 'name', value => 'isindex'};
4737              delete $at->{action};
4738              delete $at->{prompt};
4739              my @tokens = (
4740                            {type => 'start tag', tag_name => 'form',
4741                             attributes => $form_attrs},
4742                            {type => 'start tag', tag_name => 'hr'},
4743                            {type => 'start tag', tag_name => 'p'},
4744                            {type => 'start tag', tag_name => 'label'},
4745                           );
4746              if ($prompt_attr) {
4747                push @tokens, {type => 'character', data => $prompt_attr->{value}};
4748              } else {
4749                push @tokens, {type => 'character',
4750                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4751                ## TODO: make this configurable
4752              }
4753              push @tokens,
4754                            {type => 'start tag', tag_name => 'input', attributes => $at},
4755                            #{type => 'character', data => ''}, # SHOULD
4756                            {type => 'end tag', tag_name => 'label'},
4757                            {type => 'end tag', tag_name => 'p'},
4758                            {type => 'start tag', tag_name => 'hr'},
4759                            {type => 'end tag', tag_name => 'form'};
4760              $token = shift @tokens;
4761              !!!back-token (@tokens);
4762              redo B;
4763            }
4764          } elsif ($token->{tag_name} eq 'textarea') {
4765            my $tag_name = $token->{tag_name};
4766            my $el;
4767            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4768            
4769            ## TODO: $self->{form_element} if defined
4770            $self->{content_model} = RCDATA_CONTENT_MODEL;
4771            delete $self->{escape}; # MUST
4772            
4773            $insert->($el);
4774            
4775            my $text = '';
4776            !!!next-token;
4777            if ($token->{type} eq 'character') {
4778              $token->{data} =~ s/^\x0A//;
4779            unless (length $token->{data}) {            unless (length $token->{data}) {
4780              !!!next-token;              !!!next-token;
             redo B;  
4781            }            }
4782          }          }
4783            while ($token->{type} eq 'character') {
4784              $text .= $token->{data};
4785              !!!next-token;
4786            }
4787            if (length $text) {
4788              $el->manakai_append_text ($text);
4789            }
4790            
4791            $self->{content_model} = PCDATA_CONTENT_MODEL;
4792            
4793            if ($token->{type} eq 'end tag' and
4794                $token->{tag_name} eq $tag_name) {
4795              ## Ignore the token
4796            } else {
4797              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4798            }
4799            !!!next-token;
4800            redo B;
4801          } elsif ({
4802                    iframe => 1,
4803                    noembed => 1,
4804                    noframes => 1,
4805                    noscript => 0, ## TODO: 1 if scripting is enabled
4806                   }->{$token->{tag_name}}) {
4807            ## NOTE: There are two "as if in body" code clones.
4808            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4809            redo B;
4810          } elsif ($token->{tag_name} eq 'select') {
4811            $reconstruct_active_formatting_elements->($insert_to_current);
4812            
4813            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4814            
4815            $self->{insertion_mode} = 'in select';
4816            !!!next-token;
4817            redo B;
4818          } elsif ({
4819                    caption => 1, col => 1, colgroup => 1, frame => 1,
4820                    frameset => 1, head => 1, option => 1, optgroup => 1,
4821                    tbody => 1, td => 1, tfoot => 1, th => 1,
4822                    thead => 1, tr => 1,
4823                   }->{$token->{tag_name}}) {
4824            !!!parse-error (type => 'in body:'.$token->{tag_name});
4825            ## Ignore the token
4826            !!!next-token;
4827            redo B;
4828            
4829            ## ISSUE: An issue on HTML5 new elements in the spec.
4830          } else {
4831            $reconstruct_active_formatting_elements->($insert_to_current);
4832            
4833            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4834            
4835            !!!next-token;
4836            redo B;
4837          }
4838        } elsif ($token->{type} eq 'end tag') {
4839          if ($token->{tag_name} eq 'body') {
4840            if (@{$self->{open_elements}} > 1 and
4841                $self->{open_elements}->[1]->[1] eq 'body') {
4842              for (@{$self->{open_elements}}) {
4843                unless ({
4844                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4845                           th => 1, tr => 1, body => 1, html => 1,
4846                         tbody => 1, tfoot => 1, thead => 1,
4847                        }->{$_->[1]}) {
4848                  !!!parse-error (type => 'not closed:'.$_->[1]);
4849                }
4850              }
4851    
4852          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = 'after body';
4853          $phase = 'main';            !!!next-token;
4854          ## reprocess            redo B;
4855            } else {
4856              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4857              ## Ignore the token
4858              !!!next-token;
4859              redo B;
4860            }
4861          } elsif ($token->{tag_name} eq 'html') {
4862            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4863              ## ISSUE: There is an issue in the spec.
4864              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4865                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4866              }
4867              $self->{insertion_mode} = 'after body';
4868              ## reprocess
4869              redo B;
4870            } else {
4871              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4872              ## Ignore the token
4873              !!!next-token;
4874              redo B;
4875            }
4876          } elsif ({
4877                    address => 1, blockquote => 1, center => 1, dir => 1,
4878                    div => 1, dl => 1, fieldset => 1, listing => 1,
4879                    menu => 1, ol => 1, pre => 1, ul => 1,
4880                    p => 1,
4881                    dd => 1, dt => 1, li => 1,
4882                    button => 1, marquee => 1, object => 1,
4883                   }->{$token->{tag_name}}) {
4884            ## has an element in scope
4885            my $i;
4886            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4887              my $node = $self->{open_elements}->[$_];
4888              if ($node->[1] eq $token->{tag_name}) {
4889                ## generate implied end tags
4890                if ({
4891                     dd => ($token->{tag_name} ne 'dd'),
4892                     dt => ($token->{tag_name} ne 'dt'),
4893                     li => ($token->{tag_name} ne 'li'),
4894                     p => ($token->{tag_name} ne 'p'),
4895                     td => 1, th => 1, tr => 1,
4896                     tbody => 1, tfoot=> 1, thead => 1,
4897                    }->{$self->{open_elements}->[-1]->[1]}) {
4898                  !!!back-token;
4899                  $token = {type => 'end tag',
4900                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4901                  redo B;
4902                }
4903                $i = $_;
4904                last INSCOPE unless $token->{tag_name} eq 'p';
4905              } elsif ({
4906                        table => 1, caption => 1, td => 1, th => 1,
4907                        button => 1, marquee => 1, object => 1, html => 1,
4908                       }->{$node->[1]}) {
4909                last INSCOPE;
4910              }
4911            } # INSCOPE
4912            
4913            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4914              if (defined $i) {
4915                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4916              } else {
4917                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4918              }
4919            }
4920            
4921            if (defined $i) {
4922              splice @{$self->{open_elements}}, $i;
4923            } elsif ($token->{tag_name} eq 'p') {
4924              ## As if <p>, then reprocess the current token
4925              my $el;
4926              !!!create-element ($el, 'p');
4927              $insert->($el);
4928            }
4929            $clear_up_to_marker->()
4930              if {
4931                button => 1, marquee => 1, object => 1,
4932              }->{$token->{tag_name}};
4933            !!!next-token;
4934          redo B;          redo B;
4935        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
4936                 $token->{type} eq 'end tag') {          ## has an element in scope
4937          !!!parse-error (type => 'after html:'.$token->{tag_name});          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4938          $phase = 'main';            my $node = $self->{open_elements}->[$_];
4939          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
4940                ## generate implied end tags
4941                if ({
4942                     dd => 1, dt => 1, li => 1, p => 1,
4943                     td => 1, th => 1, tr => 1,
4944                     tbody => 1, tfoot=> 1, thead => 1,
4945                    }->{$self->{open_elements}->[-1]->[1]}) {
4946                  !!!back-token;
4947                  $token = {type => 'end tag',
4948                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4949                  redo B;
4950                }
4951                last INSCOPE;
4952              } elsif ({
4953                        table => 1, caption => 1, td => 1, th => 1,
4954                        button => 1, marquee => 1, object => 1, html => 1,
4955                       }->{$node->[1]}) {
4956                last INSCOPE;
4957              }
4958            } # INSCOPE
4959            
4960            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
4961              pop @{$self->{open_elements}};
4962            } else {
4963              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4964            }
4965    
4966            undef $self->{form_element};
4967            !!!next-token;
4968          redo B;          redo B;
4969        } elsif ($token->{type} eq 'end-of-file') {        } elsif ({
4970          ## Stop parsing                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4971          last B;                 }->{$token->{tag_name}}) {
4972            ## has an element in scope
4973            my $i;
4974            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4975              my $node = $self->{open_elements}->[$_];
4976              if ({
4977                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4978                  }->{$node->[1]}) {
4979                ## generate implied end tags
4980                if ({
4981                     dd => 1, dt => 1, li => 1, p => 1,
4982                     td => 1, th => 1, tr => 1,
4983                     tbody => 1, tfoot=> 1, thead => 1,
4984                    }->{$self->{open_elements}->[-1]->[1]}) {
4985                  !!!back-token;
4986                  $token = {type => 'end tag',
4987                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4988                  redo B;
4989                }
4990                $i = $_;
4991                last INSCOPE;
4992              } elsif ({
4993                        table => 1, caption => 1, td => 1, th => 1,
4994                        button => 1, marquee => 1, object => 1, html => 1,
4995                       }->{$node->[1]}) {
4996                last INSCOPE;
4997              }
4998            } # INSCOPE
4999            
5000            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5001              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5002            }
5003            
5004            splice @{$self->{open_elements}}, $i if defined $i;
5005            !!!next-token;
5006            redo B;
5007          } elsif ({
5008                    a => 1,
5009                    b => 1, big => 1, em => 1, font => 1, i => 1,
5010                    nobr => 1, s => 1, small => 1, strile => 1,
5011                    strong => 1, tt => 1, u => 1,
5012                   }->{$token->{tag_name}}) {
5013            $formatting_end_tag->($token->{tag_name});
5014            redo B;
5015          } elsif ($token->{tag_name} eq 'br') {
5016            !!!parse-error (type => 'unmatched end tag:br');
5017    
5018            ## As if <br>
5019            $reconstruct_active_formatting_elements->($insert_to_current);
5020            
5021            my $el;
5022            !!!create-element ($el, 'br');
5023            $insert->($el);
5024            
5025            ## Ignore the token.
5026            !!!next-token;
5027            redo B;
5028          } elsif ({
5029                    caption => 1, col => 1, colgroup => 1, frame => 1,
5030                    frameset => 1, head => 1, option => 1, optgroup => 1,
5031                    tbody => 1, td => 1, tfoot => 1, th => 1,
5032                    thead => 1, tr => 1,
5033                    area => 1, basefont => 1, bgsound => 1,
5034                    embed => 1, hr => 1, iframe => 1, image => 1,
5035                    img => 1, input => 1, isindex => 1, noembed => 1,
5036                    noframes => 1, param => 1, select => 1, spacer => 1,
5037                    table => 1, textarea => 1, wbr => 1,
5038                    noscript => 0, ## TODO: if scripting is enabled
5039                   }->{$token->{tag_name}}) {
5040            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5041            ## Ignore the token
5042            !!!next-token;
5043            redo B;
5044            
5045            ## ISSUE: Issue on HTML5 new elements in spec
5046            
5047        } else {        } else {
5048          die "$0: $token->{type}: Unknown token";          ## Step 1
5049            my $node_i = -1;
5050            my $node = $self->{open_elements}->[$node_i];
5051    
5052            ## Step 2
5053            S2: {
5054              if ($node->[1] eq $token->{tag_name}) {
5055                ## Step 1
5056                ## generate implied end tags
5057                if ({
5058                     dd => 1, dt => 1, li => 1, p => 1,
5059                     td => 1, th => 1, tr => 1,
5060                     tbody => 1, tfoot=> 1, thead => 1,
5061                    }->{$self->{open_elements}->[-1]->[1]}) {
5062                  !!!back-token;
5063                  $token = {type => 'end tag',
5064                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5065                  redo B;
5066                }
5067            
5068                ## Step 2
5069                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5070                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5071                }
5072                
5073                ## Step 3
5074                splice @{$self->{open_elements}}, $node_i;
5075    
5076                !!!next-token;
5077                last S2;
5078              } else {
5079                ## Step 3
5080                if (not $formatting_category->{$node->[1]} and
5081                    #not $phrasing_category->{$node->[1]} and
5082                    ($special_category->{$node->[1]} or
5083                     $scoping_category->{$node->[1]})) {
5084                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5085                  ## Ignore the token
5086                  !!!next-token;
5087                  last S2;
5088                }
5089              }
5090              
5091              ## Step 4
5092              $node_i--;
5093              $node = $self->{open_elements}->[$node_i];
5094              
5095              ## Step 5;
5096              redo S2;
5097            } # S2
5098            redo B;
5099        }        }
5100      }      }
5101        redo B;
5102    } # B    } # B
5103    
5104      ## NOTE: The "trailing end" phase in HTML5 is split into
5105      ## two insertion modes: "after html body" and "after html frameset".
5106      ## NOTE: States in the main stage is preserved while
5107      ## the parser stays in the trailing end phase. # MUST
5108    
5109    ## Stop parsing # MUST    ## Stop parsing # MUST
5110        
5111    ## TODO: script stuffs    ## TODO: script stuffs
# Line 4868  sub set_inner_html ($$$) { Line 5139  sub set_inner_html ($$$) {
5139      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5140    
5141      ## Step 1 # MUST      ## Step 1 # MUST
5142      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5143      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5144        $doc->manakai_is_html (1);
5145      my $p = $class->new;      my $p = $class->new;
5146      $p->{document} = $doc;      $p->{document} = $doc;
5147    
# Line 4879  sub set_inner_html ($$$) { Line 5151  sub set_inner_html ($$$) {
5151      my $column = 0;      my $column = 0;
5152      $p->{set_next_input_character} = sub {      $p->{set_next_input_character} = sub {
5153        my $self = shift;        my $self = shift;
5154    
5155          pop @{$self->{prev_input_character}};
5156          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5157    
5158        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_input_character} = -1 and return if $i >= length $$s;
5159        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_input_character} = ord substr $$s, $i++, 1;
5160        $column++;        $column++;
# Line 4887  sub set_inner_html ($$$) { Line 5163  sub set_inner_html ($$$) {
5163          $line++;          $line++;
5164          $column = 0;          $column = 0;
5165        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_input_character} == 0x000D) { # CR
5166          if ($i >= length $$s) {          $i++ if substr ($$s, $i, 1) eq "\x0A";
           #  
         } else {  
           my $next_char = ord substr $$s, $i++, 1;  
           if ($next_char == 0x000A) { # LF  
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
         }  
5167          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_input_character} = 0x000A; # LF # MUST
5168          $line++;          $line++;
5169          $column = 0;          $column = 0;
5170        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_input_character} > 0x10FFFF) {
5171          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5172        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_input_character} == 0x0000) { # NULL
5173            !!!parse-error (type => 'NULL');
5174          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5175        }        }
5176      };      };
5177        $p->{prev_input_character} = [-1, -1, -1];
5178        $p->{next_input_character} = -1;
5179            
5180      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5181        my (%opt) = @_;        my (%opt) = @_;
# Line 4920  sub set_inner_html ($$$) { Line 5190  sub set_inner_html ($$$) {
5190    
5191      ## Step 2      ## Step 2
5192      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5193      $p->{content_model_flag} = {      $p->{content_model} = {
5194        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5195        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5196        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5197        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5198        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5199        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5200        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5201        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5202        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5203        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5204      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5205         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5206            unless defined $p->{content_model};
5207            ## ISSUE: What is "the name of the element"? local name?
5208    
5209      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5210    
# Line 4984  sub set_inner_html ($$$) { Line 5256  sub set_inner_html ($$$) {
5256      ## Step 12 # MUST      ## Step 12 # MUST
5257      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5258      for (@cn) {      for (@cn) {
5259          $this_doc->adopt_node ($_);
5260        $node->append_child ($_);        $node->append_child ($_);
5261      }      }
5262      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5263    
5264      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5265    } else {    } else {
# Line 5031  sub get_inner_html ($$$) { Line 5304  sub get_inner_html ($$$) {
5304            
5305      my $nt = $child->node_type;      my $nt = $child->node_type;
5306      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5307        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5308        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5309          ## NOTE: Non-HTML case:
5310        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5311    
5312        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5313        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5314          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5315          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5316          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5317          ## escape          ## escape
# Line 5057  sub get_inner_html ($$$) { Line 5330  sub get_inner_html ($$$) {
5330          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5331        }->{$tag_name};        }->{$tag_name};
5332    
5333          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5334    
5335        if (not $in_cdata and {        if (not $in_cdata and {
5336          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5337          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5338            plaintext => 1,
5339        }->{$tag_name}) {        }->{$tag_name}) {
5340          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5341          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.53

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24