/[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.3 by wakaba, Wed May 2 13:44:34 2007 UTC revision 1.54 by wakaba, Sat Aug 11 06:37:12 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    
30  my $entity_char = {  my $c1_entity_char = {
31    AElig => "\x{00C6}",    0x80 => 0x20AC,
32    Aacute => "\x{00C1}",    0x81 => 0xFFFD,
33    Acirc => "\x{00C2}",    0x82 => 0x201A,
34    Agrave => "\x{00C0}",    0x83 => 0x0192,
35    Alpha => "\x{0391}",    0x84 => 0x201E,
36    Aring => "\x{00C5}",    0x85 => 0x2026,
37    Atilde => "\x{00C3}",    0x86 => 0x2020,
38    Auml => "\x{00C4}",    0x87 => 0x2021,
39    Beta => "\x{0392}",    0x88 => 0x02C6,
40    Ccedil => "\x{00C7}",    0x89 => 0x2030,
41    Chi => "\x{03A7}",    0x8A => 0x0160,
42    Dagger => "\x{2021}",    0x8B => 0x2039,
43    Delta => "\x{0394}",    0x8C => 0x0152,
44    ETH => "\x{00D0}",    0x8D => 0xFFFD,
45    Eacute => "\x{00C9}",    0x8E => 0x017D,
46    Ecirc => "\x{00CA}",    0x8F => 0xFFFD,
47    Egrave => "\x{00C8}",    0x90 => 0xFFFD,
48    Epsilon => "\x{0395}",    0x91 => 0x2018,
49    Eta => "\x{0397}",    0x92 => 0x2019,
50    Euml => "\x{00CB}",    0x93 => 0x201C,
51    Gamma => "\x{0393}",    0x94 => 0x201D,
52    Iacute => "\x{00CD}",    0x95 => 0x2022,
53    Icirc => "\x{00CE}",    0x96 => 0x2013,
54    Igrave => "\x{00CC}",    0x97 => 0x2014,
55    Iota => "\x{0399}",    0x98 => 0x02DC,
56    Iuml => "\x{00CF}",    0x99 => 0x2122,
57    Kappa => "\x{039A}",    0x9A => 0x0161,
58    Lambda => "\x{039B}",    0x9B => 0x203A,
59    Mu => "\x{039C}",    0x9C => 0x0153,
60    Ntilde => "\x{00D1}",    0x9D => 0xFFFD,
61    Nu => "\x{039D}",    0x9E => 0x017E,
62    OElig => "\x{0152}",    0x9F => 0x0178,
63    Oacute => "\x{00D3}",  }; # $c1_entity_char
   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}",  
 };  
64    
65  my $special_category = {  my $special_category = {
66    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,
# Line 314  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++;
106            
107      if ($self->{next_input_character} == 0x000D) { # CR      if ($self->{next_input_character} == 0x000A) { # LF
108        if ($i >= length $$s) {        $line++;
109          #        $column = 0;
110        } else {      } elsif ($self->{next_input_character} == 0x000D) { # CR
111          my $next_char = ord substr $$s, $i++, 1;        $i++ if substr ($$s, $i, 1) eq "\x0A";
         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 = -1;        $column = 0;
115      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_input_character} > 0x10FFFF) {
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');
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 367  sub new ($) { Line 150  sub new ($) {
150    return $self;    return $self;
151  } # new  } # new
152    
153    sub CM_ENTITY () { 0b001 } # & markup in data
154    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
155    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
156    
157    sub PLAINTEXT_CONTENT_MODEL () { 0 }
158    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
159    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162    sub AFTER_HTML_IMS () { 0b100 }
163    sub HEAD_IMS ()       { 0b1000 }
164    sub BODY_IMS ()       { 0b10000 }
165    sub BODY_TABLE_IMS () { 0b100000 | BODY_IMS }
166    sub TABLE_IMS ()      { 0b1000000 }
167    sub ROW_IMS ()        { 0b10000000 | TABLE_IMS }
168    sub BODY_AFTER_IMS () { 0b100000000 }
169    sub FRAME_IMS ()      { 0b1000000000 }
170    
171    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
172    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
173    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
174    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
175    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
176    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
177    sub IN_BODY_IM () { BODY_IMS }
178    sub IN_CELL_IM () { BODY_TABLE_IMS | 0b01 }
179    sub IN_CAPTION_IM () { BODY_TABLE_IMS | 0b10 }
180    sub IN_ROW_IM () { ROW_IMS | 0b01 }
181    sub IN_TABLE_BODY_IM () { ROW_IMS | 0b10 }
182    sub IN_TABLE_IM () { TABLE_IMS }
183    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
184    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
185    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
186    sub IN_SELECT_IM () { 0b01 }
187    sub IN_COLUMN_GROUP_IM () { 0b10 }
188    
189  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
190    
191  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
192    my $self = shift;    my $self = shift;
193    $self->{state} = 'data'; # MUST    $self->{state} = 'data'; # MUST
194    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
195    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
196    undef $self->{current_attribute};    undef $self->{current_attribute};
197    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 381  sub _initialize_tokenizer ($) { Line 200  sub _initialize_tokenizer ($) {
200    # $self->{next_input_character}    # $self->{next_input_character}
201    !!!next-input-character;    !!!next-input-character;
202    $self->{token} = [];    $self->{token} = [];
203      # $self->{escape}
204  } # _initialize_tokenizer  } # _initialize_tokenizer
205    
206  ## A token has:  ## A token has:
207  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',
208  ##       'character', or 'end-of-file'  ##       'character', or 'end-of-file'
209  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))
210      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{public_identifier} (DOCTYPE)
211  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE)
212    ##   ->{correct} == 1 or 0 (DOCTYPE)
213  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{attributes} isa HASH (start tag, end tag)
214  ##   ->{data} (comment, character)  ##   ->{data} (comment, character)
215    
 ## Macros  
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
   
216  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
217    
218  ## 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 414  sub _get_next_token ($) { Line 230  sub _get_next_token ($) {
230    A: {    A: {
231      if ($self->{state} eq 'data') {      if ($self->{state} eq 'data') {
232        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
233          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
             $self->{content_model_flag} eq 'RCDATA') {  
234            $self->{state} = 'entity data';            $self->{state} = 'entity data';
235            !!!next-input-character;            !!!next-input-character;
236            redo A;            redo A;
237          } else {          } else {
238            #            #
239          }          }
240          } elsif ($self->{next_input_character} == 0x002D) { # -
241            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
242              unless ($self->{escape}) {
243                if ($self->{prev_input_character}->[0] == 0x002D and # -
244                    $self->{prev_input_character}->[1] == 0x0021 and # !
245                    $self->{prev_input_character}->[2] == 0x003C) { # <
246                  $self->{escape} = 1;
247                }
248              }
249            }
250            
251            #
252        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
253          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
254                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
255                 not $self->{escape})) {
256            $self->{state} = 'tag open';            $self->{state} = 'tag open';
257            !!!next-input-character;            !!!next-input-character;
258            redo A;            redo A;
259          } else {          } else {
260            #            #
261          }          }
262          } elsif ($self->{next_input_character} == 0x003E) { # >
263            if ($self->{escape} and
264                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
265              if ($self->{prev_input_character}->[0] == 0x002D and # -
266                  $self->{prev_input_character}->[1] == 0x002D) { # -
267                delete $self->{escape};
268              }
269            }
270            
271            #
272        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
273          !!!emit ({type => 'end-of-file'});          !!!emit ({type => 'end-of-file'});
274          last A; ## TODO: ok?          last A; ## TODO: ok?
# Line 446  sub _get_next_token ($) { Line 285  sub _get_next_token ($) {
285      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
286        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
287                
288        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
289    
290        $self->{state} = 'data';        $self->{state} = 'data';
291        # next-input-character is already done        # next-input-character is already done
# Line 459  sub _get_next_token ($) { Line 298  sub _get_next_token ($) {
298    
299        redo A;        redo A;
300      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} eq 'tag open') {
301        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
302          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
303            !!!next-input-character;            !!!next-input-character;
304            $self->{state} = 'close tag open';            $self->{state} = 'close tag open';
# Line 473  sub _get_next_token ($) { Line 311  sub _get_next_token ($) {
311    
312            redo A;            redo A;
313          }          }
314        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
315          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
316            $self->{state} = 'markup declaration open';            $self->{state} = 'markup declaration open';
317            !!!next-input-character;            !!!next-input-character;
# Line 520  sub _get_next_token ($) { Line 358  sub _get_next_token ($) {
358            redo A;            redo A;
359          }          }
360        } else {        } else {
361          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
362        }        }
363      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
364        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
365            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
366          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
367          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
368              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
369                push @next_char, $self->{next_input_character};
370                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
371                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
372                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
373                  !!!next-input-character;
374                  next TAGNAME;
375                } else {
376                  $self->{next_input_character} = shift @next_char; # reconsume
377                  !!!back-next-input-character (@next_char);
378                  $self->{state} = 'data';
379    
380                  !!!emit ({type => 'character', data => '</'});
381      
382                  redo A;
383                }
384              }
385            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
386            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
387            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
388            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
389              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
390              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
391            } else {                    $self->{next_input_character} == 0x0020 or # SP
392              !!!parse-error (type => 'unmatched end tag');                    $self->{next_input_character} == 0x003E or # >
393                      $self->{next_input_character} == 0x002F or # /
394                      $self->{next_input_character} == -1) {
395              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
396              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
397              $self->{state} = 'data';              $self->{state} = 'data';
   
398              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => 'character', data => '</'});
   
399              redo A;              redo A;
400              } else {
401                $self->{next_input_character} = shift @next_char;
402                !!!back-next-input-character (@next_char);
403                # and consume...
404            }            }
405          }          } else {
406          push @next_char, $self->{next_input_character};            ## No start tag token has ever been emitted
407                  # 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);  
408            $self->{state} = 'data';            $self->{state} = 'data';
   
409            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => 'character', data => '</'});
   
