/[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.1 by wakaba, Tue May 1 10:36:06 2007 UTC revision 1.42 by wakaba, Sat Jul 21 06:59:16 2007 UTC
# Line 1  Line 1 
1  package What::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    
5  ## This is an early version of an HTML parser.  ## ISSUE:
6    ## var doc = implementation.createDocument (null, null, null);
7    ## doc.write ('');
8    ## alert (doc.compatMode);
9    
10    ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT
11    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
12    ## is not yet clear.
13    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
14    ## "{U+FEFF}..." in GB18030?
15    
16  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
17    base => 1,    base => 1,
# Line 18  my $permitted_slash_tag_name = { Line 27  my $permitted_slash_tag_name = {
27    input => 1,    input => 1,
28  };  };
29    
30  my $entity_char = {  my $c1_entity_char = {
31    AElig => "\x{00C6}",    0x80 => 0x20AC,
32    Aacute => "\x{00C1}",    0x81 => 0xFFFD,
33    Acirc => "\x{00C2}",    0x82 => 0x201A,
34    Agrave => "\x{00C0}",    0x83 => 0x0192,
35    Alpha => "\x{0391}",    0x84 => 0x201E,
36    Aring => "\x{00C5}",    0x85 => 0x2026,
37    Atilde => "\x{00C3}",    0x86 => 0x2020,
38    Auml => "\x{00C4}",    0x87 => 0x2021,
39    Beta => "\x{0392}",    0x88 => 0x02C6,
40    Ccedil => "\x{00C7}",    0x89 => 0x2030,
41    Chi => "\x{03A7}",    0x8A => 0x0160,
42    Dagger => "\x{2021}",    0x8B => 0x2039,
43    Delta => "\x{0394}",    0x8C => 0x0152,
44    ETH => "\x{00D0}",    0x8D => 0xFFFD,
45    Eacute => "\x{00C9}",    0x8E => 0x017D,
46    Ecirc => "\x{00CA}",    0x8F => 0xFFFD,
47    Egrave => "\x{00C8}",    0x90 => 0xFFFD,
48    Epsilon => "\x{0395}",    0x91 => 0x2018,
49    Eta => "\x{0397}",    0x92 => 0x2019,
50    Euml => "\x{00CB}",    0x93 => 0x201C,
51    Gamma => "\x{0393}",    0x94 => 0x201D,
52    Iacute => "\x{00CD}",    0x95 => 0x2022,
53    Icirc => "\x{00CE}",    0x96 => 0x2013,
54    Igrave => "\x{00CC}",    0x97 => 0x2014,
55    Iota => "\x{0399}",    0x98 => 0x02DC,
56    Iuml => "\x{00CF}",    0x99 => 0x2122,
57    Kappa => "\x{039A}",    0x9A => 0x0161,
58    Lambda => "\x{039B}",    0x9B => 0x203A,
59    Mu => "\x{039C}",    0x9C => 0x0153,
60    Ntilde => "\x{00D1}",    0x9D => 0xFFFD,
61    Nu => "\x{039D}",    0x9E => 0x017E,
62    OElig => "\x{0152}",    0x9F => 0x0178,
63    Oacute => "\x{00D3}",  }; # $c1_entity_char
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 };  
64    
65  my $special_category = {  my $special_category = {
66    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,
# Line 307  sub parse_string ($$$;$) { Line 89  sub parse_string ($$$;$) {
89    my $s = \$_[0];    my $s = \$_[0];
90    $self->{document} = $_[1];    $self->{document} = $_[1];
91    
92    my $i;    ## NOTE: |set_inner_html| copies most of this method's code
93    
94    my $i = 0;    my $i = 0;
95      my $line = 1;
96      my $column = 0;
97    $self->{set_next_input_character} = sub {    $self->{set_next_input_character} = sub {
98      my $self = shift;      my $self = shift;
99    
100        pop @{$self->{prev_input_character}};
101        unshift @{$self->{prev_input_character}}, $self->{next_input_character};
102    
103      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_input_character} = -1 and return if $i >= length $$s;
104      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_input_character} = ord substr $$s, $i++, 1;
105        $column++;
106            
107      if ($self->{next_input_character} == 0x000D) { # CR      if ($self->{next_input_character} == 0x000A) { # LF
108        if ($i >= length $$s) {        $line++;
109          #        $column = 0;
110        } else {      } elsif ($self->{next_input_character} == 0x000D) { # CR
111          my $next_char = ord substr $$s, $i++, 1;        $i++ if substr ($$s, $i, 1) eq "\x0A";
         if ($next_char == 0x000A) { # LF  
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
       }  
112        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_input_character} = 0x000A; # LF # MUST
113          $line++;
114          $column = 0;
115      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_input_character} > 0x10FFFF) {
116        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
117      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_input_character} == 0x0000) { # NULL
118          !!!parse-error (type => 'NULL');
119        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
120      }      }
121    };    };
122      $self->{prev_input_character} = [-1, -1, -1];
123      $self->{next_input_character} = -1;
124    
125    $self->{parse_error} = $_[2] || sub {    my $onerror = $_[2] || sub {
126      warn "Parse error at character $i\n"; ## TODO: Report (line, column) pair      my (%opt) = @_;
127        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";
128      };
129      $self->{parse_error} = sub {
130        $onerror->(@_, line => $line, column => $column);
131    };    };
132    
133    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 357  sub new ($) { Line 150  sub new ($) {
150    return $self;    return $self;
151  } # new  } # new
152    
153    sub CM_ENTITY () { 0b001 } # & markup in data
154    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
155    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
156    
157    sub PLAINTEXT_CONTENT_MODEL () { 0 }
158    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
159    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
163    
164  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
165    my $self = shift;    my $self = shift;
166    $self->{state} = 'data'; # MUST    $self->{state} = 'data'; # MUST
167    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
168    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
169    undef $self->{current_attribute};    undef $self->{current_attribute};
170    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 371  sub _initialize_tokenizer ($) { Line 173  sub _initialize_tokenizer ($) {
173    # $self->{next_input_character}    # $self->{next_input_character}
174    !!!next-input-character;    !!!next-input-character;
175    $self->{token} = [];    $self->{token} = [];
176      # $self->{escape}
177  } # _initialize_tokenizer  } # _initialize_tokenizer
178    
179  ## A token has:  ## A token has:
180  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',
181  ##       'character', or 'end-of-file'  ##       'character', or 'end-of-file'
182  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))
183      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{public_identifier} (DOCTYPE)
184  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE)
185    ##   ->{correct} == 1 or 0 (DOCTYPE)
186  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{attributes} isa HASH (start tag, end tag)
187  ##   ->{data} (comment, character)  ##   ->{data} (comment, character)
188    
 ## Macros  
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
   
189  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
190    
191  ## Before each step, UA MAY check to see if either one of the scripts in  ## Before each step, UA MAY check to see if either one of the scripts in
# Line 404  sub _get_next_token ($) { Line 203  sub _get_next_token ($) {
203    A: {    A: {
204      if ($self->{state} eq 'data') {      if ($self->{state} eq 'data') {
205        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
206          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
             $self->{content_model_flag} eq 'RCDATA') {  
207            $self->{state} = 'entity data';            $self->{state} = 'entity data';
208            !!!next-input-character;            !!!next-input-character;
209            redo A;            redo A;
210          } else {          } else {
211            #            #
212          }          }
213          } elsif ($self->{next_input_character} == 0x002D) { # -
214            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
215              unless ($self->{escape}) {
216                if ($self->{prev_input_character}->[0] == 0x002D and # -
217                    $self->{prev_input_character}->[1] == 0x0021 and # !
218                    $self->{prev_input_character}->[2] == 0x003C) { # <
219                  $self->{escape} = 1;
220                }
221              }
222            }
223            
224            #
225        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
226          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
227                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
228                 not $self->{escape})) {
229            $self->{state} = 'tag open';            $self->{state} = 'tag open';
230            !!!next-input-character;            !!!next-input-character;
231            redo A;            redo A;
232          } else {          } else {
233            #            #
234          }          }
235          } elsif ($self->{next_input_character} == 0x003E) { # >
236            if ($self->{escape} and
237                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
238              if ($self->{prev_input_character}->[0] == 0x002D and # -
239                  $self->{prev_input_character}->[1] == 0x002D) { # -
240                delete $self->{escape};
241              }
242            }
243            
244            #
245        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
246          !!!emit ({type => 'end-of-file'});          !!!emit ({type => 'end-of-file'});
247          last A; ## TODO: ok?          last A; ## TODO: ok?
# Line 436  sub _get_next_token ($) { Line 258  sub _get_next_token ($) {
258      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
259        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
260                
261        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
262    
263        $self->{state} = 'data';        $self->{state} = 'data';
264        # next-input-character is already done        # next-input-character is already done
# Line 449  sub _get_next_token ($) { Line 271  sub _get_next_token ($) {
271    
272        redo A;        redo A;
273      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} eq 'tag open') {
274        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
275          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
276            !!!next-input-character;            !!!next-input-character;
277            $self->{state} = 'close tag open';            $self->{state} = 'close tag open';
# Line 463  sub _get_next_token ($) { Line 284  sub _get_next_token ($) {
284    
285            redo A;            redo A;
286          }          }
287        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
288          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
289            $self->{state} = 'markup declaration open';            $self->{state} = 'markup declaration open';
290            !!!next-input-character;            !!!next-input-character;
# Line 488  sub _get_next_token ($) { Line 309  sub _get_next_token ($) {
309            !!!next-input-character;            !!!next-input-character;
310            redo A;            redo A;
311          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
312            !!!parse-error;            !!!parse-error (type => 'empty start tag');
313            $self->{state} = 'data';            $self->{state} = 'data';
314            !!!next-input-character;            !!!next-input-character;
315    
# Line 496  sub _get_next_token ($) { Line 317  sub _get_next_token ($) {
317    
318            redo A;            redo A;
319          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
320            !!!parse-error;            !!!parse-error (type => 'pio');
321            $self->{state} = 'bogus comment';            $self->{state} = 'bogus comment';
322            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
323            redo A;            redo A;
324          } else {          } else {
325            !!!parse-error;            !!!parse-error (type => 'bare stago');
326            $self->{state} = 'data';            $self->{state} = 'data';
327            ## reconsume            ## reconsume
328    
# Line 510  sub _get_next_token ($) { Line 331  sub _get_next_token ($) {
331            redo A;            redo A;
332          }          }
333        } else {        } else {
334          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
335        }        }
336      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
337        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
338            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
339          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
340          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
341              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
342                push @next_char, $self->{next_input_character};
343                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
344                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
345                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
346                  !!!next-input-character;
347                  next TAGNAME;
348                } else {
349                  $self->{next_input_character} = shift @next_char; # reconsume
350                  !!!back-next-input-character (@next_char);
351                  $self->{state} = 'data';
352    
353                  !!!emit ({type => 'character', data => '</'});
354      
355                  redo A;
356                }
357              }
358            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
359            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
360            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
361            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
362              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
363              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
364            } else {                    $self->{next_input_character} == 0x0020 or # SP
365              !!!parse-error;                    $self->{next_input_character} == 0x003E or # >
366                      $self->{next_input_character} == 0x002F or # /
367                      $self->{next_input_character} == -1) {
368              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
369              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
370              $self->{state} = 'data';              $self->{state} = 'data';
   
371              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => 'character', data => '</'});
   
372              redo A;              redo A;
373              } else {
374                $self->{next_input_character} = shift @next_char;
375                !!!back-next-input-character (@next_char);
376                # and consume...
377            }            }
378          }          } else {
379          push @next_char, $self->{next_input_character};            ## No start tag token has ever been emitted
380                  # next-input-character is already done
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error;  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
381            $self->{state} = 'data';            $self->{state} = 'data';
   
382            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => 'character', data => '</'});
   
