/[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.2 by wakaba, Tue May 1 10:47:37 2007 UTC revision 1.25 by wakaba, Sun Jun 24 05:12:11 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  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
11    base => 1,    base => 1,
# Line 18  my $permitted_slash_tag_name = { Line 21  my $permitted_slash_tag_name = {
21    input => 1,    input => 1,
22  };  };
23    
24  my $entity_char = {  my $c1_entity_char = {
25    AElig => "\x{00C6}",    0x80 => 0x20AC,
26    Aacute => "\x{00C1}",    0x81 => 0xFFFD,
27    Acirc => "\x{00C2}",    0x82 => 0x201A,
28    Agrave => "\x{00C0}",    0x83 => 0x0192,
29    Alpha => "\x{0391}",    0x84 => 0x201E,
30    Aring => "\x{00C5}",    0x85 => 0x2026,
31    Atilde => "\x{00C3}",    0x86 => 0x2020,
32    Auml => "\x{00C4}",    0x87 => 0x2021,
33    Beta => "\x{0392}",    0x88 => 0x02C6,
34    Ccedil => "\x{00C7}",    0x89 => 0x2030,
35    Chi => "\x{03A7}",    0x8A => 0x0160,
36    Dagger => "\x{2021}",    0x8B => 0x2039,
37    Delta => "\x{0394}",    0x8C => 0x0152,
38    ETH => "\x{00D0}",    0x8D => 0xFFFD,
39    Eacute => "\x{00C9}",    0x8E => 0x017D,
40    Ecirc => "\x{00CA}",    0x8F => 0xFFFD,
41    Egrave => "\x{00C8}",    0x90 => 0xFFFD,
42    Epsilon => "\x{0395}",    0x91 => 0x2018,
43    Eta => "\x{0397}",    0x92 => 0x2019,
44    Euml => "\x{00CB}",    0x93 => 0x201C,
45    Gamma => "\x{0393}",    0x94 => 0x201D,
46    Iacute => "\x{00CD}",    0x95 => 0x2022,
47    Icirc => "\x{00CE}",    0x96 => 0x2013,
48    Igrave => "\x{00CC}",    0x97 => 0x2014,
49    Iota => "\x{0399}",    0x98 => 0x02DC,
50    Iuml => "\x{00CF}",    0x99 => 0x2122,
51    Kappa => "\x{039A}",    0x9A => 0x0161,
52    Lambda => "\x{039B}",    0x9B => 0x203A,
53    Mu => "\x{039C}",    0x9C => 0x0153,
54    Ntilde => "\x{00D1}",    0x9D => 0xFFFD,
55    Nu => "\x{039D}",    0x9E => 0x017E,
56    OElig => "\x{0152}",    0x9F => 0x0178,
57    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}",  
 };  
58    
59  my $special_category = {  my $special_category = {
60    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,
# Line 307  sub parse_string ($$$;$) { Line 83  sub parse_string ($$$;$) {
83    my $s = \$_[0];    my $s = \$_[0];
84    $self->{document} = $_[1];    $self->{document} = $_[1];
85    
86    my $i;    ## NOTE: |set_inner_html| copies most of this method's code
87    
88    my $i = 0;    my $i = 0;
89      my $line = 1;
90      my $column = 0;
91    $self->{set_next_input_character} = sub {    $self->{set_next_input_character} = sub {
92      my $self = shift;      my $self = shift;
93    
94        pop @{$self->{prev_input_character}};
95        unshift @{$self->{prev_input_character}}, $self->{next_input_character};
96    
97      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_input_character} = -1 and return if $i >= length $$s;
98      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_input_character} = ord substr $$s, $i++, 1;
99        $column++;
100            
101      if ($self->{next_input_character} == 0x000D) { # CR      if ($self->{next_input_character} == 0x000A) { # LF
102        if ($i >= length $$s) {        $line++;
103          #        $column = 0;
104        } else {      } elsif ($self->{next_input_character} == 0x000D) { # CR
105          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;  
         }  
       }  
106        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_input_character} = 0x000A; # LF # MUST
107          $line++;
108          $column = 0;
109      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_input_character} > 0x10FFFF) {
110        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
111      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_input_character} == 0x0000) { # NULL
112          !!!parse-error (type => 'NULL');
113        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
114      }      }
115    };    };
116      $self->{prev_input_character} = [-1, -1, -1];
117      $self->{next_input_character} = -1;
118    
119    $self->{parse_error} = $_[2] || sub {    my $onerror = $_[2] || sub {
120      warn "Parse error at character $i\n"; ## TODO: Report (line, column) pair      my (%opt) = @_;
121        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";
122      };
123      $self->{parse_error} = sub {
124        $onerror->(@_, line => $line, column => $column);
125    };    };
126    
127    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 371  sub _initialize_tokenizer ($) { Line 158  sub _initialize_tokenizer ($) {
158    # $self->{next_input_character}    # $self->{next_input_character}
159    !!!next-input-character;    !!!next-input-character;
160    $self->{token} = [];    $self->{token} = [];
161      # $self->{escape}
162  } # _initialize_tokenizer  } # _initialize_tokenizer
163    
164  ## A token has:  ## A token has:
165  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',
166  ##       'character', or 'end-of-file'  ##       'character', or 'end-of-file'
167  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))
168      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{public_identifier} (DOCTYPE)
169  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE)
170    ##   ->{correct} == 1 or 0 (DOCTYPE)
171  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{attributes} isa HASH (start tag, end tag)
172  ##   ->{data} (comment, character)  ##   ->{data} (comment, character)
173    
 ## Macros  
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
   
174  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
175    
176  ## 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 412  sub _get_next_token ($) { Line 196  sub _get_next_token ($) {
196          } else {          } else {
197            #            #
198          }          }
199          } elsif ($self->{next_input_character} == 0x002D) { # -
200            if ($self->{content_model_flag} eq 'RCDATA' or
201                $self->{content_model_flag} eq 'CDATA') {
202              unless ($self->{escape}) {
203                if ($self->{prev_input_character}->[0] == 0x002D and # -
204                    $self->{prev_input_character}->[1] == 0x0021 and # !
205                    $self->{prev_input_character}->[2] == 0x003C) { # <
206                  $self->{escape} = 1;
207                }
208              }
209            }
210            
211            #
212        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
213          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model_flag} eq 'PCDATA' or
214                (($self->{content_model_flag} eq 'CDATA' or
215                  $self->{content_model_flag} eq 'RCDATA') and
216                 not $self->{escape})) {
217            $self->{state} = 'tag open';            $self->{state} = 'tag open';
218            !!!next-input-character;            !!!next-input-character;
219            redo A;            redo A;
220          } else {          } else {
221            #            #
222          }          }
223          } elsif ($self->{next_input_character} == 0x003E) { # >
224            if ($self->{escape} and
225                ($self->{content_model_flag} eq 'RCDATA' or
226                 $self->{content_model_flag} eq 'CDATA')) {
227              if ($self->{prev_input_character}->[0] == 0x002D and # -
228                  $self->{prev_input_character}->[1] == 0x002D) { # -
229                delete $self->{escape};
230              }
231            }
232            
233            #
234        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
235          !!!emit ({type => 'end-of-file'});          !!!emit ({type => 'end-of-file'});
236          last A; ## TODO: ok?          last A; ## TODO: ok?
# Line 488  sub _get_next_token ($) { Line 299  sub _get_next_token ($) {
299            !!!next-input-character;            !!!next-input-character;
300            redo A;            redo A;
301          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
302            !!!parse-error;            !!!parse-error (type => 'empty start tag');
303            $self->{state} = 'data';            $self->{state} = 'data';
304            !!!next-input-character;            !!!next-input-character;
305    
# Line 496  sub _get_next_token ($) { Line 307  sub _get_next_token ($) {
307    
308            redo A;            redo A;
309          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
310            !!!parse-error;            !!!parse-error (type => 'pio');
311            $self->{state} = 'bogus comment';            $self->{state} = 'bogus comment';
312            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
313            redo A;            redo A;
314          } else {          } else {
315            !!!parse-error;            !!!parse-error (type => 'bare stago');
316            $self->{state} = 'data';            $self->{state} = 'data';
317            ## reconsume            ## reconsume
318    
# Line 515  sub _get_next_token ($) { Line 326  sub _get_next_token ($) {
326      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
327        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model_flag} eq 'RCDATA' or
328            $self->{content_model_flag} eq 'CDATA') {            $self->{content_model_flag} eq 'CDATA') {
329          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
330          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
331              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
332                push @next_char, $self->{next_input_character};
333                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
334                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
335                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
336                  !!!next-input-character;
337                  next TAGNAME;
338                } else {
339                  $self->{next_input_character} = shift @next_char; # reconsume
340                  !!!back-next-input-character (@next_char);
341                  $self->{state} = 'data';
342    
343                  !!!emit ({type => 'character', data => '</'});
344      
345                  redo A;
346                }
347              }
348            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
349            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
350            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
351            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
352              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
353              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
354            } else {                    $self->{next_input_character} == 0x0020 or # SP
355              !!!parse-error;                    $self->{next_input_character} == 0x003E or # >
356                      $self->{next_input_character} == 0x002F or # /
357                      $self->{next_input_character} == -1) {
358              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
359              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
360              $self->{state} = 'data';              $self->{state} = 'data';
   
361              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => 'character', data => '</'});
   
362              redo A;              redo A;
363              } else {
364                $self->{next_input_character} = shift @next_char;
365                !!!back-next-input-character (@next_char);
366                # and consume...
367            }            }
368          }          } else {
369          push @next_char, $self->{next_input_character};            ## No start tag token has ever been emitted
370                  # 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;  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
371            $self->{state} = 'data';            $self->{state} = 'data';
   
372            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => 'character', data => '</'});
   