410            redo A;            redo A;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
411          }          }
412        }        }
413                
# Line 614  sub _get_next_token ($) { Line 455  sub _get_next_token ($) {
455          redo A;          redo A;
456        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
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 627  sub _get_next_token ($) { Line 470  sub _get_next_token ($) {
470          !!!next-input-character;          !!!next-input-character;
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 (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 637  sub _get_next_token ($) { Line 479  sub _get_next_token ($) {
479          ## Stay in this state          ## Stay in this state
480          !!!next-input-character;          !!!next-input-character;
481          redo A;          redo A;
482        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
483          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
484          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
485              $self->{current_token}->{first_start_tag}
486                  = not defined $self->{last_emitted_start_tag_name};
487            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
488          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
489            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
490            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
491              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
492            }            }
# Line 654  sub _get_next_token ($) { Line 497  sub _get_next_token ($) {
497          # reconsume          # reconsume
498    
499          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
500    
501          redo A;          redo A;
502        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
# Line 688  sub _get_next_token ($) { Line 530  sub _get_next_token ($) {
530          redo A;          redo A;
531        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
532          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
533              $self->{current_token}->{first_start_tag}
534                  = not defined $self->{last_emitted_start_tag_name};
535            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
536          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
537            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
538            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
539              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
540            }            }
# Line 701  sub _get_next_token ($) { Line 545  sub _get_next_token ($) {
545          !!!next-input-character;          !!!next-input-character;
546    
547          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
548    
549          redo A;          redo A;
550        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 724  sub _get_next_token ($) { Line 567  sub _get_next_token ($) {
567          ## Stay in the state          ## Stay in the state
568          # next-input-character is already done          # next-input-character is already done
569          redo A;          redo A;
570        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
571          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
572          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
573              $self->{current_token}->{first_start_tag}
574                  = not defined $self->{last_emitted_start_tag_name};
575            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
576          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
577            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
578            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
579              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
580            }            }
# Line 741  sub _get_next_token ($) { Line 585  sub _get_next_token ($) {
585          # reconsume          # reconsume
586    
587          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
588    
589          redo A;          redo A;
590        } else {        } else {
# Line 755  sub _get_next_token ($) { Line 598  sub _get_next_token ($) {
598        my $before_leave = sub {        my $before_leave = sub {
599          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
600              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
601            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
602            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
603          } else {          } else {
604            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 780  sub _get_next_token ($) { Line 623  sub _get_next_token ($) {
623        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
624          $before_leave->();          $before_leave->();
625          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
626              $self->{current_token}->{first_start_tag}
627                  = not defined $self->{last_emitted_start_tag_name};
628            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
629          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
630            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
631            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
632              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
633            }            }
# Line 793  sub _get_next_token ($) { Line 638  sub _get_next_token ($) {
638          !!!next-input-character;          !!!next-input-character;
639    
640          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
641    
642          redo A;          redo A;
643        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 816  sub _get_next_token ($) { Line 660  sub _get_next_token ($) {
660          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
661          # next-input-character is already done          # next-input-character is already done
662          redo A;          redo A;
663        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
664          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
665          $before_leave->();          $before_leave->();
666          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
667              $self->{current_token}->{first_start_tag}
668                  = not defined $self->{last_emitted_start_tag_name};
669            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
670          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
671            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
672            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
673              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
674            }            }
# Line 834  sub _get_next_token ($) { Line 679  sub _get_next_token ($) {
679          # reconsume          # reconsume
680    
681          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
682    
683          redo A;          redo A;
684        } else {        } else {
# Line 858  sub _get_next_token ($) { Line 702  sub _get_next_token ($) {
702          redo A;          redo A;
703        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
704          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
705              $self->{current_token}->{first_start_tag}
706                  = not defined $self->{last_emitted_start_tag_name};
707            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
708          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
709            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
710            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
711              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
712            }            }
# Line 871  sub _get_next_token ($) { Line 717  sub _get_next_token ($) {
717          !!!next-input-character;          !!!next-input-character;
718    
719          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
720    
721          redo A;          redo A;
722        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 890  sub _get_next_token ($) { Line 735  sub _get_next_token ($) {
735            #            #
736          } else {          } else {
737            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
738              ## TODO: Different error type for <aa / bb> than <aa/>
739          }          }
740          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
741          # next-input-character is already done          # next-input-character is already done
742          redo A;          redo A;
743        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
744          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
745          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
746              $self->{current_token}->{first_start_tag}
747                  = not defined $self->{last_emitted_start_tag_name};
748            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
749          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
750            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
751            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
752              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
753            }            }
# Line 911  sub _get_next_token ($) { Line 758  sub _get_next_token ($) {
758          # reconsume          # reconsume
759    
760          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
761    
762          redo A;          redo A;
763        } else {        } else {
# Line 944  sub _get_next_token ($) { Line 790  sub _get_next_token ($) {
790          redo A;          redo A;
791        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
792          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
793              $self->{current_token}->{first_start_tag}
794                  = not defined $self->{last_emitted_start_tag_name};
795            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
796          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
797            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
798            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
799              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
800            }            }
# Line 957  sub _get_next_token ($) { Line 805  sub _get_next_token ($) {
805          !!!next-input-character;          !!!next-input-character;
806    
807          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
808    
809          redo A;          redo A;
810        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
811          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
812          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
813              $self->{current_token}->{first_start_tag}
814                  = not defined $self->{last_emitted_start_tag_name};
815            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
816          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
817            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
818            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
819              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
820            }            }
# Line 977  sub _get_next_token ($) { Line 825  sub _get_next_token ($) {
825          ## reconsume          ## reconsume
826    
827          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
828    
829          redo A;          redo A;
830        } else {        } else {
# Line 999  sub _get_next_token ($) { Line 846  sub _get_next_token ($) {
846        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
847          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
848          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
849              $self->{current_token}->{first_start_tag}
850                  = not defined $self->{last_emitted_start_tag_name};
851            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
852          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
853            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
854            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
855              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
856            }            }
# Line 1012  sub _get_next_token ($) { Line 861  sub _get_next_token ($) {
861          ## reconsume          ## reconsume
862    
863          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
864    
865          redo A;          redo A;
866        } else {        } else {
# Line 1034  sub _get_next_token ($) { Line 882  sub _get_next_token ($) {
882        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
883          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
884          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
885              $self->{current_token}->{first_start_tag}
886                  = not defined $self->{last_emitted_start_tag_name};
887            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
888          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
889            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
890            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
891              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
892            }            }
# Line 1047  sub _get_next_token ($) { Line 897  sub _get_next_token ($) {
897          ## reconsume          ## reconsume
898    
899          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
900    
901          redo A;          redo A;
902        } else {        } else {
# Line 1072  sub _get_next_token ($) { Line 921  sub _get_next_token ($) {
921          redo A;          redo A;
922        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
923          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
924              $self->{current_token}->{first_start_tag}
925                  = not defined $self->{last_emitted_start_tag_name};
926            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
927          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
928            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
929            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
930              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
931            }            }
# Line 1085  sub _get_next_token ($) { Line 936  sub _get_next_token ($) {
936          !!!next-input-character;          !!!next-input-character;
937    
938          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
939    
940          redo A;          redo A;
941        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
942          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
943          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
944              $self->{current_token}->{first_start_tag}
945                  = not defined $self->{last_emitted_start_tag_name};
946            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
947          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
948            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
949            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
950              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
951            }            }
# Line 1105  sub _get_next_token ($) { Line 956  sub _get_next_token ($) {
956          ## reconsume          ## reconsume
957    
958          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
959    
960          redo A;          redo A;
961        } else {        } else {
# Line 1115  sub _get_next_token ($) { Line 965  sub _get_next_token ($) {
965          redo A;          redo A;
966        }        }
967      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
968        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
969    
970        unless (defined $token) {        unless (defined $token) {
971          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 1164  sub _get_next_token ($) { Line 1014  sub _get_next_token ($) {
1014          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1015          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1016            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => 'comment', data => ''};
1017            $self->{state} = 'comment';            $self->{state} = 'comment start';
1018            !!!next-input-character;            !!!next-input-character;
1019            redo A;            redo A;
1020          }          }
# Line 1206  sub _get_next_token ($) { Line 1056  sub _get_next_token ($) {
1056          }          }
1057        }        }
1058    
1059        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1060        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1061        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1062        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1214  sub _get_next_token ($) { Line 1064  sub _get_next_token ($) {
1064                
1065        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1066        ## 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?
1067        } elsif ($self->{state} eq 'comment start') {
1068          if ($self->{next_input_character} == 0x002D) { # -
1069            $self->{state} = 'comment start dash';
1070            !!!next-input-character;
1071            redo A;
1072          } elsif ($self->{next_input_character} == 0x003E) { # >
1073            !!!parse-error (type => 'bogus comment');
1074            $self->{state} = 'data';
1075            !!!next-input-character;
1076    
1077            !!!emit ($self->{current_token}); # comment
1078    
1079            redo A;
1080          } elsif ($self->{next_input_character} == -1) {
1081            !!!parse-error (type => 'unclosed comment');
1082            $self->{state} = 'data';
1083            ## reconsume
1084    
1085            !!!emit ($self->{current_token}); # comment
1086    
1087            redo A;
1088          } else {
1089            $self->{current_token}->{data} # comment
1090                .= chr ($self->{next_input_character});
1091            $self->{state} = 'comment';
1092            !!!next-input-character;
1093            redo A;
1094          }
1095        } elsif ($self->{state} eq 'comment start dash') {
1096          if ($self->{next_input_character} == 0x002D) { # -
1097            $self->{state} = 'comment end';
1098            !!!next-input-character;
1099            redo A;
1100          } elsif ($self->{next_input_character} == 0x003E) { # >
1101            !!!parse-error (type => 'bogus comment');
1102            $self->{state} = 'data';
1103            !!!next-input-character;
1104    
1105            !!!emit ($self->{current_token}); # comment
1106    
1107            redo A;
1108          } elsif ($self->{next_input_character} == -1) {
1109            !!!parse-error (type => 'unclosed comment');
1110            $self->{state} = 'data';
1111            ## reconsume
1112    
1113            !!!emit ($self->{current_token}); # comment
1114    
1115            redo A;
1116          } else {
1117            $self->{current_token}->{data} # comment
1118                .= '-' . chr ($self->{next_input_character});
1119            $self->{state} = 'comment';
1120            !!!next-input-character;
1121            redo A;
1122          }
1123      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} eq 'comment') {
1124        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1125          $self->{state} = 'comment dash';          $self->{state} = 'comment end dash';
1126          !!!next-input-character;          !!!next-input-character;
1127          redo A;          redo A;
1128        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1225  sub _get_next_token ($) { Line 1131  sub _get_next_token ($) {
1131          ## reconsume          ## reconsume
1132    
1133          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1134    
1135          redo A;          redo A;
1136        } else {        } else {
# Line 1234  sub _get_next_token ($) { Line 1139  sub _get_next_token ($) {
1139          !!!next-input-character;          !!!next-input-character;
1140          redo A;          redo A;
1141        }        }
1142      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} eq 'comment end dash') {
1143        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1144          $self->{state} = 'comment end';          $self->{state} = 'comment end';
1145          !!!next-input-character;          !!!next-input-character;
# Line 1245  sub _get_next_token ($) { Line 1150  sub _get_next_token ($) {
1150          ## reconsume          ## reconsume
1151    
1152          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1153    
1154          redo A;          redo A;
1155        } else {        } else {
# Line 1260  sub _get_next_token ($) { Line 1164  sub _get_next_token ($) {
1164          !!!next-input-character;          !!!next-input-character;
1165    
1166          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1167    
1168          redo A;          redo A;
1169        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1275  sub _get_next_token ($) { Line 1178  sub _get_next_token ($) {
1178          ## reconsume          ## reconsume
1179    
1180          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1181    
1182          redo A;          redo A;
1183        } else {        } else {
# Line 1309  sub _get_next_token ($) { Line 1211  sub _get_next_token ($) {
1211          ## Stay in the state          ## Stay in the state
1212          !!!next-input-character;          !!!next-input-character;
1213          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
1214        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1215          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1216          $self->{state} = 'data';          $self->{state} = 'data';
1217          !!!next-input-character;          !!!next-input-character;
1218    
1219          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1220    
1221          redo A;          redo A;
1222        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1330  sub _get_next_token ($) { Line 1224  sub _get_next_token ($) {
1224          $self->{state} = 'data';          $self->{state} = 'data';
1225          ## reconsume          ## reconsume
1226    
1227          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1228    
1229          redo A;          redo A;
1230        } else {        } else {
1231          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1232                            name => chr ($self->{next_input_character}),              = {type => 'DOCTYPE',
1233                            error => 1};                 name => chr ($self->{next_input_character}),
1234                   correct => 1};
1235    ## ISSUE: "Set the token's name name to the" in the spec
1236          $self->{state} = 'DOCTYPE name';          $self->{state} = 'DOCTYPE name';
1237          !!!next-input-character;          !!!next-input-character;
1238          redo A;          redo A;
1239        }        }
1240      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} eq 'DOCTYPE name') {
1241    ## ISSUE: Redundant "First," in the spec.
1242        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1243            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1244            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1245            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1246            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1247          $self->{state} = 'after DOCTYPE name';          $self->{state} = 'after DOCTYPE name';
1248          !!!next-input-character;          !!!next-input-character;
1249          redo A;          redo A;
1250        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1251          $self->{state} = 'data';          $self->{state} = 'data';
1252          !!!next-input-character;          !!!next-input-character;
1253    
1254          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1255    
1256          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;  
1257        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1258          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1259          $self->{state} = 'data';          $self->{state} = 'data';
1260          ## reconsume          ## reconsume
1261    
1262          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1263          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1264    
1265          redo A;          redo A;
1266        } else {        } else {
1267          $self->{current_token}->{name}          $self->{current_token}->{name}
1268            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1269          ## Stay in the state          ## Stay in the state
1270          !!!next-input-character;          !!!next-input-character;
1271          redo A;          redo A;
# Line 1399  sub _get_next_token ($) { Line 1284  sub _get_next_token ($) {
1284          !!!next-input-character;          !!!next-input-character;
1285    
1286          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1287    
1288          redo A;          redo A;
1289        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1407  sub _get_next_token ($) { Line 1291  sub _get_next_token ($) {
1291          $self->{state} = 'data';          $self->{state} = 'data';
1292          ## reconsume          ## reconsume
1293    
1294            delete $self->{current_token}->{correct};
1295            !!!emit ($self->{current_token}); # DOCTYPE
1296    
1297            redo A;
1298          } elsif ($self->{next_input_character} == 0x0050 or # P
1299                   $self->{next_input_character} == 0x0070) { # p
1300            !!!next-input-character;
1301            if ($self->{next_input_character} == 0x0055 or # U
1302                $self->{next_input_character} == 0x0075) { # u
1303              !!!next-input-character;
1304              if ($self->{next_input_character} == 0x0042 or # B
1305                  $self->{next_input_character} == 0x0062) { # b
1306                !!!next-input-character;
1307                if ($self->{next_input_character} == 0x004C or # L
1308                    $self->{next_input_character} == 0x006C) { # l
1309                  !!!next-input-character;
1310                  if ($self->{next_input_character} == 0x0049 or # I
1311                      $self->{next_input_character} == 0x0069) { # i
1312                    !!!next-input-character;
1313                    if ($self->{next_input_character} == 0x0043 or # C
1314                        $self->{next_input_character} == 0x0063) { # c
1315                      $self->{state} = 'before DOCTYPE public identifier';
1316                      !!!next-input-character;
1317                      redo A;
1318                    }
1319                  }
1320                }
1321              }
1322            }
1323    
1324            #
1325          } elsif ($self->{next_input_character} == 0x0053 or # S
1326                   $self->{next_input_character} == 0x0073) { # s
1327            !!!next-input-character;
1328            if ($self->{next_input_character} == 0x0059 or # Y
1329                $self->{next_input_character} == 0x0079) { # y
1330              !!!next-input-character;
1331              if ($self->{next_input_character} == 0x0053 or # S
1332                  $self->{next_input_character} == 0x0073) { # s
1333                !!!next-input-character;
1334                if ($self->{next_input_character} == 0x0054 or # T
1335                    $self->{next_input_character} == 0x0074) { # t
1336                  !!!next-input-character;
1337                  if ($self->{next_input_character} == 0x0045 or # E
1338                      $self->{next_input_character} == 0x0065) { # e
1339                    !!!next-input-character;
1340                    if ($self->{next_input_character} == 0x004D or # M
1341                        $self->{next_input_character} == 0x006D) { # m
1342                      $self->{state} = 'before DOCTYPE system identifier';
1343                      !!!next-input-character;
1344                      redo A;
1345                    }
1346                  }
1347                }
1348              }
1349            }
1350    
1351            #
1352          } else {
1353            !!!next-input-character;
1354            #
1355          }
1356    
1357          !!!parse-error (type => 'string after DOCTYPE name');
1358          $self->{state} = 'bogus DOCTYPE';
1359          # next-input-character is already done
1360          redo A;
1361        } elsif ($self->{state} eq 'before DOCTYPE public identifier') {
1362          if ({
1363                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1364                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1365              }->{$self->{next_input_character}}) {
1366            ## Stay in the state
1367            !!!next-input-character;
1368            redo A;
1369          } elsif ($self->{next_input_character} eq 0x0022) { # "
1370            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1371            $self->{state} = 'DOCTYPE public identifier (double-quoted)';
1372            !!!next-input-character;
1373            redo A;
1374          } elsif ($self->{next_input_character} eq 0x0027) { # '
1375            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1376            $self->{state} = 'DOCTYPE public identifier (single-quoted)';
1377            !!!next-input-character;
1378            redo A;
1379          } elsif ($self->{next_input_character} eq 0x003E) { # >
1380            !!!parse-error (type => 'no PUBLIC literal');
1381    
1382            $self->{state} = 'data';
1383            !!!next-input-character;
1384    
1385            delete $self->{current_token}->{correct};
1386            !!!emit ($self->{current_token}); # DOCTYPE
1387    
1388            redo A;
1389          } elsif ($self->{next_input_character} == -1) {
1390            !!!parse-error (type => 'unclosed DOCTYPE');
1391    
1392            $self->{state} = 'data';
1393            ## reconsume
1394    
1395            delete $self->{current_token}->{correct};
1396            !!!emit ($self->{current_token}); # DOCTYPE
1397    
1398            redo A;
1399          } else {
1400            !!!parse-error (type => 'string after PUBLIC');
1401            $self->{state} = 'bogus DOCTYPE';
1402            !!!next-input-character;
1403            redo A;
1404          }
1405        } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {
1406          if ($self->{next_input_character} == 0x0022) { # "
1407            $self->{state} = 'after DOCTYPE public identifier';
1408            !!!next-input-character;
1409            redo A;
1410          } elsif ($self->{next_input_character} == -1) {
1411            !!!parse-error (type => 'unclosed PUBLIC literal');
1412    
1413            $self->{state} = 'data';
1414            ## reconsume
1415    
1416            delete $self->{current_token}->{correct};
1417            !!!emit ($self->{current_token}); # DOCTYPE
1418    
1419            redo A;
1420          } else {
1421            $self->{current_token}->{public_identifier} # DOCTYPE
1422                .= chr $self->{next_input_character};
1423            ## Stay in the state
1424            !!!next-input-character;
1425            redo A;
1426          }
1427        } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {
1428          if ($self->{next_input_character} == 0x0027) { # '
1429            $self->{state} = 'after DOCTYPE public identifier';
1430            !!!next-input-character;
1431            redo A;
1432          } elsif ($self->{next_input_character} == -1) {
1433            !!!parse-error (type => 'unclosed PUBLIC literal');
1434    
1435            $self->{state} = 'data';
1436            ## reconsume
1437    
1438            delete $self->{current_token}->{correct};
1439            !!!emit ($self->{current_token}); # DOCTYPE
1440    
1441            redo A;
1442          } else {
1443            $self->{current_token}->{public_identifier} # DOCTYPE
1444                .= chr $self->{next_input_character};
1445            ## Stay in the state
1446            !!!next-input-character;
1447            redo A;
1448          }
1449        } elsif ($self->{state} eq 'after DOCTYPE public identifier') {
1450          if ({
1451                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1452                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1453              }->{$self->{next_input_character}}) {
1454            ## Stay in the state
1455            !!!next-input-character;
1456            redo A;
1457          } elsif ($self->{next_input_character} == 0x0022) { # "
1458            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1459            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1460            !!!next-input-character;
1461            redo A;
1462          } elsif ($self->{next_input_character} == 0x0027) { # '
1463            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1464            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1465            !!!next-input-character;
1466            redo A;
1467          } elsif ($self->{next_input_character} == 0x003E) { # >
1468            $self->{state} = 'data';
1469            !!!next-input-character;
1470    
1471            !!!emit ($self->{current_token}); # DOCTYPE
1472    
1473            redo A;
1474          } elsif ($self->{next_input_character} == -1) {
1475            !!!parse-error (type => 'unclosed DOCTYPE');
1476    
1477            $self->{state} = 'data';
1478            ## reconsume
1479    
1480            delete $self->{current_token}->{correct};
1481            !!!emit ($self->{current_token}); # DOCTYPE
1482    
1483            redo A;
1484          } else {
1485            !!!parse-error (type => 'string after PUBLIC literal');
1486            $self->{state} = 'bogus DOCTYPE';
1487            !!!next-input-character;
1488            redo A;
1489          }
1490        } elsif ($self->{state} eq 'before DOCTYPE system identifier') {
1491          if ({
1492                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1493                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1494              }->{$self->{next_input_character}}) {
1495            ## Stay in the state
1496            !!!next-input-character;
1497            redo A;
1498          } elsif ($self->{next_input_character} == 0x0022) { # "
1499            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1500            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1501            !!!next-input-character;
1502            redo A;
1503          } elsif ($self->{next_input_character} == 0x0027) { # '
1504            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1505            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1506            !!!next-input-character;
1507            redo A;
1508          } elsif ($self->{next_input_character} == 0x003E) { # >
1509            !!!parse-error (type => 'no SYSTEM literal');
1510            $self->{state} = 'data';
1511            !!!next-input-character;
1512    
1513            delete $self->{current_token}->{correct};
1514            !!!emit ($self->{current_token}); # DOCTYPE
1515    
1516            redo A;
1517          } elsif ($self->{next_input_character} == -1) {
1518            !!!parse-error (type => 'unclosed DOCTYPE');
1519    
1520            $self->{state} = 'data';
1521            ## reconsume
1522    
1523            delete $self->{current_token}->{correct};
1524            !!!emit ($self->{current_token}); # DOCTYPE
1525    
1526            redo A;
1527          } else {
1528            !!!parse-error (type => 'string after SYSTEM');
1529            $self->{state} = 'bogus DOCTYPE';
1530            !!!next-input-character;
1531            redo A;
1532          }
1533        } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {
1534          if ($self->{next_input_character} == 0x0022) { # "
1535            $self->{state} = 'after DOCTYPE system identifier';
1536            !!!next-input-character;
1537            redo A;
1538          } elsif ($self->{next_input_character} == -1) {
1539            !!!parse-error (type => 'unclosed SYSTEM literal');
1540    
1541            $self->{state} = 'data';
1542            ## reconsume
1543    
1544            delete $self->{current_token}->{correct};
1545            !!!emit ($self->{current_token}); # DOCTYPE
1546    
1547            redo A;
1548          } else {
1549            $self->{current_token}->{system_identifier} # DOCTYPE
1550                .= chr $self->{next_input_character};
1551            ## Stay in the state
1552            !!!next-input-character;
1553            redo A;
1554          }
1555        } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {
1556          if ($self->{next_input_character} == 0x0027) { # '
1557            $self->{state} = 'after DOCTYPE system identifier';
1558            !!!next-input-character;
1559            redo A;
1560          } elsif ($self->{next_input_character} == -1) {
1561            !!!parse-error (type => 'unclosed SYSTEM literal');
1562    
1563            $self->{state} = 'data';
1564            ## reconsume
1565    
1566            delete $self->{current_token}->{correct};
1567            !!!emit ($self->{current_token}); # DOCTYPE
1568    
1569            redo A;
1570          } else {
1571            $self->{current_token}->{system_identifier} # DOCTYPE
1572                .= chr $self->{next_input_character};
1573            ## Stay in the state
1574            !!!next-input-character;
1575            redo A;
1576          }
1577        } elsif ($self->{state} eq 'after DOCTYPE system identifier') {
1578          if ({
1579                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1580                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1581              }->{$self->{next_input_character}}) {
1582            ## Stay in the state
1583            !!!next-input-character;
1584            redo A;
1585          } elsif ($self->{next_input_character} == 0x003E) { # >
1586            $self->{state} = 'data';
1587            !!!next-input-character;
1588    
1589            !!!emit ($self->{current_token}); # DOCTYPE
1590    
1591            redo A;
1592          } elsif ($self->{next_input_character} == -1) {
1593            !!!parse-error (type => 'unclosed DOCTYPE');
1594    
1595            $self->{state} = 'data';
1596            ## reconsume
1597    
1598            delete $self->{current_token}->{correct};
1599          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1600    
1601          redo A;          redo A;
1602        } else {        } else {
1603          !!!parse-error (type => 'string after DOCTYPE name');          !!!parse-error (type => 'string after SYSTEM literal');
         $self->{current_token}->{error} = 1; # DOCTYPE  
1604          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1605          !!!next-input-character;          !!!next-input-character;
1606          redo A;          redo A;
# Line 1423  sub _get_next_token ($) { Line 1610  sub _get_next_token ($) {
1610          $self->{state} = 'data';          $self->{state} = 'data';
1611          !!!next-input-character;          !!!next-input-character;
1612    
1613            delete $self->{current_token}->{correct};
1614          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1615    
1616          redo A;          redo A;
1617        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1432  sub _get_next_token ($) { Line 1619  sub _get_next_token ($) {
1619          $self->{state} = 'data';          $self->{state} = 'data';
1620          ## reconsume          ## reconsume
1621    
1622            delete $self->{current_token}->{correct};
1623          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1624    
1625          redo A;          redo A;
1626        } else {        } else {
# Line 1449  sub _get_next_token ($) { Line 1636  sub _get_next_token ($) {
1636    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1637  } # _get_next_token  } # _get_next_token
1638    
1639  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1640    my $self = shift;    my ($self, $in_attr) = @_;
1641      
1642    if ($self->{next_input_character} == 0x0023) { # #    if ({
1643           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1644           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1645          }->{$self->{next_input_character}}) {
1646        ## Don't consume
1647        ## No error
1648        return undef;
1649      } elsif ($self->{next_input_character} == 0x0023) { # #
1650      !!!next-input-character;      !!!next-input-character;
     my $num;  
1651      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1652          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1653          my $code;
1654        X: {        X: {
1655          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1656          !!!next-input-character;          !!!next-input-character;
1657          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1658              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1659            $num ||= 0;            $code ||= 0;
1660            $num *= 0x10;            $code *= 0x10;
1661            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1662            redo X;            redo X;
1663          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1664                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1665            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1666            $num ||= 0;            $code *= 0x10;
1667            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1668            redo X;            redo X;
1669          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1670                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1671            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1672            $num ||= 0;            $code *= 0x10;
1673            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1674            redo X;            redo X;
1675          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1676            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1677              !!!back-next-input-character ($x_char, $self->{next_input_character});
1678            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1679            return undef;            return undef;
1680          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1681            !!!next-input-character;            !!!next-input-character;
# Line 1491  sub _tokenize_attempt_to_consume_an_enti Line 1683  sub _tokenize_attempt_to_consume_an_enti
1683            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1684          }          }
1685    
1686          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1687          if ($num > 1114111 or $num == 0) {            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1688            $num = 0xFFFD; # REPLACEMENT CHARACTER            $code = 0xFFFD;
1689            ## ISSUE: Why this is not an error?          } elsif ($code > 0x10FFFF) {
1690              !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1691              $code = 0xFFFD;
1692            } elsif ($code == 0x000D) {
1693              !!!parse-error (type => 'CR character reference');
1694              $code = 0x000A;
1695            } elsif (0x80 <= $code and $code <= 0x9F) {
1696              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1697              $code = $c1_entity_char->{$code};
1698          }          }
1699    
1700          return {type => 'character', data => chr $num};          return {type => 'character', data => chr $code};
1701        } # X        } # X
1702      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1703               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1518  sub _tokenize_attempt_to_consume_an_enti Line 1718  sub _tokenize_attempt_to_consume_an_enti
1718          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1719        }        }
1720    
1721        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1722        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1723          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1724          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1725            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1726            $code = 0xFFFD;
1727          } elsif ($code == 0x000D) {
1728            !!!parse-error (type => 'CR character reference');
1729            $code = 0x000A;
1730          } elsif (0x80 <= $code and $code <= 0x9F) {
1731            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1732            $code = $c1_entity_char->{$code};
1733        }        }
1734                
1735        return {type => 'character', data => chr $code};        return {type => 'character', data => chr $code};
# Line 1539  sub _tokenize_attempt_to_consume_an_enti Line 1747  sub _tokenize_attempt_to_consume_an_enti
1747      !!!next-input-character;      !!!next-input-character;
1748    
1749      my $value = $entity_name;      my $value = $entity_name;
1750      my $match;      my $match = 0;
1751        require Whatpm::_NamedEntityList;
1752        our $EntityChar;
1753    
1754      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1755             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1756             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1757               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1758              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1759               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1760              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1761               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1762                $self->{next_input_character} == 0x003B)) { # ;
1763        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1764        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1765          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1766          $match = 1;            $value = $EntityChar->{$entity_name};
1767              $match = 1;
1768              !!!next-input-character;
1769              last;
1770            } else {
1771              $value = $EntityChar->{$entity_name};
1772              $match = -1;
1773              !!!next-input-character;
1774            }
1775        } else {        } else {
1776          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1777            $match *= 2;
1778            !!!next-input-character;
1779        }        }
       !!!next-input-character;  