383            redo A;            redo A;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
384          }          }
385        }        }
386                
# Line 575  sub _get_next_token ($) { Line 399  sub _get_next_token ($) {
399          !!!next-input-character;          !!!next-input-character;
400          redo A;          redo A;
401        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
402          !!!parse-error;          !!!parse-error (type => 'empty end tag');
403          $self->{state} = 'data';          $self->{state} = 'data';
404          !!!next-input-character;          !!!next-input-character;
405          redo A;          redo A;
406        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
407          !!!parse-error;          !!!parse-error (type => 'bare etago');
408          $self->{state} = 'data';          $self->{state} = 'data';
409          # reconsume          # reconsume
410    
# Line 588  sub _get_next_token ($) { Line 412  sub _get_next_token ($) {
412    
413          redo A;          redo A;
414        } else {        } else {
415          !!!parse-error;          !!!parse-error (type => 'bogus end tag');
416          $self->{state} = 'bogus comment';          $self->{state} = 'bogus comment';
417          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
418          redo A;          redo A;
# Line 604  sub _get_next_token ($) { Line 428  sub _get_next_token ($) {
428          redo A;          redo A;
429        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
430          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
431              $self->{current_token}->{first_start_tag}
432                  = not defined $self->{last_emitted_start_tag_name};
433            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
434          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
435            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
436            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
437              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
438            }            }
439          } else {          } else {
440            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 443  sub _get_next_token ($) {
443          !!!next-input-character;          !!!next-input-character;
444    
445          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
446    
447          redo A;          redo A;
448        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 627  sub _get_next_token ($) { Line 452  sub _get_next_token ($) {
452          ## Stay in this state          ## Stay in this state
453          !!!next-input-character;          !!!next-input-character;
454          redo A;          redo A;
455        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
456                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
457          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
458              $self->{current_token}->{first_start_tag}
459                  = not defined $self->{last_emitted_start_tag_name};
460            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
461          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
462            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
463            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
464              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
465            }            }
466          } else {          } else {
467            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 470  sub _get_next_token ($) {
470          # reconsume          # reconsume
471    
472          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
473    
474          redo A;          redo A;
475        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
# Line 655  sub _get_next_token ($) { Line 480  sub _get_next_token ($) {
480            # permitted slash            # permitted slash
481            #            #
482          } else {          } else {
483            !!!parse-error;            !!!parse-error (type => 'nestc');
484          }          }
485          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
486          # next-input-character is already done          # next-input-character is already done
# Line 678  sub _get_next_token ($) { Line 503  sub _get_next_token ($) {
503          redo A;          redo A;
504        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
505          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
506              $self->{current_token}->{first_start_tag}
507                  = not defined $self->{last_emitted_start_tag_name};
508            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
509          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
510            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
511            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
512              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
513            }            }
514          } else {          } else {
515            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 518  sub _get_next_token ($) {
518          !!!next-input-character;          !!!next-input-character;
519    
520          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
521    
522          redo A;          redo A;
523        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 709  sub _get_next_token ($) { Line 535  sub _get_next_token ($) {
535            # permitted slash            # permitted slash
536            #            #
537          } else {          } else {
538            !!!parse-error;            !!!parse-error (type => 'nestc');
539          }          }
540          ## Stay in the state          ## Stay in the state
541          # next-input-character is already done          # next-input-character is already done
542          redo A;          redo A;
543        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
544                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
545          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
546              $self->{current_token}->{first_start_tag}
547                  = not defined $self->{last_emitted_start_tag_name};
548            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
549          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
550            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
551            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
552              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
553            }            }
554          } else {          } else {
555            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 558  sub _get_next_token ($) {
558          # reconsume          # reconsume
559    
560          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
561    
562          redo A;          redo A;
563        } else {        } else {
# Line 745  sub _get_next_token ($) { Line 571  sub _get_next_token ($) {
571        my $before_leave = sub {        my $before_leave = sub {
572          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
573              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
574            !!!parse-error;            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
575            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
576          } else {          } else {
577            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 770  sub _get_next_token ($) { Line 596  sub _get_next_token ($) {
596        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
597          $before_leave->();          $before_leave->();
598          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
599              $self->{current_token}->{first_start_tag}
600                  = not defined $self->{last_emitted_start_tag_name};
601            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
602          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
603            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
604            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
605              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
606            }            }
607          } else {          } else {
608            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 611  sub _get_next_token ($) {
611          !!!next-input-character;          !!!next-input-character;
612    
613          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
614    
615          redo A;          redo A;
616        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 801  sub _get_next_token ($) { Line 628  sub _get_next_token ($) {
628            # permitted slash            # permitted slash
629            #            #
630          } else {          } else {
631            !!!parse-error;            !!!parse-error (type => 'nestc');
632          }          }
633          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
634          # next-input-character is already done          # next-input-character is already done
635          redo A;          redo A;
636        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
637                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
638          $before_leave->();          $before_leave->();
639          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
640              $self->{current_token}->{first_start_tag}
641                  = not defined $self->{last_emitted_start_tag_name};
642            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
643          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
644            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
645            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
646              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
647            }            }
648          } else {          } else {
649            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 652  sub _get_next_token ($) {
652          # reconsume          # reconsume
653    
654          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
655    
656          redo A;          redo A;
657        } else {        } else {
# Line 848  sub _get_next_token ($) { Line 675  sub _get_next_token ($) {
675          redo A;          redo A;
676        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
677          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
678              $self->{current_token}->{first_start_tag}
679                  = not defined $self->{last_emitted_start_tag_name};
680            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
681          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
682            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
683            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
684              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
685            }            }
686          } else {          } else {
687            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 690  sub _get_next_token ($) {
690          !!!next-input-character;          !!!next-input-character;
691    
692          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
693    
694          redo A;          redo A;
695        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 879  sub _get_next_token ($) { Line 707  sub _get_next_token ($) {
707            # permitted slash            # permitted slash
708            #            #
709          } else {          } else {
710            !!!parse-error;            !!!parse-error (type => 'nestc');
711              ## TODO: Different error type for <aa / bb> than <aa/>
712          }          }
713          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
714          # next-input-character is already done          # next-input-character is already done
715          redo A;          redo A;
716        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
717                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
718          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
719              $self->{current_token}->{first_start_tag}
720                  = not defined $self->{last_emitted_start_tag_name};
721            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
722          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
723            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
724            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
725              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
726            }            }
727          } else {          } else {
728            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 731  sub _get_next_token ($) {
731          # reconsume          # reconsume
732    
733          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
734    
735          redo A;          redo A;
736        } else {        } else {
# Line 934  sub _get_next_token ($) { Line 763  sub _get_next_token ($) {
763          redo A;          redo A;
764        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
765          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
766              $self->{current_token}->{first_start_tag}
767                  = not defined $self->{last_emitted_start_tag_name};
768            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
769          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
770            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
771            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
772              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
773            }            }
774          } else {          } else {
775            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 778  sub _get_next_token ($) {
778          !!!next-input-character;          !!!next-input-character;
779    
780          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
781    
782          redo A;          redo A;
783        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
784                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
785          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
786              $self->{current_token}->{first_start_tag}
787                  = not defined $self->{last_emitted_start_tag_name};
788            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
789          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
790            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
791            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
792              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
793            }            }
794          } else {          } else {
795            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 798  sub _get_next_token ($) {
798          ## reconsume          ## reconsume
799    
800          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
801    
802          redo A;          redo A;
803        } else {        } else {
# Line 987  sub _get_next_token ($) { Line 817  sub _get_next_token ($) {
817          !!!next-input-character;          !!!next-input-character;
818          redo A;          redo A;
819        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
820          !!!parse-error;          !!!parse-error (type => 'unclosed attribute value');
821          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
822              $self->{current_token}->{first_start_tag}
823                  = not defined $self->{last_emitted_start_tag_name};
824            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
825          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
826            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
827            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
828              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
829            }            }
830          } else {          } else {
831            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 834  sub _get_next_token ($) {
834          ## reconsume          ## reconsume
835    
836          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
837    
838          redo A;          redo A;
839        } else {        } else {
# Line 1022  sub _get_next_token ($) { Line 853  sub _get_next_token ($) {
853          !!!next-input-character;          !!!next-input-character;
854          redo A;          redo A;
855        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
856          !!!parse-error;          !!!parse-error (type => 'unclosed attribute value');
857          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
858              $self->{current_token}->{first_start_tag}
859                  = not defined $self->{last_emitted_start_tag_name};
860            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
861          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
862            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
863            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
864              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
865            }            }
866          } else {          } else {
867            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 870  sub _get_next_token ($) {
870          ## reconsume          ## reconsume
871    
872          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
873    
874          redo A;          redo A;
875        } else {        } else {
# Line 1062  sub _get_next_token ($) { Line 894  sub _get_next_token ($) {
894          redo A;          redo A;
895        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
896          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
897              $self->{current_token}->{first_start_tag}
898                  = not defined $self->{last_emitted_start_tag_name};
899            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
900          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
901            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
902            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
903              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
904            }            }
905          } else {          } else {
906            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 909  sub _get_next_token ($) {
909          !!!next-input-character;          !!!next-input-character;
910    
911          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
912    
913          redo A;          redo A;
914        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
915                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
916          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
917              $self->{current_token}->{first_start_tag}
918                  = not defined $self->{last_emitted_start_tag_name};
919            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
920          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
921            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
922            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
923              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
924            }            }
925          } else {          } else {
926            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 929  sub _get_next_token ($) {
929          ## reconsume          ## reconsume
930    
931          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
932    
933          redo A;          redo A;
934        } else {        } else {
# Line 1105  sub _get_next_token ($) { Line 938  sub _get_next_token ($) {
938          redo A;          redo A;
939        }        }
940      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
941        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
942    
943        unless (defined $token) {        unless (defined $token) {
944          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 1154  sub _get_next_token ($) { Line 987  sub _get_next_token ($) {
987          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
988          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
989            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => 'comment', data => ''};
990            $self->{state} = 'comment';            $self->{state} = 'comment start';
991            !!!next-input-character;            !!!next-input-character;
992            redo A;            redo A;
993          }          }
# Line 1196  sub _get_next_token ($) { Line 1029  sub _get_next_token ($) {
1029          }          }
1030        }        }
1031    
1032        !!!parse-error;        !!!parse-error (type => 'bogus comment');
1033        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1034        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1035        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1204  sub _get_next_token ($) { Line 1037  sub _get_next_token ($) {
1037                
1038        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1039        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1040        } elsif ($self->{state} eq 'comment start') {
1041          if ($self->{next_input_character} == 0x002D) { # -
1042            $self->{state} = 'comment start dash';
1043            !!!next-input-character;
1044            redo A;
1045          } elsif ($self->{next_input_character} == 0x003E) { # >
1046            !!!parse-error (type => 'bogus comment');
1047            $self->{state} = 'data';
1048            !!!next-input-character;
1049    
1050            !!!emit ($self->{current_token}); # comment
1051    
1052            redo A;
1053          } elsif ($self->{next_input_character} == -1) {
1054            !!!parse-error (type => 'unclosed comment');
1055            $self->{state} = 'data';
1056            ## reconsume
1057    
1058            !!!emit ($self->{current_token}); # comment
1059    
1060            redo A;
1061          } else {
1062            $self->{current_token}->{data} # comment
1063                .= chr ($self->{next_input_character});
1064            $self->{state} = 'comment';
1065            !!!next-input-character;
1066            redo A;
1067          }
1068        } elsif ($self->{state} eq 'comment start dash') {
1069          if ($self->{next_input_character} == 0x002D) { # -
1070            $self->{state} = 'comment end';
1071            !!!next-input-character;
1072            redo A;
1073          } elsif ($self->{next_input_character} == 0x003E) { # >
1074            !!!parse-error (type => 'bogus comment');
1075            $self->{state} = 'data';
1076            !!!next-input-character;
1077    
1078            !!!emit ($self->{current_token}); # comment
1079    
1080            redo A;
1081          } elsif ($self->{next_input_character} == -1) {
1082            !!!parse-error (type => 'unclosed comment');
1083            $self->{state} = 'data';
1084            ## reconsume
1085    
1086            !!!emit ($self->{current_token}); # comment
1087    
1088            redo A;
1089          } else {
1090            $self->{current_token}->{data} # comment
1091                .= '-' . chr ($self->{next_input_character});
1092            $self->{state} = 'comment';
1093            !!!next-input-character;
1094            redo A;
1095          }
1096      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} eq 'comment') {
1097        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1098          $self->{state} = 'comment dash';          $self->{state} = 'comment end dash';
1099          !!!next-input-character;          !!!next-input-character;
1100          redo A;          redo A;
1101        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1102          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1103          $self->{state} = 'data';          $self->{state} = 'data';
1104          ## reconsume          ## reconsume
1105    
1106          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1107    
1108          redo A;          redo A;
1109        } else {        } else {
# Line 1224  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112          !!!next-input-character;          !!!next-input-character;
1113          redo A;          redo A;
1114        }        }
1115      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} eq 'comment end dash') {
1116        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1117          $self->{state} = 'comment end';          $self->{state} = 'comment end';
1118          !!!next-input-character;          !!!next-input-character;
1119          redo A;          redo A;
1120        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1121          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1122          $self->{state} = 'data';          $self->{state} = 'data';
1123          ## reconsume          ## reconsume
1124    
1125          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1126    
1127          redo A;          redo A;
1128        } else {        } else {
# Line 1250  sub _get_next_token ($) { Line 1137  sub _get_next_token ($) {
1137          !!!next-input-character;          !!!next-input-character;
1138    
1139          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1140    
1141          redo A;          redo A;
1142        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
1143          !!!parse-error;          !!!parse-error (type => 'dash in comment');
1144          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1145          ## Stay in the state          ## Stay in the state
1146          !!!next-input-character;          !!!next-input-character;
1147          redo A;          redo A;
1148        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1149          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1150          $self->{state} = 'data';          $self->{state} = 'data';
1151          ## reconsume          ## reconsume
1152    
1153          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1154    
1155          redo A;          redo A;
1156        } else {        } else {
1157          !!!parse-error;          !!!parse-error (type => 'dash in comment');
1158          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1159          $self->{state} = 'comment';          $self->{state} = 'comment';
1160          !!!next-input-character;          !!!next-input-character;
# Line 1285  sub _get_next_token ($) { Line 1170  sub _get_next_token ($) {
1170          !!!next-input-character;          !!!next-input-character;
1171          redo A;          redo A;
1172        } else {        } else {
1173          !!!parse-error;          !!!parse-error (type => 'no space before DOCTYPE name');
1174          $self->{state} = 'before DOCTYPE name';          $self->{state} = 'before DOCTYPE name';
1175          ## reconsume          ## reconsume
1176          redo A;          redo A;
# Line 1299  sub _get_next_token ($) { Line 1184  sub _get_next_token ($) {
1184          ## Stay in the state          ## Stay in the state
1185          !!!next-input-character;          !!!next-input-character;
1186          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
1187        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1188          !!!parse-error;          !!!parse-error (type => 'no DOCTYPE name');
1189          $self->{state} = 'data';          $self->{state} = 'data';
1190          !!!next-input-character;          !!!next-input-character;
1191    
1192          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1193    
1194          redo A;          redo A;
1195        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1196          !!!parse-error;          !!!parse-error (type => 'no DOCTYPE name');
1197          $self->{state} = 'data';          $self->{state} = 'data';
1198          ## reconsume          ## reconsume
1199    
1200          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1201    
1202          redo A;          redo A;
1203        } else {        } else {
1204          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1205                            name => chr ($self->{next_input_character}),              = {type => 'DOCTYPE',
1206                            error => 1};                 name => chr ($self->{next_input_character}),
1207                   correct => 1};
1208    ## ISSUE: "Set the token's name name to the" in the spec
1209          $self->{state} = 'DOCTYPE name';          $self->{state} = 'DOCTYPE name';
1210          !!!next-input-character;          !!!next-input-character;
1211          redo A;          redo A;
1212        }        }
1213      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} eq 'DOCTYPE name') {
1214    ## ISSUE: Redundant "First," in the spec.
1215        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1216            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1217            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1218            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1219            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1220          $self->{state} = 'after DOCTYPE name';          $self->{state} = 'after DOCTYPE name';
1221          !!!next-input-character;          !!!next-input-character;
1222          redo A;          redo A;
1223        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1224          $self->{state} = 'data';          $self->{state} = 'data';
1225          !!!next-input-character;          !!!next-input-character;
1226    
1227          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1228    
1229          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
         $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE  
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
1230        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1231          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1232          $self->{state} = 'data';          $self->{state} = 'data';
1233          ## reconsume          ## reconsume
1234    
1235          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1236          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1237    
1238          redo A;          redo A;
1239        } else {        } else {
1240          $self->{current_token}->{name}          $self->{current_token}->{name}
1241            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1242          ## Stay in the state          ## Stay in the state
1243          !!!next-input-character;          !!!next-input-character;
1244          redo A;          redo A;
# Line 1389  sub _get_next_token ($) { Line 1257  sub _get_next_token ($) {
1257          !!!next-input-character;          !!!next-input-character;
1258    
1259          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1260    
1261          redo A;          redo A;
1262        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1263          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
1264            $self->{state} = 'data';
1265            ## reconsume
1266    
1267            delete $self->{current_token}->{correct};
1268            !!!emit ($self->{current_token}); # DOCTYPE
1269    
1270            redo A;
1271          } elsif ($self->{next_input_character} == 0x0050 or # P
1272                   $self->{next_input_character} == 0x0070) { # p
1273            !!!next-input-character;
1274            if ($self->{next_input_character} == 0x0055 or # U
1275                $self->{next_input_character} == 0x0075) { # u
1276              !!!next-input-character;
1277              if ($self->{next_input_character} == 0x0042 or # B
1278                  $self->{next_input_character} == 0x0062) { # b
1279                !!!next-input-character;
1280                if ($self->{next_input_character} == 0x004C or # L
1281                    $self->{next_input_character} == 0x006C) { # l
1282                  !!!next-input-character;
1283                  if ($self->{next_input_character} == 0x0049 or # I
1284                      $self->{next_input_character} == 0x0069) { # i
1285                    !!!next-input-character;
1286                    if ($self->{next_input_character} == 0x0043 or # C
1287                        $self->{next_input_character} == 0x0063) { # c
1288                      $self->{state} = 'before DOCTYPE public identifier';
1289                      !!!next-input-character;
1290                      redo A;
1291                    }
1292                  }
1293                }
1294              }
1295            }
1296    
1297            #
1298          } elsif ($self->{next_input_character} == 0x0053 or # S
1299                   $self->{next_input_character} == 0x0073) { # s
1300            !!!next-input-character;
1301            if ($self->{next_input_character} == 0x0059 or # Y
1302                $self->{next_input_character} == 0x0079) { # y
1303              !!!next-input-character;
1304              if ($self->{next_input_character} == 0x0053 or # S
1305                  $self->{next_input_character} == 0x0073) { # s
1306                !!!next-input-character;
1307                if ($self->{next_input_character} == 0x0054 or # T
1308                    $self->{next_input_character} == 0x0074) { # t
1309                  !!!next-input-character;
1310                  if ($self->{next_input_character} == 0x0045 or # E
1311                      $self->{next_input_character} == 0x0065) { # e
1312                    !!!next-input-character;
1313                    if ($self->{next_input_character} == 0x004D or # M
1314                        $self->{next_input_character} == 0x006D) { # m
1315                      $self->{state} = 'before DOCTYPE system identifier';
1316                      !!!next-input-character;
1317                      redo A;
1318                    }
1319                  }
1320                }
1321              }
1322            }
1323    
1324            #
1325          } else {
1326            !!!next-input-character;
1327            #
1328          }
1329    
1330          !!!parse-error (type => 'string after DOCTYPE name');
1331          $self->{state} = 'bogus DOCTYPE';
1332          # next-input-character is already done
1333          redo A;
1334        } elsif ($self->{state} eq 'before DOCTYPE public identifier') {
1335          if ({
1336                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1337                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1338              }->{$self->{next_input_character}}) {
1339            ## Stay in the state
1340            !!!next-input-character;
1341            redo A;
1342          } elsif ($self->{next_input_character} eq 0x0022) { # "
1343            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1344            $self->{state} = 'DOCTYPE public identifier (double-quoted)';
1345            !!!next-input-character;
1346            redo A;
1347          } elsif ($self->{next_input_character} eq 0x0027) { # '
1348            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1349            $self->{state} = 'DOCTYPE public identifier (single-quoted)';
1350            !!!next-input-character;
1351            redo A;
1352          } elsif ($self->{next_input_character} eq 0x003E) { # >
1353            !!!parse-error (type => 'no PUBLIC literal');
1354    
1355            $self->{state} = 'data';
1356            !!!next-input-character;
1357    
1358            delete $self->{current_token}->{correct};
1359            !!!emit ($self->{current_token}); # DOCTYPE
1360    
1361            redo A;
1362          } elsif ($self->{next_input_character} == -1) {
1363            !!!parse-error (type => 'unclosed DOCTYPE');
1364    
1365            $self->{state} = 'data';
1366            ## reconsume
1367    
1368            delete $self->{current_token}->{correct};
1369            !!!emit ($self->{current_token}); # DOCTYPE
1370    
1371            redo A;
1372          } else {
1373            !!!parse-error (type => 'string after PUBLIC');
1374            $self->{state} = 'bogus DOCTYPE';
1375            !!!next-input-character;
1376            redo A;
1377          }
1378        } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {
1379          if ($self->{next_input_character} == 0x0022) { # "
1380            $self->{state} = 'after DOCTYPE public identifier';
1381            !!!next-input-character;
1382            redo A;
1383          } elsif ($self->{next_input_character} == -1) {
1384            !!!parse-error (type => 'unclosed PUBLIC literal');
1385    
1386            $self->{state} = 'data';
1387            ## reconsume
1388    
1389            delete $self->{current_token}->{correct};
1390            !!!emit ($self->{current_token}); # DOCTYPE
1391    
1392            redo A;
1393          } else {
1394            $self->{current_token}->{public_identifier} # DOCTYPE
1395                .= chr $self->{next_input_character};
1396            ## Stay in the state
1397            !!!next-input-character;
1398            redo A;
1399          }
1400        } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {
1401          if ($self->{next_input_character} == 0x0027) { # '
1402            $self->{state} = 'after DOCTYPE public identifier';
1403            !!!next-input-character;
1404            redo A;
1405          } elsif ($self->{next_input_character} == -1) {
1406            !!!parse-error (type => 'unclosed PUBLIC literal');
1407    
1408            $self->{state} = 'data';
1409            ## reconsume
1410    
1411            delete $self->{current_token}->{correct};
1412            !!!emit ($self->{current_token}); # DOCTYPE
1413    
1414            redo A;
1415          } else {
1416            $self->{current_token}->{public_identifier} # DOCTYPE
1417                .= chr $self->{next_input_character};
1418            ## Stay in the state
1419            !!!next-input-character;
1420            redo A;
1421          }
1422        } elsif ($self->{state} eq 'after DOCTYPE public identifier') {
1423          if ({
1424                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1425                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1426              }->{$self->{next_input_character}}) {
1427            ## Stay in the state
1428            !!!next-input-character;
1429            redo A;
1430          } elsif ($self->{next_input_character} == 0x0022) { # "
1431            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1432            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1433            !!!next-input-character;
1434            redo A;
1435          } elsif ($self->{next_input_character} == 0x0027) { # '
1436            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1437            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1438            !!!next-input-character;
1439            redo A;
1440          } elsif ($self->{next_input_character} == 0x003E) { # >
1441            $self->{state} = 'data';
1442            !!!next-input-character;
1443    
1444            !!!emit ($self->{current_token}); # DOCTYPE
1445    
1446            redo A;
1447          } elsif ($self->{next_input_character} == -1) {
1448            !!!parse-error (type => 'unclosed DOCTYPE');
1449    
1450            $self->{state} = 'data';
1451            ## reconsume
1452    
1453            delete $self->{current_token}->{correct};
1454            !!!emit ($self->{current_token}); # DOCTYPE
1455    
1456            redo A;
1457          } else {
1458            !!!parse-error (type => 'string after PUBLIC literal');
1459            $self->{state} = 'bogus DOCTYPE';
1460            !!!next-input-character;
1461            redo A;
1462          }
1463        } elsif ($self->{state} eq 'before DOCTYPE system identifier') {
1464          if ({
1465                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1466                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1467              }->{$self->{next_input_character}}) {
1468            ## Stay in the state
1469            !!!next-input-character;
1470            redo A;
1471          } elsif ($self->{next_input_character} == 0x0022) { # "
1472            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1473            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1474            !!!next-input-character;
1475            redo A;
1476          } elsif ($self->{next_input_character} == 0x0027) { # '
1477            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1478            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1479            !!!next-input-character;
1480            redo A;
1481          } elsif ($self->{next_input_character} == 0x003E) { # >
1482            !!!parse-error (type => 'no SYSTEM literal');
1483            $self->{state} = 'data';
1484            !!!next-input-character;
1485    
1486            delete $self->{current_token}->{correct};
1487            !!!emit ($self->{current_token}); # DOCTYPE
1488    
1489            redo A;
1490          } elsif ($self->{next_input_character} == -1) {
1491            !!!parse-error (type => 'unclosed DOCTYPE');
1492    
1493            $self->{state} = 'data';
1494            ## reconsume
1495    
1496            delete $self->{current_token}->{correct};
1497            !!!emit ($self->{current_token}); # DOCTYPE
1498    
1499            redo A;
1500          } else {
1501            !!!parse-error (type => 'string after SYSTEM');
1502            $self->{state} = 'bogus DOCTYPE';
1503            !!!next-input-character;
1504            redo A;
1505          }
1506        } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {
1507          if ($self->{next_input_character} == 0x0022) { # "
1508            $self->{state} = 'after DOCTYPE system identifier';
1509            !!!next-input-character;
1510            redo A;
1511          } elsif ($self->{next_input_character} == -1) {
1512            !!!parse-error (type => 'unclosed SYSTEM literal');
1513    
1514            $self->{state} = 'data';
1515            ## reconsume
1516    
1517            delete $self->{current_token}->{correct};
1518            !!!emit ($self->{current_token}); # DOCTYPE
1519    
1520            redo A;
1521          } else {
1522            $self->{current_token}->{system_identifier} # DOCTYPE
1523                .= chr $self->{next_input_character};
1524            ## Stay in the state
1525            !!!next-input-character;
1526            redo A;
1527          }
1528        } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {
1529          if ($self->{next_input_character} == 0x0027) { # '
1530            $self->{state} = 'after DOCTYPE system identifier';
1531            !!!next-input-character;
1532            redo A;
1533          } elsif ($self->{next_input_character} == -1) {
1534            !!!parse-error (type => 'unclosed SYSTEM literal');
1535    
1536            $self->{state} = 'data';
1537            ## reconsume
1538    
1539            delete $self->{current_token}->{correct};
1540            !!!emit ($self->{current_token}); # DOCTYPE
1541    
1542            redo A;
1543          } else {
1544            $self->{current_token}->{system_identifier} # DOCTYPE
1545                .= chr $self->{next_input_character};
1546            ## Stay in the state
1547            !!!next-input-character;
1548            redo A;
1549          }
1550        } elsif ($self->{state} eq 'after DOCTYPE system identifier') {
1551          if ({
1552                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1553                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1554              }->{$self->{next_input_character}}) {
1555            ## Stay in the state
1556            !!!next-input-character;
1557            redo A;
1558          } elsif ($self->{next_input_character} == 0x003E) { # >
1559            $self->{state} = 'data';
1560            !!!next-input-character;
1561    
1562            !!!emit ($self->{current_token}); # DOCTYPE
1563    
1564            redo A;
1565          } elsif ($self->{next_input_character} == -1) {
1566            !!!parse-error (type => 'unclosed DOCTYPE');
1567    
1568          $self->{state} = 'data';          $self->{state} = 'data';
1569          ## reconsume          ## reconsume
1570    
1571            delete $self->{current_token}->{correct};
1572          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1573    
1574          redo A;          redo A;
1575        } else {        } else {
1576          !!!parse-error;          !!!parse-error (type => 'string after SYSTEM literal');
         $self->{current_token}->{error} = 1; # DOCTYPE  
1577          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1578          !!!next-input-character;          !!!next-input-character;
1579          redo A;          redo A;
# Line 1413  sub _get_next_token ($) { Line 1583  sub _get_next_token ($) {
1583          $self->{state} = 'data';          $self->{state} = 'data';
1584          !!!next-input-character;          !!!next-input-character;
1585    
1586            delete $self->{current_token}->{correct};
1587          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1588    
1589          redo A;          redo A;
1590        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1591          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
1592          $self->{state} = 'data';          $self->{state} = 'data';
1593          ## reconsume          ## reconsume
1594    
1595            delete $self->{current_token}->{correct};
1596          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1597    
1598          redo A;          redo A;
1599        } else {        } else {
# Line 1439  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1610  } # _get_next_token  } # _get_next_token
1611    
1612  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1613    my $self = shift;    my ($self, $in_attr) = @_;
1614      
1615    if ($self->{next_input_character} == 0x0023) { # #    if ({
1616           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1617           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1618          }->{$self->{next_input_character}}) {
1619        ## Don't consume
1620        ## No error
1621        return undef;
1622      } elsif ($self->{next_input_character} == 0x0023) { # #
1623      !!!next-input-character;      !!!next-input-character;
     my $num;  
1624      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1625          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1626          my $code;
1627        X: {        X: {
1628          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1629          !!!next-input-character;          !!!next-input-character;
1630          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1631              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1632            $num ||= 0;            $code ||= 0;
1633            $num *= 0x10;            $code *= 0x10;
1634            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1635            redo X;            redo X;
1636          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1637                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1638            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1639            $num ||= 0;            $code *= 0x10;
1640            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1641            redo X;            redo X;
1642          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1643                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1644            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1645            $num ||= 0;            $code *= 0x10;
1646            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1647            redo X;            redo X;
1648          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1649            !!!parse-error;            !!!parse-error (type => 'bare hcro');
1650              !!!back-next-input-character ($x_char, $self->{next_input_character});
1651            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1652            return undef;            return undef;
1653          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1654            !!!next-input-character;            !!!next-input-character;
1655          } else {          } else {
1656            !!!parse-error;            !!!parse-error (type => 'no refc');
1657          }          }
1658    
1659          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1660          if ($num > 1114111 or $num == 0) {            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1661            $num = 0xFFFD; # REPLACEMENT CHARACTER            $code = 0xFFFD;
1662            ## ISSUE: Why this is not an error?          } elsif ($code > 0x10FFFF) {
1663              !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1664              $code = 0xFFFD;
1665            } elsif ($code == 0x000D) {
1666              !!!parse-error (type => 'CR character reference');
1667              $code = 0x000A;
1668            } elsif (0x80 <= $code and $code <= 0x9F) {
1669              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1670              $code = $c1_entity_char->{$code};
1671          }          }
1672    
1673          return {type => 'character', data => chr $num};          return {type => 'character', data => chr $code};
1674        } # X        } # X
1675      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1676               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1505  sub _tokenize_attempt_to_consume_an_enti Line 1688  sub _tokenize_attempt_to_consume_an_enti
1688        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_input_character} == 0x003B) { # ;
1689          !!!next-input-character;          !!!next-input-character;
1690        } else {        } else {
1691          !!!parse-error;          !!!parse-error (type => 'no refc');
1692        }        }
1693    
1694        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1695        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1696          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1697          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1698            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1699            $code = 0xFFFD;
1700          } elsif ($code == 0x000D) {
1701            !!!parse-error (type => 'CR character reference');
1702            $code = 0x000A;
1703          } elsif (0x80 <= $code and $code <= 0x9F) {
1704            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1705            $code = $c1_entity_char->{$code};
1706        }        }
1707                
1708        return {type => 'character', data => chr $code};        return {type => 'character', data => chr $code};
1709      } else {      } else {
1710        !!!parse-error;        !!!parse-error (type => 'bare nero');
1711        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
1712        $self->{next_input_character} = 0x0023; # #        $self->{next_input_character} = 0x0023; # #
1713        return undef;        return undef;
# Line 1529  sub _tokenize_attempt_to_consume_an_enti Line 1720  sub _tokenize_attempt_to_consume_an_enti
1720      !!!next-input-character;      !!!next-input-character;
1721    
1722      my $value = $entity_name;      my $value = $entity_name;
1723      my $match;      my $match = 0;
1724        require Whatpm::_NamedEntityList;
1725        our $EntityChar;
1726    
1727      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1728             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1729             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1730               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1731              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1732               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1733              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1734               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1735                $self->{next_input_character} == 0x003B)) { # ;
1736        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1737        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1738          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1739          $match = 1;            $value = $EntityChar->{$entity_name};
1740              $match = 1;
1741              !!!next-input-character;
1742              last;
1743            } else {
1744              $value = $EntityChar->{$entity_name};
1745              $match = -1;
1746              !!!next-input-character;
1747            }
1748        } else {        } else {
1749          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1750            $match *= 2;
1751            !!!next-input-character;
1752        }        }
       !!!next-input-character;  