373            redo A;            redo A;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
374          }          }
375        }        }
376                
# Line 575  sub _get_next_token ($) { Line 389  sub _get_next_token ($) {
389          !!!next-input-character;          !!!next-input-character;
390          redo A;          redo A;
391        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
392          !!!parse-error;          !!!parse-error (type => 'empty end tag');
393          $self->{state} = 'data';          $self->{state} = 'data';
394          !!!next-input-character;          !!!next-input-character;
395          redo A;          redo A;
396        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
397          !!!parse-error;          !!!parse-error (type => 'bare etago');
398          $self->{state} = 'data';          $self->{state} = 'data';
399          # reconsume          # reconsume
400    
# Line 588  sub _get_next_token ($) { Line 402  sub _get_next_token ($) {
402    
403          redo A;          redo A;
404        } else {        } else {
405          !!!parse-error;          !!!parse-error (type => 'bogus end tag');
406          $self->{state} = 'bogus comment';          $self->{state} = 'bogus comment';
407          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
408          redo A;          redo A;
# Line 608  sub _get_next_token ($) { Line 422  sub _get_next_token ($) {
422          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
423            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
424            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
425              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
426            }            }
427          } else {          } else {
428            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 617  sub _get_next_token ($) { Line 431  sub _get_next_token ($) {
431          !!!next-input-character;          !!!next-input-character;
432    
433          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
434    
435          redo A;          redo A;
436        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 627  sub _get_next_token ($) { Line 440  sub _get_next_token ($) {
440          ## Stay in this state          ## Stay in this state
441          !!!next-input-character;          !!!next-input-character;
442          redo A;          redo A;
443        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
444                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
445          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
446            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
447          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
448            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
449            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
450              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
451            }            }
452          } else {          } else {
453            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 644  sub _get_next_token ($) { Line 456  sub _get_next_token ($) {
456          # reconsume          # reconsume
457    
458          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
459    
460          redo A;          redo A;
461        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
# Line 655  sub _get_next_token ($) { Line 466  sub _get_next_token ($) {
466            # permitted slash            # permitted slash
467            #            #
468          } else {          } else {
469            !!!parse-error;            !!!parse-error (type => 'nestc');
470          }          }
471          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
472          # next-input-character is already done          # next-input-character is already done
# Line 682  sub _get_next_token ($) { Line 493  sub _get_next_token ($) {
493          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
494            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
495            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
496              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
497            }            }
498          } else {          } else {
499            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 691  sub _get_next_token ($) { Line 502  sub _get_next_token ($) {
502          !!!next-input-character;          !!!next-input-character;
503    
504          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
505    
506          redo A;          redo A;
507        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 709  sub _get_next_token ($) { Line 519  sub _get_next_token ($) {
519            # permitted slash            # permitted slash
520            #            #
521          } else {          } else {
522            !!!parse-error;            !!!parse-error (type => 'nestc');
523          }          }
524          ## Stay in the state          ## Stay in the state
525          # next-input-character is already done          # next-input-character is already done
526          redo A;          redo A;
527        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
528                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
529          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
530            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
531          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
532            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
533            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
534              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
535            }            }
536          } else {          } else {
537            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 731  sub _get_next_token ($) { Line 540  sub _get_next_token ($) {
540          # reconsume          # reconsume
541    
542          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
543    
544          redo A;          redo A;
545        } else {        } else {
# Line 745  sub _get_next_token ($) { Line 553  sub _get_next_token ($) {
553        my $before_leave = sub {        my $before_leave = sub {
554          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
555              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
556            !!!parse-error;            !!!parse-error (type => 'dupulicate attribute');
557            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
558          } else {          } else {
559            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 774  sub _get_next_token ($) { Line 582  sub _get_next_token ($) {
582          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
583            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
584            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
585              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
586            }            }
587          } else {          } else {
588            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 783  sub _get_next_token ($) { Line 591  sub _get_next_token ($) {
591          !!!next-input-character;          !!!next-input-character;
592    
593          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
594    
595          redo A;          redo A;
596        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 801  sub _get_next_token ($) { Line 608  sub _get_next_token ($) {
608            # permitted slash            # permitted slash
609            #            #
610          } else {          } else {
611            !!!parse-error;            !!!parse-error (type => 'nestc');
612          }          }
613          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
614          # next-input-character is already done          # next-input-character is already done
615          redo A;          redo A;
616        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
617                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
618          $before_leave->();          $before_leave->();
619          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
620            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
621          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
622            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
623            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
624              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
625            }            }
626          } else {          } else {
627            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 824  sub _get_next_token ($) { Line 630  sub _get_next_token ($) {
630          # reconsume          # reconsume
631    
632          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
633    
634          redo A;          redo A;
635        } else {        } else {
# Line 852  sub _get_next_token ($) { Line 657  sub _get_next_token ($) {
657          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
658            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
659            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
660              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
661            }            }
662          } else {          } else {
663            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 861  sub _get_next_token ($) { Line 666  sub _get_next_token ($) {
666          !!!next-input-character;          !!!next-input-character;
667    
668          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
669    
670          redo A;          redo A;
671        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 879  sub _get_next_token ($) { Line 683  sub _get_next_token ($) {
683            # permitted slash            # permitted slash
684            #            #
685          } else {          } else {
686            !!!parse-error;            !!!parse-error (type => 'nestc');
687          }          }
688          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
689          # next-input-character is already done          # next-input-character is already done
690          redo A;          redo A;
691        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
692                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
693          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
694            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
695          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
696            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
697            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
698              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
699            }            }
700          } else {          } else {
701            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 901  sub _get_next_token ($) { Line 704  sub _get_next_token ($) {
704          # reconsume          # reconsume
705    
706          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
707    
708          redo A;          redo A;
709        } else {        } else {
# Line 938  sub _get_next_token ($) { Line 740  sub _get_next_token ($) {
740          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
741            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
742            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
743              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
744            }            }
745          } else {          } else {
746            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 947  sub _get_next_token ($) { Line 749  sub _get_next_token ($) {
749          !!!next-input-character;          !!!next-input-character;
750    
751          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
752    
753          redo A;          redo A;
754        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
755                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
756          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
757            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
758          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
759            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
760            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
761              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
762            }            }
763          } else {          } else {
764            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 967  sub _get_next_token ($) { Line 767  sub _get_next_token ($) {
767          ## reconsume          ## reconsume
768    
769          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
770    
771          redo A;          redo A;
772        } else {        } else {
# Line 987  sub _get_next_token ($) { Line 786  sub _get_next_token ($) {
786          !!!next-input-character;          !!!next-input-character;
787          redo A;          redo A;
788        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
789          !!!parse-error;          !!!parse-error (type => 'unclosed attribute value');
790          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
791            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
792          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
793            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
794            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
795              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
796            }            }
797          } else {          } else {
798            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1002  sub _get_next_token ($) { Line 801  sub _get_next_token ($) {
801          ## reconsume          ## reconsume
802    
803          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
804    
805          redo A;          redo A;
806        } else {        } else {
# Line 1022  sub _get_next_token ($) { Line 820  sub _get_next_token ($) {
820          !!!next-input-character;          !!!next-input-character;
821          redo A;          redo A;
822        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
823          !!!parse-error;          !!!parse-error (type => 'unclosed attribute value');
824          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
825            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
826          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
827            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
828            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
829              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
830            }            }
831          } else {          } else {
832            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1037  sub _get_next_token ($) { Line 835  sub _get_next_token ($) {
835          ## reconsume          ## reconsume
836    
837          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
838    
839          redo A;          redo A;
840        } else {        } else {
# Line 1066  sub _get_next_token ($) { Line 863  sub _get_next_token ($) {
863          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
864            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
865            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
866              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
867            }            }
868          } else {          } else {
869            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1075  sub _get_next_token ($) { Line 872  sub _get_next_token ($) {
872          !!!next-input-character;          !!!next-input-character;
873    
874          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
875    
876          redo A;          redo A;
877        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
878                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
879          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
880            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
881          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
882            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
883            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
884              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
885            }            }
886          } else {          } else {
887            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1095  sub _get_next_token ($) { Line 890  sub _get_next_token ($) {
890          ## reconsume          ## reconsume
891    
892          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
893    
894          redo A;          redo A;
895        } else {        } else {
# Line 1154  sub _get_next_token ($) { Line 948  sub _get_next_token ($) {
948          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
949          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
950            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => 'comment', data => ''};
951            $self->{state} = 'comment';            $self->{state} = 'comment start';
952            !!!next-input-character;            !!!next-input-character;
953            redo A;            redo A;
954          }          }
# Line 1196  sub _get_next_token ($) { Line 990  sub _get_next_token ($) {
990          }          }
991        }        }
992    
993        !!!parse-error;        !!!parse-error (type => 'bogus comment open');
994        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
995        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
996        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1204  sub _get_next_token ($) { Line 998  sub _get_next_token ($) {
998                
999        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1000        ## 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?
1001        } elsif ($self->{state} eq 'comment start') {
1002          if ($self->{next_input_character} == 0x002D) { # -
1003            $self->{state} = 'comment start dash';
1004            !!!next-input-character;
1005            redo A;
1006          } elsif ($self->{next_input_character} == 0x003E) { # >
1007            !!!parse-error (type => 'bogus comment');
1008            $self->{state} = 'data';
1009            !!!next-input-character;
1010    
1011            !!!emit ($self->{current_token}); # comment
1012    
1013            redo A;
1014          } elsif ($self->{next_input_character} == -1) {
1015            !!!parse-error (type => 'unclosed comment');
1016            $self->{state} = 'data';
1017            ## reconsume
1018    
1019            !!!emit ($self->{current_token}); # comment
1020    
1021            redo A;
1022          } else {
1023            $self->{current_token}->{data} # comment
1024                .= chr ($self->{next_input_character});
1025            $self->{state} = 'comment';
1026            !!!next-input-character;
1027            redo A;
1028          }
1029        } elsif ($self->{state} eq 'comment start dash') {
1030          if ($self->{next_input_character} == 0x002D) { # -
1031            $self->{state} = 'comment end';
1032            !!!next-input-character;
1033            redo A;
1034          } elsif ($self->{next_input_character} == 0x003E) { # >
1035            !!!parse-error (type => 'bogus comment');
1036            $self->{state} = 'data';
1037            !!!next-input-character;
1038    
1039            !!!emit ($self->{current_token}); # comment
1040    
1041            redo A;
1042          } elsif ($self->{next_input_character} == -1) {
1043            !!!parse-error (type => 'unclosed comment');
1044            $self->{state} = 'data';
1045            ## reconsume
1046    
1047            !!!emit ($self->{current_token}); # comment
1048    
1049            redo A;
1050          } else {
1051            $self->{current_token}->{data} # comment
1052                .= chr ($self->{next_input_character});
1053            $self->{state} = 'comment';
1054            !!!next-input-character;
1055            redo A;
1056          }
1057      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} eq 'comment') {
1058        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1059          $self->{state} = 'comment dash';          $self->{state} = 'comment end dash';
1060          !!!next-input-character;          !!!next-input-character;
1061          redo A;          redo A;
1062        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1063          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1064          $self->{state} = 'data';          $self->{state} = 'data';
1065          ## reconsume          ## reconsume
1066    
1067          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1068    
1069          redo A;          redo A;
1070        } else {        } else {
# Line 1224  sub _get_next_token ($) { Line 1073  sub _get_next_token ($) {
1073          !!!next-input-character;          !!!next-input-character;
1074          redo A;          redo A;
1075        }        }
1076      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} eq 'comment end dash') {
1077        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1078          $self->{state} = 'comment end';          $self->{state} = 'comment end';
1079          !!!next-input-character;          !!!next-input-character;
1080          redo A;          redo A;
1081        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1082          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1083          $self->{state} = 'data';          $self->{state} = 'data';
1084          ## reconsume          ## reconsume
1085    
1086          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1087    
1088          redo A;          redo A;
1089        } else {        } else {
# Line 1250  sub _get_next_token ($) { Line 1098  sub _get_next_token ($) {
1098          !!!next-input-character;          !!!next-input-character;
1099    
1100          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1101    
1102          redo A;          redo A;
1103        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
1104          !!!parse-error;          !!!parse-error (type => 'dash in comment');
1105          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1106          ## Stay in the state          ## Stay in the state
1107          !!!next-input-character;          !!!next-input-character;
1108          redo A;          redo A;
1109        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1110          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1111          $self->{state} = 'data';          $self->{state} = 'data';
1112          ## reconsume          ## reconsume
1113    
1114          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1115    
1116          redo A;          redo A;
1117        } else {        } else {
1118          !!!parse-error;          !!!parse-error (type => 'dash in comment');
1119          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1120          $self->{state} = 'comment';          $self->{state} = 'comment';
1121          !!!next-input-character;          !!!next-input-character;
# Line 1285  sub _get_next_token ($) { Line 1131  sub _get_next_token ($) {
1131          !!!next-input-character;          !!!next-input-character;
1132          redo A;          redo A;
1133        } else {        } else {
1134          !!!parse-error;          !!!parse-error (type => 'no space before DOCTYPE name');
1135          $self->{state} = 'before DOCTYPE name';          $self->{state} = 'before DOCTYPE name';
1136          ## reconsume          ## reconsume
1137          redo A;          redo A;
# Line 1299  sub _get_next_token ($) { Line 1145  sub _get_next_token ($) {
1145          ## Stay in the state          ## Stay in the state
1146          !!!next-input-character;          !!!next-input-character;
1147          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;  
1148        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1149          !!!parse-error;          !!!parse-error (type => 'no DOCTYPE name');
1150          $self->{state} = 'data';          $self->{state} = 'data';
1151          !!!next-input-character;          !!!next-input-character;
1152    
1153          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1154    
1155          redo A;          redo A;
1156        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1157          !!!parse-error;          !!!parse-error (type => 'no DOCTYPE name');
1158          $self->{state} = 'data';          $self->{state} = 'data';
1159          ## reconsume          ## reconsume
1160    
1161          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1162    
1163          redo A;          redo A;
1164        } else {        } else {
1165          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1166                            name => chr ($self->{next_input_character}),              = {type => 'DOCTYPE',
1167                            error => 1};                 name => chr ($self->{next_input_character}),
1168                   correct => 1};
1169    ## ISSUE: "Set the token's name name to the" in the spec
1170          $self->{state} = 'DOCTYPE name';          $self->{state} = 'DOCTYPE name';
1171          !!!next-input-character;          !!!next-input-character;
1172          redo A;          redo A;
1173        }        }
1174      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} eq 'DOCTYPE name') {
1175    ## ISSUE: Redundant "First," in the spec.
1176        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1177            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1178            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1179            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1180            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1181          $self->{state} = 'after DOCTYPE name';          $self->{state} = 'after DOCTYPE name';
1182          !!!next-input-character;          !!!next-input-character;
1183          redo A;          redo A;
1184        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1185          $self->{state} = 'data';          $self->{state} = 'data';
1186          !!!next-input-character;          !!!next-input-character;
1187    
1188          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1189    
1190          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;  
1191        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1192          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1193          $self->{state} = 'data';          $self->{state} = 'data';
1194          ## reconsume          ## reconsume
1195    
1196          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1197          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1198    
1199          redo A;          redo A;
1200        } else {        } else {
1201          $self->{current_token}->{name}          $self->{current_token}->{name}
1202            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1203          ## Stay in the state          ## Stay in the state
1204          !!!next-input-character;          !!!next-input-character;
1205          redo A;          redo A;
# Line 1389  sub _get_next_token ($) { Line 1218  sub _get_next_token ($) {
1218          !!!next-input-character;          !!!next-input-character;
1219    
1220          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1221    
1222          redo A;          redo A;
1223        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1224          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
1225          $self->{state} = 'data';          $self->{state} = 'data';
1226          ## reconsume          ## reconsume
1227    
1228            delete $self->{current_token}->{correct};
1229          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1230    
1231          redo A;          redo A;
1232          } elsif ($self->{next_input_character} == 0x0050 or # P
1233                   $self->{next_input_character} == 0x0070) { # p
1234            !!!next-input-character;
1235            if ($self->{next_input_character} == 0x0055 or # U
1236                $self->{next_input_character} == 0x0075) { # u
1237              !!!next-input-character;
1238              if ($self->{next_input_character} == 0x0042 or # B
1239                  $self->{next_input_character} == 0x0062) { # b
1240                !!!next-input-character;
1241                if ($self->{next_input_character} == 0x004C or # L
1242                    $self->{next_input_character} == 0x006C) { # l
1243                  !!!next-input-character;
1244                  if ($self->{next_input_character} == 0x0049 or # I
1245                      $self->{next_input_character} == 0x0069) { # i
1246                    !!!next-input-character;
1247                    if ($self->{next_input_character} == 0x0043 or # C
1248                        $self->{next_input_character} == 0x0063) { # c
1249                      $self->{state} = 'before DOCTYPE public identifier';
1250                      !!!next-input-character;
1251                      redo A;
1252                    }
1253                  }
1254                }
1255              }
1256            }
1257    
1258            #
1259          } elsif ($self->{next_input_character} == 0x0053 or # S
1260                   $self->{next_input_character} == 0x0073) { # s
1261            !!!next-input-character;
1262            if ($self->{next_input_character} == 0x0059 or # Y
1263                $self->{next_input_character} == 0x0079) { # y
1264              !!!next-input-character;
1265              if ($self->{next_input_character} == 0x0053 or # S
1266                  $self->{next_input_character} == 0x0073) { # s
1267                !!!next-input-character;
1268                if ($self->{next_input_character} == 0x0054 or # T
1269                    $self->{next_input_character} == 0x0074) { # t
1270                  !!!next-input-character;
1271                  if ($self->{next_input_character} == 0x0045 or # E
1272                      $self->{next_input_character} == 0x0065) { # e
1273                    !!!next-input-character;
1274                    if ($self->{next_input_character} == 0x004D or # M
1275                        $self->{next_input_character} == 0x006D) { # m
1276                      $self->{state} = 'before DOCTYPE system identifier';
1277                      !!!next-input-character;
1278                      redo A;
1279                    }
1280                  }
1281                }
1282              }
1283            }
1284    
1285            #
1286        } else {        } else {
1287          !!!parse-error;          !!!next-input-character;
1288          $self->{current_token}->{error} = 1; # DOCTYPE          #
1289          }
1290    
1291          !!!parse-error (type => 'string after DOCTYPE name');
1292          $self->{state} = 'bogus DOCTYPE';
1293          # next-input-character is already done
1294          redo A;
1295        } elsif ($self->{state} eq 'before DOCTYPE public identifier') {
1296          if ({
1297                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1298                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1299              }->{$self->{next_input_character}}) {
1300            ## Stay in the state
1301            !!!next-input-character;
1302            redo A;
1303          } elsif ($self->{next_input_character} eq 0x0022) { # "
1304            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1305            $self->{state} = 'DOCTYPE public identifier (double-quoted)';
1306            !!!next-input-character;
1307            redo A;
1308          } elsif ($self->{next_input_character} eq 0x0027) { # '
1309            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1310            $self->{state} = 'DOCTYPE public identifier (single-quoted)';
1311            !!!next-input-character;
1312            redo A;
1313          } elsif ($self->{next_input_character} eq 0x003E) { # >
1314            !!!parse-error (type => 'no PUBLIC literal');
1315    
1316            $self->{state} = 'data';
1317            !!!next-input-character;
1318    
1319            delete $self->{current_token}->{correct};
1320            !!!emit ($self->{current_token}); # DOCTYPE
1321    
1322            redo A;
1323          } elsif ($self->{next_input_character} == -1) {
1324            !!!parse-error (type => 'unclosed DOCTYPE');
1325    
1326            $self->{state} = 'data';
1327            ## reconsume
1328    
1329            delete $self->{current_token}->{correct};
1330            !!!emit ($self->{current_token}); # DOCTYPE
1331    
1332            redo A;
1333          } else {
1334            !!!parse-error (type => 'string after PUBLIC');
1335            $self->{state} = 'bogus DOCTYPE';
1336            !!!next-input-character;
1337            redo A;
1338          }
1339        } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {
1340          if ($self->{next_input_character} == 0x0022) { # "
1341            $self->{state} = 'after DOCTYPE public identifier';
1342            !!!next-input-character;
1343            redo A;
1344          } elsif ($self->{next_input_character} == -1) {
1345            !!!parse-error (type => 'unclosed PUBLIC literal');
1346    
1347            $self->{state} = 'data';
1348            ## reconsume
1349    
1350            delete $self->{current_token}->{correct};
1351            !!!emit ($self->{current_token}); # DOCTYPE
1352    
1353            redo A;
1354          } else {
1355            $self->{current_token}->{public_identifier} # DOCTYPE
1356                .= chr $self->{next_input_character};
1357            ## Stay in the state
1358            !!!next-input-character;
1359            redo A;
1360          }
1361        } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {
1362          if ($self->{next_input_character} == 0x0027) { # '
1363            $self->{state} = 'after DOCTYPE public identifier';
1364            !!!next-input-character;
1365            redo A;
1366          } elsif ($self->{next_input_character} == -1) {
1367            !!!parse-error (type => 'unclosed PUBLIC literal');
1368    
1369            $self->{state} = 'data';
1370            ## reconsume
1371    
1372            delete $self->{current_token}->{correct};
1373            !!!emit ($self->{current_token}); # DOCTYPE
1374    
1375            redo A;
1376          } else {
1377            $self->{current_token}->{public_identifier} # DOCTYPE
1378                .= chr $self->{next_input_character};
1379            ## Stay in the state
1380            !!!next-input-character;
1381            redo A;
1382          }
1383        } elsif ($self->{state} eq 'after DOCTYPE public identifier') {
1384          if ({
1385                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1386                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1387              }->{$self->{next_input_character}}) {
1388            ## Stay in the state
1389            !!!next-input-character;
1390            redo A;
1391          } elsif ($self->{next_input_character} == 0x0022) { # "
1392            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1393            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1394            !!!next-input-character;
1395            redo A;
1396          } elsif ($self->{next_input_character} == 0x0027) { # '
1397            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1398            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1399            !!!next-input-character;
1400            redo A;
1401          } elsif ($self->{next_input_character} == 0x003E) { # >
1402            $self->{state} = 'data';
1403            !!!next-input-character;
1404    
1405            !!!emit ($self->{current_token}); # DOCTYPE
1406    
1407            redo A;
1408          } elsif ($self->{next_input_character} == -1) {
1409            !!!parse-error (type => 'unclosed DOCTYPE');
1410    
1411            $self->{state} = 'data';
1412            ## recomsume
1413    
1414            delete $self->{current_token}->{correct};
1415            !!!emit ($self->{current_token}); # DOCTYPE
1416    
1417            redo A;
1418          } else {
1419            !!!parse-error (type => 'string after PUBLIC literal');
1420            $self->{state} = 'bogus DOCTYPE';
1421            !!!next-input-character;
1422            redo A;
1423          }
1424        } elsif ($self->{state} eq 'before DOCTYPE system identifier') {
1425          if ({
1426                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1427                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1428              }->{$self->{next_input_character}}) {
1429            ## Stay in the state
1430            !!!next-input-character;
1431            redo A;
1432          } elsif ($self->{next_input_character} == 0x0022) { # "
1433            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1434            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1435            !!!next-input-character;
1436            redo A;
1437          } elsif ($self->{next_input_character} == 0x0027) { # '
1438            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1439            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1440            !!!next-input-character;
1441            redo A;
1442          } elsif ($self->{next_input_character} == 0x003E) { # >
1443            !!!parse-error (type => 'no SYSTEM literal');
1444            $self->{state} = 'data';
1445            !!!next-input-character;
1446    
1447            delete $self->{current_token}->{correct};
1448            !!!emit ($self->{current_token}); # DOCTYPE
1449    
1450            redo A;
1451          } elsif ($self->{next_input_character} == -1) {
1452            !!!parse-error (type => 'unclosed DOCTYPE');
1453    
1454            $self->{state} = 'data';
1455            ## recomsume
1456    
1457            delete $self->{current_token}->{correct};
1458            !!!emit ($self->{current_token}); # DOCTYPE
1459    
1460            redo A;
1461          } else {
1462            !!!parse-error (type => 'string after PUBLIC literal');
1463            $self->{state} = 'bogus DOCTYPE';
1464            !!!next-input-character;
1465            redo A;
1466          }
1467        } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {
1468          if ($self->{next_input_character} == 0x0022) { # "
1469            $self->{state} = 'after DOCTYPE system identifier';
1470            !!!next-input-character;
1471            redo A;
1472          } elsif ($self->{next_input_character} == -1) {
1473            !!!parse-error (type => 'unclosed SYSTEM literal');
1474    
1475            $self->{state} = 'data';
1476            ## reconsume
1477    
1478            delete $self->{current_token}->{correct};
1479            !!!emit ($self->{current_token}); # DOCTYPE
1480    
1481            redo A;
1482          } else {
1483            $self->{current_token}->{system_identifier} # DOCTYPE
1484                .= chr $self->{next_input_character};
1485            ## Stay in the state
1486            !!!next-input-character;
1487            redo A;
1488          }
1489        } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {
1490          if ($self->{next_input_character} == 0x0027) { # '
1491            $self->{state} = 'after DOCTYPE system identifier';
1492            !!!next-input-character;
1493            redo A;
1494          } elsif ($self->{next_input_character} == -1) {
1495            !!!parse-error (type => 'unclosed SYSTEM literal');
1496    
1497            $self->{state} = 'data';
1498            ## reconsume
1499    
1500            delete $self->{current_token}->{correct};
1501            !!!emit ($self->{current_token}); # DOCTYPE
1502    
1503            redo A;
1504          } else {
1505            $self->{current_token}->{system_identifier} # DOCTYPE
1506                .= chr $self->{next_input_character};
1507            ## Stay in the state
1508            !!!next-input-character;
1509            redo A;
1510          }
1511        } elsif ($self->{state} eq 'after DOCTYPE system identifier') {
1512          if ({
1513                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1514                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1515              }->{$self->{next_input_character}}) {
1516            ## Stay in the state
1517            !!!next-input-character;
1518            redo A;
1519          } elsif ($self->{next_input_character} == 0x003E) { # >
1520            $self->{state} = 'data';
1521            !!!next-input-character;
1522    
1523            !!!emit ($self->{current_token}); # DOCTYPE
1524    
1525            redo A;
1526          } elsif ($self->{next_input_character} == -1) {
1527            !!!parse-error (type => 'unclosed DOCTYPE');
1528    
1529            $self->{state} = 'data';
1530            ## recomsume
1531    
1532            delete $self->{current_token}->{correct};
1533            !!!emit ($self->{current_token}); # DOCTYPE
1534    
1535            redo A;
1536          } else {
1537            !!!parse-error (type => 'string after SYSTEM literal');
1538          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1539          !!!next-input-character;          !!!next-input-character;
1540          redo A;          redo A;
# Line 1413  sub _get_next_token ($) { Line 1544  sub _get_next_token ($) {
1544          $self->{state} = 'data';          $self->{state} = 'data';
1545          !!!next-input-character;          !!!next-input-character;
1546    
1547            delete $self->{current_token}->{correct};
1548          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1549    
1550          redo A;          redo A;
1551        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1552          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
1553          $self->{state} = 'data';          $self->{state} = 'data';
1554          ## reconsume          ## reconsume
1555    
1556            delete $self->{current_token}->{correct};
1557          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1558    
1559          redo A;          redo A;
1560        } else {        } else {
# Line 1441  sub _get_next_token ($) { Line 1572  sub _get_next_token ($) {
1572    
1573  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($) {
1574    my $self = shift;    my $self = shift;
1575      
1576    if ($self->{next_input_character} == 0x0023) { # #    if ({
1577           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1578           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1579          }->{$self->{next_input_character}}) {
1580        ## Don't consume
1581        ## No error
1582        return undef;
1583      } elsif ($self->{next_input_character} == 0x0023) { # #
1584      !!!next-input-character;      !!!next-input-character;
     my $num;  
1585      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1586          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1587          my $num;
1588        X: {        X: {
1589          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1590          !!!next-input-character;          !!!next-input-character;
# Line 1471  sub _tokenize_attempt_to_consume_an_enti Line 1609  sub _tokenize_attempt_to_consume_an_enti
1609            $num += $self->{next_input_character} - 0x0040 + 9;            $num += $self->{next_input_character} - 0x0040 + 9;
1610            redo X;            redo X;
1611          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $num) { # no hexadecimal digit
1612            !!!parse-error;            !!!parse-error (type => 'bare hcro');
1613            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
1614            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char);
1615            return undef;            return undef;
1616          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1617            !!!next-input-character;            !!!next-input-character;
1618          } else {          } else {
1619            !!!parse-error;            !!!parse-error (type => 'no refc');
1620          }          }
1621    
1622          ## TODO: check the definition for |a valid Unicode character|.          ## TODO: check the definition for |a valid Unicode character|.
1623            ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>
1624          if ($num > 1114111 or $num == 0) {          if ($num > 1114111 or $num == 0) {
1625            $num = 0xFFFD; # REPLACEMENT CHARACTER            $num = 0xFFFD; # REPLACEMENT CHARACTER
1626            ## ISSUE: Why this is not an error?            ## ISSUE: Why this is not an error?
1627            } elsif (0x80 <= $num and $num <= 0x9F) {
1628              !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);
1629              $num = $c1_entity_char->{$num};
1630          }          }
1631    
1632          return {type => 'character', data => chr $num};          return {type => 'character', data => chr $num};
# Line 1505  sub _tokenize_attempt_to_consume_an_enti Line 1647  sub _tokenize_attempt_to_consume_an_enti
1647        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_input_character} == 0x003B) { # ;
1648          !!!next-input-character;          !!!next-input-character;
1649        } else {        } else {
1650          !!!parse-error;          !!!parse-error (type => 'no refc');
1651        }        }
1652    
1653        ## TODO: check the definition for |a valid Unicode character|.        ## TODO: check the definition for |a valid Unicode character|.
1654        if ($code > 1114111 or $code == 0) {        if ($code > 1114111 or $code == 0) {
1655          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD; # REPLACEMENT CHARACTER
1656          ## ISSUE: Why this is not an error?          ## ISSUE: Why this is not an error?
1657          } elsif (0x80 <= $code and $code <= 0x9F) {
1658            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);
1659            $code = $c1_entity_char->{$code};
1660        }        }
1661                
1662        return {type => 'character', data => chr $code};        return {type => 'character', data => chr $code};
1663      } else {      } else {
1664        !!!parse-error;        !!!parse-error (type => 'bare nero');
1665        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
1666        $self->{next_input_character} = 0x0023; # #        $self->{next_input_character} = 0x0023; # #
1667        return undef;        return undef;
# Line 1530  sub _tokenize_attempt_to_consume_an_enti Line 1675  sub _tokenize_attempt_to_consume_an_enti
1675    
1676      my $value = $entity_name;      my $value = $entity_name;
1677      my $match;      my $match;
1678        require Whatpm::_NamedEntityList;
1679        our $EntityChar;
1680    
1681      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1682             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1683             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1684               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1685              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1686               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1687              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1688               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1689                $self->{next_input_character} == 0x003B)) { # ;
1690        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1691        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1692          $value = $entity_char->{$entity_name};          $value = $EntityChar->{$entity_name};
1693          $match = 1;          if ($self->{next_input_character} == 0x003B) { # ;
1694              $match = 1;
1695              !!!next-input-character;
1696              last;
1697            } else {
1698              $match = -1;
1699            }
1700        } else {        } else {
1701          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1702        }        }
1703        !!!next-input-character;        !!!next-input-character;
1704      }      }
1705            
1706      if ($match) {      if ($match > 0) {
1707        if ($self->{next_input_character} == 0x003B) { # ;        return {type => 'character', data => $value};
1708          !!!next-input-character;      } elsif ($match < 0) {
1709        } else {        !!!parse-error (type => 'refc');
         !!!parse-error;  
       }  
   
1710        return {type => 'character', data => $value};        return {type => 'character', data => $value};
1711      } else {      } else {
1712        !!!parse-error;        !!!parse-error (type => 'bare ero');
1713        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1714        !!!back-token ({type => 'character', data => $value});        !!!back-token ({type => 'character', data => $value});
1715        return undef;        return undef;
1716      }      }
1717    } else {    } else {
1718      ## no characters are consumed      ## no characters are consumed
1719      !!!parse-error;      !!!parse-error (type => 'bare ero');
1720      return undef;      return undef;
1721    }    }
1722  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1576  sub _initialize_tree_constructor ($) { Line 1727  sub _initialize_tree_constructor ($) {
1727    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1728    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1729    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1730    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1731  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1732    
1733  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1587  sub _terminate_tree_constructor ($) { Line 1738  sub _terminate_tree_constructor ($) {
1738    
1739  ## ISSUE: Should append_child (for example) in script executed in tree construction stage fire mutation events?  ## ISSUE: Should append_child (for example) in script executed in tree construction stage fire mutation events?
1740    
1741    { # tree construction stage
1742      my $token;
1743    
1744  sub _construct_tree ($) {  sub _construct_tree ($) {
1745    my ($self) = @_;    my ($self) = @_;
1746    
# Line 1598  sub _construct_tree ($) { Line 1752  sub _construct_tree ($) {
1752    ## characters and insert one Text node whose data is concatenation    ## characters and insert one Text node whose data is concatenation
1753    ## of all those characters. # MUST    ## of all those characters. # MUST
1754        
   my $token;  
1755    !!!next-token;    !!!next-token;
1756    
1757    my $phase = 'initial'; # MUST    $self->{insertion_mode} = 'before head';
1758      undef $self->{form_element};
1759      undef $self->{head_element};
1760      $self->{open_elements} = [];
1761      undef $self->{inner_html_node};
1762    
1763      $self->_tree_construction_initial; # MUST
1764      $self->_tree_construction_root_element;
1765      $self->_tree_construction_main;
1766    } # _construct_tree
1767    
1768    sub _tree_construction_initial ($) {
1769      my $self = shift;
1770      INITIAL: {
1771        if ($token->{type} eq 'DOCTYPE') {
1772          ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1773          ## error, switch to a conformance checking mode for another
1774          ## language.
1775          my $doctype_name = $token->{name};
1776          $doctype_name = '' unless defined $doctype_name;
1777          $doctype_name =~ tr/a-z/A-Z/;
1778          if (not defined $token->{name} or # <!DOCTYPE>
1779              defined $token->{public_identifier} or
1780              defined $token->{system_identifier}) {
1781            !!!parse-error (type => 'not HTML5');
1782          } elsif ($doctype_name ne 'HTML') {
1783            ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1784            !!!parse-error (type => 'not HTML5');
1785          }
1786          
1787          my $doctype = $self->{document}->create_document_type_definition
1788            ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1789          $doctype->public_id ($token->{public_identifier})
1790              if defined $token->{public_identifier};
1791          $doctype->system_id ($token->{system_identifier})
1792              if defined $token->{system_identifier};
1793          ## NOTE: Other DocumentType attributes are null or empty lists.
1794          ## ISSUE: internalSubset = null??
1795          $self->{document}->append_child ($doctype);
1796          
1797          if (not $token->{correct} or $doctype_name ne 'HTML') {
1798            $self->{document}->manakai_compat_mode ('quirks');
1799          } elsif (defined $token->{public_identifier}) {
1800            my $pubid = $token->{public_identifier};
1801            $pubid =~ tr/a-z/A-z/;
1802            if ({
1803              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1804              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1805              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1806              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1807              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1808              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1809              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1810              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1811              "-//IETF//DTD HTML 2.0//EN" => 1,
1812              "-//IETF//DTD HTML 2.1E//EN" => 1,
1813              "-//IETF//DTD HTML 3.0//EN" => 1,
1814              "-//IETF//DTD HTML 3.0//EN//" => 1,
1815              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1816              "-//IETF//DTD HTML 3.2//EN" => 1,
1817              "-//IETF//DTD HTML 3//EN" => 1,
1818              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1819              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1820              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1821              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1822              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1823              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1824              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1825              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1826              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1827              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1828              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1829              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1830              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1831              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1832              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1833              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1834              "-//IETF//DTD HTML STRICT//EN" => 1,
1835              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1836              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1837              "-//IETF//DTD HTML//EN" => 1,
1838              "-//IETF//DTD HTML//EN//2.0" => 1,
1839              "-//IETF//DTD HTML//EN//3.0" => 1,
1840              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1841              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1842              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1843              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1844              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1845              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1846              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1847              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1848              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1849              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1850              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1851              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1852              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1853              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1854              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1855              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1856              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1857              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1858              "-//W3C//DTD HTML 3.2//EN" => 1,
1859              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1860              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
1861              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
1862              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
1863              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
1864              "-//W3C//DTD W3 HTML//EN" => 1,
1865              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
1866              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
1867              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
1868              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
1869              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
1870              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
1871              "HTML" => 1,
1872            }->{$pubid}) {
1873              $self->{document}->manakai_compat_mode ('quirks');
1874            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
1875                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
1876              if (defined $token->{system_identifier}) {
1877                $self->{document}->manakai_compat_mode ('quirks');
1878              } else {
1879                $self->{document}->manakai_compat_mode ('limited quirks');
1880              }
1881            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
1882                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
1883              $self->{document}->manakai_compat_mode ('limited quirks');
1884            }
1885          }
1886          if (defined $token->{system_identifier}) {
1887            my $sysid = $token->{system_identifier};
1888            $sysid =~ tr/A-Z/a-z/;
1889            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1890              $self->{document}->manakai_compat_mode ('quirks');
1891            }
1892          }
1893          
1894          ## Go to the root element phase.
1895          !!!next-token;
1896          return;
1897        } elsif ({
1898                  'start tag' => 1,
1899                  'end tag' => 1,
1900                  'end-of-file' => 1,
1901                 }->{$token->{type}}) {
1902          !!!parse-error (type => 'no DOCTYPE');
1903          $self->{document}->manakai_compat_mode ('quirks');
1904          ## Go to the root element phase
1905          ## reprocess
1906          return;
1907        } elsif ($token->{type} eq 'character') {
1908          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1909            ## Ignore the token
1910            unless (length $token->{data}) {
1911              ## Stay in the phase
1912              !!!next-token;
1913              redo INITIAL;
1914            }
1915          }
1916    
1917          !!!parse-error (type => 'no DOCTYPE');
1918          $self->{document}->manakai_compat_mode ('quirks');
1919          ## Go to the root element phase
1920          ## reprocess
1921          return;
1922        } elsif ($token->{type} eq 'comment') {
1923          my $comment = $self->{document}->create_comment ($token->{data});
1924          $self->{document}->append_child ($comment);
1925          
1926          ## Stay in the phase.
1927          !!!next-token;
1928          redo INITIAL;
1929        } else {
1930          die "$0: $token->{type}: Unknown token";
1931        }
1932      } # INITIAL
1933    } # _tree_construction_initial
1934    
1935    sub _tree_construction_root_element ($) {
1936      my $self = shift;
1937      
1938      B: {
1939          if ($token->{type} eq 'DOCTYPE') {
1940            !!!parse-error (type => 'in html:#DOCTYPE');
1941            ## Ignore the token
1942            ## Stay in the phase
1943            !!!next-token;
1944            redo B;
1945          } elsif ($token->{type} eq 'comment') {
1946            my $comment = $self->{document}->create_comment ($token->{data});
1947            $self->{document}->append_child ($comment);
1948            ## Stay in the phase
1949            !!!next-token;
1950            redo B;
1951          } elsif ($token->{type} eq 'character') {
1952            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
1953              $self->{document}->manakai_append_text ($1);
1954              ## ISSUE: DOM3 Core does not allow Document > Text
1955              unless (length $token->{data}) {
1956                ## Stay in the phase
1957                !!!next-token;
1958                redo B;
1959              }
1960            }
1961            #
1962          } elsif ({
1963                    'start tag' => 1,
1964                    'end tag' => 1,
1965                    'end-of-file' => 1,
1966                   }->{$token->{type}}) {
1967            ## ISSUE: There is an issue in the spec
1968            #
1969          } else {
1970            die "$0: $token->{type}: Unknown token";
1971          }
1972          my $root_element; !!!create-element ($root_element, 'html');
1973          $self->{document}->append_child ($root_element);
1974          push @{$self->{open_elements}}, [$root_element, 'html'];
1975          #$phase = 'main';
1976          ## reprocess
1977          #redo B;
1978          return;
1979      } # B
1980    } # _tree_construction_root_element
1981    
1982    sub _reset_insertion_mode ($) {
1983      my $self = shift;
1984    
1985        ## Step 1
1986        my $last;
1987        
1988        ## Step 2
1989        my $i = -1;
1990        my $node = $self->{open_elements}->[$i];
1991        
1992        ## Step 3
1993        S3: {
1994          $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];
1995          if (defined $self->{inner_html_node}) {
1996            if ($self->{inner_html_node}->[1] eq 'td' or
1997                $self->{inner_html_node}->[1] eq 'th') {
1998              #
1999            } else {
2000              $node = $self->{inner_html_node};
2001            }
2002          }
2003        
2004          ## Step 4..13
2005          my $new_mode = {
2006                          select => 'in select',
2007                          td => 'in cell',
2008                          th => 'in cell',
2009                          tr => 'in row',
2010                          tbody => 'in table body',
2011                          thead => 'in table head',
2012                          tfoot => 'in table foot',
2013                          caption => 'in caption',
2014                          colgroup => 'in column group',
2015                          table => 'in table',
2016                          head => 'in body', # not in head!
2017                          body => 'in body',
2018                          frameset => 'in frameset',
2019                         }->{$node->[1]};
2020          $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2021          
2022          ## Step 14
2023          if ($node->[1] eq 'html') {
2024            unless (defined $self->{head_element}) {
2025              $self->{insertion_mode} = 'before head';
2026            } else {
2027              $self->{insertion_mode} = 'after head';
2028            }
2029            return;
2030          }
2031          
2032          ## Step 15
2033          $self->{insertion_mode} = 'in body' and return if $last;
2034          
2035          ## Step 16
2036          $i--;
2037          $node = $self->{open_elements}->[$i];
2038          
2039          ## Step 17
2040          redo S3;
2041        } # S3
2042    } # _reset_insertion_mode
2043    
2044    sub _tree_construction_main ($) {
2045      my $self = shift;
2046    
2047      my $phase = 'main';
2048    
   my $open_elements = [];  
2049    my $active_formatting_elements = [];    my $active_formatting_elements = [];
   my $head_element;  
   my $form_element;  
   my $insertion_mode = 'before head';  
2050    
2051    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
2052      my $insert = shift;      my $insert = shift;
# Line 1621  sub _construct_tree ($) { Line 2060  sub _construct_tree ($) {
2060    
2061      ## Step 2      ## Step 2
2062      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2063      for (@$open_elements) {      for (@{$self->{open_elements}}) {
2064        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2065          return;          return;
2066        }        }
# Line 1640  sub _construct_tree ($) { Line 2079  sub _construct_tree ($) {
2079          #          #
2080        } else {        } else {
2081          my $in_open_elements;          my $in_open_elements;
2082          OE: for (@$open_elements) {          OE: for (@{$self->{open_elements}}) {
2083            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2084              $in_open_elements = 1;              $in_open_elements = 1;
2085              last OE;              last OE;
# Line 1664  sub _construct_tree ($) { Line 2103  sub _construct_tree ($) {
2103            
2104        ## Step 9        ## Step 9
2105        $insert->($clone->[0]);        $insert->($clone->[0]);
2106        push @$open_elements, $clone;        push @{$self->{open_elements}}, $clone;
2107                
2108        ## Step 10        ## Step 10
2109        $active_formatting_elements->[$i] = $open_elements->[-1];        $active_formatting_elements->[$i] = $self->{open_elements}->[-1];
2110    
2111        ## Step 11        ## Step 11
2112        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
# Line 1689  sub _construct_tree ($) { Line 2128  sub _construct_tree ($) {
2128      }      }
2129    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2130    
2131    my $reset_insertion_mode = sub {    my $parse_rcdata = sub ($$) {
2132        my ($content_model_flag, $insert) = @_;
2133    
2134      ## Step 1      ## Step 1
2135      my $last;      my $start_tag_name = $token->{tag_name};
2136            my $el;
2137        !!!create-element ($el, $start_tag_name, $token->{attributes});
2138    
2139      ## Step 2      ## Step 2
2140      my $i = -1;      $insert->($el); # /context node/->append_child ($el)
2141      my $node = $open_elements->[$i];  
       
2142      ## Step 3      ## Step 3
2143      S3: {      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA
2144        $last = 1 if $open_elements->[0]->[0] eq $node->[0];      delete $self->{escape}; # MUST
       ## TODO: the element whose inner_html is set is neither td nor th, then $node = the element  
       
       ## Step 4..13  
       my $new_mode = {  
                       select => 'in select',  
                       td => 'in cell',  
                       th => 'in cell',  
                       tr => 'in row',  
                       tbody => 'in table body',  
                       thead => 'in table head',  
                       tfoot => 'in table foot',  
                       caption => 'in caption',  
                       colgroup => 'in column group',  
                       table => 'in table',  
                       head => 'in body', # not in head!  
                       body => 'in body',  
                       frameset => 'in frameset',  
                      }->{$node->[1]};  
       $insertion_mode = $new_mode and return if defined $new_mode;  
         
       ## Step 14  
       if ($node->[1] eq 'html') {  
         unless (defined $head_element) {  
           $insertion_mode = 'before head';  
         } else {  
           $insertion_mode = 'after head';  
         }  
         return;  
       }  
         
       ## Step 15  
       $insertion_mode = 'in body' and return if $last;  
         
       ## Step 16  
       $i--;  
       $node = $open_elements->[$i];  
         
       ## Step 17  
       redo S3;  
     } # S3  
   }; # $reset_insertion_mode  
2145    
2146    my $style_start_tag = sub {      ## Step 4
     my $style_el; !!!create-element ($style_el, 'style');  
     ## $insertion_mode eq 'in head' and ... (always true)  
     (($insertion_mode eq 'in head' and defined $head_element)  
      ? $head_element : $open_elements->[-1]->[0])  
       ->append_child ($style_el);  
     $self->{content_model_flag} = 'CDATA';  
                 
2147      my $text = '';      my $text = '';
2148      !!!next-token;      !!!next-token;
2149      while ($token->{type} eq 'character') {      while ($token->{type} eq 'character') { # or until stop tokenizing
2150        $text .= $token->{data};        $text .= $token->{data};
2151        !!!next-token;        !!!next-token;
2152      } # stop if non-character token or tokenizer stops tokenising      }
2153    
2154        ## Step 5
2155      if (length $text) {      if (length $text) {
2156        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2157          $el->append_child ($text);
2158      }      }
2159        
2160        ## Step 6
2161      $self->{content_model_flag} = 'PCDATA';      $self->{content_model_flag} = 'PCDATA';
2162                  
2163      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {      ## Step 7
2164        if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2165        ## Ignore the token        ## Ignore the token
2166      } else {      } else {
2167        !!!parse-error;        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});
       ## ISSUE: And ignore?  
2168      }      }
2169      !!!next-token;      !!!next-token;
2170    }; # $style_start_tag    }; # $parse_rcdata
2171    
2172    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2173        my $insert = $_[0];
2174      my $script_el;      my $script_el;
2175      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2176      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2177    
2178      $self->{content_model_flag} = 'CDATA';      $self->{content_model_flag} = 'CDATA';
2179        delete $self->{escape}; # MUST
2180            
2181      my $text = '';      my $text = '';
2182      !!!next-token;      !!!next-token;
# Line 1794  sub _construct_tree ($) { Line 2194  sub _construct_tree ($) {
2194          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2195        ## Ignore the token        ## Ignore the token
2196      } else {      } else {
2197        !!!parse-error;        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2198        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2199        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2200      }      }
2201            
2202      ## TODO: inner_html mode then mark as "already executed" and skip      if (defined $self->{inner_html_node}) {
2203      if (1) {        ## TODO: mark as "already executed"
2204        } else {
2205        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2206        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2207          
2208        (($insertion_mode eq 'in head' and defined $head_element)        $insert->($script_el);
        ? $head_element : $open_elements->[-1]->[0])->append_child ($script_el);  
2209                
2210        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2211                
# Line 1832  sub _construct_tree ($) { Line 2232  sub _construct_tree ($) {
2232          }          }
2233        } # AFE        } # AFE
2234        unless (defined $formatting_element) {        unless (defined $formatting_element) {
2235          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
2236          ## Ignore the token          ## Ignore the token
2237          !!!next-token;          !!!next-token;
2238          return;          return;
# Line 1840  sub _construct_tree ($) { Line 2240  sub _construct_tree ($) {
2240        ## has an element in scope        ## has an element in scope
2241        my $in_scope = 1;        my $in_scope = 1;
2242        my $formatting_element_i_in_open;          my $formatting_element_i_in_open;  
2243        INSCOPE: for (reverse 0..$#$open_elements) {        INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2244          my $node = $open_elements->[$_];          my $node = $self->{open_elements}->[$_];
2245          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
2246            if ($in_scope) {            if ($in_scope) {
2247              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
2248              last INSCOPE;              last INSCOPE;
2249            } else { # in open elements but not in scope            } else { # in open elements but not in scope
2250              !!!parse-error;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2251              ## Ignore the token              ## Ignore the token
2252              !!!next-token;              !!!next-token;
2253              return;              return;
# Line 1860  sub _construct_tree ($) { Line 2260  sub _construct_tree ($) {
2260          }          }
2261        } # INSCOPE        } # INSCOPE
2262        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
2263          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2264          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
2265          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
2266          return;          return;
2267        }        }
2268        if (not $open_elements->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
2269          !!!parse-error;          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2270        }        }
2271                
2272        ## Step 2        ## Step 2
2273        my $furthest_block;        my $furthest_block;
2274        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
2275        OE: for (reverse 0..$#$open_elements) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
2276          my $node = $open_elements->[$_];          my $node = $self->{open_elements}->[$_];
2277          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
2278              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
2279              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
# Line 1887  sub _construct_tree ($) { Line 2287  sub _construct_tree ($) {
2287                
2288        ## Step 3        ## Step 3
2289        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
2290          splice @$open_elements, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
2291          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
2292          !!!next-token;          !!!next-token;
2293          return;          return;
2294        }        }
2295                
2296        ## Step 4        ## Step 4
2297        my $common_ancestor_node = $open_elements->[$formatting_element_i_in_open - 1];        my $common_ancestor_node = $self->{open_elements}->[$formatting_element_i_in_open - 1];
2298                
2299        ## Step 5        ## Step 5
2300        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
# Line 1914  sub _construct_tree ($) { Line 2314  sub _construct_tree ($) {
2314        S7: {        S7: {
2315          ## Step 1          ## Step 1
2316          $node_i_in_open--;          $node_i_in_open--;
2317          $node = $open_elements->[$node_i_in_open];          $node = $self->{open_elements}->[$node_i_in_open];
2318                    
2319          ## Step 2          ## Step 2
2320          my $node_i_in_active;          my $node_i_in_active;
# Line 1925  sub _construct_tree ($) { Line 2325  sub _construct_tree ($) {
2325                last S7S2;                last S7S2;
2326              }              }
2327            }            }
2328            splice @$open_elements, $node_i_in_open, 1;            splice @{$self->{open_elements}}, $node_i_in_open, 1;
2329            redo S7;            redo S7;
2330          } # S7S2          } # S7S2
2331                    
# Line 1941  sub _construct_tree ($) { Line 2341  sub _construct_tree ($) {
2341          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
2342            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
2343            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
2344            $open_elements->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
2345            $node = $clone;            $node = $clone;
2346          }          }
2347                    
# Line 1983  sub _construct_tree ($) { Line 2383  sub _construct_tree ($) {
2383                
2384        ## Step 13        ## Step 13
2385        undef $i;        undef $i;
2386        OE: for (reverse 0..$#$open_elements) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
2387          if ($open_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
2388            splice @$open_elements, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
2389            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
2390          } elsif ($open_elements->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
2391            $i = $_;            $i = $_;
2392          }          }
2393        } # OE        } # OE
2394        splice @$open_elements, $i + 1, 1, $clone;        splice @{$self->{open_elements}}, $i + 1, 1, $clone;
2395                
2396        ## Step 14        ## Step 14
2397        redo FET;        redo FET;
# Line 1999  sub _construct_tree ($) { Line 2399  sub _construct_tree ($) {
2399    }; # $formatting_end_tag    }; # $formatting_end_tag
2400    
2401    my $insert_to_current = sub {    my $insert_to_current = sub {
2402      $open_elements->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2403    }; # $insert_to_current    }; # $insert_to_current
2404    
2405    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2007  sub _construct_tree ($) { Line 2407  sub _construct_tree ($) {
2407                         if ({                         if ({
2408                              table => 1, tbody => 1, tfoot => 1,                              table => 1, tbody => 1, tfoot => 1,
2409                              thead => 1, tr => 1,                              thead => 1, tr => 1,
2410                             }->{$open_elements->[-1]->[1]}) {                             }->{$self->{open_elements}->[-1]->[1]}) {
2411                           # MUST                           # MUST
2412                           my $foster_parent_element;                           my $foster_parent_element;
2413                           my $next_sibling;                           my $next_sibling;
2414                           OE: for (reverse 0..$#$open_elements) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
2415                             if ($open_elements->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
2416                               my $parent = $open_elements->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
2417                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
2418                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
2419                                 $next_sibling = $open_elements->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
2420                               } else {                               } else {
2421                                 $foster_parent_element                                 $foster_parent_element
2422                                   = $open_elements->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
2423                               }                               }
2424                               last OE;                               last OE;
2425                             }                             }
2426                           } # OE                           } # OE
2427                           $foster_parent_element = $open_elements->[0]->[0]                           $foster_parent_element = $self->{open_elements}->[0]->[0]
2428                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
2429                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
2430                             ($child, $next_sibling);                             ($child, $next_sibling);
2431                         } else {                         } else {
2432                           $open_elements->[-1]->[0]->append_child ($child);                           $self->{open_elements}->[-1]->[0]->append_child ($child);
2433                         }                         }
2434    }; # $insert_to_foster    }; # $insert_to_foster
2435    
# Line 2037  sub _construct_tree ($) { Line 2437  sub _construct_tree ($) {
2437      my $insert = shift;      my $insert = shift;
2438      if ($token->{type} eq 'start tag') {      if ($token->{type} eq 'start tag') {
2439        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
2440          $script_start_tag->();          ## NOTE: This is an "as if in head" code clone
2441            $script_start_tag->($insert);
2442          return;          return;
2443        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
2444          $style_start_tag->();          ## NOTE: This is an "as if in head" code clone
2445            $parse_rcdata->('CDATA', $insert);
2446          return;          return;
2447        } elsif ({        } elsif ({
2448                  base => 1, link => 1, meta => 1,                  base => 1, link => 1, meta => 1,
2449                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2450          !!!parse-error ($token->{tag_name}.' in body');          ## NOTE: This is an "as if in head" code clone, only "-t" differs
2451          ## NOTE: This is an "as if in head" code clone          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2452          my $el;          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
         if (defined $head_element) {  
           $head_element->append_child ($el);  
         } else {  
           $insert->($el);  
         }  
           
2453          !!!next-token;          !!!next-token;
2454          return;          return;
2455        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
2456          !!!parse-error ('title in body');          !!!parse-error (type => 'in body:title');
2457          ## NOTE: There is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
2458          my $title_el;          $parse_rcdata->('RCDATA', $insert);
         !!!create-element ($title_el, 'title', $token->{attributes});  
         (defined $head_element ? $head_element : $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;  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
2459          return;          return;
2460        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
2461          !!!parse-error;          !!!parse-error (type => 'in body:body');
2462                                
2463          if (@$open_elements == 1 or          if (@{$self->{open_elements}} == 1 or
2464              $open_elements->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
2465            ## Ignore the token            ## Ignore the token
2466          } else {          } else {
2467            my $body_el = $open_elements->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
2468            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
2469              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
2470                $body_el->set_attribute_ns                $body_el->set_attribute_ns
# Line 2112  sub _construct_tree ($) { Line 2482  sub _construct_tree ($) {
2482                  pre => 1,                  pre => 1,
2483                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2484          ## has a p element in scope          ## has a p element in scope
2485          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2486            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2487              !!!back-token;              !!!back-token;
2488              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2139  sub _construct_tree ($) { Line 2509  sub _construct_tree ($) {
2509          }          }
2510          return;          return;
2511        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
2512          if (defined $form_element) {          if (defined $self->{form_element}) {
2513            !!!parse-error;            !!!parse-error (type => 'in form:form');
2514            ## Ignore the token            ## Ignore the token
2515              !!!next-token;
2516              return;
2517          } else {          } else {
2518            ## has a p element in scope            ## has a p element in scope
2519            INSCOPE: for (reverse @$open_elements) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
2520              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
2521                !!!back-token;                !!!back-token;
2522                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => 'end tag', tag_name => 'p'};
# Line 2158  sub _construct_tree ($) { Line 2530  sub _construct_tree ($) {
2530            } # INSCOPE            } # INSCOPE
2531                            
2532            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2533            $form_element = $open_elements->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
2534            !!!next-token;            !!!next-token;
2535            return;            return;
2536          }          }
2537        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
2538          ## has a p element in scope          ## has a p element in scope
2539          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2540            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2541              !!!back-token;              !!!back-token;
2542              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2179  sub _construct_tree ($) { Line 2551  sub _construct_tree ($) {
2551                        
2552          ## Step 1          ## Step 1
2553          my $i = -1;          my $i = -1;
2554          my $node = $open_elements->[$i];          my $node = $self->{open_elements}->[$i];
2555          LI: {          LI: {
2556            ## Step 2            ## Step 2
2557            if ($node->[1] eq 'li') {            if ($node->[1] eq 'li') {
2558              splice @$open_elements, $i;              if ($i != -1) {
2559                  !!!parse-error (type => 'end tag missing:'.
2560                                  $self->{open_elements}->[-1]->[1]);
2561                  ## TODO: test
2562                }
2563                splice @{$self->{open_elements}}, $i;
2564              last LI;              last LI;
2565            }            }
2566                        
# Line 2198  sub _construct_tree ($) { Line 2575  sub _construct_tree ($) {
2575                        
2576            ## Step 4            ## Step 4
2577            $i--;            $i--;
2578            $node = $open_elements->[$i];            $node = $self->{open_elements}->[$i];
2579            redo LI;            redo LI;
2580          } # LI          } # LI
2581                        
# Line 2207  sub _construct_tree ($) { Line 2584  sub _construct_tree ($) {
2584          return;          return;
2585        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
2586          ## has a p element in scope          ## has a p element in scope
2587          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2588            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2589              !!!back-token;              !!!back-token;
2590              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2222  sub _construct_tree ($) { Line 2599  sub _construct_tree ($) {
2599                        
2600          ## Step 1          ## Step 1
2601          my $i = -1;          my $i = -1;
2602          my $node = $open_elements->[$i];          my $node = $self->{open_elements}->[$i];
2603          LI: {          LI: {
2604            ## Step 2            ## Step 2
2605            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
2606              splice @$open_elements, $i;              if ($i != -1) {
2607                  !!!parse-error (type => 'end tag missing:'.
2608                                  $self->{open_elements}->[-1]->[1]);
2609                  ## TODO: test
2610                }
2611                splice @{$self->{open_elements}}, $i;
2612              last LI;              last LI;
2613            }            }
2614                        
# Line 2241  sub _construct_tree ($) { Line 2623  sub _construct_tree ($) {
2623                        
2624            ## Step 4            ## Step 4
2625            $i--;            $i--;
2626            $node = $open_elements->[$i];            $node = $self->{open_elements}->[$i];
2627            redo LI;            redo LI;
2628          } # LI          } # LI
2629                        
# Line 2250  sub _construct_tree ($) { Line 2632  sub _construct_tree ($) {
2632          return;          return;
2633        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
2634          ## has a p element in scope          ## has a p element in scope
2635          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2636            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2637              !!!back-token;              !!!back-token;
2638              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2273  sub _construct_tree ($) { Line 2655  sub _construct_tree ($) {
2655                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
2656                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2657          ## has a p element in scope          ## has a p element in scope
2658          INSCOPE: for (reverse 0..$#$open_elements) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2659            my $node = $open_elements->[$_];            my $node = $self->{open_elements}->[$_];
2660            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
2661              !!!back-token;              !!!back-token;
2662              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2287  sub _construct_tree ($) { Line 2669  sub _construct_tree ($) {
2669            }            }
2670          } # INSCOPE          } # INSCOPE
2671                        
2672            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
2673          ## has an element in scope          ## has an element in scope
2674          my $i;          #my $i;
2675          INSCOPE: for (reverse 0..$#$open_elements) {          #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2676            my $node = $open_elements->[$_];          #  my $node = $self->{open_elements}->[$_];
2677            if ({          #  if ({
2678                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,          #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
2679                }->{$node->[1]}) {          #      }->{$node->[1]}) {
2680              $i = $_;          #    $i = $_;
2681              last INSCOPE;          #    last INSCOPE;
2682            } elsif ({          #  } elsif ({
2683                      table => 1, caption => 1, td => 1, th => 1,          #            table => 1, caption => 1, td => 1, th => 1,
2684                      button => 1, marquee => 1, object => 1, html => 1,          #            button => 1, marquee => 1, object => 1, html => 1,
2685                     }->{$node->[1]}) {          #           }->{$node->[1]}) {
2686              last INSCOPE;          #    last INSCOPE;
2687            }          #  }
2688          } # INSCOPE          #} # INSCOPE
2689                      #  
2690          if (defined $i) {          #if (defined $i) {
2691            !!!parse-error;          #  !!! parse-error (type => 'in hn:hn');
2692            splice @$open_elements, $i;          #  splice @{$self->{open_elements}}, $i;
2693          }          #}
2694                        
2695          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2696                        
# Line 2317  sub _construct_tree ($) { Line 2700  sub _construct_tree ($) {
2700          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
2701            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
2702            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
2703              !!!parse-error ('a in a');              !!!parse-error (type => 'in a:a');
2704                            
2705              !!!back-token;              !!!back-token;
2706              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => 'end tag', tag_name => 'a'};
# Line 2329  sub _construct_tree ($) { Line 2712  sub _construct_tree ($) {
2712                  last AFE2;                  last AFE2;
2713                }                }
2714              } # AFE2              } # AFE2
2715              OE: for (reverse 0..$#$open_elements) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
2716                if ($open_elements->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
2717                  splice @$open_elements, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
2718                  last OE;                  last OE;
2719                }                }
2720              } # OE              } # OE
# Line 2344  sub _construct_tree ($) { Line 2727  sub _construct_tree ($) {
2727          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2728    
2729          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2730          push @$active_formatting_elements, $open_elements->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
2731    
2732          !!!next-token;          !!!next-token;
2733          return;          return;
2734        } elsif ({        } elsif ({
2735                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
2736                  nobr => 1, s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
2737                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
2738                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2739          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2740                    
2741          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2742          push @$active_formatting_elements, $open_elements->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
2743            
2744            !!!next-token;
2745            return;
2746          } elsif ($token->{tag_name} eq 'nobr') {
2747            $reconstruct_active_formatting_elements->($insert_to_current);
2748    
2749            ## has a |nobr| element in scope
2750            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2751              my $node = $self->{open_elements}->[$_];
2752              if ($node->[1] eq 'nobr') {
2753                !!!back-token;
2754                $token = {type => 'end tag', tag_name => 'nobr'};
2755                return;
2756              } elsif ({
2757                        table => 1, caption => 1, td => 1, th => 1,
2758                        button => 1, marquee => 1, object => 1, html => 1,
2759                       }->{$node->[1]}) {
2760                last INSCOPE;
2761              }
2762            } # INSCOPE
2763            
2764            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2765            push @$active_formatting_elements, $self->{open_elements}->[-1];
2766                    
2767          !!!next-token;          !!!next-token;
2768          return;          return;
2769        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
2770          ## has a button element in scope          ## has a button element in scope
2771          INSCOPE: for (reverse 0..$#$open_elements) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2772            my $node = $open_elements->[$_];            my $node = $self->{open_elements}->[$_];
2773            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
2774              !!!parse-error;              !!!parse-error (type => 'in button:button');
2775              !!!back-token;              !!!back-token;
2776              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => 'end tag', tag_name => 'button'};
2777              return;              return;
# Line 2395  sub _construct_tree ($) { Line 2801  sub _construct_tree ($) {
2801          return;          return;
2802        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
2803          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2804                    $parse_rcdata->('CDATA', $insert);
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!next-token;  
2805          return;          return;
2806        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
2807          ## has a p element in scope          ## has a p element in scope
2808          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2809            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2810              !!!back-token;              !!!back-token;
2811              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2419  sub _construct_tree ($) { Line 2820  sub _construct_tree ($) {
2820                        
2821          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2822                        
2823          $insertion_mode = 'in table';          $self->{insertion_mode} = 'in table';
2824                        
2825          !!!next-token;          !!!next-token;
2826          return;          return;
# Line 2429  sub _construct_tree ($) { Line 2830  sub _construct_tree ($) {
2830                  image => 1,                  image => 1,
2831                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2832          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'image') {
2833            !!!parse-error;            !!!parse-error (type => 'image');
2834            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
2835          }          }
2836                    
2837          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2838                    
2839          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2840          pop @$open_elements;          pop @{$self->{open_elements}};
2841                    
2842          !!!next-token;          !!!next-token;
2843          return;          return;
2844        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
2845          ## has a p element in scope          ## has a p element in scope
2846          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2847            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2848              !!!back-token;              !!!back-token;
2849              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2456  sub _construct_tree ($) { Line 2857  sub _construct_tree ($) {
2857          } # INSCOPE          } # INSCOPE
2858                        
2859          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2860          pop @$open_elements;          pop @{$self->{open_elements}};
2861                        
2862          !!!next-token;          !!!next-token;
2863          return;          return;
# Line 2464  sub _construct_tree ($) { Line 2865  sub _construct_tree ($) {
2865          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2866                    
2867          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2868          ## TODO: associate with $form_element if defined          ## TODO: associate with $self->{form_element} if defined
2869          pop @$open_elements;          pop @{$self->{open_elements}};
2870                    
2871          !!!next-token;          !!!next-token;
2872          return;          return;
2873        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
2874          !!!parse-error;          !!!parse-error (type => 'isindex');
2875                    
2876          if (defined $form_element) {          if (defined $self->{form_element}) {
2877            ## Ignore the token            ## Ignore the token
2878            !!!next-token;            !!!next-token;
2879            return;            return;
2880          } else {          } else {
2881            my $at = $token->{attributes};            my $at = $token->{attributes};
2882              my $form_attrs;
2883              $form_attrs->{action} = $at->{action} if $at->{action};
2884              my $prompt_attr = $at->{prompt};
2885            $at->{name} = {name => 'name', value => 'isindex'};            $at->{name} = {name => 'name', value => 'isindex'};
2886              delete $at->{action};
2887              delete $at->{prompt};
2888            my @tokens = (            my @tokens = (
2889                          {type => 'start tag', tag_name => 'form'},                          {type => 'start tag', tag_name => 'form',
2890                             attributes => $form_attrs},
2891                          {type => 'start tag', tag_name => 'hr'},                          {type => 'start tag', tag_name => 'hr'},
2892                          {type => 'start tag', tag_name => 'p'},                          {type => 'start tag', tag_name => 'p'},
2893                          {type => 'start tag', tag_name => 'label'},                          {type => 'start tag', tag_name => 'label'},
2894                          {type => 'character',                         );
2895                           data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD            if ($prompt_attr) {
2896                          ## TODO: make this configurable              push @tokens, {type => 'character', data => $prompt_attr->{value}};
2897              } else {
2898                push @tokens, {type => 'character',
2899                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
2900                ## TODO: make this configurable
2901              }
2902              push @tokens,
2903                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => 'start tag', tag_name => 'input', attributes => $at},
2904                          #{type => 'character', data => ''}, # SHOULD                          #{type => 'character', data => ''}, # SHOULD
2905                          {type => 'end tag', tag_name => 'label'},                          {type => 'end tag', tag_name => 'label'},
2906                          {type => 'end tag', tag_name => 'p'},                          {type => 'end tag', tag_name => 'p'},
2907                          {type => 'start tag', tag_name => 'hr'},                          {type => 'start tag', tag_name => 'hr'},
2908                          {type => 'end tag', tag_name => 'form'},                          {type => 'end tag', tag_name => 'form'};
                        );  
2909            $token = shift @tokens;            $token = shift @tokens;
2910            !!!back-token (@tokens);            !!!back-token (@tokens);
2911            return;            return;
2912          }          }
2913        } elsif ({        } elsif ($token->{tag_name} eq 'textarea') {
                 textarea => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
2914          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
2915          my $el;          my $el;
2916          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
2917                    
2918          if ($token->{tag_name} eq 'textarea') {          ## TODO: $self->{form_element} if defined
2919            ## TODO: form_element if defined          $self->{content_model_flag} = 'RCDATA';
2920            $self->{content_model_flag} = 'RCDATA';          delete $self->{escape}; # MUST
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
2921                    
2922          $insert->($el);          $insert->($el);
2923                    
2924          my $text = '';          my $text = '';
2925          !!!next-token;          !!!next-token;
2926            if ($token->{type} eq 'character') {
2927              $token->{data} =~ s/^\x0A//;
2928              unless (length $token->{data}) {
2929                !!!next-token;
2930              }
2931            }
2932          while ($token->{type} eq 'character') {          while ($token->{type} eq 'character') {
2933            $text .= $token->{data};            $text .= $token->{data};
2934            !!!next-token;            !!!next-token;
# Line 2533  sub _construct_tree ($) { Line 2943  sub _construct_tree ($) {
2943              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
2944            ## Ignore the token            ## Ignore the token
2945          } else {          } else {
2946            !!!parse-error;            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
           ## ISSUE: And ignore?  
2947          }          }
2948          !!!next-token;          !!!next-token;
2949          return;          return;
2950          } elsif ({
2951                    iframe => 1,
2952                    noembed => 1,
2953                    noframes => 1,
2954                    noscript => 0, ## TODO: 1 if scripting is enabled
2955                   }->{$token->{tag_name}}) {
2956            $parse_rcdata->('CDATA', $insert);
2957            return;
2958        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
2959          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2960                    
2961          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2962                    
2963          $insertion_mode = 'in select';          $self->{insertion_mode} = 'in select';
2964          !!!next-token;          !!!next-token;
2965          return;          return;
2966        } elsif ({        } elsif ({
# Line 2552  sub _construct_tree ($) { Line 2969  sub _construct_tree ($) {
2969                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
2970                  thead => 1, tr => 1,                  thead => 1, tr => 1,
2971                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2972          !!!parse-error;          !!!parse-error (type => 'in body:'.$token->{tag_name});
2973          ## Ignore the token          ## Ignore the token
2974          !!!next-token;          !!!next-token;
2975          return;          return;
# Line 2568  sub _construct_tree ($) { Line 2985  sub _construct_tree ($) {
2985        }        }
2986      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} eq 'end tag') {
2987        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
2988          if (@$open_elements > 1 and $open_elements->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and
2989            ## ISSUE: There is an issue in the spec.              $self->{open_elements}->[1]->[1] eq 'body') {
2990            if ($open_elements->[-1]->[1] ne 'body') {            for (@{$self->{open_elements}}) {
2991              !!!parse-error;              unless ({
2992                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
2993                           th => 1, tr => 1, body => 1, html => 1,
2994                        }->{$_->[1]}) {
2995                  !!!parse-error (type => 'not closed:'.$_->[1]);
2996                }
2997            }            }
2998            $insertion_mode = 'after body';  
2999              $self->{insertion_mode} = 'after body';
3000            !!!next-token;            !!!next-token;
3001            return;            return;
3002          } else {          } else {
3003            !!!parse-error;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3004            ## Ignore the token            ## Ignore the token
3005            !!!next-token;            !!!next-token;
3006            return;            return;
3007          }          }
3008        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
3009          if (@$open_elements > 1 and $open_elements->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
3010            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
3011            if ($open_elements->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
3012              !!!parse-error;              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
3013            }            }
3014            $insertion_mode = 'after body';            $self->{insertion_mode} = 'after body';
3015            ## reprocess            ## reprocess
3016            return;            return;
3017          } else {          } else {
3018            !!!parse-error;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3019            ## Ignore the token            ## Ignore the token
3020            !!!next-token;            !!!next-token;
3021            return;            return;
# Line 2601  sub _construct_tree ($) { Line 3024  sub _construct_tree ($) {
3024                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
3025                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
3026                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 form => 1,  
3027                  p => 1,                  p => 1,
3028                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
3029                  button => 1, marquee => 1, object => 1,                  button => 1, marquee => 1, object => 1,
3030                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3031          ## has an element in scope          ## has an element in scope
3032          my $i;          my $i;
3033          INSCOPE: for (reverse 0..$#$open_elements) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3034            my $node = $open_elements->[$_];            my $node = $self->{open_elements}->[$_];
3035            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
3036              ## generate implied end tags              ## generate implied end tags
3037              if ({              if ({
# Line 2618  sub _construct_tree ($) { Line 3040  sub _construct_tree ($) {
3040                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
3041                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
3042                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3043                  }->{$open_elements->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3044                !!!back-token;                !!!back-token;
3045                $token = {type => 'end tag',                $token = {type => 'end tag',
3046                          tag_name => $open_elements->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3047                return;                return;
3048              }              }
3049              $i = $_;              $i = $_;
# Line 2634  sub _construct_tree ($) { Line 3056  sub _construct_tree ($) {
3056            }            }
3057          } # INSCOPE          } # INSCOPE
3058                    
3059          if ($open_elements->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3060            !!!parse-error;            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3061          }          }
3062                    
3063          splice @$open_elements, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
         undef $form_element if $token->{tag_name} eq 'form';  
3064          $clear_up_to_marker->()          $clear_up_to_marker->()
3065            if {            if {
3066              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
3067            }->{$token->{tag_name}};            }->{$token->{tag_name}};
3068          !!!next-token;          !!!next-token;
3069          return;          return;
3070          } elsif ($token->{tag_name} eq 'form') {
3071            ## has an element in scope
3072            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3073              my $node = $self->{open_elements}->[$_];
3074              if ($node->[1] eq $token->{tag_name}) {
3075                ## generate implied end tags
3076                if ({
3077                     dd => 1, dt => 1, li => 1, p => 1,
3078                     td => 1, th => 1, tr => 1,
3079                    }->{$self->{open_elements}->[-1]->[1]}) {
3080                  !!!back-token;
3081                  $token = {type => 'end tag',
3082                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3083                  return;
3084                }
3085                last INSCOPE;
3086              } elsif ({
3087                        table => 1, caption => 1, td => 1, th => 1,
3088                        button => 1, marquee => 1, object => 1, html => 1,
3089                       }->{$node->[1]}) {
3090                last INSCOPE;
3091              }
3092            } # INSCOPE
3093            
3094            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
3095              pop @{$self->{open_elements}};
3096            } else {
3097              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3098            }
3099    
3100            undef $self->{form_element};
3101            !!!next-token;
3102            return;
3103        } elsif ({        } elsif ({
3104                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
3105                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3106          ## has an element in scope          ## has an element in scope
3107          my $i;          my $i;
3108          INSCOPE: for (reverse 0..$#$open_elements) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3109            my $node = $open_elements->[$_];            my $node = $self->{open_elements}->[$_];
3110            if ({            if ({
3111                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
3112                }->{$node->[1]}) {                }->{$node->[1]}) {
# Line 2660  sub _construct_tree ($) { Line 3114  sub _construct_tree ($) {
3114              if ({              if ({
3115                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3116                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3117                  }->{$open_elements->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3118                !!!back-token;                !!!back-token;
3119                $token = {type => 'end tag',                $token = {type => 'end tag',
3120                          tag_name => $open_elements->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3121                return;                return;
3122              }              }
3123              $i = $_;              $i = $_;
# Line 2676  sub _construct_tree ($) { Line 3130  sub _construct_tree ($) {
3130            }            }
3131          } # INSCOPE          } # INSCOPE
3132                    
3133          if ($open_elements->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3134            !!!parse-error;            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3135          }          }
3136                    
3137          splice @$open_elements, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
3138          !!!next-token;          !!!next-token;
3139          return;          return;
3140        } elsif ({        } elsif ({
# Line 2690  sub _construct_tree ($) { Line 3144  sub _construct_tree ($) {
3144                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
3145                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3146          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
3147    ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>
3148          return;          return;
3149        } elsif ({        } elsif ({
3150                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
# Line 2698  sub _construct_tree ($) { Line 3153  sub _construct_tree ($) {
3153                  thead => 1, tr => 1,                  thead => 1, tr => 1,
3154                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
3155                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
3156                  img => 1, input => 1, isindex=> 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
3157                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
3158                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
3159                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
3160                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3161          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3162          ## Ignore the token          ## Ignore the token
3163          !!!next-token;          !!!next-token;
3164          return;          return;
# Line 2713  sub _construct_tree ($) { Line 3168  sub _construct_tree ($) {
3168        } else {        } else {
3169          ## Step 1          ## Step 1
3170          my $node_i = -1;          my $node_i = -1;
3171          my $node = $open_elements->[$node_i];          my $node = $self->{open_elements}->[$node_i];
3172    
3173          ## Step 2          ## Step 2
3174          S2: {          S2: {
# Line 2723  sub _construct_tree ($) { Line 3178  sub _construct_tree ($) {
3178              if ({              if ({
3179                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3180                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3181                  }->{$open_elements->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3182                !!!back-token;                !!!back-token;
3183                $token = {type => 'end tag',                $token = {type => 'end tag',
3184                          tag_name => $open_elements->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3185                return;                return;
3186              }              }
3187                    
3188              ## Step 2              ## Step 2
3189              if ($token->{tag_name} ne $open_elements->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
3190                !!!parse-error;                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3191              }              }
3192                            
3193              ## Step 3              ## Step 3
3194              splice @$open_elements, $node_i;              splice @{$self->{open_elements}}, $node_i;
3195    
3196                !!!next-token;
3197              last S2;              last S2;
3198            } else {            } else {
3199              ## Step 3              ## Step 3
# Line 2744  sub _construct_tree ($) { Line 3201  sub _construct_tree ($) {
3201                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
3202                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
3203                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
3204                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3205                ## Ignore the token                ## Ignore the token
3206                !!!next-token;                !!!next-token;
3207                last S2;                last S2;
# Line 2753  sub _construct_tree ($) { Line 3210  sub _construct_tree ($) {
3210                        
3211            ## Step 4            ## Step 4
3212            $node_i--;            $node_i--;
3213            $node = $open_elements->[$node_i];            $node = $self->{open_elements}->[$node_i];
3214                        
3215            ## Step 5;            ## Step 5;
3216            redo S2;            redo S2;
3217          } # S2          } # S2
3218            return;
3219        }        }
3220      }      }
3221    }; # $in_body    }; # $in_body
3222    
3223    B: {    B: {
3224      if ($phase eq 'initial') {      if ($phase eq 'main') {
       if ($token->{type} eq 'DOCTYPE') {  
         if ($token->{error}) {  
           ## ISSUE: Spec currently left this case undefined.  
           !!!parse-error ('bogus DOCTYPE');  
         }  
         my $doctype = $self->{document}->create_document_type_definition  
           ($token->{name});  
         $self->{document}->append_child ($doctype);  
         $phase = 'root element';  
         !!!next-token;  
         redo B;  
       } elsif ({  
                 comment => 1,  
                 'start tag' => 1,  
                 'end tag' => 1,  
                 'end-of-file' => 1,  
                }->{$token->{type}}) {  
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error ('missing DOCTYPE');  
         $phase = 'root element';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           $self->{document}->manakai_append_text ($1);  
           ## ISSUE: DOM3 Core does not allow Document > Text  
           unless (length $token->{data}) {  
             ## Stay in the phase  
             !!!next-token;  
             redo B;  
           }  
         }  
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error ('missing DOCTYPE');  
         $phase = 'root element';  
         ## reprocess  
         redo B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
       }  
     } elsif ($phase eq 'root element') {  
3225        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} eq 'DOCTYPE') {
3226          !!!parse-error;          !!!parse-error (type => 'in html:#DOCTYPE');
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           $self->{document}->manakai_append_text ($1);  
           ## ISSUE: DOM3 Core does not allow Document > Text  
           unless (length $token->{data}) {  
             ## Stay in the phase  
             !!!next-token;  
             redo B;  
           }  
         }  
         #  
       } elsif ({  
                 'start tag' => 1,  
                 'end tag' => 1,  
                 'end-of-file' => 1,  
                }->{$token->{type}}) {  
         ## ISSUE: There is an issue in the spec  
         #  
       } else {  
         die "$0: $token->{type}: Unknown token";  
       }  
       my $root_element; !!!create-element ($root_element, 'html');  
       $self->{document}->append_child ($root_element);  
       $open_elements = [[$root_element, 'html']];  
       $phase = 'main';  
       ## reprocess  
       redo B;  
     } elsif ($phase eq 'main') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error;  
3227          ## Ignore the token          ## Ignore the token
3228          ## Stay in the phase          ## Stay in the phase
3229          !!!next-token;          !!!next-token;
# Line 2854  sub _construct_tree ($) { Line 3231  sub _construct_tree ($) {
3231        } elsif ($token->{type} eq 'start tag' and        } elsif ($token->{type} eq 'start tag' and
3232                 $token->{tag_name} eq 'html') {                 $token->{tag_name} eq 'html') {
3233          ## TODO: unless it is the first start tag token, parse-error          ## TODO: unless it is the first start tag token, parse-error
3234          my $top_el = $open_elements->[0]->[0];          my $top_el = $self->{open_elements}->[0]->[0];
3235          for my $attr_name (keys %{$token->{attributes}}) {          for my $attr_name (keys %{$token->{attributes}}) {
3236            unless ($top_el->has_attribute_ns (undef, $attr_name)) {            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3237              $top_el->set_attribute_ns              $top_el->set_attribute_ns
# Line 2868  sub _construct_tree ($) { Line 3245  sub _construct_tree ($) {
3245          ## Generate implied end tags          ## Generate implied end tags
3246          if ({          if ({
3247               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,
3248              }->{$open_elements->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
3249            !!!back-token;            !!!back-token;
3250            $token = {type => 'end tag', tag_name => $open_elements->[-1]->[1]};            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
3251            redo B;            redo B;
3252          }          }
3253                    
3254          if (@$open_elements > 2 or          if (@{$self->{open_elements}} > 2 or
3255              (@$open_elements == 2 and $open_elements->[1]->[1] ne 'body')) {              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
3256            !!!parse-error;            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3257          } else {          } elsif (defined $self->{inner_html_node} and
3258            ## TODO: inner_html parser and @$open_elements > 1 and $open_elements->[1] ne 'body', then parse-error                   @{$self->{open_elements}} > 1 and
3259                     $self->{open_elements}->[1]->[1] ne 'body') {
3260              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3261          }          }
3262    
3263          ## Stop parsing          ## Stop parsing
# Line 2886  sub _construct_tree ($) { Line 3265  sub _construct_tree ($) {
3265    
3266          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
3267        } else {        } else {
3268          if ($insertion_mode eq 'before head') {          if ($self->{insertion_mode} eq 'before head') {
3269            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3270              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3271                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3272                unless (length $token->{data}) {                unless (length $token->{data}) {
3273                  !!!next-token;                  !!!next-token;
3274                  redo B;                  redo B;
3275                }                }
3276              }              }
3277              ## As if <head>              ## As if <head>
3278              !!!create-element ($head_element, 'head');              !!!create-element ($self->{head_element}, 'head');
3279              $open_elements->[-1]->[0]->append_child ($head_element);              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3280              push @$open_elements, [$head_element, 'head'];              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3281              $insertion_mode = 'in head';              $self->{insertion_mode} = 'in head';
3282              ## reprocess              ## reprocess
3283              redo B;              redo B;
3284            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
3285              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
3286              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
3287              !!!next-token;              !!!next-token;
3288              redo B;              redo B;
3289            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3290              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};
3291              !!!create-element ($head_element, 'head', $attr);              !!!create-element ($self->{head_element}, 'head', $attr);
3292              $open_elements->[-1]->[0]->append_child ($head_element);              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3293              push @$open_elements, [$head_element, 'head'];              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3294              $insertion_mode = 'in head';              $self->{insertion_mode} = 'in head';
3295              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3296                !!!next-token;                !!!next-token;
3297              #} elsif ({              #} elsif ({
# Line 2925  sub _construct_tree ($) { Line 3304  sub _construct_tree ($) {
3304              }              }
3305              redo B;              redo B;
3306            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3307              if ($token->{tag_name} eq 'html') {              if ({head => 1, body => 1, html => 1}->{$token->{tag_name}}) {
3308                ## As if <head>                ## As if <head>
3309                !!!create-element ($head_element, 'head');                !!!create-element ($self->{head_element}, 'head');
3310                $open_elements->[-1]->[0]->append_child ($head_element);                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3311                push @$open_elements, [$head_element, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3312                $insertion_mode = 'in head';                $self->{insertion_mode} = 'in head';
3313                ## reprocess                ## reprocess
3314                redo B;                redo B;
3315              } else {              } else {
3316                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3317                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3318                !!!next-token;                !!!next-token;
3319                redo B;                redo B;
3320              }              }
3321            } else {            } else {
3322              die "$0: $token->{type}: Unknown type";              die "$0: $token->{type}: Unknown type";
3323            }            }
3324          } elsif ($insertion_mode eq 'in head') {          } elsif ($self->{insertion_mode} eq 'in head' or
3325                     $self->{insertion_mode} eq 'in head noscript' or
3326                     $self->{insertion_mode} eq 'after head') {
3327            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3328              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3329                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3330                unless (length $token->{data}) {                unless (length $token->{data}) {
3331                  !!!next-token;                  !!!next-token;
3332                  redo B;                  redo B;
# Line 2955  sub _construct_tree ($) { Line 3336  sub _construct_tree ($) {
3336              #              #
3337            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
3338              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
3339              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
3340              !!!next-token;              !!!next-token;
3341              redo B;              redo B;
3342            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3343              if ($token->{tag_name} eq 'title') {              if ({base => ($self->{insertion_mode} eq 'in head' or
3344                ## NOTE: There is an "as if in head" code clone                            $self->{insertion_mode} eq 'after head'),
3345                my $title_el;                   link => 1, meta => 1}->{$token->{tag_name}}) {
3346                !!!create-element ($title_el, 'title', $token->{attributes});                ## NOTE: There is a "as if in head" code clone.
3347                (defined $head_element ? $head_element : $open_elements->[-1]->[0])                if ($self->{insertion_mode} eq 'after head') {
3348                  ->append_child ($title_el);                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3349                $self->{content_model_flag} = 'RCDATA';                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3350                  }
3351                my $text = '';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3352                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3353                  pop @{$self->{open_elements}}
3354                      if $self->{insertion_mode} eq 'after head';
3355                !!!next-token;                !!!next-token;
3356                while ($token->{type} eq 'character') {                redo B;
3357                  $text .= $token->{data};              } elsif ($token->{tag_name} eq 'title' and
3358                         $self->{insertion_mode} eq 'in head') {
3359                  ## NOTE: There is a "as if in head" code clone.
3360                  if ($self->{insertion_mode} eq 'after head') {
3361                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3362                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3363                  }
3364                  $parse_rcdata->('RCDATA', $insert_to_current);
3365                  pop @{$self->{open_elements}}
3366                      if $self->{insertion_mode} eq 'after head';
3367                  redo B;
3368                } elsif ($token->{tag_name} eq 'style') {
3369                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3370                  ## insertion mode 'in head')
3371                  ## NOTE: There is a "as if in head" code clone.
3372                  if ($self->{insertion_mode} eq 'after head') {
3373                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3374                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3375                  }
3376                  $parse_rcdata->('CDATA', $insert_to_current);
3377                  pop @{$self->{open_elements}}
3378                      if $self->{insertion_mode} eq 'after head';
3379                  redo B;
3380                } elsif ($token->{tag_name} eq 'noscript') {
3381                  if ($self->{insertion_mode} eq 'in head') {
3382                    ## NOTE: and scripting is disalbed
3383                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3384                    $self->{insertion_mode} = 'in head noscript';
3385                  !!!next-token;                  !!!next-token;
3386                }                  redo B;
3387                if (length $text) {                } elsif ($self->{insertion_mode} eq 'in head noscript') {
3388                  $title_el->manakai_append_text ($text);                  !!!parse-error (type => 'noscript in noscript');
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
                 
               if ($token->{type} eq 'end tag' and  
                   $token->{tag_name} eq 'title') {  
3389                  ## Ignore the token                  ## Ignore the token
3390                    redo B;
3391                } else {                } else {
3392                  !!!parse-error;                  #
                 ## ISSUE: And ignore?  
3393                }                }
3394                } elsif ($token->{tag_name} eq 'head' and
3395                         $self->{insertion_mode} ne 'after head') {
3396                  !!!parse-error (type => 'in head:head'); # or in head noscript
3397                  ## Ignore the token
3398                !!!next-token;                !!!next-token;
3399                redo B;                redo B;
3400              } elsif ($token->{tag_name} eq 'style') {              } elsif ($self->{insertion_mode} ne 'in head noscript' and
3401                $style_start_tag->();                       $token->{tag_name} eq 'script') {
3402                redo B;                if ($self->{insertion_mode} eq 'after head') {
3403              } elsif ($token->{tag_name} eq 'script') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3404                $script_start_tag->();                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3405                  }
3406                  ## NOTE: There is a "as if in head" code clone.
3407                  $script_start_tag->($insert_to_current);
3408                  pop @{$self->{open_elements}}
3409                      if $self->{insertion_mode} eq 'after head';
3410                redo B;                redo B;
3411              } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {              } elsif ($self->{insertion_mode} eq 'after head' and
3412                ## NOTE: There are "as if in head" code clones                       $token->{tag_name} eq 'body') {
3413                my $el;                !!!insert-element ('body', $token->{attributes});
3414                !!!create-element ($el, $token->{tag_name}, $token->{attributes});                $self->{insertion_mode} = 'in body';
               (defined $head_element ? $head_element : $open_elements->[-1]->[0])  
                 ->append_child ($el);  
   
3415                !!!next-token;                !!!next-token;
3416                redo B;                redo B;
3417              } elsif ($token->{tag_name} eq 'head') {              } elsif ($self->{insertion_mode} eq 'after head' and
3418                !!!parse-error;                       $token->{tag_name} eq 'frameset') {
3419                ## Ignore the token                !!!insert-element ('frameset', $token->{attributes});
3420                  $self->{insertion_mode} = 'in frameset';
3421                !!!next-token;                !!!next-token;
3422                redo B;                redo B;
3423              } else {              } else {
3424                #                #
3425              }              }
3426            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3427              if ($token->{tag_name} eq 'head') {              if ($self->{insertion_mode} eq 'in head' and
3428                if ($open_elements->[-1]->[1] eq 'head') {                  $token->{tag_name} eq 'head') {
3429                  pop @$open_elements;                pop @{$self->{open_elements}};
3430                } else {                $self->{insertion_mode} = 'after head';
3431                  !!!parse-error;                !!!next-token;
3432                }                redo B;
3433                $insertion_mode = 'after head';              } elsif ($self->{insertion_mode} eq 'in head noscript' and
3434                    $token->{tag_name} eq 'noscript') {
3435                  pop @{$self->{open_elements}};
3436                  $self->{insertion_mode} = 'in head';
3437                !!!next-token;                !!!next-token;
3438                redo B;                redo B;
3439              } elsif ($token->{tag_name} eq 'html') {              } elsif ($self->{insertion_mode} eq 'in head' and
3440                         ($token->{tag_name} eq 'body' or
3441                          $token->{tag_name} eq 'html')) {
3442                #                #
3443              } else {              } elsif ($self->{insertion_mode} ne 'after head') {
3444                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3445                ## Ignore the token                ## Ignore the token
3446                !!!next-token;                !!!next-token;
3447                redo B;                redo B;
3448                } else {
3449                  #
3450              }              }
3451            } else {            } else {
3452              #              #
3453            }            }
3454    
3455            if ($open_elements->[-1]->[1] eq 'head') {            ## As if </head> or </noscript> or <body>
3456              ## As if </head>            if ($self->{insertion_mode} eq 'in head') {
3457              pop @$open_elements;              pop @{$self->{open_elements}};
3458                $self->{insertion_mode} = 'after head';
3459              } elsif ($self->{insertion_mode} eq 'in head noscript') {
3460                pop @{$self->{open_elements}};
3461                !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));
3462                $self->{insertion_mode} = 'in head';
3463              } else { # 'after head'
3464                !!!insert-element ('body');
3465                $self->{insertion_mode} = 'in body';
3466            }            }
           $insertion_mode = 'after head';  
3467            ## reprocess            ## reprocess
3468            redo B;            redo B;
3469    
3470            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3471          } elsif ($insertion_mode eq 'after head') {          } elsif ($self->{insertion_mode} eq 'in body') {
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $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});  
             $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});  
               $insertion_mode = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $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;  
                $insertion_mode = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $insertion_mode = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($insertion_mode eq 'in body') {  
3472            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3473              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3474              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3475                            
3476              $open_elements->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3477    
3478              !!!next-token;              !!!next-token;
3479              redo B;              redo B;
3480            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
3481              ## NOTE: There is a code clone of "comment in body".              ## NOTE: There is a code clone of "comment in body".
3482              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
3483              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
3484              !!!next-token;              !!!next-token;
3485              redo B;              redo B;
3486            } else {            } else {
3487              $in_body->($insert_to_current);              $in_body->($insert_to_current);
3488              redo B;              redo B;
3489            }            }
3490          } elsif ($insertion_mode eq 'in table') {          } elsif ($self->{insertion_mode} eq 'in table') {
3491            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3492              ## NOTE: There are "character in table" code clones.              ## NOTE: There are "character in table" code clones.
3493              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3494                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3495                                
3496                unless (length $token->{data}) {                unless (length $token->{data}) {
3497                  !!!next-token;                  !!!next-token;
# Line 3120  sub _construct_tree ($) { Line 3499  sub _construct_tree ($) {
3499                }                }
3500              }              }
3501    
3502                !!!parse-error (type => 'in table:#character');
3503    
3504              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
3505              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
3506              ## into the current node" while characters might not be              ## into the current node" while characters might not be
# Line 3129  sub _construct_tree ($) { Line 3510  sub _construct_tree ($) {
3510              if ({              if ({
3511                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3512                   thead => 1, tr => 1,                   thead => 1, tr => 1,
3513                  }->{$open_elements->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3514                # MUST                # MUST
3515                my $foster_parent_element;                my $foster_parent_element;
3516                my $next_sibling;                my $next_sibling;
3517                my $prev_sibling;                my $prev_sibling;
3518                OE: for (reverse 0..$#$open_elements) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
3519                  if ($open_elements->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
3520                    my $parent = $open_elements->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3521                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
3522                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
3523                      $next_sibling = $open_elements->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
3524                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
3525                    } else {                    } else {
3526                      $foster_parent_element = $open_elements->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3527                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
3528                    }                    }
3529                    last OE;                    last OE;
3530                  }                  }
3531                } # OE                } # OE
3532                $foster_parent_element = $open_elements->[0]->[0] and                $foster_parent_element = $self->{open_elements}->[0]->[0] and
3533                $prev_sibling = $foster_parent_element->last_child                $prev_sibling = $foster_parent_element->last_child
3534                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
3535                if (defined $prev_sibling and                if (defined $prev_sibling and
# Line 3160  sub _construct_tree ($) { Line 3541  sub _construct_tree ($) {
3541                     $next_sibling);                     $next_sibling);
3542                }                }
3543              } else {              } else {
3544                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3545              }              }
3546                            
3547              !!!next-token;              !!!next-token;
3548              redo B;              redo B;
3549            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
3550              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
3551              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
3552              !!!next-token;              !!!next-token;
3553              redo B;              redo B;
3554            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 3177  sub _construct_tree ($) { Line 3558  sub _construct_tree ($) {
3558                   tbody => 1, tfoot => 1, thead => 1,                   tbody => 1, tfoot => 1, thead => 1,
3559                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3560                ## Clear back to table context                ## Clear back to table context
3561                while ($open_elements->[-1]->[1] ne 'table' and                while ($self->{open_elements}->[-1]->[1] ne 'table' and
3562                       $open_elements->[-1]->[1] ne 'html') {                       $self->{open_elements}->[-1]->[1] ne 'html') {
3563                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3564                  pop @$open_elements;                  pop @{$self->{open_elements}};
3565                }                }
3566    
3567                push @$active_formatting_elements, ['#marker', '']                push @$active_formatting_elements, ['#marker', '']
3568                  if $token->{tag_name} eq 'caption';                  if $token->{tag_name} eq 'caption';
3569    
3570                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3571                $insertion_mode = {                $self->{insertion_mode} = {
3572                                   caption => 'in caption',                                   caption => 'in caption',
3573                                   colgroup => 'in column group',                                   colgroup => 'in column group',
3574                                   tbody => 'in table body',                                   tbody => 'in table body',
# Line 3201  sub _construct_tree ($) { Line 3582  sub _construct_tree ($) {
3582                        td => 1, th => 1, tr => 1,                        td => 1, th => 1, tr => 1,
3583                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3584                ## Clear back to table context                ## Clear back to table context
3585                while ($open_elements->[-1]->[1] ne 'table' and                while ($self->{open_elements}->[-1]->[1] ne 'table' and
3586                       $open_elements->[-1]->[1] ne 'html') {                       $self->{open_elements}->[-1]->[1] ne 'html') {
3587                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3588                  pop @$open_elements;                  pop @{$self->{open_elements}};
3589                }                }
3590    
3591                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');
3592                $insertion_mode = $token->{tag_name} eq 'col'                $self->{insertion_mode} = $token->{tag_name} eq 'col'
3593                  ? 'in column group' : 'in table body';                  ? 'in column group' : 'in table body';
3594                ## reprocess                ## reprocess
3595                redo B;                redo B;
3596              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3597                ## NOTE: There are code clones for this "table in table"                ## NOTE: There are code clones for this "table in table"
3598                !!!parse-error;                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3599    
3600                ## As if </table>                ## As if </table>
3601                ## have a table element in table scope                ## have a table element in table scope
3602                my $i;                my $i;
3603                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3604                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3605                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
3606                    $i = $_;                    $i = $_;
3607                    last INSCOPE;                    last INSCOPE;
# Line 3231  sub _construct_tree ($) { Line 3612  sub _construct_tree ($) {
3612                  }                  }
3613                } # INSCOPE                } # INSCOPE
3614                unless (defined $i) {                unless (defined $i) {
3615                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:table');
3616                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
3617                  !!!next-token;                  !!!next-token;
3618                  redo B;                  redo B;
# Line 3241  sub _construct_tree ($) { Line 3622  sub _construct_tree ($) {
3622                if ({                if ({
3623                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3624                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3625                    }->{$open_elements->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3626                  !!!back-token; # <table>                  !!!back-token; # <table>
3627                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
3628                  !!!back-token;                  !!!back-token;
3629                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3630                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3631                  redo B;                  redo B;
3632                }                }
3633    
3634                if ($open_elements->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3635                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3636                }                }
3637    
3638                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3639    
3640                $reset_insertion_mode->();                              $self->_reset_insertion_mode;
3641    
3642                ## reprocess                ## reprocess
3643                redo B;                redo B;
# Line 3267  sub _construct_tree ($) { Line 3648  sub _construct_tree ($) {
3648              if ($token->{tag_name} eq 'table') {              if ($token->{tag_name} eq 'table') {
3649                ## have a table element in table scope                ## have a table element in table scope
3650                my $i;                my $i;
3651                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3652                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3653                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3654                    $i = $_;                    $i = $_;
3655                    last INSCOPE;                    last INSCOPE;
# Line 3279  sub _construct_tree ($) { Line 3660  sub _construct_tree ($) {
3660                  }                  }
3661                } # INSCOPE                } # INSCOPE
3662                unless (defined $i) {                unless (defined $i) {
3663                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3664                  ## Ignore the token                  ## Ignore the token
3665                  !!!next-token;                  !!!next-token;
3666                  redo B;                  redo B;
# Line 3289  sub _construct_tree ($) { Line 3670  sub _construct_tree ($) {
3670                if ({                if ({
3671                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3672                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3673                    }->{$open_elements->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3674                  !!!back-token;                  !!!back-token;
3675                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3676                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3677                  redo B;                  redo B;
3678                }                }
3679    
3680                if ($open_elements->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3681                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3682                }                }
3683    
3684                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3685    
3686                $reset_insertion_mode->();                $self->_reset_insertion_mode;
3687    
3688                !!!next-token;                !!!next-token;
3689                redo B;                redo B;
# Line 3311  sub _construct_tree ($) { Line 3692  sub _construct_tree ($) {
3692                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,
3693                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3694                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3695                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3696                ## Ignore the token                ## Ignore the token
3697                !!!next-token;                !!!next-token;
3698                redo B;                redo B;
# Line 3322  sub _construct_tree ($) { Line 3703  sub _construct_tree ($) {
3703              #              #
3704            }            }
3705    
3706            !!!parse-error;            !!!parse-error (type => 'in table:'.$token->{tag_name});
3707            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
3708            redo B;            redo B;
3709          } elsif ($insertion_mode eq 'in caption') {          } elsif ($self->{insertion_mode} eq 'in caption') {
3710            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3711              ## NOTE: This is a code clone of "character in body".              ## NOTE: This is a code clone of "character in body".
3712              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3713                            
3714              $open_elements->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3715    
3716              !!!next-token;              !!!next-token;
3717              redo B;              redo B;
3718            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
3719              ## NOTE: This is a code clone of "comment in body".              ## NOTE: This is a code clone of "comment in body".
3720              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
3721              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
3722              !!!next-token;              !!!next-token;
3723              redo B;              redo B;
3724            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 3345  sub _construct_tree ($) { Line 3726  sub _construct_tree ($) {
3726                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3727                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3728                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3729                !!!parse-error;                !!!parse-error (type => 'not closed:caption');
3730    
3731                ## As if </caption>                ## As if </caption>
3732                ## have a table element in table scope                ## have a table element in table scope
3733                my $i;                my $i;
3734                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3735                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3736                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
3737                    $i = $_;                    $i = $_;
3738                    last INSCOPE;                    last INSCOPE;
# Line 3362  sub _construct_tree ($) { Line 3743  sub _construct_tree ($) {
3743                  }                  }
3744                } # INSCOPE                } # INSCOPE
3745                unless (defined $i) {                unless (defined $i) {
3746                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:caption');
3747                  ## Ignore the token                  ## Ignore the token
3748                  !!!next-token;                  !!!next-token;
3749                  redo B;                  redo B;
# Line 3372  sub _construct_tree ($) { Line 3753  sub _construct_tree ($) {
3753                if ({                if ({
3754                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3755                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3756                    }->{$open_elements->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3757                  !!!back-token; # <?>                  !!!back-token; # <?>
3758                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
3759                  !!!back-token;                  !!!back-token;
3760                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3761                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3762                  redo B;                  redo B;
3763                }                }
3764    
3765                if ($open_elements->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3766                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3767                }                }
3768    
3769                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3770    
3771                $clear_up_to_marker->();                $clear_up_to_marker->();
3772    
3773                $insertion_mode = 'in table';                $self->{insertion_mode} = 'in table';
3774    
3775                ## reprocess                ## reprocess
3776                redo B;                redo B;
# Line 3400  sub _construct_tree ($) { Line 3781  sub _construct_tree ($) {
3781              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'caption') {
3782                ## have a table element in table scope                ## have a table element in table scope
3783                my $i;                my $i;
3784                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3785                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3786                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3787                    $i = $_;                    $i = $_;
3788                    last INSCOPE;                    last INSCOPE;
# Line 3412  sub _construct_tree ($) { Line 3793  sub _construct_tree ($) {
3793                  }                  }
3794                } # INSCOPE                } # INSCOPE
3795                unless (defined $i) {                unless (defined $i) {
3796                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3797                  ## Ignore the token                  ## Ignore the token
3798                  !!!next-token;                  !!!next-token;
3799                  redo B;                  redo B;
# Line 3422  sub _construct_tree ($) { Line 3803  sub _construct_tree ($) {
3803                if ({                if ({
3804                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3805                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3806                    }->{$open_elements->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3807                  !!!back-token;                  !!!back-token;
3808                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3809                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3810                  redo B;                  redo B;
3811                }                }
3812    
3813                if ($open_elements->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3814                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3815                }                }
3816    
3817                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3818    
3819                $clear_up_to_marker->();                $clear_up_to_marker->();
3820    
3821                $insertion_mode = 'in table';                $self->{insertion_mode} = 'in table';
3822    
3823                !!!next-token;                !!!next-token;
3824                redo B;                redo B;
3825              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3826                !!!parse-error;                !!!parse-error (type => 'not closed:caption');
3827    
3828                ## As if </caption>                ## As if </caption>
3829                ## have a table element in table scope                ## have a table element in table scope
3830                my $i;                my $i;
3831                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3832                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3833                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
3834                    $i = $_;                    $i = $_;
3835                    last INSCOPE;                    last INSCOPE;
# Line 3459  sub _construct_tree ($) { Line 3840  sub _construct_tree ($) {
3840                  }                  }
3841                } # INSCOPE                } # INSCOPE
3842                unless (defined $i) {                unless (defined $i) {
3843                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:caption');
3844                  ## Ignore the token                  ## Ignore the token
3845                  !!!next-token;                  !!!next-token;
3846                  redo B;                  redo B;
# Line 3469  sub _construct_tree ($) { Line 3850  sub _construct_tree ($) {
3850                if ({                if ({
3851                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3852                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3853                    }->{$open_elements->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3854                  !!!back-token; # </table>                  !!!back-token; # </table>
3855                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
3856                  !!!back-token;                  !!!back-token;
3857                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3858                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3859                  redo B;                  redo B;
3860                }                }
3861    
3862                if ($open_elements->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3863                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3864                }                }
3865    
3866                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3867    
3868                $clear_up_to_marker->();                $clear_up_to_marker->();
3869    
3870                $insertion_mode = 'in table';                $self->{insertion_mode} = 'in table';
3871    
3872                ## reprocess                ## reprocess
3873                redo B;                redo B;
# Line 3495  sub _construct_tree ($) { Line 3876  sub _construct_tree ($) {
3876                        html => 1, tbody => 1, td => 1, tfoot => 1,                        html => 1, tbody => 1, td => 1, tfoot => 1,
3877                        th => 1, thead => 1, tr => 1,                        th => 1, thead => 1, tr => 1,
3878                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3879                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3880                ## Ignore the token                ## Ignore the token
3881                redo B;                redo B;
3882              } else {              } else {
# Line 3507  sub _construct_tree ($) { Line 3888  sub _construct_tree ($) {
3888                                
3889            $in_body->($insert_to_current);            $in_body->($insert_to_current);
3890            redo B;            redo B;
3891          } elsif ($insertion_mode eq 'in column group') {          } elsif ($self->{insertion_mode} eq 'in column group') {
3892            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3893              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3894                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3895                unless (length $token->{data}) {                unless (length $token->{data}) {
3896                  !!!next-token;                  !!!next-token;
3897                  redo B;                  redo B;
# Line 3520  sub _construct_tree ($) { Line 3901  sub _construct_tree ($) {
3901              #              #
3902            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
3903              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
3904              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
3905              !!!next-token;              !!!next-token;
3906              redo B;              redo B;
3907            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3908              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
3909                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3910                pop @$open_elements;                pop @{$self->{open_elements}};
3911                !!!next-token;                !!!next-token;
3912                redo B;                redo B;
3913              } else {              } else {
# Line 3534  sub _construct_tree ($) { Line 3915  sub _construct_tree ($) {
3915              }              }
3916            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3917              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
3918                if ($open_elements->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3919                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:colgroup');
3920                  ## Ignore the token                  ## Ignore the token
3921                  !!!next-token;                  !!!next-token;
3922                  redo B;                  redo B;
3923                } else {                } else {
3924                  pop @$open_elements; # colgroup                  pop @{$self->{open_elements}}; # colgroup
3925                  $insertion_mode = 'in table';                  $self->{insertion_mode} = 'in table';
3926                  !!!next-token;                  !!!next-token;
3927                  redo B;                              redo B;            
3928                }                }
3929              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
3930                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:col');
3931                ## Ignore the token                ## Ignore the token
3932                !!!next-token;                !!!next-token;
3933                redo B;                redo B;
# Line 3558  sub _construct_tree ($) { Line 3939  sub _construct_tree ($) {
3939            }            }
3940    
3941            ## As if </colgroup>            ## As if </colgroup>
3942            if ($open_elements->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3943              !!!parse-error;              !!!parse-error (type => 'unmatched end tag:colgroup');
3944              ## Ignore the token              ## Ignore the token
3945              !!!next-token;              !!!next-token;
3946              redo B;              redo B;
3947            } else {            } else {
3948              pop @$open_elements; # colgroup              pop @{$self->{open_elements}}; # colgroup
3949              $insertion_mode = 'in table';              $self->{insertion_mode} = 'in table';
3950              ## reprocess              ## reprocess
3951              redo B;              redo B;
3952            }            }
3953          } elsif ($insertion_mode eq 'in table body') {          } elsif ($self->{insertion_mode} eq 'in table body') {
3954            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3955              ## NOTE: This is a "character in table" code clone.              ## NOTE: This is a "character in table" code clone.
3956              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3957                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3958                                
3959                unless (length $token->{data}) {                unless (length $token->{data}) {
3960                  !!!next-token;                  !!!next-token;
# Line 3581  sub _construct_tree ($) { Line 3962  sub _construct_tree ($) {
3962                }                }
3963              }              }
3964    
3965                !!!parse-error (type => 'in table:#character');
3966    
3967              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
3968              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
3969              ## into the current node" while characters might not be              ## into the current node" while characters might not be
# Line 3590  sub _construct_tree ($) { Line 3973  sub _construct_tree ($) {
3973              if ({              if ({
3974                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3975                   thead => 1, tr => 1,                   thead => 1, tr => 1,
3976                  }->{$open_elements->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3977                # MUST                # MUST
3978                my $foster_parent_element;                my $foster_parent_element;
3979                my $next_sibling;                my $next_sibling;
3980                my $prev_sibling;                my $prev_sibling;
3981                OE: for (reverse 0..$#$open_elements) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
3982                  if ($open_elements->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
3983                    my $parent = $open_elements->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3984                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
3985                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
3986                      $next_sibling = $open_elements->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
3987                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
3988                    } else {                    } else {
3989                      $foster_parent_element = $open_elements->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3990                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
3991                    }                    }
3992                    last OE;                    last OE;
3993                  }                  }
3994                } # OE                } # OE
3995                $foster_parent_element = $open_elements->[0]->[0] and                $foster_parent_element = $self->{open_elements}->[0]->[0] and
3996                $prev_sibling = $foster_parent_element->last_child                $prev_sibling = $foster_parent_element->last_child
3997                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
3998                if (defined $prev_sibling and                if (defined $prev_sibling and
# Line 3621  sub _construct_tree ($) { Line 4004  sub _construct_tree ($) {
4004                     $next_sibling);                     $next_sibling);
4005                }                }
4006              } else {              } else {
4007                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4008              }              }
4009                            
4010              !!!next-token;              !!!next-token;
# Line 3629  sub _construct_tree ($) { Line 4012  sub _construct_tree ($) {
4012            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
4013              ## Copied from 'in table'              ## Copied from 'in table'
4014              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
4015              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
4016              !!!next-token;              !!!next-token;
4017              redo B;              redo B;
4018            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 3637  sub _construct_tree ($) { Line 4020  sub _construct_tree ($) {
4020                   tr => 1,                   tr => 1,
4021                   th => 1, td => 1,                   th => 1, td => 1,
4022                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4023                  unless ($token->{tag_name} eq 'tr') {
4024                    !!!parse-error (type => 'missing start tag:tr');
4025                  }
4026    
4027                ## Clear back to table body context                ## Clear back to table body context
4028                while (not {                while (not {
4029                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4030                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4031                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4032                  pop @$open_elements;                  pop @{$self->{open_elements}};
4033                }                }
4034                                
4035                $insertion_mode = 'in row';                $self->{insertion_mode} = 'in row';
4036                if ($token->{tag_name} eq 'tr') {                if ($token->{tag_name} eq 'tr') {
4037                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4038                  !!!next-token;                  !!!next-token;
# Line 3660  sub _construct_tree ($) { Line 4047  sub _construct_tree ($) {
4047                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4048                ## have an element in table scope                ## have an element in table scope
4049                my $i;                my $i;
4050                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4051                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4052                  if ({                  if ({
4053                       tbody => 1, thead => 1, tfoot => 1,                       tbody => 1, thead => 1, tfoot => 1,
4054                      }->{$node->[1]}) {                      }->{$node->[1]}) {
# Line 3674  sub _construct_tree ($) { Line 4061  sub _construct_tree ($) {
4061                  }                  }
4062                } # INSCOPE                } # INSCOPE
4063                unless (defined $i) {                unless (defined $i) {
4064                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4065                  ## Ignore the token                  ## Ignore the token
4066                  !!!next-token;                  !!!next-token;
4067                  redo B;                  redo B;
# Line 3683  sub _construct_tree ($) { Line 4070  sub _construct_tree ($) {
4070                ## Clear back to table body context                ## Clear back to table body context
4071                while (not {                while (not {
4072                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4073                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4074                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4075                  pop @$open_elements;                  pop @{$self->{open_elements}};
4076                }                }
4077    
4078                ## As if <{current node}>                ## As if <{current node}>
# Line 3695  sub _construct_tree ($) { Line 4082  sub _construct_tree ($) {
4082                ## Clear back to table body context                ## Clear back to table body context
4083                ## nop by definition                ## nop by definition
4084    
4085                pop @$open_elements;                pop @{$self->{open_elements}};
4086                $insertion_mode = 'in table';                $self->{insertion_mode} = 'in table';
4087                ## reprocess                ## reprocess
4088                redo B;                redo B;
4089              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4090                ## NOTE: This is a code clone of "table in table"                ## NOTE: This is a code clone of "table in table"
4091                !!!parse-error;                !!!parse-error (type => 'not closed:table');
4092    
4093                ## As if </table>                ## As if </table>
4094                ## have a table element in table scope                ## have a table element in table scope
4095                my $i;                my $i;
4096                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4097                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4098                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4099                    $i = $_;                    $i = $_;
4100                    last INSCOPE;                    last INSCOPE;
# Line 3718  sub _construct_tree ($) { Line 4105  sub _construct_tree ($) {
4105                  }                  }
4106                } # INSCOPE                } # INSCOPE
4107                unless (defined $i) {                unless (defined $i) {
4108                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:table');
4109                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4110                  !!!next-token;                  !!!next-token;
4111                  redo B;                  redo B;
# Line 3728  sub _construct_tree ($) { Line 4115  sub _construct_tree ($) {
4115                if ({                if ({
4116                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4117                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4118                    }->{$open_elements->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4119                  !!!back-token; # <table>                  !!!back-token; # <table>
4120                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
4121                  !!!back-token;                  !!!back-token;
4122                  $token = {type => 'end tag',                  $token = {type => 'end tag',
4123                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4124                  redo B;                  redo B;
4125                }                }
4126    
4127                if ($open_elements->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4128                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4129                }                }
4130    
4131                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4132    
4133                $reset_insertion_mode->();                              $self->_reset_insertion_mode;
4134    
4135                ## reprocess                ## reprocess
4136                redo B;                redo B;
# Line 3756  sub _construct_tree ($) { Line 4143  sub _construct_tree ($) {
4143                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4144                ## have an element in table scope                ## have an element in table scope
4145                my $i;                my $i;
4146                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4147                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4148                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4149                    $i = $_;                    $i = $_;
4150                    last INSCOPE;                    last INSCOPE;
# Line 3768  sub _construct_tree ($) { Line 4155  sub _construct_tree ($) {
4155                  }                  }
4156                } # INSCOPE                } # INSCOPE
4157                unless (defined $i) {                unless (defined $i) {
4158                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4159                  ## Ignore the token                  ## Ignore the token
4160                  !!!next-token;                  !!!next-token;
4161                  redo B;                  redo B;
# Line 3777  sub _construct_tree ($) { Line 4164  sub _construct_tree ($) {
4164                ## Clear back to table body context                ## Clear back to table body context
4165                while (not {                while (not {
4166                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4167                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4168                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4169                  pop @$open_elements;                  pop @{$self->{open_elements}};
4170                }                }
4171    
4172                pop @$open_elements;                pop @{$self->{open_elements}};
4173                $insertion_mode = 'in table';                $self->{insertion_mode} = 'in table';
4174                !!!next-token;                !!!next-token;
4175                redo B;                redo B;
4176              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4177                ## have an element in table scope                ## have an element in table scope
4178                my $i;                my $i;
4179                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4180                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4181                  if ({                  if ({
4182                       tbody => 1, thead => 1, tfoot => 1,                       tbody => 1, thead => 1, tfoot => 1,
4183                      }->{$node->[1]}) {                      }->{$node->[1]}) {
# Line 3803  sub _construct_tree ($) { Line 4190  sub _construct_tree ($) {
4190                  }                  }
4191                } # INSCOPE                } # INSCOPE
4192                unless (defined $i) {                unless (defined $i) {
4193                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4194                  ## Ignore the token                  ## Ignore the token
4195                  !!!next-token;                  !!!next-token;
4196                  redo B;                  redo B;
# Line 3812  sub _construct_tree ($) { Line 4199  sub _construct_tree ($) {
4199                ## Clear back to table body context                ## Clear back to table body context
4200                while (not {                while (not {
4201                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4202                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4203                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4204                  pop @$open_elements;                  pop @{$self->{open_elements}};
4205                }                }
4206    
4207                ## As if <{current node}>                ## As if <{current node}>
# Line 3824  sub _construct_tree ($) { Line 4211  sub _construct_tree ($) {
4211                ## Clear back to table body context                ## Clear back to table body context
4212                ## nop by definition                ## nop by definition
4213    
4214                pop @$open_elements;                pop @{$self->{open_elements}};
4215                $insertion_mode = 'in table';                $self->{insertion_mode} = 'in table';
4216                ## reprocess                ## reprocess
4217                redo B;                redo B;
4218              } elsif ({              } elsif ({
4219                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4220                        html => 1, td => 1, th => 1, tr => 1,                        html => 1, td => 1, th => 1, tr => 1,
4221                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4222                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4223                ## Ignore the token                ## Ignore the token
4224                !!!next-token;                !!!next-token;
4225                redo B;                redo B;
# Line 3844  sub _construct_tree ($) { Line 4231  sub _construct_tree ($) {
4231            }            }
4232                        
4233            ## As if in table            ## As if in table
4234            !!!parse-error;            !!!parse-error (type => 'in table:'.$token->{tag_name});
4235            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4236            redo B;            redo B;
4237          } elsif ($insertion_mode eq 'in row') {          } elsif ($self->{insertion_mode} eq 'in row') {
4238            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4239              ## NOTE: This is a "character in table" code clone.              ## NOTE: This is a "character in table" code clone.
4240              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4241                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4242                                
4243                unless (length $token->{data}) {                unless (length $token->{data}) {
4244                  !!!next-token;                  !!!next-token;
# Line 3859  sub _construct_tree ($) { Line 4246  sub _construct_tree ($) {
4246                }                }
4247              }              }
4248    
4249                !!!parse-error (type => 'in table:#character');
4250    
4251              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4252              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
4253              ## into the current node" while characters might not be              ## into the current node" while characters might not be
# Line 3868  sub _construct_tree ($) { Line 4257  sub _construct_tree ($) {
4257              if ({              if ({
4258                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4259                   thead => 1, tr => 1,                   thead => 1, tr => 1,
4260                  }->{$open_elements->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4261                # MUST                # MUST
4262                my $foster_parent_element;                my $foster_parent_element;
4263                my $next_sibling;                my $next_sibling;
4264                my $prev_sibling;                my $prev_sibling;
4265                OE: for (reverse 0..$#$open_elements) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4266                  if ($open_elements->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4267                    my $parent = $open_elements->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4268                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4269                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4270                      $next_sibling = $open_elements->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4271                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4272                    } else {                    } else {
4273                      $foster_parent_element = $open_elements->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4274                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4275                    }                    }
4276                    last OE;                    last OE;
4277                  }                  }
4278                } # OE                } # OE
4279                $foster_parent_element = $open_elements->[0]->[0] and                $foster_parent_element = $self->{open_elements}->[0]->[0] and
4280                $prev_sibling = $foster_parent_element->last_child                $prev_sibling = $foster_parent_element->last_child
4281                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4282                if (defined $prev_sibling and                if (defined $prev_sibling and
# Line 3899  sub _construct_tree ($) { Line 4288  sub _construct_tree ($) {
4288                     $next_sibling);                     $next_sibling);
4289                }                }
4290              } else {              } else {
4291                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4292              }              }
4293                            
4294              !!!next-token;              !!!next-token;
# Line 3907  sub _construct_tree ($) { Line 4296  sub _construct_tree ($) {
4296            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
4297              ## Copied from 'in table'              ## Copied from 'in table'
4298              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
4299              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
4300              !!!next-token;              !!!next-token;
4301              redo B;              redo B;
4302            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 3916  sub _construct_tree ($) { Line 4305  sub _construct_tree ($) {
4305                ## Clear back to table row context                ## Clear back to table row context
4306                while (not {                while (not {
4307                  tr => 1, html => 1,                  tr => 1, html => 1,
4308                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4309                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4310                  pop @$open_elements;                  pop @{$self->{open_elements}};
4311                }                }
4312                                
4313                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4314                $insertion_mode = 'in cell';                $self->{insertion_mode} = 'in cell';
4315    
4316                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
4317                                
# Line 3935  sub _construct_tree ($) { Line 4324  sub _construct_tree ($) {
4324                ## As if </tr>                ## As if </tr>
4325                ## have an element in table scope                ## have an element in table scope
4326                my $i;                my $i;
4327                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4328                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4329                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq 'tr') {
4330                    $i = $_;                    $i = $_;
4331                    last INSCOPE;                    last INSCOPE;
# Line 3947  sub _construct_tree ($) { Line 4336  sub _construct_tree ($) {
4336                  }                  }
4337                } # INSCOPE                } # INSCOPE
4338                unless (defined $i) {                unless (defined $i) {
4339                  !!!parse-error;                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4340                  ## Ignore the token                  ## Ignore the token
4341                  !!!next-token;                  !!!next-token;
4342                  redo B;                  redo B;
# Line 3956  sub _construct_tree ($) { Line 4345  sub _construct_tree ($) {
4345                ## Clear back to table row context                ## Clear back to table row context
4346                while (not {                while (not {
4347                  tr => 1, html => 1,                  tr => 1, html => 1,
4348                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4349                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4350                  pop @$open_elements;                  pop @{$self->{open_elements}};
4351                }                }
4352    
4353                pop @$open_elements; # tr                pop @{$self->{open_elements}}; # tr
4354                $insertion_mode = 'in table body';                $self->{insertion_mode} = 'in table body';
4355                ## reprocess                ## reprocess
4356                redo B;                redo B;
4357              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4358                ## NOTE: This is a code clone of "table in table"                ## NOTE: This is a code clone of "table in table"
4359                !!!parse-error;                !!!parse-error (type => 'not closed:table');
4360    
4361                ## As if </table>                ## As if </table>
4362                ## have a table element in table scope                ## have a table element in table scope
4363                my $i;                my $i;
4364                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4365                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4366                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4367                    $i = $_;                    $i = $_;
4368                    last INSCOPE;                    last INSCOPE;
# Line 3984  sub _construct_tree ($) { Line 4373  sub _construct_tree ($) {
4373                  }                  }
4374                } # INSCOPE                } # INSCOPE
4375                unless (defined $i) {                unless (defined $i) {
4376                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:table');
4377                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4378                  !!!next-token;                  !!!next-token;
4379                  redo B;                  redo B;
# Line 3994  sub _construct_tree ($) { Line 4383  sub _construct_tree ($) {
4383                if ({                if ({
4384                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4385                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4386                    }->{$open_elements->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4387                  !!!back-token; # <table>                  !!!back-token; # <table>
4388                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
4389                  !!!back-token;                  !!!back-token;
4390                  $token = {type => 'end tag',                  $token = {type => 'end tag',
4391                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4392                  redo B;                  redo B;
4393                }                }
4394    
4395                if ($open_elements->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4396                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4397                }                }
4398    
4399                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4400    
4401                $reset_insertion_mode->();                              $self->_reset_insertion_mode;
4402    
4403                ## reprocess                ## reprocess
4404                redo B;                redo B;
# Line 4020  sub _construct_tree ($) { Line 4409  sub _construct_tree ($) {
4409              if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'tr') {
4410                ## have an element in table scope                ## have an element in table scope
4411                my $i;                my $i;
4412                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4413                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4414                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4415                    $i = $_;                    $i = $_;
4416                    last INSCOPE;                    last INSCOPE;
# Line 4032  sub _construct_tree ($) { Line 4421  sub _construct_tree ($) {
4421                  }                  }
4422                } # INSCOPE                } # INSCOPE
4423                unless (defined $i) {                unless (defined $i) {
4424                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4425                  ## Ignore the token                  ## Ignore the token
4426                  !!!next-token;                  !!!next-token;
4427                  redo B;                  redo B;
# Line 4041  sub _construct_tree ($) { Line 4430  sub _construct_tree ($) {
4430                ## Clear back to table row context                ## Clear back to table row context
4431                while (not {                while (not {
4432                  tr => 1, html => 1,                  tr => 1, html => 1,
4433                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4434                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4435                  pop @$open_elements;                  pop @{$self->{open_elements}};
4436                }                }
4437    
4438                pop @$open_elements; # tr                pop @{$self->{open_elements}}; # tr
4439                $insertion_mode = 'in table body';                $self->{insertion_mode} = 'in table body';
4440                !!!next-token;                !!!next-token;
4441                redo B;                redo B;
4442              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4443                ## As if </tr>                ## As if </tr>
4444                ## have an element in table scope                ## have an element in table scope
4445                my $i;                my $i;
4446                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4447                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4448                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq 'tr') {
4449                    $i = $_;                    $i = $_;
4450                    last INSCOPE;                    last INSCOPE;
# Line 4066  sub _construct_tree ($) { Line 4455  sub _construct_tree ($) {
4455                  }                  }
4456                } # INSCOPE                } # INSCOPE
4457                unless (defined $i) {                unless (defined $i) {
4458                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4459                  ## Ignore the token                  ## Ignore the token
4460                  !!!next-token;                  !!!next-token;
4461                  redo B;                  redo B;
# Line 4075  sub _construct_tree ($) { Line 4464  sub _construct_tree ($) {
4464                ## Clear back to table row context                ## Clear back to table row context
4465                while (not {                while (not {
4466                  tr => 1, html => 1,                  tr => 1, html => 1,
4467                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4468                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4469                  pop @$open_elements;                  pop @{$self->{open_elements}};
4470                }                }
4471    
4472                pop @$open_elements; # tr                pop @{$self->{open_elements}}; # tr
4473                $insertion_mode = 'in table body';                $self->{insertion_mode} = 'in table body';
4474                ## reprocess                ## reprocess
4475                redo B;                redo B;
4476              } elsif ({              } elsif ({
# Line 4089  sub _construct_tree ($) { Line 4478  sub _construct_tree ($) {
4478                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4479                ## have an element in table scope                ## have an element in table scope
4480                my $i;                my $i;
4481                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4482                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4483                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4484                    $i = $_;                    $i = $_;
4485                    last INSCOPE;                    last INSCOPE;
# Line 4101  sub _construct_tree ($) { Line 4490  sub _construct_tree ($) {
4490                  }                  }
4491                } # INSCOPE                } # INSCOPE
4492                unless (defined $i) {                unless (defined $i) {
4493                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4494                  ## Ignore the token                  ## Ignore the token
4495                  !!!next-token;                  !!!next-token;
4496                  redo B;                  redo B;
# Line 4110  sub _construct_tree ($) { Line 4499  sub _construct_tree ($) {
4499                ## As if </tr>                ## As if </tr>
4500                ## have an element in table scope                ## have an element in table scope
4501                my $i;                my $i;
4502                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4503                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4504                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq 'tr') {
4505                    $i = $_;                    $i = $_;
4506                    last INSCOPE;                    last INSCOPE;
# Line 4122  sub _construct_tree ($) { Line 4511  sub _construct_tree ($) {
4511                  }                  }
4512                } # INSCOPE                } # INSCOPE
4513                unless (defined $i) {                unless (defined $i) {
4514                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:tr');
4515                  ## Ignore the token                  ## Ignore the token
4516                  !!!next-token;                  !!!next-token;
4517                  redo B;                  redo B;
# Line 4131  sub _construct_tree ($) { Line 4520  sub _construct_tree ($) {
4520                ## Clear back to table row context                ## Clear back to table row context
4521                while (not {                while (not {
4522                  tr => 1, html => 1,                  tr => 1, html => 1,
4523                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4524                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4525                  pop @$open_elements;                  pop @{$self->{open_elements}};
4526                }                }
4527    
4528                pop @$open_elements; # tr                pop @{$self->{open_elements}}; # tr
4529                $insertion_mode = 'in table body';                $self->{insertion_mode} = 'in table body';
4530                ## reprocess                ## reprocess
4531                redo B;                redo B;
4532              } elsif ({              } elsif ({
4533                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1,
4534                        colgroup => 1, html => 1, td => 1, th => 1,                        colgroup => 1, html => 1, td => 1, th => 1,
4535                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4536                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4537                ## Ignore the token                ## Ignore the token
4538                !!!next-token;                !!!next-token;
4539                redo B;                redo B;
# Line 4156  sub _construct_tree ($) { Line 4545  sub _construct_tree ($) {
4545            }            }
4546    
4547            ## As if in table            ## As if in table
4548            !!!parse-error;            !!!parse-error (type => 'in table:'.$token->{tag_name});
4549            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4550            redo B;            redo B;
4551          } elsif ($insertion_mode eq 'in cell') {          } elsif ($self->{insertion_mode} eq 'in cell') {
4552            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4553              ## NOTE: This is a code clone of "character in body".              ## NOTE: This is a code clone of "character in body".
4554              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4555                            
4556              $open_elements->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4557    
4558              !!!next-token;              !!!next-token;
4559              redo B;              redo B;
4560            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
4561              ## NOTE: This is a code clone of "comment in body".              ## NOTE: This is a code clone of "comment in body".
4562              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
4563              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
4564              !!!next-token;              !!!next-token;
4565              redo B;              redo B;
4566            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 4182  sub _construct_tree ($) { Line 4571  sub _construct_tree ($) {
4571                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4572                ## have an element in table scope                ## have an element in table scope
4573                my $tn;                my $tn;
4574                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4575                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4576                  if ($node->[1] eq 'td' or $node->[1] eq 'th') {                  if ($node->[1] eq 'td' or $node->[1] eq 'th') {
4577                    $tn = $node->[1];                    $tn = $node->[1];
4578                    last INSCOPE;                    last INSCOPE;
# Line 4194  sub _construct_tree ($) { Line 4583  sub _construct_tree ($) {
4583                  }                  }
4584                } # INSCOPE                } # INSCOPE
4585                unless (defined $tn) {                unless (defined $tn) {
4586                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4587                  ## Ignore the token                  ## Ignore the token
4588                  !!!next-token;                  !!!next-token;
4589                  redo B;                  redo B;
# Line 4211  sub _construct_tree ($) { Line 4600  sub _construct_tree ($) {
4600              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4601                ## have an element in table scope                ## have an element in table scope
4602                my $i;                my $i;
4603                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4604                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4605                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4606                    $i = $_;                    $i = $_;
4607                    last INSCOPE;                    last INSCOPE;
# Line 4223  sub _construct_tree ($) { Line 4612  sub _construct_tree ($) {
4612                  }                  }
4613                } # INSCOPE                } # INSCOPE
4614                unless (defined $i) {                unless (defined $i) {
4615                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4616                  ## Ignore the token                  ## Ignore the token
4617                  !!!next-token;                  !!!next-token;
4618                  redo B;                  redo B;
# Line 4235  sub _construct_tree ($) { Line 4624  sub _construct_tree ($) {
4624                     td => ($token->{tag_name} eq 'th'),                     td => ($token->{tag_name} eq 'th'),
4625                     th => ($token->{tag_name} eq 'td'),                     th => ($token->{tag_name} eq 'td'),
4626                     tr => 1,                     tr => 1,
4627                    }->{$open_elements->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4628                  !!!back-token;                  !!!back-token;
4629                  $token = {type => 'end tag',                  $token = {type => 'end tag',
4630                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4631                  redo B;                  redo B;
4632                }                }
4633    
4634                if ($open_elements->[-1]->[1] ne $token->{tag_name}) {                if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4635                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4636                }                }
4637    
4638                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4639    
4640                $clear_up_to_marker->();                $clear_up_to_marker->();
4641    
4642                $insertion_mode = 'in row';                $self->{insertion_mode} = 'in row';
4643    
4644                !!!next-token;                !!!next-token;
4645                redo B;                redo B;
# Line 4258  sub _construct_tree ($) { Line 4647  sub _construct_tree ($) {
4647                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1,
4648                        colgroup => 1, html => 1,                        colgroup => 1, html => 1,
4649                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4650                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4651                ## Ignore the token                ## Ignore the token
4652                !!!next-token;                !!!next-token;
4653                redo B;                redo B;
# Line 4269  sub _construct_tree ($) { Line 4658  sub _construct_tree ($) {
4658                ## have an element in table scope                ## have an element in table scope
4659                my $i;                my $i;
4660                my $tn;                my $tn;
4661                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4662                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4663                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4664                    $i = $_;                    $i = $_;
4665                    last INSCOPE;                    last INSCOPE;
# Line 4285  sub _construct_tree ($) { Line 4674  sub _construct_tree ($) {
4674                  }                  }
4675                } # INSCOPE                } # INSCOPE
4676                unless (defined $i) {                unless (defined $i) {
4677                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4678                  ## Ignore the token                  ## Ignore the token
4679                  !!!next-token;                  !!!next-token;
4680                  redo B;                  redo B;
# Line 4304  sub _construct_tree ($) { Line 4693  sub _construct_tree ($) {
4693                        
4694            $in_body->($insert_to_current);            $in_body->($insert_to_current);
4695            redo B;            redo B;
4696          } elsif ($insertion_mode eq 'in select') {          } elsif ($self->{insertion_mode} eq 'in select') {
4697            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4698              $open_elements->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4699              !!!next-token;              !!!next-token;
4700              redo B;              redo B;
4701            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
4702              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
4703              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
4704              !!!next-token;              !!!next-token;
4705              redo B;              redo B;
4706            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4707              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4708                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4709                  ## As if </option>                  ## As if </option>
4710                  pop @$open_elements;                  pop @{$self->{open_elements}};
4711                }                }
4712    
4713                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4714                !!!next-token;                !!!next-token;
4715                redo B;                redo B;
4716              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
4717                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4718                  ## As if </option>                  ## As if </option>
4719                  pop @$open_elements;                  pop @{$self->{open_elements}};
4720                }                }
4721    
4722                if ($open_elements->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4723                  ## As if </optgroup>                  ## As if </optgroup>
4724                  pop @$open_elements;                  pop @{$self->{open_elements}};
4725                }                }
4726    
4727                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4728                !!!next-token;                !!!next-token;
4729                redo B;                redo B;
4730              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4731                !!!parse-error;                !!!parse-error (type => 'not closed:select');
4732                ## As if </select> instead                ## As if </select> instead
4733                ## have an element in table scope                ## have an element in table scope
4734                my $i;                my $i;
4735                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4736                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4737                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4738                    $i = $_;                    $i = $_;
4739                    last INSCOPE;                    last INSCOPE;
# Line 4355  sub _construct_tree ($) { Line 4744  sub _construct_tree ($) {
4744                  }                  }
4745                } # INSCOPE                } # INSCOPE
4746                unless (defined $i) {                unless (defined $i) {
4747                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:select');
4748                  ## Ignore the token                  ## Ignore the token
4749                  !!!next-token;                  !!!next-token;
4750                  redo B;                  redo B;
4751                }                }
4752                                
4753                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4754    
4755                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4756    
4757                !!!next-token;                !!!next-token;
4758                redo B;                redo B;
# Line 4372  sub _construct_tree ($) { Line 4761  sub _construct_tree ($) {
4761              }              }
4762            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4763              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4764                if ($open_elements->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4765                    $open_elements->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4766                  ## As if </option>                  ## As if </option>
4767                  splice @$open_elements, -2;                  splice @{$self->{open_elements}}, -2;
4768                } elsif ($open_elements->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4769                  pop @$open_elements;                  pop @{$self->{open_elements}};
4770                } else {                } else {
4771                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4772                  ## Ignore the token                  ## Ignore the token
4773                }                }
4774                !!!next-token;                !!!next-token;
4775                redo B;                redo B;
4776              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
4777                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4778                  pop @$open_elements;                  pop @{$self->{open_elements}};
4779                } else {                } else {
4780                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4781                  ## Ignore the token                  ## Ignore the token
4782                }                }
4783                !!!next-token;                !!!next-token;
# Line 4396  sub _construct_tree ($) { Line 4785  sub _construct_tree ($) {
4785              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4786                ## have an element in table scope                ## have an element in table scope
4787                my $i;                my $i;
4788                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4789                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4790                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4791                    $i = $_;                    $i = $_;
4792                    last INSCOPE;                    last INSCOPE;
# Line 4408  sub _construct_tree ($) { Line 4797  sub _construct_tree ($) {
4797                  }                  }
4798                } # INSCOPE                } # INSCOPE
4799                unless (defined $i) {                unless (defined $i) {
4800                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4801                  ## Ignore the token                  ## Ignore the token
4802                  !!!next-token;                  !!!next-token;
4803                  redo B;                  redo B;
4804                }                }
4805                                
4806                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4807    
4808                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4809    
4810                !!!next-token;                !!!next-token;
4811                redo B;                redo B;
# Line 4424  sub _construct_tree ($) { Line 4813  sub _construct_tree ($) {
4813                        caption => 1, table => 1, tbody => 1,                        caption => 1, table => 1, tbody => 1,
4814                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4815                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4816                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4817                                
4818                ## have an element in table scope                ## have an element in table scope
4819                my $i;                my $i;
4820                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4821                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4822                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4823                    $i = $_;                    $i = $_;
4824                    last INSCOPE;                    last INSCOPE;
# Line 4448  sub _construct_tree ($) { Line 4837  sub _construct_tree ($) {
4837                ## As if </select>                ## As if </select>
4838                ## have an element in table scope                ## have an element in table scope
4839                undef $i;                undef $i;
4840                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4841                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4842                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
4843                    $i = $_;                    $i = $_;
4844                    last INSCOPE;                    last INSCOPE;
# Line 4460  sub _construct_tree ($) { Line 4849  sub _construct_tree ($) {
4849                  }                  }
4850                } # INSCOPE                } # INSCOPE
4851                unless (defined $i) {                unless (defined $i) {
4852                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:select');
4853                  ## Ignore the </select> token                  ## Ignore the </select> token
4854                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
4855                  redo B;                  redo B;
4856                }                }
4857                                
4858                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4859    
4860                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4861    
4862                ## reprocess                ## reprocess
4863                redo B;                redo B;
# Line 4479  sub _construct_tree ($) { Line 4868  sub _construct_tree ($) {
4868              #              #
4869            }            }
4870    
4871            !!!parse-error;            !!!parse-error (type => 'in select:'.$token->{tag_name});
4872            ## Ignore the token            ## Ignore the token
4873            !!!next-token;            !!!next-token;
4874            redo B;            redo B;
4875          } elsif ($insertion_mode eq 'after body') {          } elsif ($self->{insertion_mode} eq 'after body') {
4876            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4877              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4878                ## As if in body                ## As if in body
4879                $reconstruct_active_formatting_elements->($insert_to_current);                $reconstruct_active_formatting_elements->($insert_to_current);
4880                                
4881                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4882    
4883                unless (length $token->{data}) {                unless (length $token->{data}) {
4884                  !!!next-token;                  !!!next-token;
# Line 4498  sub _construct_tree ($) { Line 4887  sub _construct_tree ($) {
4887              }              }
4888                            
4889              #              #
4890                !!!parse-error (type => 'after body:#'.$token->{type});
4891            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
4892              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
4893              $open_elements->[0]->[0]->append_child ($comment);              $self->{open_elements}->[0]->[0]->append_child ($comment);
4894              !!!next-token;              !!!next-token;
4895              redo B;              redo B;
4896              } elsif ($token->{type} eq 'start tag') {
4897                !!!parse-error (type => 'after body:'.$token->{tag_name});
4898                #
4899            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4900              if ($token->{tag_name} eq 'html') {              if ($token->{tag_name} eq 'html') {
4901                ## TODO: if inner_html, parse-error, ignore the token; otherwise,                if (defined $self->{inner_html_node}) {
4902                    !!!parse-error (type => 'unmatched end tag:html');
4903                $phase = 'trailing end';                  ## Ignore the token
4904                !!!next-token;                  !!!next-token;
4905                redo B;                  redo B;
4906                  } else {
4907                    $phase = 'trailing end';
4908                    !!!next-token;
4909                    redo B;
4910                  }
4911              } else {              } else {
4912                #                !!!parse-error (type => 'after body:/'.$token->{tag_name});
4913              }              }
4914            } else {            } else {
4915              #              !!!parse-error (type => 'after body:#'.$token->{type});
4916            }            }
4917    
4918            !!!parse-error ('data after body');            $self->{insertion_mode} = 'in body';
           $insertion_mode = 'in body';  