1780      }      }
1781            
1782      if ($match) {      if ($match > 0) {
1783        if ($self->{next_input_character} == 0x003B) { # ;        return {type => 'character', data => $value};
1784          !!!next-input-character;      } elsif ($match < 0) {
1785          !!!parse-error (type => 'no refc');
1786          if ($in_attr and $match < -1) {
1787            return {type => 'character', data => '&'.$entity_name};
1788        } else {        } else {
1789          !!!parse-error (type => 'refc');          return {type => 'character', data => $value};
1790        }        }
   
       return {type => 'character', data => $value};  
1791      } else {      } else {
1792        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1793        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1794        !!!back-token ({type => 'character', data => $value});        return {type => 'character', data => '&'.$value};
       return undef;  
1795      }      }
1796    } else {    } else {
1797      ## no characters are consumed      ## no characters are consumed
# Line 1586  sub _initialize_tree_constructor ($) { Line 1806  sub _initialize_tree_constructor ($) {
1806    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1807    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1808    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1809    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1810  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1811    
1812  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1613  sub _construct_tree ($) { Line 1833  sub _construct_tree ($) {
1833        
1834    !!!next-token;    !!!next-token;
1835    
1836    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1837    undef $self->{form_element};    undef $self->{form_element};
1838    undef $self->{head_element};    undef $self->{head_element};
1839    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1626  sub _construct_tree ($) { Line 1846  sub _construct_tree ($) {
1846    
1847  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1848    my $self = shift;    my $self = shift;
1849    B: {    INITIAL: {
1850        if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} eq 'DOCTYPE') {
1851          if ($token->{error}) {        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1852            ## ISSUE: Spec currently left this case undefined.        ## error, switch to a conformance checking mode for another
1853            !!!parse-error (type => 'bogus DOCTYPE');        ## language.
1854          }        my $doctype_name = $token->{name};
1855          my $doctype = $self->{document}->create_document_type_definition        $doctype_name = '' unless defined $doctype_name;
1856            ($token->{name});        $doctype_name =~ tr/a-z/A-Z/;
1857          $self->{document}->append_child ($doctype);        if (not defined $token->{name} or # <!DOCTYPE>
1858          #$phase = 'root element';            defined $token->{public_identifier} or
1859          !!!next-token;            defined $token->{system_identifier}) {
1860          #redo B;          !!!parse-error (type => 'not HTML5');
1861          return;        } elsif ($doctype_name ne 'HTML') {
1862        } elsif ({          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1863                  comment => 1,          !!!parse-error (type => 'not HTML5');
1864                  'start tag' => 1,        }
1865                  'end tag' => 1,        
1866                  'end-of-file' => 1,        my $doctype = $self->{document}->create_document_type_definition
1867                 }->{$token->{type}}) {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1868          ## ISSUE: Spec currently left this case undefined.        $doctype->public_id ($token->{public_identifier})
1869          !!!parse-error (type => 'missing DOCTYPE');            if defined $token->{public_identifier};
1870          #$phase = 'root element';        $doctype->system_id ($token->{system_identifier})
1871          ## reprocess            if defined $token->{system_identifier};
1872          #redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
1873          return;        ## ISSUE: internalSubset = null??
1874        } elsif ($token->{type} eq 'character') {        $self->{document}->append_child ($doctype);
1875          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        
1876            $self->{document}->manakai_append_text ($1);        if (not $token->{correct} or $doctype_name ne 'HTML') {
1877            ## ISSUE: DOM3 Core does not allow Document > Text          $self->{document}->manakai_compat_mode ('quirks');
1878            unless (length $token->{data}) {        } elsif (defined $token->{public_identifier}) {
1879              ## Stay in the phase          my $pubid = $token->{public_identifier};
1880              !!!next-token;          $pubid =~ tr/a-z/A-z/;
1881              redo B;          if ({
1882              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1883              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1884              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1885              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1886              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1887              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1888              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1889              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1890              "-//IETF//DTD HTML 2.0//EN" => 1,
1891              "-//IETF//DTD HTML 2.1E//EN" => 1,
1892              "-//IETF//DTD HTML 3.0//EN" => 1,
1893              "-//IETF//DTD HTML 3.0//EN//" => 1,
1894              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1895              "-//IETF//DTD HTML 3.2//EN" => 1,
1896              "-//IETF//DTD HTML 3//EN" => 1,
1897              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1898              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1899              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1900              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1901              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1902              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1903              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1904              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1905              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1906              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1907              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1908              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1909              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1910              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1911              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1912              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1913              "-//IETF//DTD HTML STRICT//EN" => 1,
1914              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1915              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1916              "-//IETF//DTD HTML//EN" => 1,
1917              "-//IETF//DTD HTML//EN//2.0" => 1,
1918              "-//IETF//DTD HTML//EN//3.0" => 1,
1919              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1920              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1921              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1922              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1923              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1924              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1925              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1926              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1927              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1928              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1929              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1930              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1931              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1932              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1933              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1934              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1935              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1936              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1937              "-//W3C//DTD HTML 3.2//EN" => 1,
1938              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1939              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
1940              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
1941              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
1942              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
1943              "-//W3C//DTD W3 HTML//EN" => 1,
1944              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
1945              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
1946              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
1947              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
1948              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
1949              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
1950              "HTML" => 1,
1951            }->{$pubid}) {
1952              $self->{document}->manakai_compat_mode ('quirks');
1953            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
1954                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
1955              if (defined $token->{system_identifier}) {
1956                $self->{document}->manakai_compat_mode ('quirks');
1957              } else {
1958                $self->{document}->manakai_compat_mode ('limited quirks');
1959            }            }
1960            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
1961                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
1962              $self->{document}->manakai_compat_mode ('limited quirks');
1963          }          }
         ## 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";  
1964        }        }
1965      } # B        if (defined $token->{system_identifier}) {
1966            my $sysid = $token->{system_identifier};
1967            $sysid =~ tr/A-Z/a-z/;
1968            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1969              $self->{document}->manakai_compat_mode ('quirks');
1970            }
1971          }
1972          
1973          ## Go to the root element phase.
1974          !!!next-token;
1975          return;
1976        } elsif ({
1977                  'start tag' => 1,
1978                  'end tag' => 1,
1979                  'end-of-file' => 1,
1980                 }->{$token->{type}}) {
1981          !!!parse-error (type => 'no DOCTYPE');
1982          $self->{document}->manakai_compat_mode ('quirks');
1983          ## Go to the root element phase
1984          ## reprocess
1985          return;
1986        } elsif ($token->{type} eq 'character') {
1987          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1988            ## Ignore the token
1989    
1990            unless (length $token->{data}) {
1991              ## Stay in the phase
1992              !!!next-token;
1993              redo INITIAL;
1994            }
1995          }
1996    
1997          !!!parse-error (type => 'no DOCTYPE');
1998          $self->{document}->manakai_compat_mode ('quirks');
1999          ## Go to the root element phase
2000          ## reprocess
2001          return;
2002        } elsif ($token->{type} eq 'comment') {
2003          my $comment = $self->{document}->create_comment ($token->{data});
2004          $self->{document}->append_child ($comment);
2005          
2006          ## Stay in the phase.
2007          !!!next-token;
2008          redo INITIAL;
2009        } else {
2010          die "$0: $token->{type}: Unknown token";
2011        }
2012      } # INITIAL
2013  } # _tree_construction_initial  } # _tree_construction_initial
2014    
2015  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
# Line 1690  sub _tree_construction_root_element ($) Line 2029  sub _tree_construction_root_element ($)
2029          !!!next-token;          !!!next-token;
2030          redo B;          redo B;
2031        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'character') {
2032          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2033            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2034            ## ISSUE: DOM3 Core does not allow Document > Text  
2035            unless (length $token->{data}) {            unless (length $token->{data}) {
2036              ## Stay in the phase              ## Stay in the phase
2037              !!!next-token;              !!!next-token;
# Line 1713  sub _tree_construction_root_element ($) Line 2052  sub _tree_construction_root_element ($)
2052        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2053        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2054        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2055        ## reprocess        ## reprocess
2056        #redo B;        #redo B;
2057        return;        return; ## Go to the main phase.
2058    } # B    } # B
2059  } # _tree_construction_root_element  } # _tree_construction_root_element
2060    
# Line 1732  sub _reset_insertion_mode ($) { Line 2070  sub _reset_insertion_mode ($) {
2070            
2071      ## Step 3      ## Step 3
2072      S3: {      S3: {
2073        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2074        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2075          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2076              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2077            #        ## The second "if" is in the scope of the first "if"!?
2078          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2079            $node = $self->{inner_html_node};          $last = 1;
2080            if (defined $self->{inner_html_node}) {
2081              if ($self->{inner_html_node}->[1] eq 'td' or
2082                  $self->{inner_html_node}->[1] eq 'th') {
2083                #
2084              } else {
2085                $node = $self->{inner_html_node};
2086              }
2087          }          }
2088        }        }
2089            
2090        ## Step 4..13        ## Step 4..13
2091        my $new_mode = {        my $new_mode = {
2092                        select => 'in select',                        select => IN_SELECT_IM,
2093                        td => 'in cell',                        td => IN_CELL_IM,
2094                        th => 'in cell',                        th => IN_CELL_IM,
2095                        tr => 'in row',                        tr => IN_ROW_IM,
2096                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2097                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2098                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2099                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2100                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2101                        table => 'in table',                        table => IN_TABLE_IM,
2102                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2103                        body => 'in body',                        body => IN_BODY_IM,
2104                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2105                       }->{$node->[1]};                       }->{$node->[1]};
2106        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2107                
2108        ## Step 14        ## Step 14
2109        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2110          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2111            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2112          } else {          } else {
2113            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2114          }          }
2115          return;          return;
2116        }        }
2117                
2118        ## Step 15        ## Step 15
2119        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2120                
2121        ## Step 16        ## Step 16
2122        $i--;        $i--;
# Line 1785  sub _reset_insertion_mode ($) { Line 2130  sub _reset_insertion_mode ($) {
2130  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2131    my $self = shift;    my $self = shift;
2132    
   my $phase = 'main';  
   