1753      }      }
1754            
1755      if ($match) {      if ($match > 0) {
1756        if ($self->{next_input_character} == 0x003B) { # ;        return {type => 'character', data => $value};
1757          !!!next-input-character;      } elsif ($match < 0) {
1758          !!!parse-error (type => 'no refc');
1759          if ($in_attr and $match < -1) {
1760            return {type => 'character', data => '&'.$entity_name};
1761        } else {        } else {
1762          !!!parse-error;          return {type => 'character', data => $value};
1763        }        }
   
       return {type => 'character', data => $value};  
1764      } else {      } else {
1765        !!!parse-error;        !!!parse-error (type => 'bare ero');
1766        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1767        !!!back-token ({type => 'character', data => $value});        return {type => 'character', data => '&'.$value};
       return undef;  
1768      }      }
1769    } else {    } else {
1770      ## no characters are consumed      ## no characters are consumed
1771      !!!parse-error;      !!!parse-error (type => 'bare ero');
1772      return undef;      return undef;
1773    }    }
1774  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1576  sub _initialize_tree_constructor ($) { Line 1779  sub _initialize_tree_constructor ($) {
1779    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1780    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1781    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1782    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1783  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1784    
1785  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1587  sub _terminate_tree_constructor ($) { Line 1790  sub _terminate_tree_constructor ($) {
1790    
1791  ## 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?
1792    
1793    { # tree construction stage
1794      my $token;
1795    
1796  sub _construct_tree ($) {  sub _construct_tree ($) {
1797    my ($self) = @_;    my ($self) = @_;
1798    
# Line 1598  sub _construct_tree ($) { Line 1804  sub _construct_tree ($) {
1804    ## characters and insert one Text node whose data is concatenation    ## characters and insert one Text node whose data is concatenation
1805    ## of all those characters. # MUST    ## of all those characters. # MUST
1806        
   my $token;  
1807    !!!next-token;    !!!next-token;
1808    
1809    my $phase = 'initial'; # MUST    $self->{insertion_mode} = 'before head';
1810      undef $self->{form_element};
1811      undef $self->{head_element};
1812      $self->{open_elements} = [];
1813      undef $self->{inner_html_node};
1814    
1815      $self->_tree_construction_initial; # MUST
1816      $self->_tree_construction_root_element;
1817      $self->_tree_construction_main;
1818    } # _construct_tree
1819    
1820    sub _tree_construction_initial ($) {
1821      my $self = shift;
1822      INITIAL: {
1823        if ($token->{type} eq 'DOCTYPE') {
1824          ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1825          ## error, switch to a conformance checking mode for another
1826          ## language.
1827          my $doctype_name = $token->{name};
1828          $doctype_name = '' unless defined $doctype_name;
1829          $doctype_name =~ tr/a-z/A-Z/;
1830          if (not defined $token->{name} or # <!DOCTYPE>
1831              defined $token->{public_identifier} or
1832              defined $token->{system_identifier}) {
1833            !!!parse-error (type => 'not HTML5');
1834          } elsif ($doctype_name ne 'HTML') {
1835            ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1836            !!!parse-error (type => 'not HTML5');
1837          }
1838          
1839          my $doctype = $self->{document}->create_document_type_definition
1840            ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1841          $doctype->public_id ($token->{public_identifier})
1842              if defined $token->{public_identifier};
1843          $doctype->system_id ($token->{system_identifier})
1844              if defined $token->{system_identifier};
1845          ## NOTE: Other DocumentType attributes are null or empty lists.
1846          ## ISSUE: internalSubset = null??
1847          $self->{document}->append_child ($doctype);
1848          
1849          if (not $token->{correct} or $doctype_name ne 'HTML') {
1850            $self->{document}->manakai_compat_mode ('quirks');
1851          } elsif (defined $token->{public_identifier}) {
1852            my $pubid = $token->{public_identifier};
1853            $pubid =~ tr/a-z/A-z/;
1854            if ({
1855              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1856              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1857              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1858              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1859              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1860              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1861              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1862              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1863              "-//IETF//DTD HTML 2.0//EN" => 1,
1864              "-//IETF//DTD HTML 2.1E//EN" => 1,
1865              "-//IETF//DTD HTML 3.0//EN" => 1,
1866              "-//IETF//DTD HTML 3.0//EN//" => 1,
1867              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1868              "-//IETF//DTD HTML 3.2//EN" => 1,
1869              "-//IETF//DTD HTML 3//EN" => 1,
1870              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1871              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1872              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1873              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1874              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1875              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1876              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1877              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1878              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1879              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1880              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1881              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1882              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1883              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1884              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1885              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1886              "-//IETF//DTD HTML STRICT//EN" => 1,
1887              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1888              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1889              "-//IETF//DTD HTML//EN" => 1,
1890              "-//IETF//DTD HTML//EN//2.0" => 1,
1891              "-//IETF//DTD HTML//EN//3.0" => 1,
1892              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1893              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1894              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1895              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1896              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1897              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1898              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1899              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1900              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1901              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1902              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1903              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1904              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1905              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1906              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1907              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1908              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1909              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1910              "-//W3C//DTD HTML 3.2//EN" => 1,
1911              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1912              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
1913              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
1914              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
1915              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
1916              "-//W3C//DTD W3 HTML//EN" => 1,
1917              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
1918              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
1919              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
1920              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
1921              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
1922              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
1923              "HTML" => 1,
1924            }->{$pubid}) {
1925              $self->{document}->manakai_compat_mode ('quirks');
1926            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
1927                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
1928              if (defined $token->{system_identifier}) {
1929                $self->{document}->manakai_compat_mode ('quirks');
1930              } else {
1931                $self->{document}->manakai_compat_mode ('limited quirks');
1932              }
1933            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
1934                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
1935              $self->{document}->manakai_compat_mode ('limited quirks');
1936            }
1937          }
1938          if (defined $token->{system_identifier}) {
1939            my $sysid = $token->{system_identifier};
1940            $sysid =~ tr/A-Z/a-z/;
1941            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1942              $self->{document}->manakai_compat_mode ('quirks');
1943            }
1944          }
1945          
1946          ## Go to the root element phase.
1947          !!!next-token;
1948          return;
1949        } elsif ({
1950                  'start tag' => 1,
1951                  'end tag' => 1,
1952                  'end-of-file' => 1,
1953                 }->{$token->{type}}) {
1954          !!!parse-error (type => 'no DOCTYPE');
1955          $self->{document}->manakai_compat_mode ('quirks');
1956          ## Go to the root element phase
1957          ## reprocess
1958          return;
1959        } elsif ($token->{type} eq 'character') {
1960          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1961            ## Ignore the token
1962    
1963            unless (length $token->{data}) {
1964              ## Stay in the phase
1965              !!!next-token;
1966              redo INITIAL;
1967            }
1968          }
1969    
1970          !!!parse-error (type => 'no DOCTYPE');
1971          $self->{document}->manakai_compat_mode ('quirks');
1972          ## Go to the root element phase
1973          ## reprocess
1974          return;
1975        } elsif ($token->{type} eq 'comment') {
1976          my $comment = $self->{document}->create_comment ($token->{data});
1977          $self->{document}->append_child ($comment);
1978          
1979          ## Stay in the phase.
1980          !!!next-token;
1981          redo INITIAL;
1982        } else {
1983          die "$0: $token->{type}: Unknown token";
1984        }
1985      } # INITIAL
1986    } # _tree_construction_initial
1987    
1988    sub _tree_construction_root_element ($) {
1989      my $self = shift;
1990      
1991      B: {
1992          if ($token->{type} eq 'DOCTYPE') {
1993            !!!parse-error (type => 'in html:#DOCTYPE');
1994            ## Ignore the token
1995            ## Stay in the phase
1996            !!!next-token;
1997            redo B;
1998          } elsif ($token->{type} eq 'comment') {
1999            my $comment = $self->{document}->create_comment ($token->{data});
2000            $self->{document}->append_child ($comment);
2001            ## Stay in the phase
2002            !!!next-token;
2003            redo B;
2004          } elsif ($token->{type} eq 'character') {
2005            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2006              ## Ignore the token.
2007    
2008              unless (length $token->{data}) {
2009                ## Stay in the phase
2010                !!!next-token;
2011                redo B;
2012              }
2013            }
2014            #
2015          } elsif ({
2016                    'start tag' => 1,
2017                    'end tag' => 1,
2018                    'end-of-file' => 1,
2019                   }->{$token->{type}}) {
2020            ## ISSUE: There is an issue in the spec
2021            #
2022          } else {
2023            die "$0: $token->{type}: Unknown token";
2024          }
2025          my $root_element; !!!create-element ($root_element, 'html');
2026          $self->{document}->append_child ($root_element);
2027          push @{$self->{open_elements}}, [$root_element, 'html'];
2028          ## reprocess
2029          #redo B;
2030          return; ## Go to the main phase.
2031      } # B
2032    } # _tree_construction_root_element
2033    
2034    sub _reset_insertion_mode ($) {
2035      my $self = shift;
2036    
2037        ## Step 1
2038        my $last;
2039        
2040        ## Step 2
2041        my $i = -1;
2042        my $node = $self->{open_elements}->[$i];
2043        
2044        ## Step 3
2045        S3: {
2046          ## ISSUE: Oops! "If node is the first node in the stack of open
2047          ## elements, then set last to true. If the context element of the
2048          ## HTML fragment parsing algorithm is neither a td element nor a
2049          ## th element, then set node to the context element. (fragment case)":
2050          ## The second "if" is in the scope of the first "if"!?
2051          if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2052            $last = 1;
2053            if (defined $self->{inner_html_node}) {
2054              if ($self->{inner_html_node}->[1] eq 'td' or
2055                  $self->{inner_html_node}->[1] eq 'th') {
2056                #
2057              } else {
2058                $node = $self->{inner_html_node};
2059              }
2060            }
2061          }
2062        
2063          ## Step 4..13
2064          my $new_mode = {
2065                          select => 'in select',
2066                          td => 'in cell',
2067                          th => 'in cell',
2068                          tr => 'in row',
2069                          tbody => 'in table body',
2070                          thead => 'in table head',
2071                          tfoot => 'in table foot',
2072                          caption => 'in caption',
2073                          colgroup => 'in column group',
2074                          table => 'in table',
2075                          head => 'in body', # not in head!
2076                          body => 'in body',
2077                          frameset => 'in frameset',
2078                         }->{$node->[1]};
2079          $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2080          
2081          ## Step 14
2082          if ($node->[1] eq 'html') {
2083            unless (defined $self->{head_element}) {
2084              $self->{insertion_mode} = 'before head';
2085            } else {
2086              $self->{insertion_mode} = 'after head';
2087            }
2088            return;
2089          }
2090          
2091          ## Step 15
2092          $self->{insertion_mode} = 'in body' and return if $last;
2093          
2094          ## Step 16
2095          $i--;
2096          $node = $self->{open_elements}->[$i];
2097          
2098          ## Step 17
2099          redo S3;
2100        } # S3
2101    } # _reset_insertion_mode
2102    
2103    sub _tree_construction_main ($) {
2104      my $self = shift;
2105    
2106      my $previous_insertion_mode;
2107    
   my $open_elements = [];  
2108    my $active_formatting_elements = [];    my $active_formatting_elements = [];
   my $head_element;  
   my $form_element;  
   my $insertion_mode = 'before head';  
2109    
2110    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
2111      my $insert = shift;      my $insert = shift;
# Line 1621  sub _construct_tree ($) { Line 2119  sub _construct_tree ($) {
2119    
2120      ## Step 2      ## Step 2
2121      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2122      for (@$open_elements) {      for (@{$self->{open_elements}}) {
2123        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2124          return;          return;
2125        }        }
# Line 1640  sub _construct_tree ($) { Line 2138  sub _construct_tree ($) {
2138          #          #
2139        } else {        } else {
2140          my $in_open_elements;          my $in_open_elements;
2141          OE: for (@$open_elements) {          OE: for (@{$self->{open_elements}}) {
2142            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2143              $in_open_elements = 1;              $in_open_elements = 1;
2144              last OE;              last OE;
# Line 1664  sub _construct_tree ($) { Line 2162  sub _construct_tree ($) {
2162            
2163        ## Step 9        ## Step 9
2164        $insert->($clone->[0]);        $insert->($clone->[0]);
2165        push @$open_elements, $clone;        push @{$self->{open_elements}}, $clone;
2166                
2167        ## Step 10        ## Step 10
2168        $active_formatting_elements->[$i] = $open_elements->[-1];        $active_formatting_elements->[$i] = $self->{open_elements}->[-1];
2169    
2170        ## Step 11        ## Step 11
2171        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
# Line 1689  sub _construct_tree ($) { Line 2187  sub _construct_tree ($) {
2187      }      }
2188    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2189    
2190    my $reset_insertion_mode = sub {    my $parse_rcdata = sub ($$) {
2191        my ($content_model_flag, $insert) = @_;
2192    
2193      ## Step 1      ## Step 1
2194      my $last;      my $start_tag_name = $token->{tag_name};
2195            my $el;
2196        !!!create-element ($el, $start_tag_name, $token->{attributes});
2197    
2198      ## Step 2      ## Step 2
2199      my $i = -1;      $insert->($el); # /context node/->append_child ($el)
2200      my $node = $open_elements->[$i];  
       
2201      ## Step 3      ## Step 3
2202      S3: {      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2203        $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  
2204    
2205    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';  
                 
2206      my $text = '';      my $text = '';
2207      !!!next-token;      !!!next-token;
2208      while ($token->{type} eq 'character') {      while ($token->{type} eq 'character') { # or until stop tokenizing
2209        $text .= $token->{data};        $text .= $token->{data};
2210        !!!next-token;        !!!next-token;
2211      } # stop if non-character token or tokenizer stops tokenising      }
2212    
2213        ## Step 5
2214      if (length $text) {      if (length $text) {
2215        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2216          $el->append_child ($text);
2217      }      }
2218        
2219      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2220                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2221      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2222        ## Step 7
2223        if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2224        ## Ignore the token        ## Ignore the token
2225        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2226          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2227        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2228          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2229      } else {      } else {
2230        !!!parse-error;        die "$0: $content_model_flag in parse_rcdata";
       ## ISSUE: And ignore?  
2231      }      }
2232      !!!next-token;      !!!next-token;
2233    }; # $style_start_tag    }; # $parse_rcdata
2234    
2235    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2236        my $insert = $_[0];
2237      my $script_el;      my $script_el;
2238      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2239      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2240    
2241      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2242        delete $self->{escape}; # MUST
2243            
2244      my $text = '';      my $text = '';
2245      !!!next-token;      !!!next-token;
# Line 1788  sub _construct_tree ($) { Line 2251  sub _construct_tree ($) {
2251        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2252      }      }
2253                                
2254      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2255    
2256      if ($token->{type} eq 'end tag' and      if ($token->{type} eq 'end tag' and
2257          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2258        ## Ignore the token        ## Ignore the token
2259      } else {      } else {
2260        !!!parse-error;        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2261        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2262        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2263      }      }
2264            
2265      ## TODO: inner_html mode then mark as "already executed" and skip      if (defined $self->{inner_html_node}) {
2266      if (1) {        ## TODO: mark as "already executed"
2267        } else {
2268        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2269        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2270          
2271        (($insertion_mode eq 'in head' and defined $head_element)        $insert->($script_el);
        ? $head_element : $open_elements->[-1]->[0])->append_child ($script_el);  
2272                
2273        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2274                
# Line 1832  sub _construct_tree ($) { Line 2295  sub _construct_tree ($) {
2295          }          }
2296        } # AFE        } # AFE
2297        unless (defined $formatting_element) {        unless (defined $formatting_element) {
2298          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
2299          ## Ignore the token          ## Ignore the token
2300          !!!next-token;          !!!next-token;
2301          return;          return;
# Line 1840  sub _construct_tree ($) { Line 2303  sub _construct_tree ($) {
2303        ## has an element in scope        ## has an element in scope
2304        my $in_scope = 1;        my $in_scope = 1;
2305        my $formatting_element_i_in_open;          my $formatting_element_i_in_open;  
2306        INSCOPE: for (reverse 0..$#$open_elements) {        INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2307          my $node = $open_elements->[$_];          my $node = $self->{open_elements}->[$_];
2308          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
2309            if ($in_scope) {            if ($in_scope) {
2310              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
2311              last INSCOPE;              last INSCOPE;
2312            } else { # in open elements but not in scope            } else { # in open elements but not in scope
2313              !!!parse-error;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2314              ## Ignore the token              ## Ignore the token
2315              !!!next-token;              !!!next-token;
2316              return;              return;
# Line 1860  sub _construct_tree ($) { Line 2323  sub _construct_tree ($) {
2323          }          }
2324        } # INSCOPE        } # INSCOPE
2325        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
2326          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2327          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
2328          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
2329          return;          return;
2330        }        }
2331        if (not $open_elements->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
2332          !!!parse-error;          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2333        }        }
2334                
2335        ## Step 2        ## Step 2
2336        my $furthest_block;        my $furthest_block;
2337        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
2338        OE: for (reverse 0..$#$open_elements) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
2339          my $node = $open_elements->[$_];          my $node = $self->{open_elements}->[$_];
2340          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
2341              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
2342              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
# Line 1887  sub _construct_tree ($) { Line 2350  sub _construct_tree ($) {
2350                
2351        ## Step 3        ## Step 3
2352        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
2353          splice @$open_elements, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
2354          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
2355          !!!next-token;          !!!next-token;
2356          return;          return;
2357        }        }
2358                
2359        ## Step 4        ## Step 4
2360        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];
2361                
2362        ## Step 5        ## Step 5
2363        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
# Line 1914  sub _construct_tree ($) { Line 2377  sub _construct_tree ($) {
2377        S7: {        S7: {
2378          ## Step 1          ## Step 1
2379          $node_i_in_open--;          $node_i_in_open--;
2380          $node = $open_elements->[$node_i_in_open];          $node = $self->{open_elements}->[$node_i_in_open];
2381                    
2382          ## Step 2          ## Step 2
2383          my $node_i_in_active;          my $node_i_in_active;
# Line 1925  sub _construct_tree ($) { Line 2388  sub _construct_tree ($) {
2388                last S7S2;                last S7S2;
2389              }              }
2390            }            }
2391            splice @$open_elements, $node_i_in_open, 1;            splice @{$self->{open_elements}}, $node_i_in_open, 1;
2392            redo S7;            redo S7;
2393          } # S7S2          } # S7S2
2394                    
# Line 1941  sub _construct_tree ($) { Line 2404  sub _construct_tree ($) {
2404          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
2405            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
2406            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
2407            $open_elements->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
2408            $node = $clone;            $node = $clone;
2409          }          }
2410                    
# Line 1983  sub _construct_tree ($) { Line 2446  sub _construct_tree ($) {
2446                
2447        ## Step 13        ## Step 13
2448        undef $i;        undef $i;
2449        OE: for (reverse 0..$#$open_elements) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
2450          if ($open_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
2451            splice @$open_elements, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
2452            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
2453          } elsif ($open_elements->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
2454            $i = $_;            $i = $_;
2455          }          }
2456        } # OE        } # OE
2457        splice @$open_elements, $i + 1, 1, $clone;        splice @{$self->{open_elements}}, $i + 1, 1, $clone;
2458                
2459        ## Step 14        ## Step 14
2460        redo FET;        redo FET;
# Line 1999  sub _construct_tree ($) { Line 2462  sub _construct_tree ($) {
2462    }; # $formatting_end_tag    }; # $formatting_end_tag
2463    
2464    my $insert_to_current = sub {    my $insert_to_current = sub {
2465      $open_elements->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2466    }; # $insert_to_current    }; # $insert_to_current
2467    
2468    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2007  sub _construct_tree ($) { Line 2470  sub _construct_tree ($) {
2470                         if ({                         if ({
2471                              table => 1, tbody => 1, tfoot => 1,                              table => 1, tbody => 1, tfoot => 1,
2472                              thead => 1, tr => 1,                              thead => 1, tr => 1,
2473                             }->{$open_elements->[-1]->[1]}) {                             }->{$self->{open_elements}->[-1]->[1]}) {
2474                           # MUST                           # MUST
2475                           my $foster_parent_element;                           my $foster_parent_element;
2476                           my $next_sibling;                           my $next_sibling;
2477                           OE: for (reverse 0..$#$open_elements) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
2478                             if ($open_elements->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
2479                               my $parent = $open_elements->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
2480                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
2481                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
2482                                 $next_sibling = $open_elements->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
2483                               } else {                               } else {
2484                                 $foster_parent_element                                 $foster_parent_element
2485                                   = $open_elements->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
2486                               }                               }
2487                               last OE;                               last OE;
2488                             }                             }
2489                           } # OE                           } # OE
2490                           $foster_parent_element = $open_elements->[0]->[0]                           $foster_parent_element = $self->{open_elements}->[0]->[0]
2491                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
2492                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
2493                             ($child, $next_sibling);                             ($child, $next_sibling);
2494                         } else {                         } else {
2495                           $open_elements->[-1]->[0]->append_child ($child);                           $self->{open_elements}->[-1]->[0]->append_child ($child);
2496                         }                         }
2497    }; # $insert_to_foster    }; # $insert_to_foster
2498    
# Line 2037  sub _construct_tree ($) { Line 2500  sub _construct_tree ($) {
2500      my $insert = shift;      my $insert = shift;
2501      if ($token->{type} eq 'start tag') {      if ($token->{type} eq 'start tag') {
2502        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
2503          $script_start_tag->();          ## NOTE: This is an "as if in head" code clone
2504            $script_start_tag->($insert);
2505          return;          return;
2506        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
2507          $style_start_tag->();          ## NOTE: This is an "as if in head" code clone
2508            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
2509          return;          return;
2510        } elsif ({        } elsif ({
2511                  base => 1, link => 1, meta => 1,                  base => 1, link => 1,
2512                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2513          !!!parse-error ($token->{tag_name}.' in body');          ## NOTE: This is an "as if in head" code clone, only "-t" differs
2514          ## NOTE: This is an "as if in head" code clone          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2515          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);  
         }  
           
2516          !!!next-token;          !!!next-token;
2517          return;          return;
2518        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'meta') {
2519          !!!parse-error ('title in body');          ## NOTE: This is an "as if in head" code clone, only "-t" differs
2520          ## NOTE: There is an "as if in head" code clone          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2521          my $title_el;          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2522          !!!create-element ($title_el, 'title', $token->{attributes});  
2523          (defined $head_element ? $head_element : $open_elements->[-1]->[0])          unless ($self->{confident}) {
2524            ->append_child ($title_el);            my $charset;
2525          $self->{content_model_flag} = 'RCDATA';            if ($token->{attributes}->{charset}) { ## TODO: And if supported
2526                        $charset = $token->{attributes}->{charset}->{value};
2527          my $text = '';            }
2528          !!!next-token;            if ($token->{attributes}->{'http-equiv'}) {
2529          while ($token->{type} eq 'character') {              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2530            $text .= $token->{data};              if ($token->{attributes}->{'http-equiv'}->{value}
2531            !!!next-token;                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2532          }                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2533          if (length $text) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2534            $title_el->manakai_append_text ($text);                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2535          }              } ## TODO: And if supported
2536                      }
2537          $self->{content_model_flag} = 'PCDATA';            ## TODO: Change the encoding
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error;  
           ## ISSUE: And ignore?  
2538          }          }
2539    
2540          !!!next-token;          !!!next-token;
2541          return;          return;
2542          } elsif ($token->{tag_name} eq 'title') {
2543            !!!parse-error (type => 'in body:title');
2544            ## NOTE: This is an "as if in head" code clone
2545            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
2546              if (defined $self->{head_element}) {
2547                $self->{head_element}->append_child ($_[0]);
2548              } else {
2549                $insert->($_[0]);
2550              }
2551            });
2552            return;
2553        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
2554          !!!parse-error;          !!!parse-error (type => 'in body:body');
2555                                
2556          if (@$open_elements == 1 or          if (@{$self->{open_elements}} == 1 or
2557              $open_elements->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
2558            ## Ignore the token            ## Ignore the token
2559          } else {          } else {
2560            my $body_el = $open_elements->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
2561            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
2562              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
2563                $body_el->set_attribute_ns                $body_el->set_attribute_ns
# Line 2112  sub _construct_tree ($) { Line 2575  sub _construct_tree ($) {
2575                  pre => 1,                  pre => 1,
2576                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2577          ## has a p element in scope          ## has a p element in scope
2578          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2579            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2580              !!!back-token;              !!!back-token;
2581              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2139  sub _construct_tree ($) { Line 2602  sub _construct_tree ($) {
2602          }          }
2603          return;          return;
2604        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
2605          if (defined $form_element) {          if (defined $self->{form_element}) {
2606            !!!parse-error;            !!!parse-error (type => 'in form:form');
2607            ## Ignore the token            ## Ignore the token
2608              !!!next-token;
2609              return;
2610          } else {          } else {
2611            ## has a p element in scope            ## has a p element in scope
2612            INSCOPE: for (reverse @$open_elements) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
2613              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
2614                !!!back-token;                !!!back-token;
2615                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => 'end tag', tag_name => 'p'};
# Line 2158  sub _construct_tree ($) { Line 2623  sub _construct_tree ($) {
2623            } # INSCOPE            } # INSCOPE
2624                            
2625            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2626            $form_element = $open_elements->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
2627            !!!next-token;            !!!next-token;
2628            return;            return;
2629          }          }
2630        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
2631          ## has a p element in scope          ## has a p element in scope
2632          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2633            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2634              !!!back-token;              !!!back-token;
2635              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2179  sub _construct_tree ($) { Line 2644  sub _construct_tree ($) {
2644                        
2645          ## Step 1          ## Step 1
2646          my $i = -1;          my $i = -1;
2647          my $node = $open_elements->[$i];          my $node = $self->{open_elements}->[$i];
2648          LI: {          LI: {
2649            ## Step 2            ## Step 2
2650            if ($node->[1] eq 'li') {            if ($node->[1] eq 'li') {
2651              splice @$open_elements, $i;              if ($i != -1) {
2652                  !!!parse-error (type => 'end tag missing:'.
2653                                  $self->{open_elements}->[-1]->[1]);
2654                }
2655                splice @{$self->{open_elements}}, $i;
2656              last LI;              last LI;
2657            }            }
2658                        
# Line 2198  sub _construct_tree ($) { Line 2667  sub _construct_tree ($) {
2667                        
2668            ## Step 4            ## Step 4
2669            $i--;            $i--;
2670            $node = $open_elements->[$i];            $node = $self->{open_elements}->[$i];
2671            redo LI;            redo LI;
2672          } # LI          } # LI
2673                        
# Line 2207  sub _construct_tree ($) { Line 2676  sub _construct_tree ($) {
2676          return;          return;
2677        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
2678          ## has a p element in scope          ## has a p element in scope
2679          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2680            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2681              !!!back-token;              !!!back-token;
2682              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2222  sub _construct_tree ($) { Line 2691  sub _construct_tree ($) {
2691                        
2692          ## Step 1          ## Step 1
2693          my $i = -1;          my $i = -1;
2694          my $node = $open_elements->[$i];          my $node = $self->{open_elements}->[$i];
2695          LI: {          LI: {
2696            ## Step 2            ## Step 2
2697            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
2698              splice @$open_elements, $i;              if ($i != -1) {
2699                  !!!parse-error (type => 'end tag missing:'.
2700                                  $self->{open_elements}->[-1]->[1]);
2701                }
2702                splice @{$self->{open_elements}}, $i;
2703              last LI;              last LI;
2704            }            }
2705                        
# Line 2241  sub _construct_tree ($) { Line 2714  sub _construct_tree ($) {
2714                        
2715            ## Step 4            ## Step 4
2716            $i--;            $i--;
2717            $node = $open_elements->[$i];            $node = $self->{open_elements}->[$i];
2718            redo LI;            redo LI;
2719          } # LI          } # LI
2720                        
# Line 2250  sub _construct_tree ($) { Line 2723  sub _construct_tree ($) {
2723          return;          return;
2724        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
2725          ## has a p element in scope          ## has a p element in scope
2726          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2727            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2728              !!!back-token;              !!!back-token;
2729              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2265  sub _construct_tree ($) { Line 2738  sub _construct_tree ($) {
2738                        
2739          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2740                        
2741          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
2742                        
2743          !!!next-token;          !!!next-token;
2744          return;          return;
# Line 2273  sub _construct_tree ($) { Line 2746  sub _construct_tree ($) {
2746                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
2747                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2748          ## has a p element in scope          ## has a p element in scope
2749          INSCOPE: for (reverse 0..$#$open_elements) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2750            my $node = $open_elements->[$_];            my $node = $self->{open_elements}->[$_];
2751            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
2752              !!!back-token;              !!!back-token;
2753              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2287  sub _construct_tree ($) { Line 2760  sub _construct_tree ($) {
2760            }            }
2761          } # INSCOPE          } # INSCOPE
2762                        
2763            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
2764          ## has an element in scope          ## has an element in scope
2765          my $i;          #my $i;
2766          INSCOPE: for (reverse 0..$#$open_elements) {          #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2767            my $node = $open_elements->[$_];          #  my $node = $self->{open_elements}->[$_];
2768            if ({          #  if ({
2769                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,          #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
2770                }->{$node->[1]}) {          #      }->{$node->[1]}) {
2771              $i = $_;          #    $i = $_;
2772              last INSCOPE;          #    last INSCOPE;
2773            } elsif ({          #  } elsif ({
2774                      table => 1, caption => 1, td => 1, th => 1,          #            table => 1, caption => 1, td => 1, th => 1,
2775                      button => 1, marquee => 1, object => 1, html => 1,          #            button => 1, marquee => 1, object => 1, html => 1,
2776                     }->{$node->[1]}) {          #           }->{$node->[1]}) {
2777              last INSCOPE;          #    last INSCOPE;
2778            }          #  }
2779          } # INSCOPE          #} # INSCOPE
2780                      #  
2781          if (defined $i) {          #if (defined $i) {
2782            !!!parse-error;          #  !!! parse-error (type => 'in hn:hn');
2783            splice @$open_elements, $i;          #  splice @{$self->{open_elements}}, $i;
2784          }          #}
2785                        
2786          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2787                        
# Line 2317  sub _construct_tree ($) { Line 2791  sub _construct_tree ($) {
2791          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
2792            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
2793            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
2794              !!!parse-error ('a in a');              !!!parse-error (type => 'in a:a');
2795                            
2796              !!!back-token;              !!!back-token;
2797              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => 'end tag', tag_name => 'a'};
# Line 2329  sub _construct_tree ($) { Line 2803  sub _construct_tree ($) {
2803                  last AFE2;                  last AFE2;
2804                }                }
2805              } # AFE2              } # AFE2
2806              OE: for (reverse 0..$#$open_elements) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
2807                if ($open_elements->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
2808                  splice @$open_elements, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
2809                  last OE;                  last OE;
2810                }                }
2811              } # OE              } # OE
# Line 2344  sub _construct_tree ($) { Line 2818  sub _construct_tree ($) {
2818          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2819    
2820          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2821          push @$active_formatting_elements, $open_elements->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
2822    
2823          !!!next-token;          !!!next-token;
2824          return;          return;
2825        } elsif ({        } elsif ({
2826                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
2827                  nobr => 1, s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
2828                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
2829                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2830          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2831                    
2832          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2833          push @$active_formatting_elements, $open_elements->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
2834            
2835            !!!next-token;
2836            return;
2837          } elsif ($token->{tag_name} eq 'nobr') {
2838            $reconstruct_active_formatting_elements->($insert_to_current);
2839    
2840            ## has a |nobr| element in scope
2841            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2842              my $node = $self->{open_elements}->[$_];
2843              if ($node->[1] eq 'nobr') {
2844                !!!parse-error (type => 'not closed:nobr');
2845                !!!back-token;
2846                $token = {type => 'end tag', tag_name => 'nobr'};
2847                return;
2848              } elsif ({
2849                        table => 1, caption => 1, td => 1, th => 1,
2850                        button => 1, marquee => 1, object => 1, html => 1,
2851                       }->{$node->[1]}) {
2852                last INSCOPE;
2853              }
2854            } # INSCOPE
2855            
2856            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2857            push @$active_formatting_elements, $self->{open_elements}->[-1];
2858                    
2859          !!!next-token;          !!!next-token;
2860          return;          return;
2861        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
2862          ## has a button element in scope          ## has a button element in scope
2863          INSCOPE: for (reverse 0..$#$open_elements) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2864            my $node = $open_elements->[$_];            my $node = $self->{open_elements}->[$_];
2865            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
2866              !!!parse-error;              !!!parse-error (type => 'in button:button');
2867              !!!back-token;              !!!back-token;
2868              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => 'end tag', tag_name => 'button'};
2869              return;              return;
# Line 2395  sub _construct_tree ($) { Line 2893  sub _construct_tree ($) {
2893          return;          return;
2894        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
2895          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2896                    $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!next-token;  
2897          return;          return;
2898        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
2899          ## has a p element in scope          ## has a p element in scope
2900          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2901            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2902              !!!back-token;              !!!back-token;
2903              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2419  sub _construct_tree ($) { Line 2912  sub _construct_tree ($) {
2912                        
2913          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2914                        
2915          $insertion_mode = 'in table';          $self->{insertion_mode} = 'in table';
2916                        
2917          !!!next-token;          !!!next-token;
2918          return;          return;
# Line 2429  sub _construct_tree ($) { Line 2922  sub _construct_tree ($) {
2922                  image => 1,                  image => 1,
2923                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2924          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'image') {
2925            !!!parse-error;            !!!parse-error (type => 'image');
2926            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
2927          }          }
2928            
2929            ## NOTE: There is an "as if <br>" code clone.
2930          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2931                    
2932          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2933          pop @$open_elements;          pop @{$self->{open_elements}};
2934                    
2935          !!!next-token;          !!!next-token;
2936          return;          return;
2937        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
2938          ## has a p element in scope          ## has a p element in scope
2939          INSCOPE: for (reverse @$open_elements) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
2940            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
2941              !!!back-token;              !!!back-token;
2942              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
# Line 2456  sub _construct_tree ($) { Line 2950  sub _construct_tree ($) {
2950          } # INSCOPE          } # INSCOPE
2951                        
2952          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2953          pop @$open_elements;          pop @{$self->{open_elements}};
2954                        
2955          !!!next-token;          !!!next-token;
2956          return;          return;
# Line 2464  sub _construct_tree ($) { Line 2958  sub _construct_tree ($) {
2958          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2959                    
2960          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2961          ## TODO: associate with $form_element if defined          ## TODO: associate with $self->{form_element} if defined
2962          pop @$open_elements;          pop @{$self->{open_elements}};
2963                    
2964          !!!next-token;          !!!next-token;
2965          return;          return;
2966        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
2967          !!!parse-error;          !!!parse-error (type => 'isindex');
2968                    
2969          if (defined $form_element) {          if (defined $self->{form_element}) {
2970            ## Ignore the token            ## Ignore the token
2971            !!!next-token;            !!!next-token;
2972            return;            return;
2973          } else {          } else {
2974            my $at = $token->{attributes};            my $at = $token->{attributes};
2975              my $form_attrs;
2976              $form_attrs->{action} = $at->{action} if $at->{action};
2977              my $prompt_attr = $at->{prompt};
2978            $at->{name} = {name => 'name', value => 'isindex'};            $at->{name} = {name => 'name', value => 'isindex'};
2979              delete $at->{action};
2980              delete $at->{prompt};
2981            my @tokens = (            my @tokens = (
2982                          {type => 'start tag', tag_name => 'form'},                          {type => 'start tag', tag_name => 'form',
2983                             attributes => $form_attrs},
2984                          {type => 'start tag', tag_name => 'hr'},                          {type => 'start tag', tag_name => 'hr'},
2985                          {type => 'start tag', tag_name => 'p'},                          {type => 'start tag', tag_name => 'p'},
2986                          {type => 'start tag', tag_name => 'label'},                          {type => 'start tag', tag_name => 'label'},
2987                          {type => 'character',                         );
2988                           data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD            if ($prompt_attr) {
2989                          ## TODO: make this configurable              push @tokens, {type => 'character', data => $prompt_attr->{value}};
2990              } else {
2991                push @tokens, {type => 'character',
2992                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
2993                ## TODO: make this configurable
2994              }
2995              push @tokens,
2996                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => 'start tag', tag_name => 'input', attributes => $at},
2997                          #{type => 'character', data => ''}, # SHOULD                          #{type => 'character', data => ''}, # SHOULD
2998                          {type => 'end tag', tag_name => 'label'},                          {type => 'end tag', tag_name => 'label'},
2999                          {type => 'end tag', tag_name => 'p'},                          {type => 'end tag', tag_name => 'p'},
3000                          {type => 'start tag', tag_name => 'hr'},                          {type => 'start tag', tag_name => 'hr'},
3001                          {type => 'end tag', tag_name => 'form'},                          {type => 'end tag', tag_name => 'form'};
                        );  
3002            $token = shift @tokens;            $token = shift @tokens;
3003            !!!back-token (@tokens);            !!!back-token (@tokens);
3004            return;            return;
3005          }          }
3006        } elsif ({        } elsif ($token->{tag_name} eq 'textarea') {
                 textarea => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
3007          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
3008          my $el;          my $el;
3009          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
3010                    
3011          if ($token->{tag_name} eq 'textarea') {          ## TODO: $self->{form_element} if defined
3012            ## TODO: form_element if defined          $self->{content_model} = RCDATA_CONTENT_MODEL;
3013            $self->{content_model_flag} = 'RCDATA';          delete $self->{escape}; # MUST
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
3014                    
3015          $insert->($el);          $insert->($el);
3016                    
3017          my $text = '';          my $text = '';
3018          !!!next-token;          !!!next-token;
3019            if ($token->{type} eq 'character') {
3020              $token->{data} =~ s/^\x0A//;
3021              unless (length $token->{data}) {
3022                !!!next-token;
3023              }
3024            }
3025          while ($token->{type} eq 'character') {          while ($token->{type} eq 'character') {
3026            $text .= $token->{data};            $text .= $token->{data};
3027            !!!next-token;            !!!next-token;
# Line 2527  sub _construct_tree ($) { Line 3030  sub _construct_tree ($) {
3030            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
3031          }          }
3032                    
3033          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
3034                    
3035          if ($token->{type} eq 'end tag' and          if ($token->{type} eq 'end tag' and
3036              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
3037            ## Ignore the token            ## Ignore the token
3038          } else {          } else {
3039            !!!parse-error;            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
           ## ISSUE: And ignore?  
3040          }          }
3041          !!!next-token;          !!!next-token;
3042          return;          return;
3043          } elsif ({
3044                    iframe => 1,
3045                    noembed => 1,
3046                    noframes => 1,
3047                    noscript => 0, ## TODO: 1 if scripting is enabled
3048                   }->{$token->{tag_name}}) {
3049            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
3050            return;
3051        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
3052          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
3053                    
3054          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
3055                    
3056          $insertion_mode = 'in select';          $self->{insertion_mode} = 'in select';
3057          !!!next-token;          !!!next-token;
3058          return;          return;
3059        } elsif ({        } elsif ({
# Line 2552  sub _construct_tree ($) { Line 3062  sub _construct_tree ($) {
3062                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
3063                  thead => 1, tr => 1,                  thead => 1, tr => 1,
3064                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3065          !!!parse-error;          !!!parse-error (type => 'in body:'.$token->{tag_name});
3066          ## Ignore the token          ## Ignore the token
3067          !!!next-token;          !!!next-token;
3068          return;          return;
# Line 2568  sub _construct_tree ($) { Line 3078  sub _construct_tree ($) {
3078        }        }
3079      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} eq 'end tag') {
3080        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
3081          if (@$open_elements > 1 and $open_elements->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and
3082            ## ISSUE: There is an issue in the spec.              $self->{open_elements}->[1]->[1] eq 'body') {
3083            if ($open_elements->[-1]->[1] ne 'body') {            for (@{$self->{open_elements}}) {
3084              !!!parse-error;              unless ({
3085                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
3086                           th => 1, tr => 1, body => 1, html => 1,
3087                         tbody => 1, tfoot => 1, thead => 1,
3088                        }->{$_->[1]}) {
3089                  !!!parse-error (type => 'not closed:'.$_->[1]);
3090                }
3091            }            }
3092            $insertion_mode = 'after body';  
3093              $self->{insertion_mode} = 'after body';
3094            !!!next-token;            !!!next-token;
3095            return;            return;
3096          } else {          } else {
3097            !!!parse-error;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3098            ## Ignore the token            ## Ignore the token
3099            !!!next-token;            !!!next-token;
3100            return;            return;
3101          }          }
3102        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
3103          if (@$open_elements > 1 and $open_elements->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
3104            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
3105            if ($open_elements->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
3106              !!!parse-error;              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
3107            }            }
3108            $insertion_mode = 'after body';            $self->{insertion_mode} = 'after body';
3109            ## reprocess            ## reprocess
3110            return;            return;
3111          } else {          } else {
3112            !!!parse-error;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3113            ## Ignore the token            ## Ignore the token
3114            !!!next-token;            !!!next-token;
3115            return;            return;
# Line 2601  sub _construct_tree ($) { Line 3118  sub _construct_tree ($) {
3118                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
3119                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
3120                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 form => 1,  
3121                  p => 1,                  p => 1,
3122                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
3123                  button => 1, marquee => 1, object => 1,                  button => 1, marquee => 1, object => 1,
3124                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3125          ## has an element in scope          ## has an element in scope
3126          my $i;          my $i;
3127          INSCOPE: for (reverse 0..$#$open_elements) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3128            my $node = $open_elements->[$_];            my $node = $self->{open_elements}->[$_];
3129            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
3130              ## generate implied end tags              ## generate implied end tags
3131              if ({              if ({
# Line 2618  sub _construct_tree ($) { Line 3134  sub _construct_tree ($) {
3134                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
3135                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
3136                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3137                  }->{$open_elements->[-1]->[1]}) {                   tbody => 1, tfoot=> 1, thead => 1,
3138                    }->{$self->{open_elements}->[-1]->[1]}) {
3139                !!!back-token;                !!!back-token;
3140                $token = {type => 'end tag',                $token = {type => 'end tag',
3141                          tag_name => $open_elements->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3142                return;                return;
3143              }              }
3144              $i = $_;              $i = $_;
# Line 2634  sub _construct_tree ($) { Line 3151  sub _construct_tree ($) {
3151            }            }
3152          } # INSCOPE          } # INSCOPE
3153                    
3154          if ($open_elements->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3155            !!!parse-error;            if (defined $i) {
3156                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3157              } else {
3158                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3159              }
3160          }          }
3161                    
3162          splice @$open_elements, $i if defined $i;          if (defined $i) {
3163          undef $form_element if $token->{tag_name} eq 'form';            splice @{$self->{open_elements}}, $i;
3164            } elsif ($token->{tag_name} eq 'p') {
3165              ## As if <p>, then reprocess the current token
3166              my $el;
3167              !!!create-element ($el, 'p');
3168              $insert->($el);
3169            }
3170          $clear_up_to_marker->()          $clear_up_to_marker->()
3171            if {            if {
3172              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
3173            }->{$token->{tag_name}};            }->{$token->{tag_name}};
3174          !!!next-token;          !!!next-token;
3175          return;          return;
3176          } elsif ($token->{tag_name} eq 'form') {
3177            ## has an element in scope
3178            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3179              my $node = $self->{open_elements}->[$_];
3180              if ($node->[1] eq $token->{tag_name}) {
3181                ## generate implied end tags
3182                if ({
3183                     dd => 1, dt => 1, li => 1, p => 1,
3184                     td => 1, th => 1, tr => 1,
3185                     tbody => 1, tfoot=> 1, thead => 1,
3186                    }->{$self->{open_elements}->[-1]->[1]}) {
3187                  !!!back-token;
3188                  $token = {type => 'end tag',
3189                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3190                  return;
3191                }
3192                last INSCOPE;
3193              } elsif ({
3194                        table => 1, caption => 1, td => 1, th => 1,
3195                        button => 1, marquee => 1, object => 1, html => 1,
3196                       }->{$node->[1]}) {
3197                last INSCOPE;
3198              }
3199            } # INSCOPE
3200            
3201            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
3202              pop @{$self->{open_elements}};
3203            } else {
3204              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3205            }
3206    
3207            undef $self->{form_element};
3208            !!!next-token;
3209            return;
3210        } elsif ({        } elsif ({
3211                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
3212                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3213          ## has an element in scope          ## has an element in scope
3214          my $i;          my $i;
3215          INSCOPE: for (reverse 0..$#$open_elements) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3216            my $node = $open_elements->[$_];            my $node = $self->{open_elements}->[$_];
3217            if ({            if ({
3218                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
3219                }->{$node->[1]}) {                }->{$node->[1]}) {
# Line 2660  sub _construct_tree ($) { Line 3221  sub _construct_tree ($) {
3221              if ({              if ({
3222                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3223                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3224                  }->{$open_elements->[-1]->[1]}) {                   tbody => 1, tfoot=> 1, thead => 1,
3225                    }->{$self->{open_elements}->[-1]->[1]}) {
3226                !!!back-token;                !!!back-token;
3227                $token = {type => 'end tag',                $token = {type => 'end tag',
3228                          tag_name => $open_elements->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3229                return;                return;
3230              }              }
3231              $i = $_;              $i = $_;
# Line 2676  sub _construct_tree ($) { Line 3238  sub _construct_tree ($) {
3238            }            }
3239          } # INSCOPE          } # INSCOPE
3240                    
3241          if ($open_elements->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3242            !!!parse-error;            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3243          }          }
3244                    
3245          splice @$open_elements, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
3246          !!!next-token;          !!!next-token;
3247          return;          return;
3248        } elsif ({        } elsif ({
# Line 2691  sub _construct_tree ($) { Line 3253  sub _construct_tree ($) {
3253                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3254          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
3255          return;          return;
3256          } elsif ($token->{tag_name} eq 'br') {
3257            !!!parse-error (type => 'unmatched end tag:br');
3258    
3259            ## As if <br>
3260            $reconstruct_active_formatting_elements->($insert_to_current);
3261            
3262            my $el;
3263            !!!create-element ($el, 'br');
3264            $insert->($el);
3265            
3266            ## Ignore the token.
3267            !!!next-token;
3268            return;
3269        } elsif ({        } elsif ({
3270                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
3271                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
3272                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
3273                  thead => 1, tr => 1,                  thead => 1, tr => 1,
3274                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
3275                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
3276                  img => 1, input => 1, isindex=> 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
3277                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
3278                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
3279                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
3280                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3281          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3282          ## Ignore the token          ## Ignore the token
3283          !!!next-token;          !!!next-token;
3284          return;          return;
# Line 2713  sub _construct_tree ($) { Line 3288  sub _construct_tree ($) {
3288        } else {        } else {
3289          ## Step 1          ## Step 1
3290          my $node_i = -1;          my $node_i = -1;
3291          my $node = $open_elements->[$node_i];          my $node = $self->{open_elements}->[$node_i];
3292    
3293          ## Step 2          ## Step 2
3294          S2: {          S2: {
# Line 2723  sub _construct_tree ($) { Line 3298  sub _construct_tree ($) {
3298              if ({              if ({
3299                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3300                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3301                  }->{$open_elements->[-1]->[1]}) {                   tbody => 1, tfoot=> 1, thead => 1,
3302                    }->{$self->{open_elements}->[-1]->[1]}) {
3303                !!!back-token;                !!!back-token;
3304                $token = {type => 'end tag',                $token = {type => 'end tag',
3305                          tag_name => $open_elements->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3306                return;                return;
3307              }              }
3308                    
3309              ## Step 2              ## Step 2
3310              if ($token->{tag_name} ne $open_elements->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
3311                !!!parse-error;                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3312              }              }
3313                            
3314              ## Step 3              ## Step 3
3315              splice @$open_elements, $node_i;              splice @{$self->{open_elements}}, $node_i;
3316    
3317                !!!next-token;
3318              last S2;              last S2;
3319            } else {            } else {
3320              ## Step 3              ## Step 3
# Line 2744  sub _construct_tree ($) { Line 3322  sub _construct_tree ($) {
3322                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
3323                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
3324                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
3325                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3326                ## Ignore the token                ## Ignore the token
3327                !!!next-token;                !!!next-token;
3328                last S2;                last S2;
# Line 2753  sub _construct_tree ($) { Line 3331  sub _construct_tree ($) {
3331                        
3332            ## Step 4            ## Step 4
3333            $node_i--;            $node_i--;
3334            $node = $open_elements->[$node_i];            $node = $self->{open_elements}->[$node_i];
3335                        
3336            ## Step 5;            ## Step 5;
3337            redo S2;            redo S2;
3338          } # S2          } # S2
3339            return;
3340        }        }
3341      }      }
3342    }; # $in_body    }; # $in_body
3343    
3344    B: {    B: {
3345      if ($phase eq 'initial') {      if ($token->{type} eq 'DOCTYPE') {
3346        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
3347          if ($token->{error}) {        ## Ignore the token
3348            ## ISSUE: Spec currently left this case undefined.        ## Stay in the phase
3349            !!!parse-error ('bogus DOCTYPE');        !!!next-token;
         }  
         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') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error;  
         ## 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  
3350        redo B;        redo B;
3351      } elsif ($phase eq 'main') {      } elsif ($token->{type} eq 'end-of-file') {
3352        if ($token->{type} eq 'DOCTYPE') {        if ($token->{insertion_mode} ne 'trailing end') {
         !!!parse-error;  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         my $top_el = $open_elements->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
3353          ## Generate implied end tags          ## Generate implied end tags
3354          if ({          if ({
3355               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,
3356              }->{$open_elements->[-1]->[1]}) {               tbody => 1, tfoot=> 1, thead => 1,
3357                }->{$self->{open_elements}->[-1]->[1]}) {
3358            !!!back-token;            !!!back-token;
3359            $token = {type => 'end tag', tag_name => $open_elements->[-1]->[1]};            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
3360            redo B;            redo B;
3361          }          }
3362                    
3363          if (@$open_elements > 2 or          if (@{$self->{open_elements}} > 2 or
3364              (@$open_elements == 2 and $open_elements->[1]->[1] ne 'body')) {              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
3365            !!!parse-error;            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3366          } else {          } elsif (defined $self->{inner_html_node} and
3367            ## TODO: inner_html parser and @$open_elements > 1 and $open_elements->[1] ne 'body', then parse-error                   @{$self->{open_elements}} > 1 and
3368                     $self->{open_elements}->[1]->[1] ne 'body') {
3369              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3370          }          }
3371    
         ## Stop parsing  
         last B;  
   
3372          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
3373          }
3374    
3375          ## Stop parsing
3376          last B;
3377        } elsif ($token->{type} eq 'start tag' and
3378                 $token->{tag_name} eq 'html') {
3379          if ($self->{insertion_mode} eq 'trailing end') {
3380            ## Turn into the main phase
3381            !!!parse-error (type => 'after html:html');
3382            $self->{insertion_mode} = $previous_insertion_mode;
3383          }
3384    
3385    ## ISSUE: "aa<html>" is not a parse error.
3386    ## ISSUE: "<html>" in fragment is not a parse error.
3387          unless ($token->{first_start_tag}) {
3388            !!!parse-error (type => 'not first start tag');
3389          }
3390          my $top_el = $self->{open_elements}->[0]->[0];
3391          for my $attr_name (keys %{$token->{attributes}}) {
3392            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3393              $top_el->set_attribute_ns
3394                (undef, [undef, $attr_name],
3395                 $token->{attributes}->{$attr_name}->{value});
3396            }
3397          }
3398          !!!next-token;
3399          redo B;
3400        } elsif ($token->{type} eq 'comment') {
3401          my $comment = $self->{document}->create_comment ($token->{data});
3402          if ($self->{insertion_mode} eq 'trailing end') {
3403            $self->{document}->append_child ($comment);
3404          } elsif ($self->{insertion_mode} eq 'after body') {
3405            $self->{open_elements}->[0]->[0]->append_child ($comment);
3406        } else {        } else {
3407          if ($insertion_mode eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3408          }
3409          !!!next-token;
3410          redo B;
3411        } elsif ($self->{insertion_mode} eq 'before head') {
3412            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3413              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3414                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3415                unless (length $token->{data}) {                unless (length $token->{data}) {
3416                  !!!next-token;                  !!!next-token;
3417                  redo B;                  redo B;
3418                }                }
3419              }              }
3420              ## As if <head>              ## As if <head>
3421              !!!create-element ($head_element, 'head');              !!!create-element ($self->{head_element}, 'head');
3422              $open_elements->[-1]->[0]->append_child ($head_element);              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3423              push @$open_elements, [$head_element, 'head'];              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3424              $insertion_mode = 'in head';              $self->{insertion_mode} = 'in head';
3425              ## reprocess              ## reprocess
3426              redo B;              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;  
3427            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3428              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};
3429              !!!create-element ($head_element, 'head', $attr);              !!!create-element ($self->{head_element}, 'head', $attr);
3430              $open_elements->[-1]->[0]->append_child ($head_element);              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3431              push @$open_elements, [$head_element, 'head'];              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3432              $insertion_mode = 'in head';              $self->{insertion_mode} = 'in head';
3433              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3434                !!!next-token;                !!!next-token;
3435              #} elsif ({              #} elsif ({
# Line 2925  sub _construct_tree ($) { Line 3442  sub _construct_tree ($) {
3442              }              }
3443              redo B;              redo B;
3444            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3445              if ($token->{tag_name} eq 'html') {              if ({
3446                     head => 1, body => 1, html => 1,
3447                     p => 1, br => 1,
3448                    }->{$token->{tag_name}}) {
3449                ## As if <head>                ## As if <head>
3450                !!!create-element ($head_element, 'head');                !!!create-element ($self->{head_element}, 'head');
3451                $open_elements->[-1]->[0]->append_child ($head_element);                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3452                push @$open_elements, [$head_element, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3453                $insertion_mode = 'in head';                $self->{insertion_mode} = 'in head';
3454                ## reprocess                ## reprocess
3455                redo B;                redo B;
3456              } else {              } else {
3457                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3458                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3459                !!!next-token;                !!!next-token;
3460                redo B;                redo B;
3461              }              }
3462            } else {            } else {
3463              die "$0: $token->{type}: Unknown type";              die "$0: $token->{type}: Unknown type";
3464            }            }
3465          } elsif ($insertion_mode eq 'in head') {          } elsif ($self->{insertion_mode} eq 'in head' or
3466                     $self->{insertion_mode} eq 'in head noscript' or
3467                     $self->{insertion_mode} eq 'after head') {
3468            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3469              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3470                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3471                unless (length $token->{data}) {                unless (length $token->{data}) {
3472                  !!!next-token;                  !!!next-token;
3473                  redo B;                  redo B;
# Line 2953  sub _construct_tree ($) { Line 3475  sub _construct_tree ($) {
3475              }              }
3476                            
3477              #              #
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
3478            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3479              if ($token->{tag_name} eq 'title') {              if ({base => ($self->{insertion_mode} eq 'in head' or
3480                ## NOTE: There is an "as if in head" code clone                            $self->{insertion_mode} eq 'after head'),
3481                my $title_el;                   link => 1}->{$token->{tag_name}}) {
3482                !!!create-element ($title_el, 'title', $token->{attributes});                ## NOTE: There is a "as if in head" code clone.
3483                (defined $head_element ? $head_element : $open_elements->[-1]->[0])                if ($self->{insertion_mode} eq 'after head') {
3484                  ->append_child ($title_el);                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3485                $self->{content_model_flag} = 'RCDATA';                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3486                  }
3487                my $text = '';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3488                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3489                  pop @{$self->{open_elements}}
3490                      if $self->{insertion_mode} eq 'after head';
3491                !!!next-token;                !!!next-token;
3492                while ($token->{type} eq 'character') {                redo B;
3493                  $text .= $token->{data};              } elsif ($token->{tag_name} eq 'meta') {
3494                  !!!next-token;                ## NOTE: There is a "as if in head" code clone.
3495                  if ($self->{insertion_mode} eq 'after head') {
3496                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3497                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3498                }                }
3499                if (length $text) {                !!!insert-element ($token->{tag_name}, $token->{attributes});
3500                  $title_el->manakai_append_text ($text);                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3501    
3502                  unless ($self->{confident}) {
3503                    my $charset;
3504                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
3505                      $charset = $token->{attributes}->{charset}->{value};
3506                    }
3507                    if ($token->{attributes}->{'http-equiv'}) {
3508                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3509                      if ($token->{attributes}->{'http-equiv'}->{value}
3510                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
3511                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3512                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3513                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
3514                      } ## TODO: And if supported
3515                    }
3516                    ## TODO: Change the encoding
3517                }                }
3518                  
3519                $self->{content_model_flag} = 'PCDATA';                ## TODO: Extracting |charset| from |meta|.
3520                                pop @{$self->{open_elements}}
3521                if ($token->{type} eq 'end tag' and                    if $self->{insertion_mode} eq 'after head';
3522                    $token->{tag_name} eq 'title') {                !!!next-token;
3523                  redo B;
3524                } elsif ($token->{tag_name} eq 'title' and
3525                         $self->{insertion_mode} eq 'in head') {
3526                  ## NOTE: There is a "as if in head" code clone.
3527                  if ($self->{insertion_mode} eq 'after head') {
3528                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3529                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3530                  }
3531                  my $parent = defined $self->{head_element} ? $self->{head_element}
3532                      : $self->{open_elements}->[-1]->[0];
3533                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
3534                                  sub { $parent->append_child ($_[0]) });
3535                  pop @{$self->{open_elements}}
3536                      if $self->{insertion_mode} eq 'after head';
3537                  redo B;
3538                } elsif ($token->{tag_name} eq 'style') {
3539                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3540                  ## insertion mode 'in head')
3541                  ## NOTE: There is a "as if in head" code clone.
3542                  if ($self->{insertion_mode} eq 'after head') {
3543                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3544                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3545                  }
3546                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3547                  pop @{$self->{open_elements}}
3548                      if $self->{insertion_mode} eq 'after head';
3549                  redo B;
3550                } elsif ($token->{tag_name} eq 'noscript') {
3551                  if ($self->{insertion_mode} eq 'in head') {
3552                    ## NOTE: and scripting is disalbed
3553                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3554                    $self->{insertion_mode} = 'in head noscript';
3555                    !!!next-token;
3556                    redo B;
3557                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
3558                    !!!parse-error (type => 'in noscript:noscript');
3559                  ## Ignore the token                  ## Ignore the token
3560                    !!!next-token;
3561                    redo B;
3562                } else {                } else {
3563                  !!!parse-error;                  #
                 ## ISSUE: And ignore?  
3564                }                }
3565                } elsif ($token->{tag_name} eq 'head' and
3566                         $self->{insertion_mode} ne 'after head') {
3567                  !!!parse-error (type => 'in head:head'); # or in head noscript
3568                  ## Ignore the token
3569                !!!next-token;                !!!next-token;
3570                redo B;                redo B;
3571              } elsif ($token->{tag_name} eq 'style') {              } elsif ($self->{insertion_mode} ne 'in head noscript' and
3572                $style_start_tag->();                       $token->{tag_name} eq 'script') {
3573                redo B;                if ($self->{insertion_mode} eq 'after head') {
3574              } elsif ($token->{tag_name} eq 'script') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3575                $script_start_tag->();                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3576                  }
3577                  ## NOTE: There is a "as if in head" code clone.
3578                  $script_start_tag->($insert_to_current);
3579                  pop @{$self->{open_elements}}
3580                      if $self->{insertion_mode} eq 'after head';
3581                redo B;                redo B;
3582              } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {              } elsif ($self->{insertion_mode} eq 'after head' and
3583                ## NOTE: There are "as if in head" code clones                       $token->{tag_name} eq 'body') {
3584                my $el;                !!!insert-element ('body', $token->{attributes});
3585                !!!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);  
   
3586                !!!next-token;                !!!next-token;
3587                redo B;                redo B;
3588              } elsif ($token->{tag_name} eq 'head') {              } elsif ($self->{insertion_mode} eq 'after head' and
3589                !!!parse-error;                       $token->{tag_name} eq 'frameset') {
3590                ## Ignore the token                !!!insert-element ('frameset', $token->{attributes});
3591                  $self->{insertion_mode} = 'in frameset';
3592                !!!next-token;                !!!next-token;
3593                redo B;                redo B;
3594              } else {              } else {
3595                #                #
3596              }              }
3597            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3598              if ($token->{tag_name} eq 'head') {              if ($self->{insertion_mode} eq 'in head' and
3599                if ($open_elements->[-1]->[1] eq 'head') {                  $token->{tag_name} eq 'head') {
3600                  pop @$open_elements;                pop @{$self->{open_elements}};
3601                } else {                $self->{insertion_mode} = 'after head';
                 !!!parse-error;  
               }  
               $insertion_mode = 'after head';  
3602                !!!next-token;                !!!next-token;
3603                redo B;                redo B;
3604              } elsif ($token->{tag_name} eq 'html') {              } elsif ($self->{insertion_mode} eq 'in head noscript' and
3605                    $token->{tag_name} eq 'noscript') {
3606                  pop @{$self->{open_elements}};
3607                  $self->{insertion_mode} = 'in head';
3608                  !!!next-token;
3609                  redo B;
3610                } elsif ($self->{insertion_mode} eq 'in head' and
3611                         {
3612                          body => 1, html => 1,
3613                          p => 1, br => 1,
3614                         }->{$token->{tag_name}}) {
3615                #                #
3616              } else {              } elsif ($self->{insertion_mode} eq 'in head noscript' and
3617                !!!parse-error;                       {
3618                          p => 1, br => 1,
3619                         }->{$token->{tag_name}}) {
3620                  #
3621                } elsif ($self->{insertion_mode} ne 'after head') {
3622                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3623                ## Ignore the token                ## Ignore the token
3624                !!!next-token;                !!!next-token;
3625                redo B;                redo B;
3626                } else {
3627                  #
3628              }              }
3629            } else {            } else {
3630              #              #
3631            }            }
3632    
3633            if ($open_elements->[-1]->[1] eq 'head') {            ## As if </head> or </noscript> or <body>
3634              ## As if </head>            if ($self->{insertion_mode} eq 'in head') {
3635              pop @$open_elements;              pop @{$self->{open_elements}};
3636                $self->{insertion_mode} = 'after head';
3637              } elsif ($self->{insertion_mode} eq 'in head noscript') {
3638                pop @{$self->{open_elements}};
3639                !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));
3640                $self->{insertion_mode} = 'in head';
3641              } else { # 'after head'
3642                !!!insert-element ('body');
3643                $self->{insertion_mode} = 'in body';
3644            }            }
           $insertion_mode = 'after head';  