4919            ## reprocess            ## reprocess
4920            redo B;            redo B;
4921          } elsif ($insertion_mode eq 'in frameset') {          } elsif ($self->{insertion_mode} eq 'in frameset') {
4922            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4923              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4924                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4925    
4926                unless (length $token->{data}) {                unless (length $token->{data}) {
4927                  !!!next-token;                  !!!next-token;
# Line 4535  sub _construct_tree ($) { Line 4932  sub _construct_tree ($) {
4932              #              #
4933            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
4934              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
4935              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
4936              !!!next-token;              !!!next-token;
4937              redo B;              redo B;
4938            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 4545  sub _construct_tree ($) { Line 4942  sub _construct_tree ($) {
4942                redo B;                redo B;
4943              } elsif ($token->{tag_name} eq 'frame') {              } elsif ($token->{tag_name} eq 'frame') {
4944                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4945                pop @$open_elements;                pop @{$self->{open_elements}};
4946                !!!next-token;                !!!next-token;
4947                redo B;                redo B;
4948              } elsif ($token->{tag_name} eq 'noframes') {              } elsif ($token->{tag_name} eq 'noframes') {
# Line 4556  sub _construct_tree ($) { Line 4953  sub _construct_tree ($) {
4953              }              }
4954            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4955              if ($token->{tag_name} eq 'frameset') {              if ($token->{tag_name} eq 'frameset') {
4956                if ($open_elements->[-1]->[1] eq 'html' and                if ($self->{open_elements}->[-1]->[1] eq 'html' and
4957                    @$open_elements == 1) {                    @{$self->{open_elements}} == 1) {
4958                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4959                  ## Ignore the token                  ## Ignore the token
4960                  !!!next-token;                  !!!next-token;
4961                } else {                } else {
4962                  pop @$open_elements;                  pop @{$self->{open_elements}};
4963                  !!!next-token;                  !!!next-token;
4964                }                }
4965                                
4966                ## if not inner_html and                ## if not inner_html and
4967                if ($open_elements->[-1]->[1] ne 'frameset') {                if ($self->{open_elements}->[-1]->[1] ne 'frameset') {
4968                  $insertion_mode = 'after frameset';                  $self->{insertion_mode} = 'after frameset';
4969                }                }
4970                redo B;                redo B;
4971              } else {              } else {
# Line 4578  sub _construct_tree ($) { Line 4975  sub _construct_tree ($) {
4975              #              #
4976            }            }
4977                        
4978            !!!parse-error;            if (defined $token->{tag_name}) {
4979                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4980              } else {
4981                !!!parse-error (type => 'in frameset:#'.$token->{type});
4982              }
4983            ## Ignore the token            ## Ignore the token
4984            !!!next-token;            !!!next-token;
4985            redo B;            redo B;
4986          } elsif ($insertion_mode eq 'after frameset') {          } elsif ($self->{insertion_mode} eq 'after frameset') {
4987            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4988              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4989                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4990    
4991                unless (length $token->{data}) {                unless (length $token->{data}) {
4992                  !!!next-token;                  !!!next-token;
# Line 4596  sub _construct_tree ($) { Line 4997  sub _construct_tree ($) {
4997              #              #
4998            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
4999              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
5000              $open_elements->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
5001              !!!next-token;              !!!next-token;
5002              redo B;              redo B;
5003            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 4618  sub _construct_tree ($) { Line 5019  sub _construct_tree ($) {
5019              #              #
5020            }            }
5021                        
5022            !!!parse-error;            if (defined $token->{tag_name}) {
5023                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5024              } else {
5025                !!!parse-error (type => 'after frameset:#'.$token->{type});
5026              }
5027            ## Ignore the token            ## Ignore the token
5028            !!!next-token;            !!!next-token;
5029            redo B;            redo B;
5030    
5031            ## ISSUE: An issue in spec there            ## ISSUE: An issue in spec there
5032          } else {          } else {
5033            die "$0: $insertion_mode: Unknown insertion mode";            die "$0: $self->{insertion_mode}: Unknown insertion mode";
5034          }          }
5035        }        }
5036      } elsif ($phase eq 'trailing end') {      } elsif ($phase eq 'trailing end') {
5037        ## states in the main stage is preserved yet # MUST        ## states in the main stage is preserved yet # MUST
5038                
5039        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} eq 'DOCTYPE') {
5040          !!!parse-error;          !!!parse-error (type => 'after html:#DOCTYPE');
5041          ## Ignore the token          ## Ignore the token
5042          !!!next-token;          !!!next-token;
5043          redo B;          redo B;
# Line 4651  sub _construct_tree ($) { Line 5056  sub _construct_tree ($) {
5056            $reconstruct_active_formatting_elements->($insert_to_current)            $reconstruct_active_formatting_elements->($insert_to_current)
5057              if $phase eq 'main';              if $phase eq 'main';
5058                        
5059            $open_elements->[-1]->[0]->manakai_append_text ($data);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);
5060                        
5061            unless (length $token->{data}) {            unless (length $token->{data}) {
5062              !!!next-token;              !!!next-token;
# Line 4659  sub _construct_tree ($) { Line 5064  sub _construct_tree ($) {
5064            }            }
5065          }          }
5066    
5067          !!!parse-error;          !!!parse-error (type => 'after html:#character');
5068          $phase = 'main';          $phase = 'main';
5069          ## reprocess          ## reprocess
5070          redo B;          redo B;
5071        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{type} eq 'start tag' or
5072                 $token->{type} eq 'end tag') {                 $token->{type} eq 'end tag') {
5073          !!!parse-error;          !!!parse-error (type => 'after html:'.$token->{tag_name});
5074          $phase = 'main';          $phase = 'main';
5075          ## reprocess          ## reprocess
5076          redo B;          redo B;
# Line 4681  sub _construct_tree ($) { Line 5086  sub _construct_tree ($) {
5086    ## Stop parsing # MUST    ## Stop parsing # MUST
5087        
5088    ## TODO: script stuffs    ## TODO: script stuffs
5089  } # _construct_tree  } # _tree_construct_main
5090    
5091    sub set_inner_html ($$$) {
5092      my $class = shift;
5093      my $node = shift;
5094      my $s = \$_[0];
5095      my $onerror = $_[1];
5096    
5097      my $nt = $node->node_type;
5098      if ($nt == 9) {
5099        # MUST
5100        
5101        ## Step 1 # MUST
5102        ## TODO: If the document has an active parser, ...
5103        ## ISSUE: There is an issue in the spec.
5104        
5105        ## Step 2 # MUST
5106        my @cn = @{$node->child_nodes};
5107        for (@cn) {
5108          $node->remove_child ($_);
5109        }
5110    
5111        ## Step 3, 4, 5 # MUST
5112        $class->parse_string ($$s => $node, $onerror);
5113      } elsif ($nt == 1) {
5114        ## TODO: If non-html element
5115    
5116        ## NOTE: Most of this code is copied from |parse_string|
5117    
5118        ## Step 1 # MUST
5119        my $this_doc = $node->owner_document;
5120        my $doc = $this_doc->implementation->create_document;
5121        $doc->manakai_is_html (1);
5122        my $p = $class->new;
5123        $p->{document} = $doc;
5124    
5125        ## Step 9 # MUST
5126        my $i = 0;
5127        my $line = 1;
5128        my $column = 0;
5129        $p->{set_next_input_character} = sub {
5130          my $self = shift;
5131    
5132          pop @{$self->{prev_input_character}};
5133          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5134    
5135          $self->{next_input_character} = -1 and return if $i >= length $$s;
5136          $self->{next_input_character} = ord substr $$s, $i++, 1;
5137          $column++;
5138    
5139          if ($self->{next_input_character} == 0x000A) { # LF
5140            $line++;
5141            $column = 0;
5142          } elsif ($self->{next_input_character} == 0x000D) { # CR
5143            $i++ if substr ($$s, $i, 1) eq "\x0A";
5144            $self->{next_input_character} = 0x000A; # LF # MUST
5145            $line++;
5146            $column = 0;
5147          } elsif ($self->{next_input_character} > 0x10FFFF) {
5148            $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5149          } elsif ($self->{next_input_character} == 0x0000) { # NULL
5150            !!!parse-error (type => 'NULL');
5151            $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5152          }
5153        };
5154        $p->{prev_input_character} = [-1, -1, -1];
5155        $p->{next_input_character} = -1;
5156        
5157        my $ponerror = $onerror || sub {
5158          my (%opt) = @_;
5159          warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";
5160        };
5161        $p->{parse_error} = sub {
5162          $ponerror->(@_, line => $line, column => $column);
5163        };
5164        
5165        $p->_initialize_tokenizer;
5166        $p->_initialize_tree_constructor;
5167    
5168        ## Step 2
5169        my $node_ln = $node->local_name;
5170        $p->{content_model_flag} = {
5171          title => 'RCDATA',
5172          textarea => 'RCDATA',
5173          style => 'CDATA',
5174          script => 'CDATA',
5175          xmp => 'CDATA',
5176          iframe => 'CDATA',
5177          noembed => 'CDATA',
5178          noframes => 'CDATA',
5179          noscript => 'CDATA',
5180          plaintext => 'PLAINTEXT',
5181        }->{$node_ln} || 'PCDATA';
5182           ## ISSUE: What is "the name of the element"? local name?
5183    
5184        $p->{inner_html_node} = [$node, $node_ln];
5185    
5186        ## Step 4
5187        my $root = $doc->create_element_ns
5188          ('http://www.w3.org/1999/xhtml', [undef, 'html']);
5189    
5190        ## Step 5 # MUST
5191        $doc->append_child ($root);
5192    
5193        ## Step 6 # MUST
5194        push @{$p->{open_elements}}, [$root, 'html'];
5195    
5196        undef $p->{head_element};
5197    
5198        ## Step 7 # MUST
5199        $p->_reset_insertion_mode;
5200    
5201        ## Step 8 # MUST
5202        my $anode = $node;
5203        AN: while (defined $anode) {
5204          if ($anode->node_type == 1) {
5205            my $nsuri = $anode->namespace_uri;
5206            if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5207              if ($anode->local_name eq 'form') { ## TODO: case?
5208                $p->{form_element} = $anode;
5209                last AN;
5210              }
5211            }
5212          }
5213          $anode = $anode->parent_node;
5214        } # AN
5215        
5216        ## Step 3 # MUST
5217        ## Step 10 # MUST
5218        {
5219          my $self = $p;
5220          !!!next-token;
5221        }
5222        $p->_tree_construction_main;
5223    
5224        ## Step 11 # MUST
5225        my @cn = @{$node->child_nodes};
5226        for (@cn) {
5227          $node->remove_child ($_);
5228        }
5229        ## ISSUE: mutation events? read-only?
5230    
5231        ## Step 12 # MUST
5232        @cn = @{$root->child_nodes};
5233        for (@cn) {
5234          $this_doc->adopt_node ($_);
5235          $node->append_child ($_);
5236        }
5237        ## ISSUE: mutation events?
5238    
5239        $p->_terminate_tree_constructor;
5240      } else {
5241        die "$0: |set_inner_html| is not defined for node of type $nt";
5242      }
5243    } # set_inner_html
5244    
5245    } # tree construction stage
5246    
5247  sub get_inner_html ($$$) {  sub get_inner_html ($$$) {
5248    my ($class, $node, $on_error) = @_;    my (undef, $node, $on_error) = @_;
5249    
5250    ## Step 1    ## Step 1
5251    my $s = '';    my $s = '';
# Line 4744  sub get_inner_html ($$$) { Line 5305  sub get_inner_html ($$$) {
5305          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5306        }->{$tag_name};        }->{$tag_name};
5307    
5308          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5309    
5310        if (not $in_cdata and {        if (not $in_cdata and {
5311          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5312          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24