2133    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2134    
2135    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1869  sub _tree_construction_main ($) { Line 2212  sub _tree_construction_main ($) {
2212      }      }
2213    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2214    
2215    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2216      my $style_el; !!!create-element ($style_el, 'style');      my ($content_model_flag, $insert) = @_;
2217      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2218      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2219       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2220        ->append_child ($style_el);      my $el;
2221      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2222                  
2223        ## Step 2
2224        $insert->($el); # /context node/->append_child ($el)
2225    
2226        ## Step 3
2227        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2228        delete $self->{escape}; # MUST
2229    
2230        ## Step 4
2231      my $text = '';      my $text = '';
2232      !!!next-token;      !!!next-token;
2233      while ($token->{type} eq 'character') {      while ($token->{type} eq 'character') { # or until stop tokenizing
2234        $text .= $token->{data};        $text .= $token->{data};
2235        !!!next-token;        !!!next-token;
2236      } # stop if non-character token or tokenizer stops tokenising      }
2237    
2238        ## Step 5
2239      if (length $text) {      if (length $text) {
2240        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2241          $el->append_child ($text);
2242      }      }
2243        
2244      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2245                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2246      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2247        ## Step 7
2248        if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2249        ## Ignore the token        ## Ignore the token
2250      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2251        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2252        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2253          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2254        } else {
2255          die "$0: $content_model_flag in parse_rcdata";
2256      }      }
2257      !!!next-token;      !!!next-token;
2258    }; # $style_start_tag    }; # $parse_rcdata
2259    
2260    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2261        my $insert = $_[0];
2262      my $script_el;      my $script_el;
2263      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2264      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2265    
2266      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2267        delete $self->{escape}; # MUST
2268            
2269      my $text = '';      my $text = '';
2270      !!!next-token;      !!!next-token;
# Line 1915  sub _tree_construction_main ($) { Line 2276  sub _tree_construction_main ($) {
2276        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2277      }      }
2278                                
2279      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2280    
2281      if ($token->{type} eq 'end tag' and      if ($token->{type} eq 'end tag' and
2282          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
# Line 1931  sub _tree_construction_main ($) { Line 2292  sub _tree_construction_main ($) {
2292      } else {      } else {
2293        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2294        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2295          
2296        (($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);  
2297                
2298        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2299                
# Line 1975  sub _tree_construction_main ($) { Line 2335  sub _tree_construction_main ($) {
2335              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
2336              last INSCOPE;              last INSCOPE;
2337            } else { # in open elements but not in scope            } else { # in open elements but not in scope
2338              !!!parse-error;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2339              ## Ignore the token              ## Ignore the token
2340              !!!next-token;              !!!next-token;
2341              return;              return;
# Line 1988  sub _tree_construction_main ($) { Line 2348  sub _tree_construction_main ($) {
2348          }          }
2349        } # INSCOPE        } # INSCOPE
2350        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
2351          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2352          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
2353          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
2354          return;          return;
2355        }        }
2356        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
2357          !!!parse-error;          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2358        }        }
2359                
2360        ## Step 2        ## Step 2
# Line 2127  sub _tree_construction_main ($) { Line 2487  sub _tree_construction_main ($) {
2487    }; # $formatting_end_tag    }; # $formatting_end_tag
2488    
2489    my $insert_to_current = sub {    my $insert_to_current = sub {
2490      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2491    }; # $insert_to_current    }; # $insert_to_current
2492    
2493    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2161  sub _tree_construction_main ($) { Line 2521  sub _tree_construction_main ($) {
2521                         }                         }
2522    }; # $insert_to_foster    }; # $insert_to_foster
2523    
2524    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  
         } 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') {  
             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') {  
             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,  
                 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 = '';  
         !!!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 CDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in RCDATA:#'.$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});  
         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  
2525    
2526    B: {    B: {
2527      if ($phase eq 'main') {      if ($token->{type} eq 'DOCTYPE') {
2528        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
2529          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
2530          ## Ignore the token        ## Stay in the phase
2531          ## Stay in the phase        !!!next-token;
2532          !!!next-token;        redo B;
2533          redo B;      } elsif ($token->{type} eq 'end-of-file') {
2534        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM or
2535                 $token->{tag_name} eq 'html') {            $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2536          ## TODO: unless it is the first start tag token, parse-error          #
2537          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') {  
2538          ## Generate implied end tags          ## Generate implied end tags
2539          if ({          if ({
2540               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,
2541                 tbody => 1, tfoot=> 1, thead => 1,
2542              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2543            !!!back-token;            !!!back-token;
2544            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
# Line 2937  sub _tree_construction_main ($) { Line 2554  sub _tree_construction_main ($) {
2554            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2555          }          }
2556    
         ## Stop parsing  
         last B;  
   
2557          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
2558          }
2559    
2560          ## Stop parsing
2561          last B;
2562        } elsif ($token->{type} eq 'start tag' and
2563                 $token->{tag_name} eq 'html') {
2564          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2565            ## Turn into the main phase
2566            !!!parse-error (type => 'after html:html');
2567            $self->{insertion_mode} = AFTER_BODY_IM;
2568          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2569            ## Turn into the main phase
2570            !!!parse-error (type => 'after html:html');
2571            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2572          }
2573    
2574    ## ISSUE: "aa<html>" is not a parse error.
2575    ## ISSUE: "<html>" in fragment is not a parse error.
2576          unless ($token->{first_start_tag}) {
2577            !!!parse-error (type => 'not first start tag');
2578          }
2579          my $top_el = $self->{open_elements}->[0]->[0];
2580          for my $attr_name (keys %{$token->{attributes}}) {
2581            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2582              $top_el->set_attribute_ns
2583                (undef, [undef, $attr_name],
2584                 $token->{attributes}->{$attr_name}->{value});
2585            }
2586          }
2587          !!!next-token;
2588          redo B;
2589        } elsif ($token->{type} eq 'comment') {
2590          my $comment = $self->{document}->create_comment ($token->{data});
2591          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM or
2592              $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2593            $self->{document}->append_child ($comment);
2594          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2595            $self->{open_elements}->[0]->[0]->append_child ($comment);
2596        } else {        } else {
2597          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2598            if ($token->{type} eq 'character') {        }
2599              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        !!!next-token;
2600                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);        redo B;
2601                unless (length $token->{data}) {      } elsif ($self->{insertion_mode} == IN_HEAD_IM or
2602                  !!!next-token;               $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM or
2603                  redo B;               $self->{insertion_mode} == AFTER_HEAD_IM or
2604                }               $self->{insertion_mode} == BEFORE_HEAD_IM) {
2605              }        if ($token->{type} eq 'character') {
2606              ## As if <head>          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2607              !!!create-element ($self->{head_element}, 'head');            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2608              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            unless (length $token->{data}) {
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
2609              !!!next-token;              !!!next-token;
2610              redo B;              redo B;
2611              }
2612            }
2613    
2614            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2615              ## As if <head>
2616              !!!create-element ($self->{head_element}, 'head');
2617              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2618              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2619    
2620              ## Reprocess in the "in head" insertion mode...
2621              pop @{$self->{open_elements}};
2622    
2623              ## Reprocess in the "after head" insertion mode...
2624            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2625              ## As if </noscript>
2626              pop @{$self->{open_elements}};
2627              !!!parse-error (type => 'in noscript:#character');
2628              
2629              ## Reprocess in the "in head" insertion mode...
2630              ## As if </head>
2631              pop @{$self->{open_elements}};
2632    
2633              ## Reprocess in the "after head" insertion mode...
2634            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2635              pop @{$self->{open_elements}};
2636    
2637              ## Reprocess in the "after head" insertion mode...
2638            }
2639    
2640                ## "after head" insertion mode
2641                ## As if <body>
2642                !!!insert-element ('body');
2643                $self->{insertion_mode} = IN_BODY_IM;
2644                ## reprocess
2645                redo B;
2646            } 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';  
2647              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2648                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2649              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2650              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2651              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2652              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2653              #  ## reprocess                  !!!next-token;
2654              } else {                  redo B;
2655                ## reprocess                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2656              }                  #
2657              redo B;                } else {
2658            } elsif ($token->{type} eq 'end tag') {                  !!!parse-error (type => 'in head:head'); # or in head noscript
2659              if ($token->{tag_name} eq 'html') {                  ## Ignore the token
2660                    !!!next-token;
2661                    redo B;
2662                  }
2663                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2664                ## As if <head>                ## As if <head>
2665                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2666                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2667                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2668                $self->{insertion_mode} = 'in head';  
2669                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2670                redo B;                ## Reprocess in the "in head" insertion mode...
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
           } 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;  
               }  
2671              }              }
               
             #  
           } 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';  
2672    
2673                my $text = '';              if ($token->{tag_name} eq 'base') {
2674                !!!next-token;                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2675                while ($token->{type} eq 'character') {                  ## As if </noscript>
2676                  $text .= $token->{data};                  pop @{$self->{open_elements}};
2677                  !!!next-token;                  !!!parse-error (type => 'in noscript:base');
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
2678                                
2679                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
2680                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
2681                  ## Ignore the token                }
2682                } else {  
2683                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                ## NOTE: There is a "as if in head" code clone.
2684                  ## ISSUE: And ignore?                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2685                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2686                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2687                }                }
2688                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2689                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2690                  pop @{$self->{open_elements}}
2691                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2692                !!!next-token;                !!!next-token;
2693                redo B;                redo B;
2694              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'link') {
2695                $style_start_tag->();                ## NOTE: There is a "as if in head" code clone.
2696                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2697              } elsif ($token->{tag_name} eq 'script') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2698                $script_start_tag->();                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2699                  }
2700                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2701                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2702                  pop @{$self->{open_elements}}
2703                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2704                  !!!next-token;
2705                redo B;                redo B;
2706              } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {              } elsif ($token->{tag_name} eq 'meta') {
2707                ## NOTE: There are "as if in head" code clones                ## NOTE: There is a "as if in head" code clone.
2708                my $el;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2709                !!!create-element ($el, $token->{tag_name}, $token->{attributes});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2710                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2711                  ->append_child ($el);                }
2712                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2713                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2714    
2715                  unless ($self->{confident}) {
2716                    my $charset;
2717                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2718                      $charset = $token->{attributes}->{charset}->{value};
2719                    }
2720                    if ($token->{attributes}->{'http-equiv'}) {
2721                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2722                      if ($token->{attributes}->{'http-equiv'}->{value}
2723                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2724                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2725                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2726                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2727                      } ## TODO: And if supported
2728                    }
2729                    ## TODO: Change the encoding
2730                  }
2731    
2732                  ## TODO: Extracting |charset| from |meta|.
2733                  pop @{$self->{open_elements}}
2734                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2735                !!!next-token;                !!!next-token;
2736                redo B;                redo B;
2737              } elsif ($token->{tag_name} eq 'head') {              } elsif ($token->{tag_name} eq 'title') {
2738                !!!parse-error (type => 'in head:head');                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2739                ## Ignore the token                  ## As if </noscript>
2740                !!!next-token;                  pop @{$self->{open_elements}};
2741                    !!!parse-error (type => 'in noscript:title');
2742                  
2743                    $self->{insertion_mode} = IN_HEAD_IM;
2744                    ## Reprocess in the "in head" insertion mode...
2745                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2746                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2747                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2748                  }
2749    
2750                  ## NOTE: There is a "as if in head" code clone.
2751                  my $parent = defined $self->{head_element} ? $self->{head_element}
2752                      : $self->{open_elements}->[-1]->[0];
2753                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2754                                  sub { $parent->append_child ($_[0]) });
2755                  pop @{$self->{open_elements}}
2756                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2757                redo B;                redo B;
2758              } else {              } elsif ($token->{tag_name} eq 'style') {
2759                #                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2760              }                ## insertion mode IN_HEAD_IM)
2761            } elsif ($token->{type} eq 'end tag') {                ## NOTE: There is a "as if in head" code clone.
2762              if ($token->{tag_name} eq 'head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2763                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2764                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2765                  }
2766                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2767                  pop @{$self->{open_elements}}
2768                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2769                  redo B;
2770                } elsif ($token->{tag_name} eq 'noscript') {
2771                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2772                    ## NOTE: and scripting is disalbed
2773                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2774                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2775                    !!!next-token;
2776                    redo B;
2777                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2778                    !!!parse-error (type => 'in noscript:noscript');
2779                    ## Ignore the token
2780                    !!!next-token;
2781                    redo B;
2782                  } else {
2783                    #
2784                  }
2785                } elsif ($token->{tag_name} eq 'script') {
2786                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2787                    ## As if </noscript>
2788                    pop @{$self->{open_elements}};
2789                    !!!parse-error (type => 'in noscript:script');
2790                  
2791                    $self->{insertion_mode} = IN_HEAD_IM;
2792                    ## Reprocess in the "in head" insertion mode...
2793                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2794                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2795                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2796                  }
2797    
2798                  ## NOTE: There is a "as if in head" code clone.
2799                  $script_start_tag->($insert_to_current);
2800                  pop @{$self->{open_elements}}
2801                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2802                  redo B;
2803                } elsif ($token->{tag_name} eq 'body' or
2804                         $token->{tag_name} eq 'frameset') {
2805                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2806                    ## As if </noscript>
2807                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2808                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2809                    
2810                    ## Reprocess in the "in head" insertion mode...
2811                    ## As if </head>
2812                    pop @{$self->{open_elements}};
2813                    
2814                    ## Reprocess in the "after head" insertion mode...
2815                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2816                    pop @{$self->{open_elements}};
2817                    
2818                    ## Reprocess in the "after head" insertion mode...
2819                  }
2820    
2821                  ## "after head" insertion mode
2822                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2823                  if ($token->{tag_name} eq 'body') {
2824                    $self->{insertion_mode} = IN_BODY_IM;
2825                  } elsif ($token->{tag_name} eq 'frameset') {
2826                    $self->{insertion_mode} = IN_FRAMESET_IM;
2827                } else {                } else {
2828                  !!!parse-error (type => 'unmatched end tag:head');                  die "$0: tag name: $self->{tag_name}";
2829                }                }
               $self->{insertion_mode} = 'after head';  
2830                !!!next-token;                !!!next-token;
2831                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'html') {  
               #  
2832              } else {              } else {
2833                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                #
               ## Ignore the token  
               !!!next-token;  
               redo B;  
2834              }              }
           } else {  
             #  
           }  
2835    
2836            if ($self->{open_elements}->[-1]->[1] eq 'head') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2837              ## As if </head>                ## As if </noscript>
2838              pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2839            }                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2840            $self->{insertion_mode} = 'after head';                
2841            ## reprocess                ## Reprocess in the "in head" insertion mode...
2842            redo B;                ## As if </head>
2843                  pop @{$self->{open_elements}};
2844    
2845            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
2846          } elsif ($self->{insertion_mode} eq 'after head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2847            if ($token->{type} eq 'character') {                ## As if </head>
2848              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                pop @{$self->{open_elements}};
2849                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
2850                unless (length $token->{data}) {                ## Reprocess in the "after head" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
2851              }              }
               
             #  
           } 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 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               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;  
             } else {  
               #  
             }  
           } 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});  
2852    
2853              !!!next-token;              ## "after head" insertion mode
2854              redo B;              ## As if <body>
2855            } elsif ($token->{type} eq 'comment') {              !!!insert-element ('body');
2856              ## NOTE: There is a code clone of "comment in body".              $self->{insertion_mode} = IN_BODY_IM;
2857              my $comment = $self->{document}->create_comment ($token->{data});              ## reprocess
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
2858              redo B;              redo B;
2859            }            } elsif ($token->{type} eq 'end tag') {
2860          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
2861            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2862              ## NOTE: There are "character in table" code clones.                  ## As if <head>
2863              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
2864                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2865                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2866                unless (length $token->{data}) {  
2867                    ## Reprocess in the "in head" insertion mode...
2868                    pop @{$self->{open_elements}};
2869                    $self->{insertion_mode} = AFTER_HEAD_IM;
2870                    !!!next-token;
2871                    redo B;
2872                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2873                    ## As if </noscript>
2874                    pop @{$self->{open_elements}};
2875                    !!!parse-error (type => 'in noscript:script');
2876                    
2877                    ## Reprocess in the "in head" insertion mode...
2878                    pop @{$self->{open_elements}};
2879                    $self->{insertion_mode} = AFTER_HEAD_IM;
2880                    !!!next-token;
2881                    redo B;
2882                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2883                    pop @{$self->{open_elements}};
2884                    $self->{insertion_mode} = AFTER_HEAD_IM;
2885                  !!!next-token;                  !!!next-token;
2886                  redo B;                  redo B;
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
2887                } else {                } else {
2888                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
2889                }                }
2890              } else {              } elsif ($token->{tag_name} eq 'noscript') {
2891                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq '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]);  
2892                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2893                    $self->{insertion_mode} = IN_HEAD_IM;
2894                    !!!next-token;
2895                    redo B;
2896                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2897                    !!!parse-error (type => 'unmatched end tag:noscript');
2898                    ## Ignore the token ## ISSUE: An issue in the spec.
2899                    !!!next-token;
2900                    redo B;
2901                  } else {
2902                    #
2903                }                }
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
2904              } elsif ({              } elsif ({
2905                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
2906                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2907                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2908                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
2909                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
2910                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2911                  pop @{$self->{open_elements}};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2912                }  
2913                    $self->{insertion_mode} = IN_HEAD_IM;
2914                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
2915                $self->{insertion_mode} = $token->{tag_name} eq 'col'                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2916                  ? 'in column group' : 'in table body';                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2917                ## reprocess                  ## Ignore the token
               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>  
2918                  !!!next-token;                  !!!next-token;
2919                  redo B;                  redo B;
2920                }                }
2921                                
2922                ## generate implied end tags                #
2923                if ({              } elsif ({
2924                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
2925                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
2926                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2927                  !!!back-token; # <table>                  ## As if <head>
2928                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
2929                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2930                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
2931    
2932                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
2933                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
2934                }                }
2935    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
2936                #                #
2937              }              } else {
2938            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2939              if ($token->{tag_name} eq 'table') {                  #
2940                ## have a table element in table scope                } else {
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
2941                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2942                  ## Ignore the token                  ## Ignore the token
2943                  !!!next-token;                  !!!next-token;
2944                  redo B;                  redo B;
2945                }                }
2946                              }
               ## 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]);  
               }  