3645            ## reprocess            ## reprocess
3646            redo B;            redo B;
3647    
3648            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3649          } elsif ($insertion_mode eq 'after head') {          } elsif ($self->{insertion_mode} eq 'in body' or
3650            if ($token->{type} eq 'character') {                   $self->{insertion_mode} eq 'in caption') {
             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') {  
3651            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3652              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3653              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3654                            
3655              $open_elements->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($insertion_mode eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $open_elements->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
3656    
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$open_elements->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#$open_elements) {  
                 if ($open_elements->[$_]->[1] eq 'table') {  
                   my $parent = $open_elements->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $open_elements->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $open_elements->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $open_elements->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $open_elements->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
3657              !!!next-token;              !!!next-token;
3658              redo B;              redo B;
3659            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3660              if ({              if ({
3661                   caption => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3662                   colgroup => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3663                   tbody => 1, tfoot => 1, thead => 1,                  }->{$token->{tag_name}} and
3664                  }->{$token->{tag_name}}) {                  $self->{insertion_mode} eq 'in caption') {
3665                ## Clear back to table context                !!!parse-error (type => 'not closed:caption');
               while ($open_elements->[-1]->[1] ne 'table' and  
                      $open_elements->[-1]->[1] ne 'html') {  
                 !!!parse-error;  
                 pop @$open_elements;  
               }  
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $insertion_mode = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($open_elements->[-1]->[1] ne 'table' and  
                      $open_elements->[-1]->[1] ne 'html') {  
                 !!!parse-error;  
                 pop @$open_elements;  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $insertion_mode = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error;  
3666    
3667                ## As if </table>                ## As if </caption>
3668                ## have a table element in table scope                ## have a table element in table scope
3669                my $i;                my $i;
3670                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3671                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3672                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'caption') {
3673                    $i = $_;                    $i = $_;
3674                    last INSCOPE;                    last INSCOPE;
3675                  } elsif ({                  } elsif ({
# Line 3231  sub _construct_tree ($) { Line 3679  sub _construct_tree ($) {
3679                  }                  }
3680                } # INSCOPE                } # INSCOPE
3681                unless (defined $i) {                unless (defined $i) {
3682                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:caption');
3683                  ## Ignore tokens </table><table>                  ## Ignore the token
3684                  !!!next-token;                  !!!next-token;
3685                  redo B;                  redo B;
3686                }                }
# Line 3241  sub _construct_tree ($) { Line 3689  sub _construct_tree ($) {
3689                if ({                if ({
3690                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3691                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3692                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
3693                  !!!back-token; # <table>                    }->{$self->{open_elements}->[-1]->[1]}) {
3694                  $token = {type => 'end tag', tag_name => 'table'};                  !!!back-token; # <?>
3695                    $token = {type => 'end tag', tag_name => 'caption'};
3696                  !!!back-token;                  !!!back-token;
3697                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3698                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3699                  redo B;                  redo B;
3700                }                }
3701    
3702                if ($open_elements->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3703                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3704                }                }
3705    
3706                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3707    
3708                $reset_insertion_mode->();                              $clear_up_to_marker->();
3709    
3710                  $self->{insertion_mode} = 'in table';
3711    
3712                ## reprocess                ## reprocess
3713                redo B;                redo B;
# Line 3264  sub _construct_tree ($) { Line 3715  sub _construct_tree ($) {
3715                #                #
3716              }              }
3717            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3718              if ($token->{tag_name} eq 'table') {              if ($token->{tag_name} eq 'caption' and
3719                    $self->{insertion_mode} eq 'in caption') {
3720                ## have a table element in table scope                ## have a table element in table scope
3721                my $i;                my $i;
3722                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3723                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3724                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3725                    $i = $_;                    $i = $_;
3726                    last INSCOPE;                    last INSCOPE;
# Line 3279  sub _construct_tree ($) { Line 3731  sub _construct_tree ($) {
3731                  }                  }
3732                } # INSCOPE                } # INSCOPE
3733                unless (defined $i) {                unless (defined $i) {
3734                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3735                  ## Ignore the token                  ## Ignore the token
3736                  !!!next-token;                  !!!next-token;
3737                  redo B;                  redo B;
# Line 3289  sub _construct_tree ($) { Line 3741  sub _construct_tree ($) {
3741                if ({                if ({
3742                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3743                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3744                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
3745                      }->{$self->{open_elements}->[-1]->[1]}) {
3746                  !!!back-token;                  !!!back-token;
3747                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3748                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3749                  redo B;                  redo B;
3750                }                }
3751    
3752                if ($open_elements->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3753                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3754                }                }
3755    
3756                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3757    
3758                $reset_insertion_mode->();                $clear_up_to_marker->();
3759    
3760                  $self->{insertion_mode} = 'in table';
3761    
3762                !!!next-token;                !!!next-token;
3763                redo B;                redo B;
3764              } elsif ({              } elsif ($token->{tag_name} eq 'table' and
3765                        body => 1, caption => 1, col => 1, colgroup => 1,                       $self->{insertion_mode} eq 'in caption') {
3766                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'not closed:caption');
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error;  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error;  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($insertion_mode eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $open_elements->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error;  
3767    
3768                ## As if </caption>                ## As if </caption>
3769                ## have a table element in table scope                ## have a table element in table scope
3770                my $i;                my $i;
3771                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3772                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3773                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
3774                    $i = $_;                    $i = $_;
3775                    last INSCOPE;                    last INSCOPE;
# Line 3362  sub _construct_tree ($) { Line 3780  sub _construct_tree ($) {
3780                  }                  }
3781                } # INSCOPE                } # INSCOPE
3782                unless (defined $i) {                unless (defined $i) {
3783                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:caption');
3784                  ## Ignore the token                  ## Ignore the token
3785                  !!!next-token;                  !!!next-token;
3786                  redo B;                  redo B;
# Line 3372  sub _construct_tree ($) { Line 3790  sub _construct_tree ($) {
3790                if ({                if ({
3791                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3792                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3793                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
3794                  !!!back-token; # <?>                    }->{$self->{open_elements}->[-1]->[1]}) {
3795                    !!!back-token; # </table>
3796                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
3797                  !!!back-token;                  !!!back-token;
3798                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3799                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3800                  redo B;                  redo B;
3801                }                }
3802    
3803                if ($open_elements->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3804                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3805                }                }
3806    
3807                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3808    
3809                $clear_up_to_marker->();                $clear_up_to_marker->();
3810    
3811                $insertion_mode = 'in table';                $self->{insertion_mode} = 'in table';
3812    
3813                ## reprocess                ## reprocess
3814                redo B;                redo B;
3815                } elsif ({
3816                          body => 1, col => 1, colgroup => 1,
3817                          html => 1, tbody => 1, td => 1, tfoot => 1,
3818                          th => 1, thead => 1, tr => 1,
3819                         }->{$token->{tag_name}} and
3820                         $self->{insertion_mode} eq 'in caption') {
3821                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3822                  ## Ignore the token
3823                  !!!next-token;
3824                  redo B;
3825              } else {              } else {
3826                #                #
3827              }              }
3828            } elsif ($token->{type} eq 'end tag') {            } else {
3829              if ($token->{tag_name} eq 'caption') {              #
3830              }
3831    
3832              $in_body->($insert_to_current);
3833              redo B;
3834            } elsif ($self->{insertion_mode} eq 'in table') {
3835              if ($token->{type} eq 'character') {
3836                ## NOTE: There are "character in table" code clones.
3837                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3838                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3839                  
3840                  unless (length $token->{data}) {
3841                    !!!next-token;
3842                    redo B;
3843                  }
3844                }
3845    
3846                !!!parse-error (type => 'in table:#character');
3847    
3848                ## As if in body, but insert into foster parent element
3849                ## ISSUE: Spec says that "whenever a node would be inserted
3850                ## into the current node" while characters might not be
3851                ## result in a new Text node.
3852                $reconstruct_active_formatting_elements->($insert_to_foster);
3853                
3854                if ({
3855                     table => 1, tbody => 1, tfoot => 1,
3856                     thead => 1, tr => 1,
3857                    }->{$self->{open_elements}->[-1]->[1]}) {
3858                  # MUST
3859                  my $foster_parent_element;
3860                  my $next_sibling;
3861                  my $prev_sibling;
3862                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3863                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3864                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3865                      if (defined $parent and $parent->node_type == 1) {
3866                        $foster_parent_element = $parent;
3867                        $next_sibling = $self->{open_elements}->[$_]->[0];
3868                        $prev_sibling = $next_sibling->previous_sibling;
3869                      } else {
3870                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3871                        $prev_sibling = $foster_parent_element->last_child;
3872                      }
3873                      last OE;
3874                    }
3875                  } # OE
3876                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3877                  $prev_sibling = $foster_parent_element->last_child
3878                    unless defined $foster_parent_element;
3879                  if (defined $prev_sibling and
3880                      $prev_sibling->node_type == 3) {
3881                    $prev_sibling->manakai_append_text ($token->{data});
3882                  } else {
3883                    $foster_parent_element->insert_before
3884                      ($self->{document}->create_text_node ($token->{data}),
3885                       $next_sibling);
3886                  }
3887                } else {
3888                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3889                }
3890                
3891                !!!next-token;
3892                redo B;
3893              } elsif ($token->{type} eq 'start tag') {
3894                if ({
3895                     caption => 1,
3896                     colgroup => 1,
3897                     tbody => 1, tfoot => 1, thead => 1,
3898                    }->{$token->{tag_name}}) {
3899                  ## Clear back to table context
3900                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3901                         $self->{open_elements}->[-1]->[1] ne 'html') {
3902                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3903                    pop @{$self->{open_elements}};
3904                  }
3905    
3906                  push @$active_formatting_elements, ['#marker', '']
3907                    if $token->{tag_name} eq 'caption';
3908    
3909                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3910                  $self->{insertion_mode} = {
3911                                     caption => 'in caption',
3912                                     colgroup => 'in column group',
3913                                     tbody => 'in table body',
3914                                     tfoot => 'in table body',
3915                                     thead => 'in table body',
3916                                    }->{$token->{tag_name}};
3917                  !!!next-token;
3918                  redo B;
3919                } elsif ({
3920                          col => 1,
3921                          td => 1, th => 1, tr => 1,
3922                         }->{$token->{tag_name}}) {
3923                  ## Clear back to table context
3924                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3925                         $self->{open_elements}->[-1]->[1] ne 'html') {
3926                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3927                    pop @{$self->{open_elements}};
3928                  }
3929    
3930                  !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');
3931                  $self->{insertion_mode} = $token->{tag_name} eq 'col'
3932                    ? 'in column group' : 'in table body';
3933                  ## reprocess
3934                  redo B;
3935                } elsif ($token->{tag_name} eq 'table') {
3936                  ## NOTE: There are code clones for this "table in table"
3937                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3938    
3939                  ## As if </table>
3940                ## have a table element in table scope                ## have a table element in table scope
3941                my $i;                my $i;
3942                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3943                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3944                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'table') {
3945                    $i = $_;                    $i = $_;
3946                    last INSCOPE;                    last INSCOPE;
3947                  } elsif ({                  } elsif ({
# Line 3412  sub _construct_tree ($) { Line 3951  sub _construct_tree ($) {
3951                  }                  }
3952                } # INSCOPE                } # INSCOPE
3953                unless (defined $i) {                unless (defined $i) {
3954                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:table');
3955                  ## Ignore the token                  ## Ignore tokens </table><table>
3956                  !!!next-token;                  !!!next-token;
3957                  redo B;                  redo B;
3958                }                }
# Line 3422  sub _construct_tree ($) { Line 3961  sub _construct_tree ($) {
3961                if ({                if ({
3962                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3963                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3964                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
3965                      }->{$self->{open_elements}->[-1]->[1]}) {
3966                    !!!back-token; # <table>
3967                    $token = {type => 'end tag', tag_name => 'table'};
3968                  !!!back-token;                  !!!back-token;
3969                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3970                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3971                  redo B;                  redo B;
3972                }                }
3973    
3974                if ($open_elements->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3975                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3976                }                }
3977    
3978                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
   
               $clear_up_to_marker->();  
3979    
3980                $insertion_mode = 'in table';                $self->_reset_insertion_mode;
3981    
3982                !!!next-token;                ## reprocess
3983                redo B;                redo B;
3984              } elsif ($token->{tag_name} eq 'table') {              } else {
3985                !!!parse-error;                #
3986                }
3987                ## As if </caption>            } elsif ($token->{type} eq 'end tag') {
3988                if ($token->{tag_name} eq 'table') {
3989                ## have a table element in table scope                ## have a table element in table scope
3990                my $i;                my $i;
3991                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3992                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3993                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq $token->{tag_name}) {
3994                    $i = $_;                    $i = $_;
3995                    last INSCOPE;                    last INSCOPE;
3996                  } elsif ({                  } elsif ({
# Line 3459  sub _construct_tree ($) { Line 4000  sub _construct_tree ($) {
4000                  }                  }
4001                } # INSCOPE                } # INSCOPE
4002                unless (defined $i) {                unless (defined $i) {
4003                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4004                  ## Ignore the token                  ## Ignore the token
4005                  !!!next-token;                  !!!next-token;
4006                  redo B;                  redo B;
# Line 3469  sub _construct_tree ($) { Line 4010  sub _construct_tree ($) {
4010                if ({                if ({
4011                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4012                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4013                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
4014                  !!!back-token; # </table>                    }->{$self->{open_elements}->[-1]->[1]}) {
                 $token = {type => 'end tag', tag_name => 'caption'};  
4015                  !!!back-token;                  !!!back-token;
4016                  $token = {type => 'end tag',                  $token = {type => 'end tag',
4017                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4018                  redo B;                  redo B;
4019                }                }
4020    
4021                if ($open_elements->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4022                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4023                }                }
4024    
4025                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
   
               $clear_up_to_marker->();  
4026    
4027                $insertion_mode = 'in table';                $self->_reset_insertion_mode;
4028    
4029                ## reprocess                !!!next-token;
4030                redo B;                redo B;
4031              } elsif ({              } elsif ({
4032                        body => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4033                        html => 1, tbody => 1, td => 1, tfoot => 1,                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,
4034                        th => 1, thead => 1, tr => 1,                        thead => 1, tr => 1,
4035                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4036                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4037                ## Ignore the token                ## Ignore the token
4038                  !!!next-token;
4039                redo B;                redo B;
4040              } else {              } else {
4041                #                #
# Line 3504  sub _construct_tree ($) { Line 4043  sub _construct_tree ($) {
4043            } else {            } else {
4044              #              #
4045            }            }
4046                  
4047            $in_body->($insert_to_current);            !!!parse-error (type => 'in table:'.$token->{tag_name});
4048              $in_body->($insert_to_foster);
4049            redo B;            redo B;
4050          } elsif ($insertion_mode eq 'in column group') {          } elsif ($self->{insertion_mode} eq 'in column group') {
4051            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4052              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4053                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4054                unless (length $token->{data}) {                unless (length $token->{data}) {
4055                  !!!next-token;                  !!!next-token;
4056                  redo B;                  redo B;
# Line 3518  sub _construct_tree ($) { Line 4058  sub _construct_tree ($) {
4058              }              }
4059                            
4060              #              #
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4061            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4062              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4063                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4064                pop @$open_elements;                pop @{$self->{open_elements}};
4065                !!!next-token;                !!!next-token;
4066                redo B;                redo B;
4067              } else {              } else {
# Line 3534  sub _construct_tree ($) { Line 4069  sub _construct_tree ($) {
4069              }              }
4070            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4071              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4072                if ($open_elements->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4073                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:colgroup');
4074                  ## Ignore the token                  ## Ignore the token
4075                  !!!next-token;                  !!!next-token;
4076                  redo B;                  redo B;
4077                } else {                } else {
4078                  pop @$open_elements; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4079                  $insertion_mode = 'in table';                  $self->{insertion_mode} = 'in table';
4080                  !!!next-token;                  !!!next-token;
4081                  redo B;                              redo B;            
4082                }                }
4083              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
4084                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:col');
4085                ## Ignore the token                ## Ignore the token
4086                !!!next-token;                !!!next-token;
4087                redo B;                redo B;
# Line 3558  sub _construct_tree ($) { Line 4093  sub _construct_tree ($) {
4093            }            }
4094    
4095            ## As if </colgroup>            ## As if </colgroup>
4096            if ($open_elements->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4097              !!!parse-error;              !!!parse-error (type => 'unmatched end tag:colgroup');
4098              ## Ignore the token              ## Ignore the token
4099              !!!next-token;              !!!next-token;
4100              redo B;              redo B;
4101            } else {            } else {
4102              pop @$open_elements; # colgroup              pop @{$self->{open_elements}}; # colgroup
4103              $insertion_mode = 'in table';              $self->{insertion_mode} = 'in table';
4104              ## reprocess              ## reprocess
4105              redo B;              redo B;
4106            }            }
4107          } elsif ($insertion_mode eq 'in table body') {          } elsif ($self->{insertion_mode} eq 'in table body') {
4108            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4109              ## NOTE: This is a "character in table" code clone.              ## NOTE: This is a "character in table" code clone.
4110              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4111                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4112                                
4113                unless (length $token->{data}) {                unless (length $token->{data}) {
4114                  !!!next-token;                  !!!next-token;
# Line 3581  sub _construct_tree ($) { Line 4116  sub _construct_tree ($) {
4116                }                }
4117              }              }
4118    
4119                !!!parse-error (type => 'in table:#character');
4120    
4121              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4122              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
4123              ## into the current node" while characters might not be              ## into the current node" while characters might not be
# Line 3590  sub _construct_tree ($) { Line 4127  sub _construct_tree ($) {
4127              if ({              if ({
4128                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4129                   thead => 1, tr => 1,                   thead => 1, tr => 1,
4130                  }->{$open_elements->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4131                # MUST                # MUST
4132                my $foster_parent_element;                my $foster_parent_element;
4133                my $next_sibling;                my $next_sibling;
4134                my $prev_sibling;                my $prev_sibling;
4135                OE: for (reverse 0..$#$open_elements) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4136                  if ($open_elements->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4137                    my $parent = $open_elements->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4138                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4139                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4140                      $next_sibling = $open_elements->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4141                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4142                    } else {                    } else {
4143                      $foster_parent_element = $open_elements->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4144                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4145                    }                    }
4146                    last OE;                    last OE;
4147                  }                  }
4148                } # OE                } # OE
4149                $foster_parent_element = $open_elements->[0]->[0] and                $foster_parent_element = $self->{open_elements}->[0]->[0] and
4150                $prev_sibling = $foster_parent_element->last_child                $prev_sibling = $foster_parent_element->last_child
4151                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4152                if (defined $prev_sibling and                if (defined $prev_sibling and
# Line 3621  sub _construct_tree ($) { Line 4158  sub _construct_tree ($) {
4158                     $next_sibling);                     $next_sibling);
4159                }                }
4160              } else {              } else {
4161                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4162              }              }
4163                            
4164              !!!next-token;              !!!next-token;
4165              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4166            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4167              if ({              if ({
4168                   tr => 1,                   tr => 1,
4169                   th => 1, td => 1,                   th => 1, td => 1,
4170                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4171                  unless ($token->{tag_name} eq 'tr') {
4172                    !!!parse-error (type => 'missing start tag:tr');
4173                  }
4174    
4175                ## Clear back to table body context                ## Clear back to table body context
4176                while (not {                while (not {
4177                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4178                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4179                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4180                  pop @$open_elements;                  pop @{$self->{open_elements}};
4181                }                }
4182                                
4183                $insertion_mode = 'in row';                $self->{insertion_mode} = 'in row';
4184                if ($token->{tag_name} eq 'tr') {                if ($token->{tag_name} eq 'tr') {
4185                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4186                  !!!next-token;                  !!!next-token;
# Line 3660  sub _construct_tree ($) { Line 4195  sub _construct_tree ($) {
4195                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4196                ## have an element in table scope                ## have an element in table scope
4197                my $i;                my $i;
4198                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4199                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4200                  if ({                  if ({
4201                       tbody => 1, thead => 1, tfoot => 1,                       tbody => 1, thead => 1, tfoot => 1,
4202                      }->{$node->[1]}) {                      }->{$node->[1]}) {
# Line 3674  sub _construct_tree ($) { Line 4209  sub _construct_tree ($) {
4209                  }                  }
4210                } # INSCOPE                } # INSCOPE
4211                unless (defined $i) {                unless (defined $i) {
4212                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4213                  ## Ignore the token                  ## Ignore the token
4214                  !!!next-token;                  !!!next-token;
4215                  redo B;                  redo B;
# Line 3683  sub _construct_tree ($) { Line 4218  sub _construct_tree ($) {
4218                ## Clear back to table body context                ## Clear back to table body context
4219                while (not {                while (not {
4220                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4221                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4222                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4223                  pop @$open_elements;                  pop @{$self->{open_elements}};
4224                }                }
4225    
4226                ## As if <{current node}>                ## As if <{current node}>
# Line 3695  sub _construct_tree ($) { Line 4230  sub _construct_tree ($) {
4230                ## Clear back to table body context                ## Clear back to table body context
4231                ## nop by definition                ## nop by definition
4232    
4233                pop @$open_elements;                pop @{$self->{open_elements}};
4234                $insertion_mode = 'in table';                $self->{insertion_mode} = 'in table';
4235                ## reprocess                ## reprocess
4236                redo B;                redo B;
4237              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4238                ## NOTE: This is a code clone of "table in table"                ## NOTE: This is a code clone of "table in table"
4239                !!!parse-error;                !!!parse-error (type => 'not closed:table');
4240    
4241                ## As if </table>                ## As if </table>
4242                ## have a table element in table scope                ## have a table element in table scope
4243                my $i;                my $i;
4244                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4245                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4246                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4247                    $i = $_;                    $i = $_;
4248                    last INSCOPE;                    last INSCOPE;
# Line 3718  sub _construct_tree ($) { Line 4253  sub _construct_tree ($) {
4253                  }                  }
4254                } # INSCOPE                } # INSCOPE
4255                unless (defined $i) {                unless (defined $i) {
4256                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:table');
4257                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4258                  !!!next-token;                  !!!next-token;
4259                  redo B;                  redo B;
# Line 3728  sub _construct_tree ($) { Line 4263  sub _construct_tree ($) {
4263                if ({                if ({
4264                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4265                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4266                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
4267                      }->{$self->{open_elements}->[-1]->[1]}) {
4268                  !!!back-token; # <table>                  !!!back-token; # <table>
4269                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
4270                  !!!back-token;                  !!!back-token;
4271                  $token = {type => 'end tag',                  $token = {type => 'end tag',
4272                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4273                  redo B;                  redo B;
4274                }                }
4275    
4276                if ($open_elements->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4277                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4278                }                }
4279    
4280                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4281    
4282                $reset_insertion_mode->();                              $self->_reset_insertion_mode;
4283    
4284                ## reprocess                ## reprocess
4285                redo B;                redo B;
# Line 3756  sub _construct_tree ($) { Line 4292  sub _construct_tree ($) {
4292                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4293                ## have an element in table scope                ## have an element in table scope
4294                my $i;                my $i;
4295                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4296                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4297                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4298                    $i = $_;                    $i = $_;
4299                    last INSCOPE;                    last INSCOPE;
# Line 3768  sub _construct_tree ($) { Line 4304  sub _construct_tree ($) {
4304                  }                  }
4305                } # INSCOPE                } # INSCOPE
4306                unless (defined $i) {                unless (defined $i) {
4307                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4308                  ## Ignore the token                  ## Ignore the token
4309                  !!!next-token;                  !!!next-token;
4310                  redo B;                  redo B;
# Line 3777  sub _construct_tree ($) { Line 4313  sub _construct_tree ($) {
4313                ## Clear back to table body context                ## Clear back to table body context
4314                while (not {                while (not {
4315                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4316                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4317                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4318                  pop @$open_elements;                  pop @{$self->{open_elements}};
4319                }                }
4320    
4321                pop @$open_elements;                pop @{$self->{open_elements}};
4322                $insertion_mode = 'in table';                $self->{insertion_mode} = 'in table';
4323                !!!next-token;                !!!next-token;
4324                redo B;                redo B;
4325              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4326                ## have an element in table scope                ## have an element in table scope
4327                my $i;                my $i;
4328                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4329                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4330                  if ({                  if ({
4331                       tbody => 1, thead => 1, tfoot => 1,                       tbody => 1, thead => 1, tfoot => 1,
4332                      }->{$node->[1]}) {                      }->{$node->[1]}) {
# Line 3803  sub _construct_tree ($) { Line 4339  sub _construct_tree ($) {
4339                  }                  }
4340                } # INSCOPE                } # INSCOPE
4341                unless (defined $i) {                unless (defined $i) {
4342                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4343                  ## Ignore the token                  ## Ignore the token
4344                  !!!next-token;                  !!!next-token;
4345                  redo B;                  redo B;
# Line 3812  sub _construct_tree ($) { Line 4348  sub _construct_tree ($) {
4348                ## Clear back to table body context                ## Clear back to table body context
4349                while (not {                while (not {
4350                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4351                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4352                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4353                  pop @$open_elements;                  pop @{$self->{open_elements}};
4354                }                }
4355    
4356                ## As if <{current node}>                ## As if <{current node}>
# Line 3824  sub _construct_tree ($) { Line 4360  sub _construct_tree ($) {
4360                ## Clear back to table body context                ## Clear back to table body context
4361                ## nop by definition                ## nop by definition
4362    
4363                pop @$open_elements;                pop @{$self->{open_elements}};
4364                $insertion_mode = 'in table';                $self->{insertion_mode} = 'in table';
4365                ## reprocess                ## reprocess
4366                redo B;                redo B;
4367              } elsif ({              } elsif ({
4368                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4369                        html => 1, td => 1, th => 1, tr => 1,                        html => 1, td => 1, th => 1, tr => 1,
4370                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4371                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4372                ## Ignore the token                ## Ignore the token
4373                !!!next-token;                !!!next-token;
4374                redo B;                redo B;
# Line 3844  sub _construct_tree ($) { Line 4380  sub _construct_tree ($) {
4380            }            }
4381                        
4382            ## As if in table            ## As if in table
4383            !!!parse-error;            !!!parse-error (type => 'in table:'.$token->{tag_name});
4384            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4385            redo B;            redo B;
4386          } elsif ($insertion_mode eq 'in row') {          } elsif ($self->{insertion_mode} eq 'in row') {
4387            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4388              ## NOTE: This is a "character in table" code clone.              ## NOTE: This is a "character in table" code clone.
4389              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4390                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4391                                
4392                unless (length $token->{data}) {                unless (length $token->{data}) {
4393                  !!!next-token;                  !!!next-token;
# Line 3859  sub _construct_tree ($) { Line 4395  sub _construct_tree ($) {
4395                }                }
4396              }              }
4397    
4398                !!!parse-error (type => 'in table:#character');
4399    
4400              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4401              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
4402              ## into the current node" while characters might not be              ## into the current node" while characters might not be
# Line 3868  sub _construct_tree ($) { Line 4406  sub _construct_tree ($) {
4406              if ({              if ({
4407                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4408                   thead => 1, tr => 1,                   thead => 1, tr => 1,
4409                  }->{$open_elements->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4410                # MUST                # MUST
4411                my $foster_parent_element;                my $foster_parent_element;
4412                my $next_sibling;                my $next_sibling;
4413                my $prev_sibling;                my $prev_sibling;
4414                OE: for (reverse 0..$#$open_elements) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4415                  if ($open_elements->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4416                    my $parent = $open_elements->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4417                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4418                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4419                      $next_sibling = $open_elements->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4420                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4421                    } else {                    } else {
4422                      $foster_parent_element = $open_elements->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4423                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4424                    }                    }
4425                    last OE;                    last OE;
4426                  }                  }
4427                } # OE                } # OE
4428                $foster_parent_element = $open_elements->[0]->[0] and                $foster_parent_element = $self->{open_elements}->[0]->[0] and
4429                $prev_sibling = $foster_parent_element->last_child                $prev_sibling = $foster_parent_element->last_child
4430                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4431                if (defined $prev_sibling and                if (defined $prev_sibling and
# Line 3899  sub _construct_tree ($) { Line 4437  sub _construct_tree ($) {
4437                     $next_sibling);                     $next_sibling);
4438                }                }
4439              } else {              } else {
4440                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4441              }              }
4442                            
4443              !!!next-token;              !!!next-token;
4444              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4445            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4446              if ($token->{tag_name} eq 'th' or              if ($token->{tag_name} eq 'th' or
4447                  $token->{tag_name} eq 'td') {                  $token->{tag_name} eq 'td') {
4448                ## Clear back to table row context                ## Clear back to table row context
4449                while (not {                while (not {
4450                  tr => 1, html => 1,                  tr => 1, html => 1,
4451                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4452                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4453                  pop @$open_elements;                  pop @{$self->{open_elements}};
4454                }                }
4455                                
4456                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4457                $insertion_mode = 'in cell';                $self->{insertion_mode} = 'in cell';
4458    
4459                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
4460                                
# Line 3935  sub _construct_tree ($) { Line 4467  sub _construct_tree ($) {
4467                ## As if </tr>                ## As if </tr>
4468                ## have an element in table scope                ## have an element in table scope
4469                my $i;                my $i;
4470                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4471                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4472                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq 'tr') {
4473                    $i = $_;                    $i = $_;
4474                    last INSCOPE;                    last INSCOPE;
# Line 3947  sub _construct_tree ($) { Line 4479  sub _construct_tree ($) {
4479                  }                  }
4480                } # INSCOPE                } # INSCOPE
4481                unless (defined $i) {                unless (defined $i) {
4482                  !!!parse-error;                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4483                  ## Ignore the token                  ## Ignore the token
4484                  !!!next-token;                  !!!next-token;
4485                  redo B;                  redo B;
# Line 3956  sub _construct_tree ($) { Line 4488  sub _construct_tree ($) {
4488                ## Clear back to table row context                ## Clear back to table row context
4489                while (not {                while (not {
4490                  tr => 1, html => 1,                  tr => 1, html => 1,
4491                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4492                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4493                  pop @$open_elements;                  pop @{$self->{open_elements}};
4494                }                }
4495    
4496                pop @$open_elements; # tr                pop @{$self->{open_elements}}; # tr
4497                $insertion_mode = 'in table body';                $self->{insertion_mode} = 'in table body';
4498                ## reprocess                ## reprocess
4499                redo B;                redo B;
4500              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4501                ## NOTE: This is a code clone of "table in table"                ## NOTE: This is a code clone of "table in table"
4502                !!!parse-error;                !!!parse-error (type => 'not closed:table');
4503    
4504                ## As if </table>                ## As if </table>
4505                ## have a table element in table scope                ## have a table element in table scope
4506                my $i;                my $i;
4507                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4508                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4509                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4510                    $i = $_;                    $i = $_;
4511                    last INSCOPE;                    last INSCOPE;
# Line 3984  sub _construct_tree ($) { Line 4516  sub _construct_tree ($) {
4516                  }                  }
4517                } # INSCOPE                } # INSCOPE
4518                unless (defined $i) {                unless (defined $i) {
4519                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:table');
4520                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4521                  !!!next-token;                  !!!next-token;
4522                  redo B;                  redo B;
# Line 3994  sub _construct_tree ($) { Line 4526  sub _construct_tree ($) {
4526                if ({                if ({
4527                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4528                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4529                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
4530                      }->{$self->{open_elements}->[-1]->[1]}) {
4531                  !!!back-token; # <table>                  !!!back-token; # <table>
4532                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
4533                  !!!back-token;                  !!!back-token;
4534                  $token = {type => 'end tag',                  $token = {type => 'end tag',
4535                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4536                  redo B;                  redo B;
4537                }                }
4538    
4539                if ($open_elements->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4540                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4541                }                }
4542    
4543                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4544    
4545                $reset_insertion_mode->();                              $self->_reset_insertion_mode;
4546    
4547                ## reprocess                ## reprocess
4548                redo B;                redo B;
# Line 4020  sub _construct_tree ($) { Line 4553  sub _construct_tree ($) {
4553              if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'tr') {
4554                ## have an element in table scope                ## have an element in table scope
4555                my $i;                my $i;
4556                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4557                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4558                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4559                    $i = $_;                    $i = $_;
4560                    last INSCOPE;                    last INSCOPE;
# Line 4032  sub _construct_tree ($) { Line 4565  sub _construct_tree ($) {
4565                  }                  }
4566                } # INSCOPE                } # INSCOPE
4567                unless (defined $i) {                unless (defined $i) {
4568                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4569                  ## Ignore the token                  ## Ignore the token
4570                  !!!next-token;                  !!!next-token;
4571                  redo B;                  redo B;
# Line 4041  sub _construct_tree ($) { Line 4574  sub _construct_tree ($) {
4574                ## Clear back to table row context                ## Clear back to table row context
4575                while (not {                while (not {
4576                  tr => 1, html => 1,                  tr => 1, html => 1,
4577                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4578                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4579                  pop @$open_elements;                  pop @{$self->{open_elements}};
4580                }                }
4581    
4582                pop @$open_elements; # tr                pop @{$self->{open_elements}}; # tr
4583                $insertion_mode = 'in table body';                $self->{insertion_mode} = 'in table body';
4584                !!!next-token;                !!!next-token;
4585                redo B;                redo B;
4586              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4587                ## As if </tr>                ## As if </tr>
4588                ## have an element in table scope                ## have an element in table scope
4589                my $i;                my $i;
4590                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4591                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4592                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq 'tr') {
4593                    $i = $_;                    $i = $_;
4594                    last INSCOPE;                    last INSCOPE;
# Line 4066  sub _construct_tree ($) { Line 4599  sub _construct_tree ($) {
4599                  }                  }
4600                } # INSCOPE                } # INSCOPE
4601                unless (defined $i) {                unless (defined $i) {
4602                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4603                  ## Ignore the token                  ## Ignore the token
4604                  !!!next-token;                  !!!next-token;
4605                  redo B;                  redo B;
# Line 4075  sub _construct_tree ($) { Line 4608  sub _construct_tree ($) {
4608                ## Clear back to table row context                ## Clear back to table row context
4609                while (not {                while (not {
4610                  tr => 1, html => 1,                  tr => 1, html => 1,
4611                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4612                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4613                  pop @$open_elements;                  pop @{$self->{open_elements}};
4614                }                }
4615    
4616                pop @$open_elements; # tr                pop @{$self->{open_elements}}; # tr
4617                $insertion_mode = 'in table body';                $self->{insertion_mode} = 'in table body';
4618                ## reprocess                ## reprocess
4619                redo B;                redo B;
4620              } elsif ({              } elsif ({
# Line 4089  sub _construct_tree ($) { Line 4622  sub _construct_tree ($) {
4622                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4623                ## have an element in table scope                ## have an element in table scope
4624                my $i;                my $i;
4625                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4626                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4627                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4628                    $i = $_;                    $i = $_;
4629                    last INSCOPE;                    last INSCOPE;
# Line 4101  sub _construct_tree ($) { Line 4634  sub _construct_tree ($) {
4634                  }                  }
4635                } # INSCOPE                } # INSCOPE
4636                unless (defined $i) {                unless (defined $i) {
4637                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4638                  ## Ignore the token                  ## Ignore the token
4639                  !!!next-token;                  !!!next-token;
4640                  redo B;                  redo B;
# Line 4110  sub _construct_tree ($) { Line 4643  sub _construct_tree ($) {
4643                ## As if </tr>                ## As if </tr>
4644                ## have an element in table scope                ## have an element in table scope
4645                my $i;                my $i;
4646                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4647                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4648                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq 'tr') {
4649                    $i = $_;                    $i = $_;
4650                    last INSCOPE;                    last INSCOPE;
# Line 4122  sub _construct_tree ($) { Line 4655  sub _construct_tree ($) {
4655                  }                  }
4656                } # INSCOPE                } # INSCOPE
4657                unless (defined $i) {                unless (defined $i) {
4658                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:tr');
4659                  ## Ignore the token                  ## Ignore the token
4660                  !!!next-token;                  !!!next-token;
4661                  redo B;                  redo B;
# Line 4131  sub _construct_tree ($) { Line 4664  sub _construct_tree ($) {
4664                ## Clear back to table row context                ## Clear back to table row context
4665                while (not {                while (not {
4666                  tr => 1, html => 1,                  tr => 1, html => 1,
4667                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4668                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4669                  pop @$open_elements;                  pop @{$self->{open_elements}};
4670                }                }
4671    
4672                pop @$open_elements; # tr                pop @{$self->{open_elements}}; # tr
4673                $insertion_mode = 'in table body';                $self->{insertion_mode} = 'in table body';
4674                ## reprocess                ## reprocess
4675                redo B;                redo B;
4676              } elsif ({              } elsif ({
4677                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1,
4678                        colgroup => 1, html => 1, td => 1, th => 1,                        colgroup => 1, html => 1, td => 1, th => 1,
4679                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4680                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4681                ## Ignore the token                ## Ignore the token
4682                !!!next-token;                !!!next-token;
4683                redo B;                redo B;
# Line 4156  sub _construct_tree ($) { Line 4689  sub _construct_tree ($) {
4689            }            }
4690    
4691            ## As if in table            ## As if in table
4692            !!!parse-error;            !!!parse-error (type => 'in table:'.$token->{tag_name});
4693            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4694            redo B;            redo B;
4695          } elsif ($insertion_mode eq 'in cell') {          } elsif ($self->{insertion_mode} eq 'in cell') {
4696            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4697              ## NOTE: This is a code clone of "character in body".              ## NOTE: This is a code clone of "character in body".
4698              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4699                            
4700              $open_elements->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4701    
4702              !!!next-token;              !!!next-token;
4703              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4704            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4705              if ({              if ({
4706                   caption => 1, col => 1, colgroup => 1,                   caption => 1, col => 1, colgroup => 1,
# Line 4182  sub _construct_tree ($) { Line 4709  sub _construct_tree ($) {
4709                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4710                ## have an element in table scope                ## have an element in table scope
4711                my $tn;                my $tn;
4712                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4713                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4714                  if ($node->[1] eq 'td' or $node->[1] eq 'th') {                  if ($node->[1] eq 'td' or $node->[1] eq 'th') {
4715                    $tn = $node->[1];                    $tn = $node->[1];
4716                    last INSCOPE;                    last INSCOPE;
# Line 4194  sub _construct_tree ($) { Line 4721  sub _construct_tree ($) {
4721                  }                  }
4722                } # INSCOPE                } # INSCOPE
4723                unless (defined $tn) {                unless (defined $tn) {
4724                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4725                  ## Ignore the token                  ## Ignore the token
4726                  !!!next-token;                  !!!next-token;
4727                  redo B;                  redo B;
# Line 4211  sub _construct_tree ($) { Line 4738  sub _construct_tree ($) {
4738              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4739                ## have an element in table scope                ## have an element in table scope
4740                my $i;                my $i;
4741                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4742                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4743                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4744                    $i = $_;                    $i = $_;
4745                    last INSCOPE;                    last INSCOPE;
# Line 4223  sub _construct_tree ($) { Line 4750  sub _construct_tree ($) {
4750                  }                  }
4751                } # INSCOPE                } # INSCOPE
4752                unless (defined $i) {                unless (defined $i) {
4753                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4754                  ## Ignore the token                  ## Ignore the token
4755                  !!!next-token;                  !!!next-token;
4756                  redo B;                  redo B;
# Line 4235  sub _construct_tree ($) { Line 4762  sub _construct_tree ($) {
4762                     td => ($token->{tag_name} eq 'th'),                     td => ($token->{tag_name} eq 'th'),
4763                     th => ($token->{tag_name} eq 'td'),                     th => ($token->{tag_name} eq 'td'),
4764                     tr => 1,                     tr => 1,
4765                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
4766                      }->{$self->{open_elements}->[-1]->[1]}) {
4767                  !!!back-token;                  !!!back-token;
4768                  $token = {type => 'end tag',                  $token = {type => 'end tag',
4769                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4770                  redo B;                  redo B;
4771                }                }
4772    
4773                if ($open_elements->[-1]->[1] ne $token->{tag_name}) {                if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4774                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4775                }                }
4776    
4777                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4778    
4779                $clear_up_to_marker->();                $clear_up_to_marker->();
4780    
4781                $insertion_mode = 'in row';                $self->{insertion_mode} = 'in row';
4782    
4783                !!!next-token;                !!!next-token;
4784                redo B;                redo B;
# Line 4258  sub _construct_tree ($) { Line 4786  sub _construct_tree ($) {
4786                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1,
4787                        colgroup => 1, html => 1,                        colgroup => 1, html => 1,
4788                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4789                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4790                ## Ignore the token                ## Ignore the token
4791                !!!next-token;                !!!next-token;
4792                redo B;                redo B;
# Line 4269  sub _construct_tree ($) { Line 4797  sub _construct_tree ($) {
4797                ## have an element in table scope                ## have an element in table scope
4798                my $i;                my $i;
4799                my $tn;                my $tn;
4800                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4801                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4802                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4803                    $i = $_;                    $i = $_;
4804                    last INSCOPE;                    last INSCOPE;
# Line 4285  sub _construct_tree ($) { Line 4813  sub _construct_tree ($) {
4813                  }                  }
4814                } # INSCOPE                } # INSCOPE
4815                unless (defined $i) {                unless (defined $i) {
4816                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4817                  ## Ignore the token                  ## Ignore the token
4818                  !!!next-token;                  !!!next-token;
4819                  redo B;                  redo B;
# Line 4304  sub _construct_tree ($) { Line 4832  sub _construct_tree ($) {
4832                        
4833            $in_body->($insert_to_current);            $in_body->($insert_to_current);
4834            redo B;            redo B;
4835          } elsif ($insertion_mode eq 'in select') {          } elsif ($self->{insertion_mode} eq 'in select') {
4836            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4837              $open_elements->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
4838              !!!next-token;              !!!next-token;
4839              redo B;              redo B;
4840            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4841              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4842                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4843                  ## As if </option>                  ## As if </option>
4844                  pop @$open_elements;                  pop @{$self->{open_elements}};
4845                }                }
4846    
4847                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4848                !!!next-token;                !!!next-token;
4849                redo B;                redo B;
4850              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
4851                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4852                  ## As if </option>                  ## As if </option>
4853                  pop @$open_elements;                  pop @{$self->{open_elements}};
4854                }                }
4855    
4856                if ($open_elements->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4857                  ## As if </optgroup>                  ## As if </optgroup>
4858                  pop @$open_elements;                  pop @{$self->{open_elements}};
4859                }                }
4860    
4861                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4862                !!!next-token;                !!!next-token;
4863                redo B;                redo B;
4864              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4865                !!!parse-error;                !!!parse-error (type => 'not closed:select');
4866                ## As if </select> instead                ## As if </select> instead
4867                ## have an element in table scope                ## have an element in table scope
4868                my $i;                my $i;
4869                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4870                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4871                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4872                    $i = $_;                    $i = $_;
4873                    last INSCOPE;                    last INSCOPE;
# Line 4355  sub _construct_tree ($) { Line 4878  sub _construct_tree ($) {
4878                  }                  }
4879                } # INSCOPE                } # INSCOPE
4880                unless (defined $i) {                unless (defined $i) {
4881                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:select');
4882                  ## Ignore the token                  ## Ignore the token
4883                  !!!next-token;                  !!!next-token;
4884                  redo B;                  redo B;
4885                }                }
4886                                
4887                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4888    
4889                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4890    
4891                !!!next-token;                !!!next-token;
4892                redo B;                redo B;
# Line 4372  sub _construct_tree ($) { Line 4895  sub _construct_tree ($) {
4895              }              }
4896            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4897              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4898                if ($open_elements->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4899                    $open_elements->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4900                  ## As if </option>                  ## As if </option>
4901                  splice @$open_elements, -2;                  splice @{$self->{open_elements}}, -2;
4902                } elsif ($open_elements->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4903                  pop @$open_elements;                  pop @{$self->{open_elements}};
4904                } else {                } else {
4905                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4906                  ## Ignore the token                  ## Ignore the token
4907                }                }
4908                !!!next-token;                !!!next-token;
4909                redo B;                redo B;
4910              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
4911                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4912                  pop @$open_elements;                  pop @{$self->{open_elements}};
4913                } else {                } else {
4914                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4915                  ## Ignore the token                  ## Ignore the token
4916                }                }
4917                !!!next-token;                !!!next-token;
# Line 4396  sub _construct_tree ($) { Line 4919  sub _construct_tree ($) {
4919              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4920                ## have an element in table scope                ## have an element in table scope
4921                my $i;                my $i;
4922                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4923                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4924                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4925                    $i = $_;                    $i = $_;
4926                    last INSCOPE;                    last INSCOPE;
# Line 4408  sub _construct_tree ($) { Line 4931  sub _construct_tree ($) {
4931                  }                  }
4932                } # INSCOPE                } # INSCOPE
4933                unless (defined $i) {                unless (defined $i) {
4934                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4935                  ## Ignore the token                  ## Ignore the token
4936                  !!!next-token;                  !!!next-token;
4937                  redo B;                  redo B;
4938                }                }
4939                                
4940                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4941    
4942                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4943    
4944                !!!next-token;                !!!next-token;
4945                redo B;                redo B;
# Line 4424  sub _construct_tree ($) { Line 4947  sub _construct_tree ($) {
4947                        caption => 1, table => 1, tbody => 1,                        caption => 1, table => 1, tbody => 1,
4948                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4949                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4950                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4951                                
4952                ## have an element in table scope                ## have an element in table scope
4953                my $i;                my $i;
4954                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4955                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4956                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4957                    $i = $_;                    $i = $_;
4958                    last INSCOPE;                    last INSCOPE;
# Line 4448  sub _construct_tree ($) { Line 4971  sub _construct_tree ($) {
4971                ## As if </select>                ## As if </select>
4972                ## have an element in table scope                ## have an element in table scope
4973                undef $i;                undef $i;
4974                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4975                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4976                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
4977                    $i = $_;                    $i = $_;
4978                    last INSCOPE;                    last INSCOPE;
# Line 4460  sub _construct_tree ($) { Line 4983  sub _construct_tree ($) {
4983                  }                  }
4984                } # INSCOPE                } # INSCOPE
4985                unless (defined $i) {                unless (defined $i) {
4986                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:select');
4987                  ## Ignore the </select> token                  ## Ignore the </select> token
4988                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
4989                  redo B;                  redo B;
4990                }                }
4991                                
4992                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4993    
4994                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4995    
4996                ## reprocess                ## reprocess
4997                redo B;                redo B;
# Line 4479  sub _construct_tree ($) { Line 5002  sub _construct_tree ($) {
5002              #              #
5003            }            }
5004    
5005            !!!parse-error;            !!!parse-error (type => 'in select:'.$token->{tag_name});
5006            ## Ignore the token            ## Ignore the token
5007            !!!next-token;            !!!next-token;
5008            redo B;            redo B;
5009          } elsif ($insertion_mode eq 'after body') {          } elsif ($self->{insertion_mode} eq 'after body') {
5010            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
5011              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5012                  my $data = $1;
5013                ## As if in body                ## As if in body
5014                $reconstruct_active_formatting_elements->($insert_to_current);                $reconstruct_active_formatting_elements->($insert_to_current);
5015                                
5016                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5017    
5018                unless (length $token->{data}) {                unless (length $token->{data}) {
5019                  !!!next-token;                  !!!next-token;
# Line 4498  sub _construct_tree ($) { Line 5022  sub _construct_tree ($) {
5022              }              }
5023                            
5024              #              #
5025            } elsif ($token->{type} eq 'comment') {              !!!parse-error (type => 'after body:#character');
5026              my $comment = $self->{document}->create_comment ($token->{data});            } elsif ($token->{type} eq 'start tag') {
5027              $open_elements->[0]->[0]->append_child ($comment);              !!!parse-error (type => 'after body:'.$token->{tag_name});
5028              !!!next-token;              #
             redo B;  
5029            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
5030              if ($token->{tag_name} eq 'html') {              if ($token->{tag_name} eq 'html') {
5031                ## TODO: if inner_html, parse-error, ignore the token; otherwise,                if (defined $self->{inner_html_node}) {
5032                    !!!parse-error (type => 'unmatched end tag:html');
5033                $phase = 'trailing end';                  ## Ignore the token
5034                !!!next-token;                  !!!next-token;
5035                redo B;                  redo B;
5036                  } else {
5037                    $previous_insertion_mode = $self->{insertion_mode};
5038                    $self->{insertion_mode} = 'trailing end';
5039                    !!!next-token;
5040                    redo B;
5041                  }
5042              } else {              } else {
5043                #                !!!parse-error (type => 'after body:/'.$token->{tag_name});
5044              }              }
5045            } else {            } else {
5046              #              die "$0: $token->{type}: Unknown token type";
5047            }            }
5048    
5049            !!!parse-error ('data after body');            $self->{insertion_mode} = 'in body';
           $insertion_mode = 'in body';  