2947    
2948                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2949                  ## As if </noscript>
2950                  pop @{$self->{open_elements}};
2951                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2952                  
2953                  ## Reprocess in the "in head" insertion mode...
2954                  ## As if </head>
2955                  pop @{$self->{open_elements}};
2956    
2957                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
2958                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2959                  ## As if </head>
2960                  pop @{$self->{open_elements}};
2961    
2962                !!!next-token;                ## Reprocess in the "after head" insertion mode...
2963                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
2964                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2965                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
2966                !!!next-token;                !!!next-token;
2967                redo B;                redo B;
             } else {  
               #  
2968              }              }
2969    
2970                ## "after head" insertion mode
2971                ## As if <body>
2972                !!!insert-element ('body');
2973                $self->{insertion_mode} = IN_BODY_IM;
2974                ## reprocess
2975                redo B;
2976            } else {            } else {
2977              #              die "$0: $token->{type}: Unknown token type";
2978            }            }
2979    
2980            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
2981            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} == IN_BODY_IM or
2982            redo B;               $self->{insertion_mode} == IN_CELL_IM or
2983          } elsif ($self->{insertion_mode} eq 'in caption') {               $self->{insertion_mode} == IN_CAPTION_IM) {
2984            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
2985              ## NOTE: This is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
2986              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
2987                            
2988              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2989    
2990              !!!next-token;              !!!next-token;
2991              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;  
2992            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
2993              if ({              if ({
2994                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
2995                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2996                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
2997                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
2998                    ## have an element in table scope
2999                ## As if </caption>                  my $tn;
3000                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3001                my $i;                    my $node = $self->{open_elements}->[$_];
3002                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3003                  my $node = $self->{open_elements}->[$_];                      $tn = $node->[1];
3004                  if ($node->[1] eq 'caption') {                      last INSCOPE;
3005                    $i = $_;                    } elsif ({
3006                    last INSCOPE;                              table => 1, html => 1,
3007                  } elsif ({                             }->{$node->[1]}) {
3008                            table => 1, html => 1,                      last INSCOPE;
3009                           }->{$node->[1]}) {                    }
3010                    last INSCOPE;                  } # INSCOPE
3011                  }                    unless (defined $tn) {
3012                } # INSCOPE                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3013                unless (defined $i) {                      ## Ignore the token
3014                  !!!parse-error (type => 'unmatched end tag:caption');                      !!!next-token;
3015                  ## Ignore the token                      redo B;
3016                  !!!next-token;                    }
3017                  redo B;                  
3018                }                  ## 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]}) {  
3019                  !!!back-token; # <?>                  !!!back-token; # <?>
3020                  $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  
3021                  redo B;                  redo B;
3022                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3023                    !!!parse-error (type => 'not closed:caption');
3024                    
3025                    ## As if </caption>
3026                    ## have a table element in table scope
3027                    my $i;
3028                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3029                      my $node = $self->{open_elements}->[$_];
3030                      if ($node->[1] eq 'caption') {
3031                        $i = $_;
3032                        last INSCOPE;
3033                      } elsif ({
3034                                table => 1, html => 1,
3035                               }->{$node->[1]}) {
3036                        last INSCOPE;
3037                      }
3038                    } # INSCOPE
3039                      unless (defined $i) {
3040                        !!!parse-error (type => 'unmatched end tag:caption');
3041                        ## Ignore the token
3042                        !!!next-token;
3043                        redo B;
3044                      }
3045                    
3046                    ## generate implied end tags
3047                    if ({
3048                         dd => 1, dt => 1, li => 1, p => 1,
3049                         td => 1, th => 1, tr => 1,
3050                         tbody => 1, tfoot=> 1, thead => 1,
3051                        }->{$self->{open_elements}->[-1]->[1]}) {
3052                      !!!back-token; # <?>
3053                      $token = {type => 'end tag', tag_name => 'caption'};
3054                      !!!back-token;
3055                      $token = {type => 'end tag',
3056                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3057                      redo B;
3058                    }
3059    
3060                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3061                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3062                    }
3063                    
3064                    splice @{$self->{open_elements}}, $i;
3065                    
3066                    $clear_up_to_marker->();
3067                    
3068                    $self->{insertion_mode} = IN_TABLE_IM;
3069                    
3070                    ## reprocess
3071                    redo B;
3072                  } else {
3073                    #
3074                }                }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
3075              } else {              } else {
3076                #                #
3077              }              }
3078            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3079              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3080                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
3081                    ## have an element in table scope
3082                    my $i;
3083                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3084                      my $node = $self->{open_elements}->[$_];
3085                      if ($node->[1] eq $token->{tag_name}) {
3086                        $i = $_;
3087                        last INSCOPE;
3088                      } elsif ({
3089                                table => 1, html => 1,
3090                               }->{$node->[1]}) {
3091                        last INSCOPE;
3092                      }
3093                    } # INSCOPE
3094                      unless (defined $i) {
3095                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3096                        ## Ignore the token
3097                        !!!next-token;
3098                        redo B;
3099                      }
3100                    
3101                    ## generate implied end tags
3102                    if ({
3103                         dd => 1, dt => 1, li => 1, p => 1,
3104                         td => ($token->{tag_name} eq 'th'),
3105                         th => ($token->{tag_name} eq 'td'),
3106                         tr => 1,
3107                         tbody => 1, tfoot=> 1, thead => 1,
3108                        }->{$self->{open_elements}->[-1]->[1]}) {
3109                      !!!back-token;
3110                      $token = {type => 'end tag',
3111                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3112                      redo B;
3113                    }
3114                    
3115                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3116                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3117                    }
3118                    
3119                    splice @{$self->{open_elements}}, $i;
3120                    
3121                    $clear_up_to_marker->();
3122                    
3123                    $self->{insertion_mode} = IN_ROW_IM;
3124                    
3125                    !!!next-token;
3126                    redo B;
3127                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3128                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3129                    ## Ignore the token
3130                    !!!next-token;
3131                    redo B;
3132                  } else {
3133                    #
3134                  }
3135                } elsif ($token->{tag_name} eq 'caption') {
3136                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3137                    ## have a table element in table scope
3138                    my $i;
3139                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3140                      my $node = $self->{open_elements}->[$_];
3141                      if ($node->[1] eq $token->{tag_name}) {
3142                        $i = $_;
3143                        last INSCOPE;
3144                      } elsif ({
3145                                table => 1, html => 1,
3146                               }->{$node->[1]}) {
3147                        last INSCOPE;
3148                      }
3149                    } # INSCOPE
3150                      unless (defined $i) {
3151                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3152                        ## Ignore the token
3153                        !!!next-token;
3154                        redo B;
3155                      }
3156                    
3157                    ## generate implied end tags
3158                    if ({
3159                         dd => 1, dt => 1, li => 1, p => 1,
3160                         td => 1, th => 1, tr => 1,
3161                         tbody => 1, tfoot=> 1, thead => 1,
3162                        }->{$self->{open_elements}->[-1]->[1]}) {
3163                      !!!back-token;
3164                      $token = {type => 'end tag',
3165                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3166                      redo B;
3167                    }
3168                    
3169                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3170                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3171                    }
3172                    
3173                    splice @{$self->{open_elements}}, $i;
3174                    
3175                    $clear_up_to_marker->();
3176                    
3177                    $self->{insertion_mode} = IN_TABLE_IM;
3178                    
3179                    !!!next-token;
3180                    redo B;
3181                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3182                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3183                    ## Ignore the token
3184                    !!!next-token;
3185                    redo B;
3186                  } else {
3187                    #
3188                  }
3189                } elsif ({
3190                          table => 1, tbody => 1, tfoot => 1,
3191                          thead => 1, tr => 1,
3192                         }->{$token->{tag_name}} and
3193                         $self->{insertion_mode} == IN_CELL_IM) {
3194                  ## have an element in table scope
3195                my $i;                my $i;
3196                  my $tn;
3197                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3198                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3199                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3200                    $i = $_;                    $i = $_;
3201                    last INSCOPE;                    last INSCOPE;
3202                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3203                      $tn = $node->[1];
3204                      ## NOTE: There is exactly one |td| or |th| element
3205                      ## in scope in the stack of open elements by definition.
3206                  } elsif ({                  } elsif ({
3207                            table => 1, html => 1,                            table => 1, html => 1,
3208                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3475  sub _tree_construction_main ($) { Line 3215  sub _tree_construction_main ($) {
3215                  !!!next-token;                  !!!next-token;
3216                  redo B;                  redo B;
3217                }                }
                 
               ## 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;  
3218    
3219                $clear_up_to_marker->();                ## Close the cell
3220                  !!!back-token; # </?>
3221                $self->{insertion_mode} = 'in table';                $token = {type => 'end tag', tag_name => $tn};
   
               !!!next-token;  
3222                redo B;                redo B;
3223              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3224                         $self->{insertion_mode} == IN_CAPTION_IM) {
3225                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3226    
3227                ## As if </caption>                ## As if </caption>
# Line 3527  sub _tree_construction_main ($) { Line 3249  sub _tree_construction_main ($) {
3249                if ({                if ({
3250                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3251                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3252                       tbody => 1, tfoot=> 1, thead => 1,
3253                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3254                  !!!back-token; # </table>                  !!!back-token; # </table>
3255                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
# Line 3544  sub _tree_construction_main ($) { Line 3267  sub _tree_construction_main ($) {
3267    
3268                $clear_up_to_marker->();                $clear_up_to_marker->();
3269    
3270                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3271    
3272                ## reprocess                ## reprocess
3273                redo B;                redo B;
3274              } elsif ({              } elsif ({
3275                        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,  
3276                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3277                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} == IN_CELL_IM or
3278                ## Ignore the token                    $self->{insertion_mode} == IN_CAPTION_IM) {
3279                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');  
3280                  ## Ignore the token                  ## Ignore the token
3281                  !!!next-token;                  !!!next-token;
3282                  redo B;                  redo B;
3283                } else {                } else {
3284                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3285                }                }
3286              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3287                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3288                          thead => 1, tr => 1,
3289                         }->{$token->{tag_name}} and
3290                         $self->{insertion_mode} == IN_CAPTION_IM) {
3291                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3292                ## Ignore the token                ## Ignore the token
3293                !!!next-token;                !!!next-token;
3294                redo B;                redo B;
3295              } else {              } else {
3296                #                #
3297              }              }
3298            } else {        } else {
3299              #          die "$0: $token->{type}: Unknown token type";
3300            }        }
3301    
3302            ## As if </colgroup>        $insert = $insert_to_current;
3303            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3304              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} == IN_ROW_IM or
3305              ## Ignore the token               $self->{insertion_mode} == IN_TABLE_BODY_IM or
3306              !!!next-token;               $self->{insertion_mode} == IN_TABLE_IM) {
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
3307            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3308              ## NOTE: This is a "character in table" code clone.              ## NOTE: There are "character in table" code clones.
3309              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3310                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3311                                
# Line 3646  sub _tree_construction_main ($) { Line 3322  sub _tree_construction_main ($) {
3322              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3323              ## result in a new Text node.              ## result in a new Text node.
3324              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3325                
3326              if ({              if ({
3327                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3328                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3686  sub _tree_construction_main ($) { Line 3362  sub _tree_construction_main ($) {
3362                            
3363              !!!next-token;              !!!next-token;
3364              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;  
3365            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3366              if ({              if ({
3367                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3368                   th => 1, td => 1,                   th => 1, td => 1,
3369                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3370                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3371                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3372                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3373                           $self->{open_elements}->[-1]->[1] ne 'html') {
3374                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3375                      pop @{$self->{open_elements}};
3376                    }
3377                    
3378                    !!!insert-element ('tbody');
3379                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3380                    ## reprocess in the "in table body" insertion mode...
3381                }                }
3382    
3383                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3384                    unless ($token->{tag_name} eq 'tr') {
3385                      !!!parse-error (type => 'missing start tag:tr');
3386                    }
3387                    
3388                    ## Clear back to table body context
3389                    while (not {
3390                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3391                    }->{$self->{open_elements}->[-1]->[1]}) {
3392                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3393                      pop @{$self->{open_elements}};
3394                    }
3395                    
3396                    $self->{insertion_mode} = IN_ROW_IM;
3397                    if ($token->{tag_name} eq 'tr') {
3398                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3399                      !!!next-token;
3400                      redo B;
3401                    } else {
3402                      !!!insert-element ('tr');
3403                      ## reprocess in the "in row" insertion mode
3404                    }
3405                  }
3406    
3407                  ## Clear back to table row context
3408                while (not {                while (not {
3409                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3410                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3411                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3412                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3413                }                }
3414                                
3415                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3416                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
3417                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3418                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3419                } else {                
3420                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3421                redo B;                redo B;
3422              } elsif ({              } elsif ({
3423                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3424                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3425                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3426                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3427                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3428                my $i;                  ## As if </tr>
3429                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3430                  my $node = $self->{open_elements}->[$_];                  my $i;
3431                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3432                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3433                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3434                    $i = $_;                      $i = $_;
3435                    last INSCOPE;                      last INSCOPE;
3436                  } elsif ({                    } elsif ({
3437                            table => 1, html => 1,                              table => 1, html => 1,
3438                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3439                    last INSCOPE;                      last INSCOPE;
3440                      }
3441                    } # INSCOPE
3442                    unless (defined $i) {
3443                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3444                      ## Ignore the token
3445                      !!!next-token;
3446                      redo B;
3447                    }
3448                    
3449                    ## Clear back to table row context
3450                    while (not {
3451                      tr => 1, html => 1,
3452                    }->{$self->{open_elements}->[-1]->[1]}) {
3453                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3454                      pop @{$self->{open_elements}};
3455                    }
3456                    
3457                    pop @{$self->{open_elements}}; # tr
3458                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3459                    if ($token->{tag_name} eq 'tr') {
3460                      ## reprocess
3461                      redo B;
3462                    } else {
3463                      ## reprocess in the "in table body" insertion mode...
3464                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3465                }                }
3466    
3467                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3468                while (not {                  ## have an element in table scope
3469                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3470                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3471                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3472                      if ({
3473                           tbody => 1, thead => 1, tfoot => 1,
3474                          }->{$node->[1]}) {
3475                        $i = $_;
3476                        last INSCOPE;
3477                      } elsif ({
3478                                table => 1, html => 1,
3479                               }->{$node->[1]}) {
3480                        last INSCOPE;
3481                      }
3482                    } # INSCOPE
3483                    unless (defined $i) {
3484                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3485                      ## Ignore the token
3486                      !!!next-token;
3487                      redo B;
3488                    }
3489    
3490                    ## Clear back to table body context
3491                    while (not {
3492                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3493                    }->{$self->{open_elements}->[-1]->[1]}) {
3494                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3495                      pop @{$self->{open_elements}};
3496                    }
3497                    
3498                    ## As if <{current node}>
3499                    ## have an element in table scope
3500                    ## true by definition
3501                    
3502                    ## Clear back to table body context
3503                    ## nop by definition
3504                    
3505                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3506                    $self->{insertion_mode} = IN_TABLE_IM;
3507                    ## reprocess in "in table" insertion mode...
3508                }                }
3509    
3510                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3511                ## have an element in table scope                  ## Clear back to table context
3512                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3513                           $self->{open_elements}->[-1]->[1] ne 'html') {
3514                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3515                ## nop by definition                    pop @{$self->{open_elements}};
3516                    }
3517                pop @{$self->{open_elements}};                  
3518                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3519                ## reprocess                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3520                redo B;                  ## reprocess
3521                    redo B;
3522                  } elsif ({
3523                            caption => 1,
3524                            colgroup => 1,
3525                            tbody => 1, tfoot => 1, thead => 1,
3526                           }->{$token->{tag_name}}) {
3527                    ## Clear back to table context
3528                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3529                           $self->{open_elements}->[-1]->[1] ne 'html') {
3530                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3531                      pop @{$self->{open_elements}};
3532                    }
3533                    
3534                    push @$active_formatting_elements, ['#marker', '']
3535                        if $token->{tag_name} eq 'caption';
3536                    
3537                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3538                    $self->{insertion_mode} = {
3539                                               caption => IN_CAPTION_IM,
3540                                               colgroup => IN_COLUMN_GROUP_IM,
3541                                               tbody => IN_TABLE_BODY_IM,
3542                                               tfoot => IN_TABLE_BODY_IM,
3543                                               thead => IN_TABLE_BODY_IM,
3544                                              }->{$token->{tag_name}};
3545                    !!!next-token;
3546                    redo B;
3547                  } else {
3548                    die "$0: in table: <>: $token->{tag_name}";
3549                  }
3550              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3551                ## NOTE: This is a code clone of "table in table"                ## NOTE: There are code clones for this "table in table"
3552                !!!parse-error (type => 'not closed:table');                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3553    
3554                ## As if </table>                ## As if </table>
3555                ## have a table element in table scope                ## have a table element in table scope
# Line 3792  sub _tree_construction_main ($) { Line 3576  sub _tree_construction_main ($) {
3576                if ({                if ({
3577                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3578                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3579                       tbody => 1, tfoot=> 1, thead => 1,
3580                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3581                  !!!back-token; # <table>                  !!!back-token; # <table>
3582                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 3807  sub _tree_construction_main ($) { Line 3592  sub _tree_construction_main ($) {
3592    
3593                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3594    
3595                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3596    
3597                ## reprocess                ## reprocess
3598                redo B;                redo B;
# Line 3815  sub _tree_construction_main ($) { Line 3600  sub _tree_construction_main ($) {
3600                #                #
3601              }              }
3602            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3603              if ({              if ($token->{tag_name} eq 'tr' and
3604                   tbody => 1, tfoot => 1, thead => 1,                  $self->{insertion_mode} == IN_ROW_IM) {
                 }->{$token->{tag_name}}) {  
3605                ## have an element in table scope                ## have an element in table scope
3606                my $i;                my $i;
3607                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3838  sub _tree_construction_main ($) { Line 3622  sub _tree_construction_main ($) {
3622                  redo B;                  redo B;
3623                }                }
3624    
3625                ## Clear back to table body context                ## Clear back to table row context
3626                while (not {                while (not {
3627                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3628                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3629                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3630                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3631                }                }
3632    
3633                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3634                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3635                !!!next-token;                !!!next-token;
3636                redo B;                redo B;
3637              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3638                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3639                my $i;                  ## As if </tr>
3640                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3641                  my $node = $self->{open_elements}->[$_];                  my $i;
3642                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3643                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3644                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3645                    $i = $_;                      $i = $_;
3646                    last INSCOPE;                      last INSCOPE;
3647                  } elsif ({                    } elsif ({
3648                            table => 1, html => 1,                              table => 1, html => 1,
3649                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3650                    last INSCOPE;                      last INSCOPE;
3651                      }
3652                    } # INSCOPE
3653                    unless (defined $i) {
3654                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3655                      ## Ignore the token
3656                      !!!next-token;
3657                      redo B;
3658                  }                  }
3659                } # INSCOPE                  
3660                unless (defined $i) {                  ## Clear back to table row context
3661                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3662                  ## 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,  
3663                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3664                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3665                my $foster_parent_element;                    pop @{$self->{open_elements}};
3666                my $next_sibling;                  }
3667                my $prev_sibling;                  
3668                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3669                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3670                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3671                    if (defined $parent and $parent->node_type == 1) {                }
3672                      $foster_parent_element = $parent;  
3673                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3674                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3675                    } else {                  my $i;
3676                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3677                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
3678                      if ({
3679                           tbody => 1, thead => 1, tfoot => 1,
3680                          }->{$node->[1]}) {
3681                        $i = $_;
3682                        last INSCOPE;
3683                      } elsif ({
3684                                table => 1, html => 1,
3685                               }->{$node->[1]}) {
3686                        last INSCOPE;
3687                    }                    }
3688                    last OE;                  } # INSCOPE
3689                    unless (defined $i) {
3690                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3691                      ## Ignore the token
3692                      !!!next-token;
3693                      redo B;
3694                  }                  }
3695                } # OE                  
3696                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3697                $prev_sibling = $foster_parent_element->last_child                  while (not {
3698                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3699                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3700                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3701                  $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;  
3702                  }                  }
3703                } # INSCOPE                  
3704                unless (defined $i) {                  ## As if <{current node}>
3705                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3706                  ## Ignore the token                  ## true by definition
3707                  !!!next-token;                  
3708                  redo B;                  ## Clear back to table body context
3709                }                  ## nop by definition
3710                    
               ## 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]);  
3711                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3712                    $self->{insertion_mode} = IN_TABLE_IM;
3713                    ## reprocess in the "in table" insertion mode...
3714                }                }
3715    
               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>  
3716                ## have a table element in table scope                ## have a table element in table scope
3717                my $i;                my $i;
3718                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3719                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3720                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3721                    $i = $_;                    $i = $_;
3722                    last INSCOPE;                    last INSCOPE;
3723                  } elsif ({                  } elsif ({
# Line 4050  sub _tree_construction_main ($) { Line 3727  sub _tree_construction_main ($) {
3727                  }                  }
3728                } # INSCOPE                } # INSCOPE
3729                unless (defined $i) {                unless (defined $i) {
3730                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3731                  ## Ignore tokens </table><table>                  ## Ignore the token
3732                  !!!next-token;                  !!!next-token;
3733                  redo B;                  redo B;
3734                }                }
3735                  
3736                ## generate implied end tags                ## generate implied end tags
3737                if ({                if ({
3738                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3739                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3740                       tbody => 1, tfoot=> 1, thead => 1,
3741                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3742                  !!!back-token;                  !!!back-token;
3743                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3744                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3745                  redo B;                  redo B;
3746                }                }
3747                  
3748                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3749                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3750                }                }
3751                    
3752                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3753                  
3754                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3755                  
               ## 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';  
3756                !!!next-token;                !!!next-token;
3757                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;  
3758              } elsif ({              } elsif ({
3759                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3760                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3761                ## have an element in table scope                       ($self->{insertion_mode} == IN_ROW_IM or
3762                my $i;                        $self->{insertion_mode} == IN_TABLE_BODY_IM)) {
3763                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == IN_ROW_IM) {
3764                  my $node = $self->{open_elements}->[$_];                  ## have an element in table scope
3765                  if ($node->[1] eq $token->{tag_name}) {                  my $i;
3766                    $i = $_;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3767                    last INSCOPE;                    my $node = $self->{open_elements}->[$_];
3768                  } elsif ({                    if ($node->[1] eq $token->{tag_name}) {
3769                            table => 1, html => 1,                      $i = $_;
3770                           }->{$node->[1]}) {                      last INSCOPE;
3771                    last INSCOPE;                    } elsif ({
3772                                table => 1, html => 1,
3773                               }->{$node->[1]}) {
3774                        last INSCOPE;
3775                      }
3776                    } # INSCOPE
3777                      unless (defined $i) {
3778                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3779                        ## Ignore the token
3780                        !!!next-token;
3781                        redo B;
3782                      }
3783                    
3784                    ## As if </tr>
3785                    ## have an element in table scope
3786                    my $i;
3787                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3788                      my $node = $self->{open_elements}->[$_];
3789                      if ($node->[1] eq 'tr') {
3790                        $i = $_;
3791                        last INSCOPE;
3792                      } elsif ({
3793                                table => 1, html => 1,
3794                               }->{$node->[1]}) {
3795                        last INSCOPE;
3796                      }
3797                    } # INSCOPE
3798                      unless (defined $i) {
3799                        !!!parse-error (type => 'unmatched end tag:tr');
3800                        ## Ignore the token
3801                        !!!next-token;
3802                        redo B;
3803                      }
3804                    
3805                    ## Clear back to table row context
3806                    while (not {
3807                      tr => 1, html => 1,
3808                    }->{$self->{open_elements}->[-1]->[1]}) {
3809                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3810                      pop @{$self->{open_elements}};
3811                  }                  }
3812                } # INSCOPE                  
3813                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3814                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3815                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3816                }                }
3817    
               ## As if </tr>  
3818                ## have an element in table scope                ## have an element in table scope
3819                my $i;                my $i;
3820                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3821                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3822                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3823                    $i = $_;                    $i = $_;
3824                    last INSCOPE;                    last INSCOPE;
3825                  } elsif ({                  } elsif ({
# Line 4188  sub _tree_construction_main ($) { Line 3829  sub _tree_construction_main ($) {
3829                  }                  }
3830                } # INSCOPE                } # INSCOPE
3831                unless (defined $i) {                unless (defined $i) {
3832                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3833                  ## Ignore the token                  ## Ignore the token
3834                  !!!next-token;                  !!!next-token;
3835                  redo B;                  redo B;
3836                }                }
3837    
3838                ## Clear back to table row context                ## Clear back to table body context
3839                while (not {                while (not {
3840                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
3841                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3842                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3843                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3844                }                }
3845    
3846                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
3847                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
3848                ## reprocess                !!!next-token;
3849                redo B;                redo B;
3850              } elsif ({              } elsif ({
3851                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3852                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3853                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3854                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3855                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3856                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3857                ## Ignore the token                ## Ignore the token
# Line 4218  sub _tree_construction_main ($) { Line 3861  sub _tree_construction_main ($) {
3861                #                #
3862              }              }
3863            } else {            } else {
3864              #              die "$0: $token->{type}: Unknown token type";
3865            }            }
3866    
3867            ## 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});  
3868    
3869              !!!next-token;        $insert = $insert_to_foster;
3870              redo B;        #
3871            } elsif ($token->{type} eq 'comment') {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3872              ## NOTE: This is a code clone of "comment in body".            if ($token->{type} eq 'character') {
3873              my $comment = $self->{document}->create_comment ($token->{data});              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3874              $self->{open_elements}->[-1]->[0]->append_child ($comment);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3875              !!!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  
3876                  !!!next-token;                  !!!next-token;
3877                  redo B;                  redo B;
3878                }                }
3879                }
3880                ## Close the cell              
3881                !!!back-token; # <?>              #
3882                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} eq 'start tag') {
3883                if ($token->{tag_name} eq 'col') {
3884                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3885                  pop @{$self->{open_elements}};
3886                  !!!next-token;
3887                redo B;                redo B;
3888              } else {              } else {
3889                #                #
3890              }              }
3891            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3892              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
3893                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3894                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});  
3895                  ## Ignore the token                  ## Ignore the token
3896                  !!!next-token;                  !!!next-token;
3897                  redo B;                  redo B;
3898                  } else {
3899                    pop @{$self->{open_elements}}; # colgroup
3900                    $self->{insertion_mode} = IN_TABLE_IM;
3901                    !!!next-token;
3902                    redo B;            
3903                }                }
3904                              } elsif ($token->{tag_name} eq 'col') {
3905                ## 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});  
3906                ## Ignore the token                ## Ignore the token
3907                !!!next-token;                !!!next-token;
3908                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;  
3909              } else {              } else {
3910                #                #
3911              }              }
3912            } else {            } else {
3913              #              #
3914            }            }
3915              
3916            $in_body->($insert_to_current);            ## As if </colgroup>
3917            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3918          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
3919            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
3920              !!!next-token;              !!!next-token;
3921              redo B;              redo B;
3922            } elsif ($token->{type} eq 'comment') {            } else {
3923              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
3924              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = IN_TABLE_IM;
3925                ## reprocess
3926                redo B;
3927              }
3928        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3929              if ($token->{type} eq 'character') {
3930                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3931              !!!next-token;              !!!next-token;
3932              redo B;              redo B;
3933            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 4549  sub _tree_construction_main ($) { Line 4099  sub _tree_construction_main ($) {
4099            ## Ignore the token            ## Ignore the token
4100            !!!next-token;            !!!next-token;
4101            redo B;            redo B;
4102          } elsif ($self->{insertion_mode} eq 'after body') {      } elsif ($self->{insertion_mode} == AFTER_BODY_IM or
4103            if ($token->{type} eq 'character') {               $self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4104              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        if ($token->{type} eq 'character') {
4105                ## As if in body          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4106                $reconstruct_active_formatting_elements->($insert_to_current);            my $data = $1;
4107              ## As if in body
4108              $reconstruct_active_formatting_elements->($insert_to_current);
4109                                
4110                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4111              
4112              unless (length $token->{data}) {
4113                !!!next-token;
4114                redo B;
4115              }
4116            }
4117            
4118            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4119              !!!parse-error (type => 'after html:#character');
4120    
4121                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4122                  !!!next-token;          }
4123                  redo B;          
4124                }          ## "after body" insertion mode
4125              }          !!!parse-error (type => 'after body:#character');
4126                
4127              #          $self->{insertion_mode} = IN_BODY_IM;
4128              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4129            } elsif ($token->{type} eq 'comment') {          redo B;
4130              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} eq 'start tag') {
4131              $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4132              !!!parse-error (type => 'after html:'.$token->{tag_name});
4133              
4134              ## Reprocess in the "main" phase, "after body" insertion mode...
4135            }
4136    
4137            ## "after body" insertion mode
4138            !!!parse-error (type => 'after body:'.$token->{tag_name});
4139    
4140            $self->{insertion_mode} = IN_BODY_IM;
4141            ## reprocess
4142            redo B;
4143          } elsif ($token->{type} eq 'end tag') {
4144            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4145              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4146              
4147              $self->{insertion_mode} = AFTER_BODY_IM;
4148              ## Reprocess in the "main" phase, "after body" insertion mode...
4149            }
4150    
4151            ## "after body" insertion mode
4152            if ($token->{tag_name} eq 'html') {
4153              if (defined $self->{inner_html_node}) {
4154                !!!parse-error (type => 'unmatched end tag:html');
4155                ## Ignore the token
4156              !!!next-token;              !!!next-token;
4157              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});  
             }  
4158            } else {            } else {
4159              !!!parse-error (type => 'after body:#'.$token->{type});              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4160                !!!next-token;
4161                redo B;
4162            }            }
4163            } else {
4164              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4165    
4166            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4167            ## reprocess            ## reprocess
4168            redo B;            redo B;
4169          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4170            if ($token->{type} eq 'character') {        } else {
4171              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4172                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4173        } elsif ($self->{insertion_mode} == IN_FRAMESET_IM or
4174                unless (length $token->{data}) {               $self->{insertion_mode} == AFTER_FRAMESET_IM or
4175                  !!!next-token;               $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4176                  redo B;        if ($token->{type} eq 'character') {
4177                }          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4178              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4179              
4180              #            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);  
4181              !!!next-token;              !!!next-token;
4182              redo B;              redo B;
4183            } elsif ($token->{type} eq 'start tag') {            }
4184              if ($token->{tag_name} eq 'frameset') {          }
4185                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4186                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4187                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4188              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4189                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4190                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
4191                !!!next-token;            } else { # "after html frameset"
4192                redo B;              !!!parse-error (type => 'after html:#character');
4193              } elsif ($token->{tag_name} eq 'noframes') {  
4194                $in_body->($insert_to_current);              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4195                redo B;              ## Reprocess in the "main" phase, "after frameset"...
4196              } 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 {  
             #  
4197            }            }
4198                        
4199            if (defined $token->{tag_name}) {            ## Ignore the token.
4200              if (length $token->{data}) {
4201                ## reprocess the rest of characters
4202              } else {
4203                !!!next-token;
4204              }
4205              redo B;
4206            }
4207            
4208            die qq[$0: Character "$token->{data}"];
4209          } elsif ($token->{type} eq 'start tag') {
4210            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4211              !!!parse-error (type => 'after html:'.$token->{tag_name});
4212    
4213              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4214              ## Process in the "main" phase, "after frameset" insertion mode...
4215            }
4216    
4217            if ($token->{tag_name} eq 'frameset' and
4218                $self->{insertion_mode} == IN_FRAMESET_IM) {
4219              !!!insert-element ($token->{tag_name}, $token->{attributes});
4220              !!!next-token;
4221              redo B;
4222            } elsif ($token->{tag_name} eq 'frame' and
4223                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4224              !!!insert-element ($token->{tag_name}, $token->{attributes});
4225              pop @{$self->{open_elements}};
4226              !!!next-token;
4227              redo B;
4228            } elsif ($token->{tag_name} eq 'noframes') {
4229              ## NOTE: As if in body.
4230              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4231              redo B;
4232            } else {
4233              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4234              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4235            } else {            } else {
4236              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4237            }            }
4238            ## Ignore the token            ## Ignore the token
4239            !!!next-token;            !!!next-token;
4240            redo B;            redo B;
4241          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4242            if ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'end tag') {
4243              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4244                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4245    
4246                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4247                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
4248                  redo B;          }
               }  
             }  
4249    
4250              #          if ($token->{tag_name} eq 'frameset' and
4251            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4252              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4253              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4254                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4255                ## Ignore the token
4256              !!!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 {  
               #  
             }  