5050            ## reprocess            ## reprocess
5051            redo B;            redo B;
5052          } elsif ($insertion_mode eq 'in frameset') {      } elsif ($self->{insertion_mode} eq 'in frameset') {
5053            if ($token->{type} eq 'character') {        if ($token->{type} eq 'character') {
5054              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5055                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
5056    
5057              #            unless (length $token->{data}) {
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
5058              !!!next-token;              !!!next-token;
5059              redo B;              redo B;
5060            } elsif ($token->{type} eq 'start tag') {            }
5061              if ($token->{tag_name} eq 'frameset') {          }
5062                !!!insert-element ($token->{tag_name}, $token->{attributes});  
5063                !!!next-token;          !!!parse-error (type => 'in frameset:#character');
5064                redo B;          ## Ignore the token
5065              } elsif ($token->{tag_name} eq 'frame') {          !!!next-token;
5066                !!!insert-element ($token->{tag_name}, $token->{attributes});          redo B;
5067                pop @$open_elements;        } elsif ($token->{type} eq 'start tag') {
5068                !!!next-token;          if ($token->{tag_name} eq 'frameset') {
5069                redo B;            !!!insert-element ($token->{tag_name}, $token->{attributes});
5070              } elsif ($token->{tag_name} eq 'noframes') {            !!!next-token;
5071                $in_body->($insert_to_current);            redo B;
5072                redo B;          } elsif ($token->{tag_name} eq 'frame') {
5073              } else {            !!!insert-element ($token->{tag_name}, $token->{attributes});
5074                #            pop @{$self->{open_elements}};
5075              }            !!!next-token;
5076            } elsif ($token->{type} eq 'end tag') {            redo B;
5077              if ($token->{tag_name} eq 'frameset') {          } elsif ($token->{tag_name} eq 'noframes') {
5078                if ($open_elements->[-1]->[1] eq 'html' and            $in_body->($insert_to_current);
5079                    @$open_elements == 1) {            redo B;
5080                  !!!parse-error;          } else {
5081                  ## Ignore the token            !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5082                  !!!next-token;            ## Ignore the token
5083                } else {            !!!next-token;
5084                  pop @$open_elements;            redo B;
5085                  !!!next-token;          }
5086                }        } elsif ($token->{type} eq 'end tag') {
5087                          if ($token->{tag_name} eq 'frameset') {
5088                ## if not inner_html and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5089                if ($open_elements->[-1]->[1] ne 'frameset') {                @{$self->{open_elements}} == 1) {
5090                  $insertion_mode = 'after frameset';              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5091                }              ## Ignore the token
5092                redo B;              !!!next-token;
             } else {  
               #  
             }  
5093            } else {            } else {
5094              #              pop @{$self->{open_elements}};
5095                !!!next-token;
5096            }            }
5097              
5098            !!!parse-error;            if (not defined $self->{inner_html_node} and
5099                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
5100                $self->{insertion_mode} = 'after frameset';
5101              }
5102              redo B;
5103            } else {
5104              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5105            ## Ignore the token            ## Ignore the token
5106            !!!next-token;            !!!next-token;
5107            redo B;            redo B;
5108          } elsif ($insertion_mode eq 'after frameset') {          }
5109            if ($token->{type} eq 'character') {        } else {
5110            die "$0: $token->{type}: Unknown token type";
5111          }
5112        } elsif ($self->{insertion_mode} eq 'after frameset') {
5113          if ($token->{type} eq 'character') {
5114              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5115                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5116    
5117                unless (length $token->{data}) {                unless (length $token->{data}) {
5118                  !!!next-token;                  !!!next-token;
# Line 4593  sub _construct_tree ($) { Line 5120  sub _construct_tree ($) {
5120                }                }
5121              }              }
5122    
5123              #              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5124            } elsif ($token->{type} eq 'comment') {                !!!parse-error (type => 'after frameset:#character');
5125              my $comment = $self->{document}->create_comment ($token->{data});  
5126              $open_elements->[-1]->[0]->append_child ($comment);                ## Ignore the token.
5127              !!!next-token;                if (length $token->{data}) {
5128              redo B;                  ## reprocess the rest of characters
5129            } elsif ($token->{type} eq 'start tag') {                } else {
5130              if ($token->{tag_name} eq 'noframes') {                  !!!next-token;
5131                $in_body->($insert_to_current);                }
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
5132                redo B;                redo B;
             } else {  
               #  
5133              }              }
5134            } else {  
5135              #          die qq[$0: Character "$token->{data}"];
5136            }        } elsif ($token->{type} eq 'start tag') {
5137                      if ($token->{tag_name} eq 'noframes') {
5138            !!!parse-error;            $in_body->($insert_to_current);
5139              redo B;
5140            } else {
5141              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5142            ## Ignore the token            ## Ignore the token
5143            !!!next-token;            !!!next-token;
5144            redo B;            redo B;
5145            }
5146            ## ISSUE: An issue in spec there        } elsif ($token->{type} eq 'end tag') {
5147            if ($token->{tag_name} eq 'html') {
5148              $previous_insertion_mode = $self->{insertion_mode};
5149              $self->{insertion_mode} = 'trailing end';
5150              !!!next-token;
5151              redo B;
5152          } else {          } else {
5153            die "$0: $insertion_mode: Unknown insertion mode";            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5154              ## Ignore the token
5155              !!!next-token;
5156              redo B;
5157          }          }
5158          } else {
5159            die "$0: $token->{type}: Unknown token type";
5160        }        }
5161      } elsif ($phase eq 'trailing end') {  
5162          ## ISSUE: An issue in spec here
5163        } elsif ($self->{insertion_mode} eq 'trailing end') {
5164        ## states in the main stage is preserved yet # MUST        ## states in the main stage is preserved yet # MUST
5165                
5166        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} eq 'character') {
         !!!parse-error;  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
5167          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5168            my $data = $1;            my $data = $1;
5169            ## As if in the main phase.            ## As if in the main phase.
5170            ## NOTE: The insertion mode in the main phase            ## NOTE: The insertion mode in the main phase
5171            ## just before the phase has been changed to the trailing            ## just before the phase has been changed to the trailing
5172            ## end phase is either "after body" or "after frameset".            ## end phase is either "after body" or "after frameset".
5173            $reconstruct_active_formatting_elements->($insert_to_current)            $reconstruct_active_formatting_elements->($insert_to_current);
             if $phase eq 'main';  
5174                        
5175            $open_elements->[-1]->[0]->manakai_append_text ($data);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);
5176                        
5177            unless (length $token->{data}) {            unless (length $token->{data}) {
5178              !!!next-token;              !!!next-token;
# Line 4659  sub _construct_tree ($) { Line 5180  sub _construct_tree ($) {
5180            }            }
5181          }          }
5182    
5183          !!!parse-error;          !!!parse-error (type => 'after html:#character');
5184          $phase = 'main';          $self->{insertion_mode} = $previous_insertion_mode;
5185            ## reprocess
5186            redo B;
5187          } elsif ($token->{type} eq 'start tag') {
5188            !!!parse-error (type => 'after html:'.$token->{tag_name});
5189            $self->{insertion_mode} = $previous_insertion_mode;
5190          ## reprocess          ## reprocess
5191          redo B;          redo B;
5192        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{type} eq 'end tag') {
5193                 $token->{type} eq 'end tag') {          !!!parse-error (type => 'after html:/'.$token->{tag_name});
5194          !!!parse-error;          $self->{insertion_mode} = $previous_insertion_mode;
         $phase = 'main';  
5195          ## reprocess          ## reprocess
5196          redo B;          redo B;
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
5197        } else {        } else {
5198          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token";
5199        }        }
5200        } else {
5201          die "$0: $self->{insertion_mode}: Unknown insertion mode";
5202      }      }
5203    } # B    } # B
5204    
5205    ## Stop parsing # MUST    ## Stop parsing # MUST
5206        
5207    ## TODO: script stuffs    ## TODO: script stuffs
5208  } # _construct_tree  } # _tree_construct_main
5209    
5210    sub set_inner_html ($$$) {
5211      my $class = shift;
5212      my $node = shift;
5213      my $s = \$_[0];
5214      my $onerror = $_[1];
5215    
5216      my $nt = $node->node_type;
5217      if ($nt == 9) {
5218        # MUST
5219        
5220        ## Step 1 # MUST
5221        ## TODO: If the document has an active parser, ...
5222        ## ISSUE: There is an issue in the spec.
5223        
5224        ## Step 2 # MUST
5225        my @cn = @{$node->child_nodes};
5226        for (@cn) {
5227          $node->remove_child ($_);
5228        }
5229    
5230        ## Step 3, 4, 5 # MUST
5231        $class->parse_string ($$s => $node, $onerror);
5232      } elsif ($nt == 1) {
5233        ## TODO: If non-html element
5234    
5235        ## NOTE: Most of this code is copied from |parse_string|
5236    
5237        ## Step 1 # MUST
5238        my $this_doc = $node->owner_document;
5239        my $doc = $this_doc->implementation->create_document;
5240        $doc->manakai_is_html (1);
5241        my $p = $class->new;
5242        $p->{document} = $doc;
5243    
5244        ## Step 9 # MUST
5245        my $i = 0;
5246        my $line = 1;
5247        my $column = 0;
5248        $p->{set_next_input_character} = sub {
5249          my $self = shift;
5250    
5251          pop @{$self->{prev_input_character}};
5252          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5253    
5254          $self->{next_input_character} = -1 and return if $i >= length $$s;
5255          $self->{next_input_character} = ord substr $$s, $i++, 1;
5256          $column++;
5257    
5258          if ($self->{next_input_character} == 0x000A) { # LF
5259            $line++;
5260            $column = 0;
5261          } elsif ($self->{next_input_character} == 0x000D) { # CR
5262            $i++ if substr ($$s, $i, 1) eq "\x0A";
5263            $self->{next_input_character} = 0x000A; # LF # MUST
5264            $line++;
5265            $column = 0;
5266          } elsif ($self->{next_input_character} > 0x10FFFF) {
5267            $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5268          } elsif ($self->{next_input_character} == 0x0000) { # NULL
5269            !!!parse-error (type => 'NULL');
5270            $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5271          }
5272        };
5273        $p->{prev_input_character} = [-1, -1, -1];
5274        $p->{next_input_character} = -1;
5275        
5276        my $ponerror = $onerror || sub {
5277          my (%opt) = @_;
5278          warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";
5279        };
5280        $p->{parse_error} = sub {
5281          $ponerror->(@_, line => $line, column => $column);
5282        };
5283        
5284        $p->_initialize_tokenizer;
5285        $p->_initialize_tree_constructor;
5286    
5287        ## Step 2
5288        my $node_ln = $node->local_name;
5289        $p->{content_model} = {
5290          title => RCDATA_CONTENT_MODEL,
5291          textarea => RCDATA_CONTENT_MODEL,
5292          style => CDATA_CONTENT_MODEL,
5293          script => CDATA_CONTENT_MODEL,
5294          xmp => CDATA_CONTENT_MODEL,
5295          iframe => CDATA_CONTENT_MODEL,
5296          noembed => CDATA_CONTENT_MODEL,
5297          noframes => CDATA_CONTENT_MODEL,
5298          noscript => CDATA_CONTENT_MODEL,
5299          plaintext => PLAINTEXT_CONTENT_MODEL,
5300        }->{$node_ln};
5301        $p->{content_model} = PCDATA_CONTENT_MODEL
5302            unless defined $p->{content_model};
5303            ## ISSUE: What is "the name of the element"? local name?
5304    
5305        $p->{inner_html_node} = [$node, $node_ln];
5306    
5307        ## Step 4
5308        my $root = $doc->create_element_ns
5309          ('http://www.w3.org/1999/xhtml', [undef, 'html']);
5310    
5311        ## Step 5 # MUST
5312        $doc->append_child ($root);
5313    
5314        ## Step 6 # MUST
5315        push @{$p->{open_elements}}, [$root, 'html'];
5316    
5317        undef $p->{head_element};
5318    
5319        ## Step 7 # MUST
5320        $p->_reset_insertion_mode;
5321    
5322        ## Step 8 # MUST
5323        my $anode = $node;
5324        AN: while (defined $anode) {
5325          if ($anode->node_type == 1) {
5326            my $nsuri = $anode->namespace_uri;
5327            if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5328              if ($anode->local_name eq 'form') { ## TODO: case?
5329                $p->{form_element} = $anode;
5330                last AN;
5331              }
5332            }
5333          }
5334          $anode = $anode->parent_node;
5335        } # AN
5336        
5337        ## Step 3 # MUST
5338        ## Step 10 # MUST
5339        {
5340          my $self = $p;
5341          !!!next-token;
5342        }
5343        $p->_tree_construction_main;
5344    
5345        ## Step 11 # MUST
5346        my @cn = @{$node->child_nodes};
5347        for (@cn) {
5348          $node->remove_child ($_);
5349        }
5350        ## ISSUE: mutation events? read-only?
5351    
5352        ## Step 12 # MUST
5353        @cn = @{$root->child_nodes};
5354        for (@cn) {
5355          $this_doc->adopt_node ($_);
5356          $node->append_child ($_);
5357        }
5358        ## ISSUE: mutation events?
5359    
5360        $p->_terminate_tree_constructor;
5361      } else {
5362        die "$0: |set_inner_html| is not defined for node of type $nt";
5363      }
5364    } # set_inner_html
5365    
5366    } # tree construction stage
5367    
5368  sub get_inner_html ($$$) {  sub get_inner_html ($$$) {
5369    my ($class, $node, $on_error) = @_;    my (undef, $node, $on_error) = @_;
5370    
5371    ## Step 1    ## Step 1
5372    my $s = '';    my $s = '';
# Line 4718  sub get_inner_html ($$$) { Line 5400  sub get_inner_html ($$$) {
5400            
5401      my $nt = $child->node_type;      my $nt = $child->node_type;
5402      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5403        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5404        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5405          ## NOTE: Non-HTML case:
5406        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5407    
5408        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5409        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5410          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5411          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5412          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5413          ## escape          ## escape
# Line 4744  sub get_inner_html ($$$) { Line 5426  sub get_inner_html ($$$) {
5426          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5427        }->{$tag_name};        }->{$tag_name};
5428    
5429          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5430    
5431        if (not $in_cdata and {        if (not $in_cdata and {
5432          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5433          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5434            plaintext => 1,
5435        }->{$tag_name}) {        }->{$tag_name}) {
5436          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5437          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.42

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24