4257            } else {            } else {
4258              #              pop @{$self->{open_elements}};
4259                !!!next-token;
4260            }            }
4261              
4262            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
4263              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4264                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4265              }
4266              redo B;
4267            } elsif ($token->{tag_name} eq 'html' and
4268                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4269              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4270              !!!next-token;
4271              redo B;
4272            } else {
4273              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4274                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4275            } else {            } else {
4276              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4277            }            }
4278            ## Ignore the token            ## Ignore the token
4279            !!!next-token;            !!!next-token;
4280            redo B;            redo B;
4281            }
4282          } else {
4283            die "$0: $token->{type}: Unknown token type";
4284          }
4285    
4286            ## ISSUE: An issue in spec there        ## ISSUE: An issue in spec here
4287        } else {
4288          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4289        }
4290    
4291        ## "in body" insertion mode
4292        if ($token->{type} eq 'start tag') {
4293          if ($token->{tag_name} eq 'script') {
4294            ## NOTE: This is an "as if in head" code clone
4295            $script_start_tag->($insert);
4296            redo B;
4297          } elsif ($token->{tag_name} eq 'style') {
4298            ## NOTE: This is an "as if in head" code clone
4299            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4300            redo B;
4301          } elsif ({
4302                    base => 1, link => 1,
4303                   }->{$token->{tag_name}}) {
4304            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4305            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4306            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4307            !!!next-token;
4308            redo B;
4309          } elsif ($token->{tag_name} eq 'meta') {
4310            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4311            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4312            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4313    
4314            unless ($self->{confident}) {
4315              my $charset;
4316              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4317                $charset = $token->{attributes}->{charset}->{value};
4318              }
4319              if ($token->{attributes}->{'http-equiv'}) {
4320                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4321                if ($token->{attributes}->{'http-equiv'}->{value}
4322                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4323                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4324                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4325                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4326                } ## TODO: And if supported
4327              }
4328              ## TODO: Change the encoding
4329            }
4330    
4331            !!!next-token;
4332            redo B;
4333          } elsif ($token->{tag_name} eq 'title') {
4334            !!!parse-error (type => 'in body:title');
4335            ## NOTE: This is an "as if in head" code clone
4336            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4337              if (defined $self->{head_element}) {
4338                $self->{head_element}->append_child ($_[0]);
4339              } else {
4340                $insert->($_[0]);
4341              }
4342            });
4343            redo B;
4344          } elsif ($token->{tag_name} eq 'body') {
4345            !!!parse-error (type => 'in body:body');
4346                  
4347            if (@{$self->{open_elements}} == 1 or
4348                $self->{open_elements}->[1]->[1] ne 'body') {
4349              ## Ignore the token
4350          } else {          } else {
4351            die "$0: $self->{insertion_mode}: Unknown insertion mode";            my $body_el = $self->{open_elements}->[1]->[0];
4352              for my $attr_name (keys %{$token->{attributes}}) {
4353                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4354                  $body_el->set_attribute_ns
4355                    (undef, [undef, $attr_name],
4356                     $token->{attributes}->{$attr_name}->{value});
4357                }
4358              }
4359          }          }
       }  
     } 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  
4360          !!!next-token;          !!!next-token;
4361          redo B;          redo B;
4362        } elsif ($token->{type} eq 'comment') {        } elsif ({
4363          my $comment = $self->{document}->create_comment ($token->{data});                  address => 1, blockquote => 1, center => 1, dir => 1,
4364          $self->{document}->append_child ($comment);                  div => 1, dl => 1, fieldset => 1, listing => 1,
4365                    menu => 1, ol => 1, p => 1, ul => 1,
4366                    pre => 1,
4367                   }->{$token->{tag_name}}) {
4368            ## has a p element in scope
4369            INSCOPE: for (reverse @{$self->{open_elements}}) {
4370              if ($_->[1] eq 'p') {
4371                !!!back-token;
4372                $token = {type => 'end tag', tag_name => 'p'};
4373                redo B;
4374              } elsif ({
4375                        table => 1, caption => 1, td => 1, th => 1,
4376                        button => 1, marquee => 1, object => 1, html => 1,
4377                       }->{$_->[1]}) {
4378                last INSCOPE;
4379              }
4380            } # INSCOPE
4381              
4382            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4383            if ($token->{tag_name} eq 'pre') {
4384              !!!next-token;
4385              if ($token->{type} eq 'character') {
4386                $token->{data} =~ s/^\x0A//;
4387                unless (length $token->{data}) {
4388                  !!!next-token;
4389                }
4390              }
4391            } else {
4392              !!!next-token;
4393            }
4394            redo B;
4395          } elsif ($token->{tag_name} eq 'form') {
4396            if (defined $self->{form_element}) {
4397              !!!parse-error (type => 'in form:form');
4398              ## Ignore the token
4399              !!!next-token;
4400              redo B;
4401            } else {
4402              ## has a p element in scope
4403              INSCOPE: for (reverse @{$self->{open_elements}}) {
4404                if ($_->[1] eq 'p') {
4405                  !!!back-token;
4406                  $token = {type => 'end tag', tag_name => 'p'};
4407                  redo B;
4408                } elsif ({
4409                          table => 1, caption => 1, td => 1, th => 1,
4410                          button => 1, marquee => 1, object => 1, html => 1,
4411                         }->{$_->[1]}) {
4412                  last INSCOPE;
4413                }
4414              } # INSCOPE
4415                
4416              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4417              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4418              !!!next-token;
4419              redo B;
4420            }
4421          } elsif ($token->{tag_name} eq 'li') {
4422            ## has a p element in scope
4423            INSCOPE: for (reverse @{$self->{open_elements}}) {
4424              if ($_->[1] eq 'p') {
4425                !!!back-token;
4426                $token = {type => 'end tag', tag_name => 'p'};
4427                redo B;
4428              } elsif ({
4429                        table => 1, caption => 1, td => 1, th => 1,
4430                        button => 1, marquee => 1, object => 1, html => 1,
4431                       }->{$_->[1]}) {
4432                last INSCOPE;
4433              }
4434            } # INSCOPE
4435              
4436            ## Step 1
4437            my $i = -1;
4438            my $node = $self->{open_elements}->[$i];
4439            LI: {
4440              ## Step 2
4441              if ($node->[1] eq 'li') {
4442                if ($i != -1) {
4443                  !!!parse-error (type => 'end tag missing:'.
4444                                  $self->{open_elements}->[-1]->[1]);
4445                }
4446                splice @{$self->{open_elements}}, $i;
4447                last LI;
4448              }
4449              
4450              ## Step 3
4451              if (not $formatting_category->{$node->[1]} and
4452                  #not $phrasing_category->{$node->[1]} and
4453                  ($special_category->{$node->[1]} or
4454                   $scoping_category->{$node->[1]}) and
4455                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4456                last LI;
4457              }
4458              
4459              ## Step 4
4460              $i--;
4461              $node = $self->{open_elements}->[$i];
4462              redo LI;
4463            } # LI
4464              
4465            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4466          !!!next-token;          !!!next-token;
4467          redo B;          redo B;
4468        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4469          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## has a p element in scope
4470            my $data = $1;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4471            ## As if in the main phase.            if ($_->[1] eq 'p') {
4472            ## NOTE: The insertion mode in the main phase              !!!back-token;
4473            ## just before the phase has been changed to the trailing              $token = {type => 'end tag', tag_name => 'p'};
4474            ## end phase is either "after body" or "after frameset".              redo B;
4475            $reconstruct_active_formatting_elements->($insert_to_current)            } elsif ({
4476              if $phase eq 'main';                      table => 1, caption => 1, td => 1, th => 1,
4477                        button => 1, marquee => 1, object => 1, html => 1,
4478                       }->{$_->[1]}) {
4479                last INSCOPE;
4480              }
4481            } # INSCOPE
4482              
4483            ## Step 1
4484            my $i = -1;
4485            my $node = $self->{open_elements}->[$i];
4486            LI: {
4487              ## Step 2
4488              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4489                if ($i != -1) {
4490                  !!!parse-error (type => 'end tag missing:'.
4491                                  $self->{open_elements}->[-1]->[1]);
4492                }
4493                splice @{$self->{open_elements}}, $i;
4494                last LI;
4495              }
4496              
4497              ## Step 3
4498              if (not $formatting_category->{$node->[1]} and
4499                  #not $phrasing_category->{$node->[1]} and
4500                  ($special_category->{$node->[1]} or
4501                   $scoping_category->{$node->[1]}) and
4502                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4503                last LI;
4504              }
4505              
4506              ## Step 4
4507              $i--;
4508              $node = $self->{open_elements}->[$i];
4509              redo LI;
4510            } # LI
4511              
4512            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4513            !!!next-token;
4514            redo B;
4515          } elsif ($token->{tag_name} eq 'plaintext') {
4516            ## has a p element in scope
4517            INSCOPE: for (reverse @{$self->{open_elements}}) {
4518              if ($_->[1] eq 'p') {
4519                !!!back-token;
4520                $token = {type => 'end tag', tag_name => 'p'};
4521                redo B;
4522              } elsif ({
4523                        table => 1, caption => 1, td => 1, th => 1,
4524                        button => 1, marquee => 1, object => 1, html => 1,
4525                       }->{$_->[1]}) {
4526                last INSCOPE;
4527              }
4528            } # INSCOPE
4529              
4530            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4531              
4532            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4533              
4534            !!!next-token;
4535            redo B;
4536          } elsif ({
4537                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4538                   }->{$token->{tag_name}}) {
4539            ## has a p element in scope
4540            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4541              my $node = $self->{open_elements}->[$_];
4542              if ($node->[1] eq 'p') {
4543                !!!back-token;
4544                $token = {type => 'end tag', tag_name => 'p'};
4545                redo B;
4546              } elsif ({
4547                        table => 1, caption => 1, td => 1, th => 1,
4548                        button => 1, marquee => 1, object => 1, html => 1,
4549                       }->{$node->[1]}) {
4550                last INSCOPE;
4551              }
4552            } # INSCOPE
4553              
4554            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4555            ## has an element in scope
4556            #my $i;
4557            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4558            #  my $node = $self->{open_elements}->[$_];
4559            #  if ({
4560            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4561            #      }->{$node->[1]}) {
4562            #    $i = $_;
4563            #    last INSCOPE;
4564            #  } elsif ({
4565            #            table => 1, caption => 1, td => 1, th => 1,
4566            #            button => 1, marquee => 1, object => 1, html => 1,
4567            #           }->{$node->[1]}) {
4568            #    last INSCOPE;
4569            #  }
4570            #} # INSCOPE
4571            #  
4572            #if (defined $i) {
4573            #  !!! parse-error (type => 'in hn:hn');
4574            #  splice @{$self->{open_elements}}, $i;
4575            #}
4576              
4577            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4578              
4579            !!!next-token;
4580            redo B;
4581          } elsif ($token->{tag_name} eq 'a') {
4582            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4583              my $node = $active_formatting_elements->[$i];
4584              if ($node->[1] eq 'a') {
4585                !!!parse-error (type => 'in a:a');
4586                
4587                !!!back-token;
4588                $token = {type => 'end tag', tag_name => 'a'};
4589                $formatting_end_tag->($token->{tag_name});
4590                
4591                AFE2: for (reverse 0..$#$active_formatting_elements) {
4592                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4593                    splice @$active_formatting_elements, $_, 1;
4594                    last AFE2;
4595                  }
4596                } # AFE2
4597                OE: for (reverse 0..$#{$self->{open_elements}}) {
4598                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4599                    splice @{$self->{open_elements}}, $_, 1;
4600                    last OE;
4601                  }
4602                } # OE
4603                last AFE;
4604              } elsif ($node->[0] eq '#marker') {
4605                last AFE;
4606              }
4607            } # AFE
4608              
4609            $reconstruct_active_formatting_elements->($insert_to_current);
4610    
4611            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4612            push @$active_formatting_elements, $self->{open_elements}->[-1];
4613    
4614            !!!next-token;
4615            redo B;
4616          } elsif ({
4617                    b => 1, big => 1, em => 1, font => 1, i => 1,
4618                    s => 1, small => 1, strile => 1,
4619                    strong => 1, tt => 1, u => 1,
4620                   }->{$token->{tag_name}}) {
4621            $reconstruct_active_formatting_elements->($insert_to_current);
4622            
4623            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4624            push @$active_formatting_elements, $self->{open_elements}->[-1];
4625            
4626            !!!next-token;
4627            redo B;
4628          } elsif ($token->{tag_name} eq 'nobr') {
4629            $reconstruct_active_formatting_elements->($insert_to_current);
4630    
4631            ## has a |nobr| element in scope
4632            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4633              my $node = $self->{open_elements}->[$_];
4634              if ($node->[1] eq 'nobr') {
4635                !!!parse-error (type => 'not closed:nobr');
4636                !!!back-token;
4637                $token = {type => 'end tag', tag_name => 'nobr'};
4638                redo B;
4639              } elsif ({
4640                        table => 1, caption => 1, td => 1, th => 1,
4641                        button => 1, marquee => 1, object => 1, html => 1,
4642                       }->{$node->[1]}) {
4643                last INSCOPE;
4644              }
4645            } # INSCOPE
4646            
4647            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4648            push @$active_formatting_elements, $self->{open_elements}->[-1];
4649            
4650            !!!next-token;
4651            redo B;
4652          } elsif ($token->{tag_name} eq 'button') {
4653            ## has a button element in scope
4654            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4655              my $node = $self->{open_elements}->[$_];
4656              if ($node->[1] eq 'button') {
4657                !!!parse-error (type => 'in button:button');
4658                !!!back-token;
4659                $token = {type => 'end tag', tag_name => 'button'};
4660                redo B;
4661              } elsif ({
4662                        table => 1, caption => 1, td => 1, th => 1,
4663                        button => 1, marquee => 1, object => 1, html => 1,
4664                       }->{$node->[1]}) {
4665                last INSCOPE;
4666              }
4667            } # INSCOPE
4668              
4669            $reconstruct_active_formatting_elements->($insert_to_current);
4670              
4671            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4672            push @$active_formatting_elements, ['#marker', ''];
4673    
4674            !!!next-token;
4675            redo B;
4676          } elsif ($token->{tag_name} eq 'marquee' or
4677                   $token->{tag_name} eq 'object') {
4678            $reconstruct_active_formatting_elements->($insert_to_current);
4679            
4680            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4681            push @$active_formatting_elements, ['#marker', ''];
4682            
4683            !!!next-token;
4684            redo B;
4685          } elsif ($token->{tag_name} eq 'xmp') {
4686            $reconstruct_active_formatting_elements->($insert_to_current);
4687            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4688            redo B;
4689          } elsif ($token->{tag_name} eq 'table') {
4690            ## has a p element in scope
4691            INSCOPE: for (reverse @{$self->{open_elements}}) {
4692              if ($_->[1] eq 'p') {
4693                !!!back-token;
4694                $token = {type => 'end tag', tag_name => 'p'};
4695                redo B;
4696              } elsif ({
4697                        table => 1, caption => 1, td => 1, th => 1,
4698                        button => 1, marquee => 1, object => 1, html => 1,
4699                       }->{$_->[1]}) {
4700                last INSCOPE;
4701              }
4702            } # INSCOPE
4703              
4704            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4705              
4706            $self->{insertion_mode} = IN_TABLE_IM;
4707              
4708            !!!next-token;
4709            redo B;
4710          } elsif ({
4711                    area => 1, basefont => 1, bgsound => 1, br => 1,
4712                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4713                    image => 1,
4714                   }->{$token->{tag_name}}) {
4715            if ($token->{tag_name} eq 'image') {
4716              !!!parse-error (type => 'image');
4717              $token->{tag_name} = 'img';
4718            }
4719    
4720            ## NOTE: There is an "as if <br>" code clone.
4721            $reconstruct_active_formatting_elements->($insert_to_current);
4722            
4723            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4724            pop @{$self->{open_elements}};
4725            
4726            !!!next-token;
4727            redo B;
4728          } elsif ($token->{tag_name} eq 'hr') {
4729            ## has a p element in scope
4730            INSCOPE: for (reverse @{$self->{open_elements}}) {
4731              if ($_->[1] eq 'p') {
4732                !!!back-token;
4733                $token = {type => 'end tag', tag_name => 'p'};
4734                redo B;
4735              } elsif ({
4736                        table => 1, caption => 1, td => 1, th => 1,
4737                        button => 1, marquee => 1, object => 1, html => 1,
4738                       }->{$_->[1]}) {
4739                last INSCOPE;
4740              }
4741            } # INSCOPE
4742                        
4743            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4744            pop @{$self->{open_elements}};
4745                        
4746            !!!next-token;
4747            redo B;
4748          } elsif ($token->{tag_name} eq 'input') {
4749            $reconstruct_active_formatting_elements->($insert_to_current);
4750            
4751            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4752            ## TODO: associate with $self->{form_element} if defined
4753            pop @{$self->{open_elements}};
4754            
4755            !!!next-token;
4756            redo B;
4757          } elsif ($token->{tag_name} eq 'isindex') {
4758            !!!parse-error (type => 'isindex');
4759            
4760            if (defined $self->{form_element}) {
4761              ## Ignore the token
4762              !!!next-token;
4763              redo B;
4764            } else {
4765              my $at = $token->{attributes};
4766              my $form_attrs;
4767              $form_attrs->{action} = $at->{action} if $at->{action};
4768              my $prompt_attr = $at->{prompt};
4769              $at->{name} = {name => 'name', value => 'isindex'};
4770              delete $at->{action};
4771              delete $at->{prompt};
4772              my @tokens = (
4773                            {type => 'start tag', tag_name => 'form',
4774                             attributes => $form_attrs},
4775                            {type => 'start tag', tag_name => 'hr'},
4776                            {type => 'start tag', tag_name => 'p'},
4777                            {type => 'start tag', tag_name => 'label'},
4778                           );
4779              if ($prompt_attr) {
4780                push @tokens, {type => 'character', data => $prompt_attr->{value}};
4781              } else {
4782                push @tokens, {type => 'character',
4783                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4784                ## TODO: make this configurable
4785              }
4786              push @tokens,
4787                            {type => 'start tag', tag_name => 'input', attributes => $at},
4788                            #{type => 'character', data => ''}, # SHOULD
4789                            {type => 'end tag', tag_name => 'label'},
4790                            {type => 'end tag', tag_name => 'p'},
4791                            {type => 'start tag', tag_name => 'hr'},
4792                            {type => 'end tag', tag_name => 'form'};
4793              $token = shift @tokens;
4794              !!!back-token (@tokens);
4795              redo B;
4796            }
4797          } elsif ($token->{tag_name} eq 'textarea') {
4798            my $tag_name = $token->{tag_name};
4799            my $el;
4800            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4801            
4802            ## TODO: $self->{form_element} if defined
4803            $self->{content_model} = RCDATA_CONTENT_MODEL;
4804            delete $self->{escape}; # MUST
4805            
4806            $insert->($el);
4807            
4808            my $text = '';
4809            !!!next-token;
4810            if ($token->{type} eq 'character') {
4811              $token->{data} =~ s/^\x0A//;
4812            unless (length $token->{data}) {            unless (length $token->{data}) {
4813              !!!next-token;              !!!next-token;
             redo B;  
4814            }            }
4815          }          }
4816            while ($token->{type} eq 'character') {
4817              $text .= $token->{data};
4818              !!!next-token;
4819            }
4820            if (length $text) {
4821              $el->manakai_append_text ($text);
4822            }
4823            
4824            $self->{content_model} = PCDATA_CONTENT_MODEL;
4825            
4826            if ($token->{type} eq 'end tag' and
4827                $token->{tag_name} eq $tag_name) {
4828              ## Ignore the token
4829            } else {
4830              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4831            }
4832            !!!next-token;
4833            redo B;
4834          } elsif ({
4835                    iframe => 1,
4836                    noembed => 1,
4837                    noframes => 1,
4838                    noscript => 0, ## TODO: 1 if scripting is enabled
4839                   }->{$token->{tag_name}}) {
4840            ## NOTE: There are two "as if in body" code clones.
4841            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4842            redo B;
4843          } elsif ($token->{tag_name} eq 'select') {
4844            $reconstruct_active_formatting_elements->($insert_to_current);
4845            
4846            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4847            
4848            $self->{insertion_mode} = IN_SELECT_IM;
4849            !!!next-token;
4850            redo B;
4851          } elsif ({
4852                    caption => 1, col => 1, colgroup => 1, frame => 1,
4853                    frameset => 1, head => 1, option => 1, optgroup => 1,
4854                    tbody => 1, td => 1, tfoot => 1, th => 1,
4855                    thead => 1, tr => 1,
4856                   }->{$token->{tag_name}}) {
4857            !!!parse-error (type => 'in body:'.$token->{tag_name});
4858            ## Ignore the token
4859            !!!next-token;
4860            redo B;
4861            
4862            ## ISSUE: An issue on HTML5 new elements in the spec.
4863          } else {
4864            $reconstruct_active_formatting_elements->($insert_to_current);
4865            
4866            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4867            
4868            !!!next-token;
4869            redo B;
4870          }
4871        } elsif ($token->{type} eq 'end tag') {
4872          if ($token->{tag_name} eq 'body') {
4873            if (@{$self->{open_elements}} > 1 and
4874                $self->{open_elements}->[1]->[1] eq 'body') {
4875              for (@{$self->{open_elements}}) {
4876                unless ({
4877                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4878                           th => 1, tr => 1, body => 1, html => 1,
4879                         tbody => 1, tfoot => 1, thead => 1,
4880                        }->{$_->[1]}) {
4881                  !!!parse-error (type => 'not closed:'.$_->[1]);
4882                }
4883              }
4884    
4885          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
4886          $phase = 'main';            !!!next-token;
4887          ## reprocess            redo B;
4888            } else {
4889              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4890              ## Ignore the token
4891              !!!next-token;
4892              redo B;
4893            }
4894          } elsif ($token->{tag_name} eq 'html') {
4895            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4896              ## ISSUE: There is an issue in the spec.
4897              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4898                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4899              }
4900              $self->{insertion_mode} = AFTER_BODY_IM;
4901              ## reprocess
4902              redo B;
4903            } else {
4904              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4905              ## Ignore the token
4906              !!!next-token;
4907              redo B;
4908            }
4909          } elsif ({
4910                    address => 1, blockquote => 1, center => 1, dir => 1,
4911                    div => 1, dl => 1, fieldset => 1, listing => 1,
4912                    menu => 1, ol => 1, pre => 1, ul => 1,
4913                    p => 1,
4914                    dd => 1, dt => 1, li => 1,
4915                    button => 1, marquee => 1, object => 1,
4916                   }->{$token->{tag_name}}) {
4917            ## has an element in scope
4918            my $i;
4919            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4920              my $node = $self->{open_elements}->[$_];
4921              if ($node->[1] eq $token->{tag_name}) {
4922                ## generate implied end tags
4923                if ({
4924                     dd => ($token->{tag_name} ne 'dd'),
4925                     dt => ($token->{tag_name} ne 'dt'),
4926                     li => ($token->{tag_name} ne 'li'),
4927                     p => ($token->{tag_name} ne 'p'),
4928                     td => 1, th => 1, tr => 1,
4929                     tbody => 1, tfoot=> 1, thead => 1,
4930                    }->{$self->{open_elements}->[-1]->[1]}) {
4931                  !!!back-token;
4932                  $token = {type => 'end tag',
4933                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4934                  redo B;
4935                }
4936                $i = $_;
4937                last INSCOPE unless $token->{tag_name} eq 'p';
4938              } elsif ({
4939                        table => 1, caption => 1, td => 1, th => 1,
4940                        button => 1, marquee => 1, object => 1, html => 1,
4941                       }->{$node->[1]}) {
4942                last INSCOPE;
4943              }
4944            } # INSCOPE
4945            
4946            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4947              if (defined $i) {
4948                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4949              } else {
4950                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4951              }
4952            }
4953            
4954            if (defined $i) {
4955              splice @{$self->{open_elements}}, $i;
4956            } elsif ($token->{tag_name} eq 'p') {
4957              ## As if <p>, then reprocess the current token
4958              my $el;
4959              !!!create-element ($el, 'p');
4960              $insert->($el);
4961            }
4962            $clear_up_to_marker->()
4963              if {
4964                button => 1, marquee => 1, object => 1,
4965              }->{$token->{tag_name}};
4966            !!!next-token;
4967          redo B;          redo B;
4968        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
4969                 $token->{type} eq 'end tag') {          ## has an element in scope
4970          !!!parse-error (type => 'after html:'.$token->{tag_name});          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4971          $phase = 'main';            my $node = $self->{open_elements}->[$_];
4972          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
4973                ## generate implied end tags
4974                if ({
4975                     dd => 1, dt => 1, li => 1, p => 1,
4976                     td => 1, th => 1, tr => 1,
4977                     tbody => 1, tfoot=> 1, thead => 1,
4978                    }->{$self->{open_elements}->[-1]->[1]}) {
4979                  !!!back-token;
4980                  $token = {type => 'end tag',
4981                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4982                  redo B;
4983                }
4984                last INSCOPE;
4985              } elsif ({
4986                        table => 1, caption => 1, td => 1, th => 1,
4987                        button => 1, marquee => 1, object => 1, html => 1,
4988                       }->{$node->[1]}) {
4989                last INSCOPE;
4990              }
4991            } # INSCOPE
4992            
4993            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
4994              pop @{$self->{open_elements}};
4995            } else {
4996              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4997            }
4998    
4999            undef $self->{form_element};
5000            !!!next-token;
5001          redo B;          redo B;
5002        } elsif ($token->{type} eq 'end-of-file') {        } elsif ({
5003          ## Stop parsing                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5004          last B;                 }->{$token->{tag_name}}) {
5005            ## has an element in scope
5006            my $i;
5007            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5008              my $node = $self->{open_elements}->[$_];
5009              if ({
5010                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5011                  }->{$node->[1]}) {
5012                ## generate implied end tags
5013                if ({
5014                     dd => 1, dt => 1, li => 1, p => 1,
5015                     td => 1, th => 1, tr => 1,
5016                     tbody => 1, tfoot=> 1, thead => 1,
5017                    }->{$self->{open_elements}->[-1]->[1]}) {
5018                  !!!back-token;
5019                  $token = {type => 'end tag',
5020                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5021                  redo B;
5022                }
5023                $i = $_;
5024                last INSCOPE;
5025              } elsif ({
5026                        table => 1, caption => 1, td => 1, th => 1,
5027                        button => 1, marquee => 1, object => 1, html => 1,
5028                       }->{$node->[1]}) {
5029                last INSCOPE;
5030              }
5031            } # INSCOPE
5032            
5033            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5034              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5035            }
5036            
5037            splice @{$self->{open_elements}}, $i if defined $i;
5038            !!!next-token;
5039            redo B;
5040          } elsif ({
5041                    a => 1,
5042                    b => 1, big => 1, em => 1, font => 1, i => 1,
5043                    nobr => 1, s => 1, small => 1, strile => 1,
5044                    strong => 1, tt => 1, u => 1,
5045                   }->{$token->{tag_name}}) {
5046            $formatting_end_tag->($token->{tag_name});
5047            redo B;
5048          } elsif ($token->{tag_name} eq 'br') {
5049            !!!parse-error (type => 'unmatched end tag:br');
5050    
5051            ## As if <br>
5052            $reconstruct_active_formatting_elements->($insert_to_current);
5053            
5054            my $el;
5055            !!!create-element ($el, 'br');
5056            $insert->($el);
5057            
5058            ## Ignore the token.
5059            !!!next-token;
5060            redo B;
5061          } elsif ({
5062                    caption => 1, col => 1, colgroup => 1, frame => 1,
5063                    frameset => 1, head => 1, option => 1, optgroup => 1,
5064                    tbody => 1, td => 1, tfoot => 1, th => 1,
5065                    thead => 1, tr => 1,
5066                    area => 1, basefont => 1, bgsound => 1,
5067                    embed => 1, hr => 1, iframe => 1, image => 1,
5068                    img => 1, input => 1, isindex => 1, noembed => 1,
5069                    noframes => 1, param => 1, select => 1, spacer => 1,
5070                    table => 1, textarea => 1, wbr => 1,
5071                    noscript => 0, ## TODO: if scripting is enabled
5072                   }->{$token->{tag_name}}) {
5073            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5074            ## Ignore the token
5075            !!!next-token;
5076            redo B;
5077            
5078            ## ISSUE: Issue on HTML5 new elements in spec
5079            
5080        } else {        } else {
5081          die "$0: $token->{type}: Unknown token";          ## Step 1
5082            my $node_i = -1;
5083            my $node = $self->{open_elements}->[$node_i];
5084    
5085            ## Step 2
5086            S2: {
5087              if ($node->[1] eq $token->{tag_name}) {
5088                ## Step 1
5089                ## generate implied end tags
5090                if ({
5091                     dd => 1, dt => 1, li => 1, p => 1,
5092                     td => 1, th => 1, tr => 1,
5093                     tbody => 1, tfoot=> 1, thead => 1,
5094                    }->{$self->{open_elements}->[-1]->[1]}) {
5095                  !!!back-token;
5096                  $token = {type => 'end tag',
5097                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5098                  redo B;
5099                }
5100            
5101                ## Step 2
5102                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5103                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5104                }
5105                
5106                ## Step 3
5107                splice @{$self->{open_elements}}, $node_i;
5108    
5109                !!!next-token;
5110                last S2;
5111              } else {
5112                ## Step 3
5113                if (not $formatting_category->{$node->[1]} and
5114                    #not $phrasing_category->{$node->[1]} and
5115                    ($special_category->{$node->[1]} or
5116                     $scoping_category->{$node->[1]})) {
5117                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5118                  ## Ignore the token
5119                  !!!next-token;
5120                  last S2;
5121                }
5122              }
5123              
5124              ## Step 4
5125              $node_i--;
5126              $node = $self->{open_elements}->[$node_i];
5127              
5128              ## Step 5;
5129              redo S2;
5130            } # S2
5131            redo B;
5132        }        }
5133      }      }
5134        redo B;
5135    } # B    } # B
5136    
5137      ## NOTE: The "trailing end" phase in HTML5 is split into
5138      ## two insertion modes: "after html body" and "after html frameset".
5139      ## NOTE: States in the main stage is preserved while
5140      ## the parser stays in the trailing end phase. # MUST
5141    
5142    ## Stop parsing # MUST    ## Stop parsing # MUST
5143        
5144    ## TODO: script stuffs    ## TODO: script stuffs
# Line 4793  sub set_inner_html ($$$) { Line 5172  sub set_inner_html ($$$) {
5172      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5173    
5174      ## Step 1 # MUST      ## Step 1 # MUST
5175      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5176      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5177        $doc->manakai_is_html (1);
5178      my $p = $class->new;      my $p = $class->new;
5179      $p->{document} = $doc;      $p->{document} = $doc;
5180    
# Line 4804  sub set_inner_html ($$$) { Line 5184  sub set_inner_html ($$$) {
5184      my $column = 0;      my $column = 0;
5185      $p->{set_next_input_character} = sub {      $p->{set_next_input_character} = sub {
5186        my $self = shift;        my $self = shift;
5187    
5188          pop @{$self->{prev_input_character}};
5189          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5190    
5191        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_input_character} = -1 and return if $i >= length $$s;
5192        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_input_character} = ord substr $$s, $i++, 1;
5193        $column++;        $column++;
5194          
5195        if ($self->{next_input_character} == 0x000D) { # CR        if ($self->{next_input_character} == 0x000A) { # LF
5196          if ($i >= length $$s) {          $line++;
5197            #          $column = 0;
5198          } else {        } elsif ($self->{next_input_character} == 0x000D) { # CR
5199            my $next_char = ord substr $$s, $i++, 1;          $i++ if substr ($$s, $i, 1) eq "\x0A";
           if ($next_char == 0x000A) { # LF  
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
         }  
5200          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_input_character} = 0x000A; # LF # MUST
5201          $line++;          $line++;
5202          $column = -1;          $column = 0;
5203        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_input_character} > 0x10FFFF) {
5204          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5205        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_input_character} == 0x0000) { # NULL
5206            !!!parse-error (type => 'NULL');
5207          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5208        }        }
5209      };      };
5210        $p->{prev_input_character} = [-1, -1, -1];
5211        $p->{next_input_character} = -1;
5212            
5213      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5214        my (%opt) = @_;        my (%opt) = @_;
# Line 4842  sub set_inner_html ($$$) { Line 5223  sub set_inner_html ($$$) {
5223    
5224      ## Step 2      ## Step 2
5225      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5226      $p->{content_model_flag} = {      $p->{content_model} = {
5227        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5228        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5229        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5230        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5231        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5232        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5233        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5234        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5235        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5236        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5237      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5238         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5239            unless defined $p->{content_model};
5240            ## ISSUE: What is "the name of the element"? local name?
5241    
5242      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5243    
# Line 4906  sub set_inner_html ($$$) { Line 5289  sub set_inner_html ($$$) {
5289      ## Step 12 # MUST      ## Step 12 # MUST
5290      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5291      for (@cn) {      for (@cn) {
5292          $this_doc->adopt_node ($_);
5293        $node->append_child ($_);        $node->append_child ($_);
5294      }      }
5295      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5296    
5297      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5298    } else {    } else {
# Line 4953  sub get_inner_html ($$$) { Line 5337  sub get_inner_html ($$$) {
5337            
5338      my $nt = $child->node_type;      my $nt = $child->node_type;
5339      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5340        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5341        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5342          ## NOTE: Non-HTML case:
5343        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5344    
5345        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5346        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5347          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5348          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5349          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5350          ## escape          ## escape
# Line 4979  sub get_inner_html ($$$) { Line 5363  sub get_inner_html ($$$) {
5363          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5364        }->{$tag_name};        }->{$tag_name};
5365    
5366          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5367    
5368        if (not $in_cdata and {        if (not $in_cdata and {
5369          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5370          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5371            plaintext => 1,
5372        }->{$tag_name}) {        }->{$tag_name}) {
5373          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5374          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.54

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24