/[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.14 by wakaba, Sat Jun 23 06:38:12 2007 UTC revision 1.116 by wakaba, Mon Mar 17 13:23:39 2008 UTC
# Line 1  Line 1 
1  package Whatpm::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    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  ## ISSUE:
7    ## var doc = implementation.createDocument (null, null, null);
8    ## doc.write ('');
9    ## alert (doc.compatMode);
10    
11    ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12    ## TODO: 1252 parse error (revision 1264)
13    ## TODO: 8859-11 = 874 (revision 1271)
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
# Line 10  my $permitted_slash_tag_name = { Line 18  my $permitted_slash_tag_name = {
18    meta => 1,    meta => 1,
19    hr => 1,    hr => 1,
20    br => 1,    br => 1,
21    img=> 1,    img => 1,
22    embed => 1,    embed => 1,
23    param => 1,    param => 1,
24    area => 1,    area => 1,
# Line 18  my $permitted_slash_tag_name = { Line 26  my $permitted_slash_tag_name = {
26    input => 1,    input => 1,
27  };  };
28    
 my $entity_char = {  
   AElig => "\x{00C6}",  
   Aacute => "\x{00C1}",  
   Acirc => "\x{00C2}",  
   Agrave => "\x{00C0}",  
   Alpha => "\x{0391}",  
   Aring => "\x{00C5}",  
   Atilde => "\x{00C3}",  
   Auml => "\x{00C4}",  
   Beta => "\x{0392}",  
   Ccedil => "\x{00C7}",  
   Chi => "\x{03A7}",  
   Dagger => "\x{2021}",  
   Delta => "\x{0394}",  
   ETH => "\x{00D0}",  
   Eacute => "\x{00C9}",  
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 }; # $entity_char  
   
29  my $c1_entity_char = {  my $c1_entity_char = {
30    0x80 => 0x20AC,    0x80 => 0x20AC,
31    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 328  my $special_category = { Line 74  my $special_category = {
74    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,
75  };  };
76  my $scoping_category = {  my $scoping_category = {
77    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,
78    table => 1, td => 1, th => 1,    table => 1, td => 1, th => 1,
79  };  };
80  my $formatting_category = {  my $formatting_category = {
# Line 337  my $formatting_category = { Line 83  my $formatting_category = {
83  };  };
84  # $phrasing_category: all other elements  # $phrasing_category: all other elements
85    
86    sub parse_byte_string ($$$$;$) {
87      my $self = ref $_[0] ? shift : shift->new;
88      my $charset = shift;
89      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
90      my $s;
91      
92      if (defined $charset) {
93        require Encode; ## TODO: decode(utf8) don't delete BOM
94        $s = \ (Encode::decode ($charset, $$bytes_s));
95        $self->{input_encoding} = lc $charset; ## TODO: normalize name
96        $self->{confident} = 1;
97      } else {
98        ## TODO: Implement HTML5 detection algorithm
99        require Whatpm::Charset::UniversalCharDet;
100        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
101            (substr ($$bytes_s, 0, 1024));
102        $charset ||= 'windows-1252';
103        $s = \ (Encode::decode ($charset, $$bytes_s));
104        $self->{input_encoding} = $charset;
105        $self->{confident} = 0;
106      }
107    
108      $self->{change_encoding} = sub {
109        my $self = shift;
110        my $charset = lc shift;
111        my $token = shift;
112        ## TODO: if $charset is supported
113        ## TODO: normalize charset name
114    
115        ## "Change the encoding" algorithm:
116    
117        ## Step 1    
118        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
119          $charset = 'utf-8';
120        }
121    
122        ## Step 2
123        if (defined $self->{input_encoding} and
124            $self->{input_encoding} eq $charset) {
125          $self->{confident} = 1;
126          return;
127        }
128    
129        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
130            ':'.$charset, level => 'w', token => $token);
131    
132        ## Step 3
133        # if (can) {
134          ## change the encoding on the fly.
135          #$self->{confident} = 1;
136          #return;
137        # }
138    
139        ## Step 4
140        throw Whatpm::HTML::RestartParser (charset => $charset);
141      }; # $self->{change_encoding}
142    
143      my @args = @_; shift @args; # $s
144      my $return;
145      try {
146        $return = $self->parse_char_string ($s, @args);  
147      } catch Whatpm::HTML::RestartParser with {
148        my $charset = shift->{charset};
149        $s = \ (Encode::decode ($charset, $$bytes_s));    
150        $self->{input_encoding} = $charset; ## TODO: normalize
151        $self->{confident} = 1;
152        $return = $self->parse_char_string ($s, @args);
153      };
154      return $return;
155    } # parse_byte_string
156    
157    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
158    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
159    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
160    ## because the core part of our HTML parser expects a string of character,
161    ## not a string of bytes or code units or anything which might contain a BOM.
162    ## Therefore, any parser interface that accepts a string of bytes,
163    ## such as |parse_byte_string| in this module, must ensure that it does
164    ## strip the BOM and never strip any ZWNBSP.
165    
166    *parse_char_string = \&parse_string;
167    
168  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
169    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
170    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
171    $self->{document} = $_[1];    $self->{document} = $_[1];
172      @{$self->{document}->child_nodes} = ();
173    
174    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
175    
176      $self->{confident} = 1 unless exists $self->{confident};
177      $self->{document}->input_encoding ($self->{input_encoding})
178          if defined $self->{input_encoding};
179    
180    my $i = 0;    my $i = 0;
181    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
182    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
183    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
184      my $self = shift;      my $self = shift;
185    
186      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
187      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
188    
189      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
190      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
191      $column++;  
192        ($self->{line_prev}, $self->{column_prev})
193            = ($self->{line}, $self->{column});
194        $self->{column}++;
195            
196      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
197        $line++;        $self->{line}++;
198        $column = 0;        $self->{column} = 0;
199      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
200        if ($i >= length $$s) {        $i++ if substr ($$s, $i, 1) eq "\x0A";
201          #        $self->{next_char} = 0x000A; # LF # MUST
202        } else {        $self->{line}++;
203          my $next_char = ord substr $$s, $i++, 1;        $self->{column} = 0;
204          if ($next_char == 0x000A) { # LF      } elsif ($self->{next_char} > 0x10FFFF) {
205            #        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
206          } else {      } elsif ($self->{next_char} == 0x0000) { # NULL
           push @{$self->{char}}, $next_char;  
         }  
       }  
       $self->{next_input_character} = 0x000A; # LF # MUST  
       $line++;  
       $column = 0;  
     } elsif ($self->{next_input_character} > 0x10FFFF) {  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_input_character} == 0x0000) { # NULL  
207        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
208        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
209      }      }
210    };    };
211    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
212    $self->{next_input_character} = -1;    $self->{next_char} = -1;
213    
214    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
215      my (%opt) = @_;      my (%opt) = @_;
216      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
217        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
218        warn "Parse error ($opt{type}) at line $line column $column\n";
219    };    };
220    $self->{parse_error} = sub {    $self->{parse_error} = sub {
221      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
222    };    };
223    
224    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 397  sub parse_string ($$$;$) { Line 226  sub parse_string ($$$;$) {
226    $self->_construct_tree;    $self->_construct_tree;
227    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
228    
229      delete $self->{parse_error}; # remove loop
230    
231    return $self->{document};    return $self->{document};
232  } # parse_string  } # parse_string
233    
234  sub new ($) {  sub new ($) {
235    my $class = shift;    my $class = shift;
236    my $self = bless {}, $class;    my $self = bless {}, $class;
237    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
238      $self->{next_input_character} = -1;      $self->{next_char} = -1;
239    };    };
240    $self->{parse_error} = sub {    $self->{parse_error} = sub {
241      #      #
242    };    };
243      $self->{change_encoding} = sub {
244        # if ($_[0] is a supported encoding) {
245        #   run "change the encoding" algorithm;
246        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
247        # }
248      };
249      $self->{application_cache_selection} = sub {
250        #
251      };
252    return $self;    return $self;
253  } # new  } # new
254    
255    sub CM_ENTITY () { 0b001 } # & markup in data
256    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
257    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
258    
259    sub PLAINTEXT_CONTENT_MODEL () { 0 }
260    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
261    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
262    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
263    
264    sub DATA_STATE () { 0 }
265    sub ENTITY_DATA_STATE () { 1 }
266    sub TAG_OPEN_STATE () { 2 }
267    sub CLOSE_TAG_OPEN_STATE () { 3 }
268    sub TAG_NAME_STATE () { 4 }
269    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
270    sub ATTRIBUTE_NAME_STATE () { 6 }
271    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
272    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
273    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
274    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
275    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
276    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
277    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
278    sub COMMENT_START_STATE () { 14 }
279    sub COMMENT_START_DASH_STATE () { 15 }
280    sub COMMENT_STATE () { 16 }
281    sub COMMENT_END_STATE () { 17 }
282    sub COMMENT_END_DASH_STATE () { 18 }
283    sub BOGUS_COMMENT_STATE () { 19 }
284    sub DOCTYPE_STATE () { 20 }
285    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
286    sub DOCTYPE_NAME_STATE () { 22 }
287    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
288    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
289    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
290    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
291    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
292    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
293    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
294    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
295    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
296    sub BOGUS_DOCTYPE_STATE () { 32 }
297    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
298    
299    sub DOCTYPE_TOKEN () { 1 }
300    sub COMMENT_TOKEN () { 2 }
301    sub START_TAG_TOKEN () { 3 }
302    sub END_TAG_TOKEN () { 4 }
303    sub END_OF_FILE_TOKEN () { 5 }
304    sub CHARACTER_TOKEN () { 6 }
305    
306    sub AFTER_HTML_IMS () { 0b100 }
307    sub HEAD_IMS ()       { 0b1000 }
308    sub BODY_IMS ()       { 0b10000 }
309    sub BODY_TABLE_IMS () { 0b100000 }
310    sub TABLE_IMS ()      { 0b1000000 }
311    sub ROW_IMS ()        { 0b10000000 }
312    sub BODY_AFTER_IMS () { 0b100000000 }
313    sub FRAME_IMS ()      { 0b1000000000 }
314    sub SELECT_IMS ()     { 0b10000000000 }
315    
316    ## NOTE: "initial" and "before html" insertion modes have no constants.
317    
318    ## NOTE: "after after body" insertion mode.
319    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
320    
321    ## NOTE: "after after frameset" insertion mode.
322    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
323    
324    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
325    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
326    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
327    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
328    sub IN_BODY_IM () { BODY_IMS }
329    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
330    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
331    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
332    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
333    sub IN_TABLE_IM () { TABLE_IMS }
334    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
335    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
336    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
337    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
338    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
339    sub IN_COLUMN_GROUP_IM () { 0b10 }
340    
341  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
342    
343  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
344    my $self = shift;    my $self = shift;
345    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
346    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
347    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
348    undef $self->{current_attribute};    undef $self->{current_attribute};
349    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
350    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
351    $self->{char} = [];    $self->{char} = [];
352    # $self->{next_input_character}    # $self->{next_char}
353    !!!next-input-character;    !!!next-input-character;
354    $self->{token} = [];    $self->{token} = [];
355      # $self->{escape}
356  } # _initialize_tokenizer  } # _initialize_tokenizer
357    
358  ## A token has:  ## A token has:
359  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
360  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
361  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
362      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
363  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
364  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
365  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
366    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
367  ## Macros  ##        ->{name}
368  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
369  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
370  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
371    
372  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
373    
# Line 450  sub _initialize_tokenizer ($) { Line 377  sub _initialize_tokenizer ($) {
377  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
378  ## and removed from the list.  ## and removed from the list.
379    
380    ## NOTE: HTML5 "Writing HTML documents" section, applied to
381    ## documents and not to user agents and conformance checkers,
382    ## contains some requirements that are not detected by the
383    ## parsing algorithm:
384    ## - Some requirements on character encoding declarations. ## TODO
385    ## - "Elements MUST NOT contain content that their content model disallows."
386    ##   ... Some are parse error, some are not (will be reported by c.c.).
387    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
388    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
389    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
390    
391    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
392    ## be detected by the HTML5 parsing algorithm:
393    ## - Text,
394    
395  sub _get_next_token ($) {  sub _get_next_token ($) {
396    my $self = shift;    my $self = shift;
397    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 457  sub _get_next_token ($) { Line 399  sub _get_next_token ($) {
399    }    }
400    
401    A: {    A: {
402      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
403        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
404          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
405              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
406            $self->{state} = 'entity data';            !!!cp (1);
407              $self->{state} = ENTITY_DATA_STATE;
408            !!!next-input-character;            !!!next-input-character;
409            redo A;            redo A;
410          } else {          } else {
411              !!!cp (2);
412            #            #
413          }          }
414        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
415          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
416            unless ($self->{escape}) {            unless ($self->{escape}) {
417              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
418                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
419                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
420                  !!!cp (3);
421                $self->{escape} = 1;                $self->{escape} = 1;
422                } else {
423                  !!!cp (4);
424              }              }
425              } else {
426                !!!cp (5);
427            }            }
428          }          }
429                    
430          #          #
431        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
432          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
433              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
434               not $self->{escape})) {               not $self->{escape})) {
435            $self->{state} = 'tag open';            !!!cp (6);
436              $self->{state} = TAG_OPEN_STATE;
437            !!!next-input-character;            !!!next-input-character;
438            redo A;            redo A;
439          } else {          } else {
440              !!!cp (7);
441            #            #
442          }          }
443        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
444          if ($self->{escape} and          if ($self->{escape} and
445              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
446               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
447            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
448                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
449              delete $self->{escape};              delete $self->{escape};
450              } else {
451                !!!cp (9);
452            }            }
453            } else {
454              !!!cp (10);
455          }          }
456                    
457          #          #
458        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
459          !!!emit ({type => 'end-of-file'});          !!!cp (11);
460            !!!emit ({type => END_OF_FILE_TOKEN,
461                      line => $self->{line}, column => $self->{column}});
462          last A; ## TODO: ok?          last A; ## TODO: ok?
463          } else {
464            !!!cp (12);
465        }        }
466        # Anything else        # Anything else
467        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
468                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
469                       line => $self->{line}, column => $self->{column}};
470        ## Stay in the data state        ## Stay in the data state
471        !!!next-input-character;        !!!next-input-character;
472    
473        !!!emit ($token);        !!!emit ($token);
474    
475        redo A;        redo A;
476      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
477        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
478    
479          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
480                
481        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
482    
483        $self->{state} = 'data';        $self->{state} = DATA_STATE;
484        # next-input-character is already done        # next-input-character is already done
485    
486        unless (defined $token) {        unless (defined $token) {
487          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
488            !!!emit ({type => CHARACTER_TOKEN, data => '&',
489                      line => $l, column => $c});
490        } else {        } else {
491            !!!cp (14);
492          !!!emit ($token);          !!!emit ($token);
493        }        }
494    
495        redo A;        redo A;
496      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
497        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
498            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
499          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
500            !!!next-input-character;            !!!next-input-character;
501            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
502            redo A;            redo A;
503          } else {          } else {
504              !!!cp (16);
505            ## reconsume            ## reconsume
506            $self->{state} = 'data';            $self->{state} = DATA_STATE;
507    
508            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
509                        line => $self->{line_prev},
510                        column => $self->{column_prev}});
511    
512            redo A;            redo A;
513          }          }
514        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
515          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
516            $self->{state} = 'markup declaration open';            !!!cp (17);
517              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
518            !!!next-input-character;            !!!next-input-character;
519            redo A;            redo A;
520          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
521            $self->{state} = 'close tag open';            !!!cp (18);
522              $self->{state} = CLOSE_TAG_OPEN_STATE;
523            !!!next-input-character;            !!!next-input-character;
524            redo A;            redo A;
525          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
526                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
527              !!!cp (19);
528            $self->{current_token}            $self->{current_token}
529              = {type => 'start tag',              = {type => START_TAG_TOKEN,
530                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
531            $self->{state} = 'tag name';                 line => $self->{line_prev},
532                   column => $self->{column_prev}};
533              $self->{state} = TAG_NAME_STATE;
534            !!!next-input-character;            !!!next-input-character;
535            redo A;            redo A;
536          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
537                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
538            $self->{current_token} = {type => 'start tag',            !!!cp (20);
539                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
540            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
541                                        line => $self->{line_prev},
542                                        column => $self->{column_prev}};
543              $self->{state} = TAG_NAME_STATE;
544            !!!next-input-character;            !!!next-input-character;
545            redo A;            redo A;
546          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
547            !!!parse-error (type => 'empty start tag');            !!!cp (21);
548            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
549                              line => $self->{line_prev},
550                              column => $self->{column_prev});
551              $self->{state} = DATA_STATE;
552            !!!next-input-character;            !!!next-input-character;
553    
554            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
555                        line => $self->{line_prev},
556                        column => $self->{column_prev}});
557    
558            redo A;            redo A;
559          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
560            !!!parse-error (type => 'pio');            !!!cp (22);
561            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
562            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
563                              column => $self->{column_prev});
564              $self->{state} = BOGUS_COMMENT_STATE;
565              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
566                                        line => $self->{line_prev},
567                                        column => $self->{column_prev}};
568              ## $self->{next_char} is intentionally left as is
569            redo A;            redo A;
570          } else {          } else {
571              !!!cp (23);
572            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
573            $self->{state} = 'data';            $self->{state} = DATA_STATE;
574            ## reconsume            ## reconsume
575    
576            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
577                        line => $self->{line_prev},
578                        column => $self->{column_prev}});
579    
580            redo A;            redo A;
581          }          }
582        } else {        } else {
583          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
584        }        }
585      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
586        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
587            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
588          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
589          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
590            push @next_char, $self->{next_input_character};            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
591            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            my @next_char;
592            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
593            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              push @next_char, $self->{next_char};
594              !!!next-input-character;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
595              next TAGNAME;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
596            } else {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
597              !!!parse-error (type => 'unmatched end tag');                !!!cp (24);
598              $self->{next_input_character} = shift @next_char; # reconsume                !!!next-input-character;
599              !!!back-next-input-character (@next_char);                next TAGNAME;
600              $self->{state} = 'data';              } else {
601                  !!!cp (25);
602              !!!emit ({type => 'character', data => '</'});                $self->{next_char} = shift @next_char; # reconsume
603                  !!!back-next-input-character (@next_char);
604                  $self->{state} = DATA_STATE;
605    
606                  !!!emit ({type => CHARACTER_TOKEN, data => '</',
607                            line => $l, column => $c});
608      
609                  redo A;
610                }
611              }
612              push @next_char, $self->{next_char};
613          
614              unless ($self->{next_char} == 0x0009 or # HT
615                      $self->{next_char} == 0x000A or # LF
616                      $self->{next_char} == 0x000B or # VT
617                      $self->{next_char} == 0x000C or # FF
618                      $self->{next_char} == 0x0020 or # SP
619                      $self->{next_char} == 0x003E or # >
620                      $self->{next_char} == 0x002F or # /
621                      $self->{next_char} == -1) {
622                !!!cp (26);
623                $self->{next_char} = shift @next_char; # reconsume
624                !!!back-next-input-character (@next_char);
625                $self->{state} = DATA_STATE;
626                !!!emit ({type => CHARACTER_TOKEN, data => '</',
627                          line => $l, column => $c});
628              redo A;              redo A;
629              } else {
630                !!!cp (27);
631                $self->{next_char} = shift @next_char;
632                !!!back-next-input-character (@next_char);
633                # and consume...
634            }            }
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error (type => 'unmatched end tag');  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
           redo A;  
635          } else {          } else {
636            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
637            !!!back-next-input-character (@next_char);            !!!cp (28);
638            # and consume...            # next-input-character is already done
639              $self->{state} = DATA_STATE;
640              !!!emit ({type => CHARACTER_TOKEN, data => '</',
641                        line => $l, column => $c});
642              redo A;
643          }          }
644        }        }
645                
646        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
647            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
648          $self->{current_token} = {type => 'end tag',          !!!cp (29);
649                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
650          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
651          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
652          redo A;                 line => $l, column => $c};
653        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
654                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
655          $self->{current_token} = {type => 'end tag',          redo A;
656                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
657          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
658            !!!cp (30);
659            $self->{current_token} = {type => END_TAG_TOKEN,
660                                      tag_name => chr ($self->{next_char}),
661                                      line => $l, column => $c};
662            $self->{state} = TAG_NAME_STATE;
663            !!!next-input-character;
664            redo A;
665          } elsif ($self->{next_char} == 0x003E) { # >
666            !!!cp (31);
667            !!!parse-error (type => 'empty end tag',
668                            line => $self->{line_prev}, ## "<" in "</>"
669                            column => $self->{column_prev} - 1);
670            $self->{state} = DATA_STATE;
671          !!!next-input-character;          !!!next-input-character;
672          redo A;          redo A;
673        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == -1) {
674          !!!parse-error (type => 'empty end tag');          !!!cp (32);
         $self->{state} = 'data';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
675          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
676          $self->{state} = 'data';          $self->{state} = DATA_STATE;
677          # reconsume          # reconsume
678    
679          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
680                      line => $l, column => $c});
681    
682          redo A;          redo A;
683        } else {        } else {
684            !!!cp (33);
685          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
686          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
687          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
688          redo A;                                    line => $self->{line_prev}, # "<" of "</"
689        }                                    column => $self->{column_prev} - 1};
690      } elsif ($self->{state} eq 'tag name') {          ## $self->{next_char} is intentionally left as is
691        if ($self->{next_input_character} == 0x0009 or # HT          redo A;
692            $self->{next_input_character} == 0x000A or # LF        }
693            $self->{next_input_character} == 0x000B or # VT      } elsif ($self->{state} == TAG_NAME_STATE) {
694            $self->{next_input_character} == 0x000C or # FF        if ($self->{next_char} == 0x0009 or # HT
695            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000A or # LF
696          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000B or # VT
697          !!!next-input-character;            $self->{next_char} == 0x000C or # FF
698          redo A;            $self->{next_char} == 0x0020) { # SP
699        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!cp (34);
700          if ($self->{current_token}->{type} eq 'start tag') {          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
701            !!!next-input-character;
702            redo A;
703          } elsif ($self->{next_char} == 0x003E) { # >
704            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
705              !!!cp (35);
706              $self->{current_token}->{first_start_tag}
707                  = not defined $self->{last_emitted_start_tag_name};
708            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
709          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
710            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
711            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
712              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
713            }            #  !!! cp (36);
714              #  !!! parse-error (type => 'end tag attribute');
715              #} else {
716                !!!cp (37);
717              #}
718          } else {          } else {
719            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
720          }          }
721          $self->{state} = 'data';          $self->{state} = DATA_STATE;
722          !!!next-input-character;          !!!next-input-character;
723    
724          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
725    
726          redo A;          redo A;
727        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
728                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
729          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
730            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
731            # start tag or end tag            # start tag or end tag
732          ## Stay in this state          ## Stay in this state
733          !!!next-input-character;          !!!next-input-character;
734          redo A;          redo A;
735        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
736          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
737          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
738              !!!cp (39);
739              $self->{current_token}->{first_start_tag}
740                  = not defined $self->{last_emitted_start_tag_name};
741            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
742          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
743            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
744            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
745              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
746            }            #  !!! cp (40);
747              #  !!! parse-error (type => 'end tag attribute');
748              #} else {
749                !!!cp (41);
750              #}
751          } else {          } else {
752            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
753          }          }
754          $self->{state} = 'data';          $self->{state} = DATA_STATE;
755          # reconsume          # reconsume
756    
757          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
758    
759          redo A;          redo A;
760        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
761          !!!next-input-character;          !!!next-input-character;
762          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
763              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
764              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
765            # permitted slash            # permitted slash
766              !!!cp (42);
767            #            #
768          } else {          } else {
769              !!!cp (43);
770            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
771          }          }
772          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
773          # next-input-character is already done          # next-input-character is already done
774          redo A;          redo A;
775        } else {        } else {
776          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
777            $self->{current_token}->{tag_name} .= chr $self->{next_char};
778            # start tag or end tag            # start tag or end tag
779          ## Stay in the state          ## Stay in the state
780          !!!next-input-character;          !!!next-input-character;
781          redo A;          redo A;
782        }        }
783      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
784        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
785            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
786            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
787            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
788            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
789            !!!cp (45);
790          ## Stay in the state          ## Stay in the state
791          !!!next-input-character;          !!!next-input-character;
792          redo A;          redo A;
793        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
794          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
795              !!!cp (46);
796              $self->{current_token}->{first_start_tag}
797                  = not defined $self->{last_emitted_start_tag_name};
798            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
799          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
800            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
801            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
802                !!!cp (47);
803              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
804              } else {
805                !!!cp (48);
806            }            }
807          } else {          } else {
808            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
809          }          }
810          $self->{state} = 'data';          $self->{state} = DATA_STATE;
811          !!!next-input-character;          !!!next-input-character;
812    
813          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
814    
815          redo A;          redo A;
816        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
817                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
818          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
819            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
820                                value => ''};                                value => ''};
821          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
822          !!!next-input-character;          !!!next-input-character;
823          redo A;          redo A;
824        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
825          !!!next-input-character;          !!!next-input-character;
826          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
827              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
828              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
829            # permitted slash            # permitted slash
830              !!!cp (50);
831            #            #
832          } else {          } else {
833              !!!cp (51);
834            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
835          }          }
836          ## Stay in the state          ## Stay in the state
837          # next-input-character is already done          # next-input-character is already done
838          redo A;          redo A;
839        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
840          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
841          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
842              !!!cp (52);
843              $self->{current_token}->{first_start_tag}
844                  = not defined $self->{last_emitted_start_tag_name};
845            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
846          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
847            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
848            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
849                !!!cp (53);
850              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
851              } else {
852                !!!cp (54);
853            }            }
854          } else {          } else {
855            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
856          }          }
857          $self->{state} = 'data';          $self->{state} = DATA_STATE;
858          # reconsume          # reconsume
859    
860          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
861    
862          redo A;          redo A;
863        } else {        } else {
864          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
865                 0x0022 => 1, # "
866                 0x0027 => 1, # '
867                 0x003D => 1, # =
868                }->{$self->{next_char}}) {
869              !!!cp (55);
870              !!!parse-error (type => 'bad attribute name');
871            } else {
872              !!!cp (56);
873            }
874            $self->{current_attribute} = {name => chr ($self->{next_char}),
875                                value => ''};                                value => ''};
876          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
877          !!!next-input-character;          !!!next-input-character;
878          redo A;          redo A;
879        }        }
880      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
881        my $before_leave = sub {        my $before_leave = sub {
882          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
883              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
884            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
885              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
886            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
887          } else {          } else {
888              !!!cp (58);
889            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
890              = $self->{current_attribute};              = $self->{current_attribute};
891          }          }
892        }; # $before_leave        }; # $before_leave
893    
894        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
895            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
896            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
897            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
898            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
899            !!!cp (59);
900          $before_leave->();          $before_leave->();
901          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
902          !!!next-input-character;          !!!next-input-character;
903          redo A;          redo A;
904        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
905            !!!cp (60);
906          $before_leave->();          $before_leave->();
907          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
908          !!!next-input-character;          !!!next-input-character;
909          redo A;          redo A;
910        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
911          $before_leave->();          $before_leave->();
912          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
913              !!!cp (61);
914              $self->{current_token}->{first_start_tag}
915                  = not defined $self->{last_emitted_start_tag_name};
916            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
917          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
918            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
919              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
920            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
921              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
922            }            }
923          } else {          } else {
924            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
925          }          }
926          $self->{state} = 'data';          $self->{state} = DATA_STATE;
927          !!!next-input-character;          !!!next-input-character;
928    
929          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
930    
931          redo A;          redo A;
932        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
933                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
934          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
935            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
936          ## Stay in the state          ## Stay in the state
937          !!!next-input-character;          !!!next-input-character;
938          redo A;          redo A;
939        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
940          $before_leave->();          $before_leave->();
941          !!!next-input-character;          !!!next-input-character;
942          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
943              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
944              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
945            # permitted slash            # permitted slash
946              !!!cp (64);
947            #            #
948          } else {          } else {
949              !!!cp (65);
950            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
951          }          }
952          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
953          # next-input-character is already done          # next-input-character is already done
954          redo A;          redo A;
955        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
956          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
957          $before_leave->();          $before_leave->();
958          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
959              !!!cp (66);
960              $self->{current_token}->{first_start_tag}
961                  = not defined $self->{last_emitted_start_tag_name};
962            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
963          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
964            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
965            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
966                !!!cp (67);
967              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
968              } else {
969                ## NOTE: This state should never be reached.
970                !!!cp (68);
971            }            }
972          } else {          } else {
973            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
974          }          }
975          $self->{state} = 'data';          $self->{state} = DATA_STATE;
976          # reconsume          # reconsume
977    
978          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
979    
980          redo A;          redo A;
981        } else {        } else {
982          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
983                $self->{next_char} == 0x0027) { # '
984              !!!cp (69);
985              !!!parse-error (type => 'bad attribute name');
986            } else {
987              !!!cp (70);
988            }
989            $self->{current_attribute}->{name} .= chr ($self->{next_char});
990          ## Stay in the state          ## Stay in the state
991          !!!next-input-character;          !!!next-input-character;
992          redo A;          redo A;
993        }        }
994      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
995        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
996            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
997            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
998            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
999            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1000            !!!cp (71);
1001          ## Stay in the state          ## Stay in the state
1002          !!!next-input-character;          !!!next-input-character;
1003          redo A;          redo A;
1004        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1005          $self->{state} = 'before attribute value';          !!!cp (72);
1006            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1007          !!!next-input-character;          !!!next-input-character;
1008          redo A;          redo A;
1009        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1010          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1011              !!!cp (73);
1012              $self->{current_token}->{first_start_tag}
1013                  = not defined $self->{last_emitted_start_tag_name};
1014            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1015          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1016            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1017            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1018                !!!cp (74);
1019              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1020              } else {
1021                ## NOTE: This state should never be reached.
1022                !!!cp (75);
1023            }            }
1024          } else {          } else {
1025            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1026          }          }
1027          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1028          !!!next-input-character;          !!!next-input-character;
1029    
1030          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1031    
1032          redo A;          redo A;
1033        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1034                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1035          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1036            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
1037                                value => ''};                                value => ''};
1038          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1039          !!!next-input-character;          !!!next-input-character;
1040          redo A;          redo A;
1041        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1042          !!!next-input-character;          !!!next-input-character;
1043          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1044              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
1045              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1046            # permitted slash            # permitted slash
1047              !!!cp (77);
1048            #            #
1049          } else {          } else {
1050              !!!cp (78);
1051            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1052              ## TODO: Different error type for <aa / bb> than <aa/>
1053          }          }
1054          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1055          # next-input-character is already done          # next-input-character is already done
1056          redo A;          redo A;
1057        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1058          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1059          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1060              !!!cp (79);
1061              $self->{current_token}->{first_start_tag}
1062                  = not defined $self->{last_emitted_start_tag_name};
1063            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1064          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1065            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1066            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1067                !!!cp (80);
1068              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1069              } else {
1070                ## NOTE: This state should never be reached.
1071                !!!cp (81);
1072            }            }
1073          } else {          } else {
1074            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1075          }          }
1076          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1077          # reconsume          # reconsume
1078    
1079          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1080    
1081          redo A;          redo A;
1082        } else {        } else {
1083          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1084            $self->{current_attribute} = {name => chr ($self->{next_char}),
1085                                value => ''};                                value => ''};
1086          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1087          !!!next-input-character;          !!!next-input-character;
1088          redo A;                  redo A;        
1089        }        }
1090      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1091        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1092            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1093            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1094            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1095            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1096            !!!cp (83);
1097          ## Stay in the state          ## Stay in the state
1098          !!!next-input-character;          !!!next-input-character;
1099          redo A;          redo A;
1100        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1101          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1102            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1103          !!!next-input-character;          !!!next-input-character;
1104          redo A;          redo A;
1105        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1106          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1107            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1108          ## reconsume          ## reconsume
1109          redo A;          redo A;
1110        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1111          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1112            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1113          !!!next-input-character;          !!!next-input-character;
1114          redo A;          redo A;
1115        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1116          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1117              !!!cp (87);
1118              $self->{current_token}->{first_start_tag}
1119                  = not defined $self->{last_emitted_start_tag_name};
1120            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1121          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1122            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1123            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1124                !!!cp (88);
1125              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1126              } else {
1127                ## NOTE: This state should never be reached.
1128                !!!cp (89);
1129            }            }
1130          } else {          } else {
1131            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1132          }          }
1133          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1134          !!!next-input-character;          !!!next-input-character;
1135    
1136          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1137    
1138          redo A;          redo A;
1139        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1140          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1141          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1142              !!!cp (90);
1143              $self->{current_token}->{first_start_tag}
1144                  = not defined $self->{last_emitted_start_tag_name};
1145            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1146          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1147            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1148            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1149                !!!cp (91);
1150              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1151              } else {
1152                ## NOTE: This state should never be reached.
1153                !!!cp (92);
1154            }            }
1155          } else {          } else {
1156            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1157          }          }
1158          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1159          ## reconsume          ## reconsume
1160    
1161          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1162    
1163          redo A;          redo A;
1164        } else {        } else {
1165          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1166          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1167              !!!parse-error (type => 'bad attribute value');
1168            } else {
1169              !!!cp (94);
1170            }
1171            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1172            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1173          !!!next-input-character;          !!!next-input-character;
1174          redo A;          redo A;
1175        }        }
1176      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1177        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1178          $self->{state} = 'before attribute name';          !!!cp (95);
1179            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1180          !!!next-input-character;          !!!next-input-character;
1181          redo A;          redo A;
1182        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1183          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1184          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1185            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1186          !!!next-input-character;          !!!next-input-character;
1187          redo A;          redo A;
1188        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1189          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1190          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1191              !!!cp (97);
1192              $self->{current_token}->{first_start_tag}
1193                  = not defined $self->{last_emitted_start_tag_name};
1194            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1195          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1196            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1197            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1198                !!!cp (98);
1199              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1200              } else {
1201                ## NOTE: This state should never be reached.
1202                !!!cp (99);
1203            }            }
1204          } else {          } else {
1205            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1206          }          }
1207          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1208          ## reconsume          ## reconsume
1209    
1210          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1211    
1212          redo A;          redo A;
1213        } else {        } else {
1214          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1215            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1216          ## Stay in the state          ## Stay in the state
1217          !!!next-input-character;          !!!next-input-character;
1218          redo A;          redo A;
1219        }        }
1220      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1221        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1222          $self->{state} = 'before attribute name';          !!!cp (101);
1223            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1224          !!!next-input-character;          !!!next-input-character;
1225          redo A;          redo A;
1226        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1227          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1228          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1229            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1230          !!!next-input-character;          !!!next-input-character;
1231          redo A;          redo A;
1232        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1233          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1234          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1235              !!!cp (103);
1236              $self->{current_token}->{first_start_tag}
1237                  = not defined $self->{last_emitted_start_tag_name};
1238            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1239          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1240            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1241            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1242                !!!cp (104);
1243              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1244              } else {
1245                ## NOTE: This state should never be reached.
1246                !!!cp (105);
1247            }            }
1248          } else {          } else {
1249            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1250          }          }
1251          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1252          ## reconsume          ## reconsume
1253    
1254          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1255    
1256          redo A;          redo A;
1257        } else {        } else {
1258          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1259            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1260          ## Stay in the state          ## Stay in the state
1261          !!!next-input-character;          !!!next-input-character;
1262          redo A;          redo A;
1263        }        }
1264      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1265        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1266            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1267            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1268            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1269            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1270          $self->{state} = 'before attribute name';          !!!cp (107);
1271          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1272          redo A;          !!!next-input-character;
1273        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1274          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1275          $self->{state} = 'entity in attribute value';          !!!cp (108);
1276          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1277          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1278        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1279          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1280          } elsif ($self->{next_char} == 0x003E) { # >
1281            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1282              !!!cp (109);
1283              $self->{current_token}->{first_start_tag}
1284                  = not defined $self->{last_emitted_start_tag_name};
1285            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1286          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1287            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1288            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1289                !!!cp (110);
1290              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1291              } else {
1292                ## NOTE: This state should never be reached.
1293                !!!cp (111);
1294            }            }
1295          } else {          } else {
1296            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1297          }          }
1298          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1299          !!!next-input-character;          !!!next-input-character;
1300    
1301          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1302    
1303          redo A;          redo A;
1304        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1305          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1306          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1307              !!!cp (112);
1308              $self->{current_token}->{first_start_tag}
1309                  = not defined $self->{last_emitted_start_tag_name};
1310            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1311          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1312            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1313            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1314                !!!cp (113);
1315              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1316              } else {
1317                ## NOTE: This state should never be reached.
1318                !!!cp (114);
1319            }            }
1320          } else {          } else {
1321            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1322          }          }
1323          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1324          ## reconsume          ## reconsume
1325    
1326          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1327    
1328          redo A;          redo A;
1329        } else {        } else {
1330          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1331                 0x0022 => 1, # "
1332                 0x0027 => 1, # '
1333                 0x003D => 1, # =
1334                }->{$self->{next_char}}) {
1335              !!!cp (115);
1336              !!!parse-error (type => 'bad attribute value');
1337            } else {
1338              !!!cp (116);
1339            }
1340            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1341          ## Stay in the state          ## Stay in the state
1342          !!!next-input-character;          !!!next-input-character;
1343          redo A;          redo A;
1344        }        }
1345      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1346        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1347              (1,
1348               $self->{last_attribute_value_state}
1349                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1350               $self->{last_attribute_value_state}
1351                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1352               -1);
1353    
1354        unless (defined $token) {        unless (defined $token) {
1355            !!!cp (117);
1356          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1357        } else {        } else {
1358            !!!cp (118);
1359          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1360            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1361          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1362        }        }
1363    
1364        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1365        # next-input-character is already done        # next-input-character is already done
1366        redo A;        redo A;
1367      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1368          if ($self->{next_char} == 0x0009 or # HT
1369              $self->{next_char} == 0x000A or # LF
1370              $self->{next_char} == 0x000B or # VT
1371              $self->{next_char} == 0x000C or # FF
1372              $self->{next_char} == 0x0020) { # SP
1373            !!!cp (118);
1374            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1375            !!!next-input-character;
1376            redo A;
1377          } elsif ($self->{next_char} == 0x003E) { # >
1378            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1379              !!!cp (119);
1380              $self->{current_token}->{first_start_tag}
1381                  = not defined $self->{last_emitted_start_tag_name};
1382              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1383            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1384              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1385              if ($self->{current_token}->{attributes}) {
1386                !!!cp (120);
1387                !!!parse-error (type => 'end tag attribute');
1388              } else {
1389                ## NOTE: This state should never be reached.
1390                !!!cp (121);
1391              }
1392            } else {
1393              die "$0: $self->{current_token}->{type}: Unknown token type";
1394            }
1395            $self->{state} = DATA_STATE;
1396            !!!next-input-character;
1397    
1398            !!!emit ($self->{current_token}); # start tag or end tag
1399    
1400            redo A;
1401          } elsif ($self->{next_char} == 0x002F) { # /
1402            !!!next-input-character;
1403            if ($self->{next_char} == 0x003E and # >
1404                $self->{current_token}->{type} == START_TAG_TOKEN and
1405                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1406              # permitted slash
1407              !!!cp (122);
1408              #
1409            } else {
1410              !!!cp (123);
1411              !!!parse-error (type => 'nestc');
1412            }
1413            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1414            # next-input-character is already done
1415            redo A;
1416          } else {
1417            !!!cp (124);
1418            !!!parse-error (type => 'no space between attributes');
1419            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1420            ## reconsume
1421            redo A;
1422          }
1423        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1424        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1425                
1426        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1427          #my $token = {type => COMMENT_TOKEN, data => ''};
1428    
1429        BC: {        BC: {
1430          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1431            $self->{state} = 'data';            !!!cp (124);
1432              $self->{state} = DATA_STATE;
1433            !!!next-input-character;            !!!next-input-character;
1434    
1435            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1436    
1437            redo A;            redo A;
1438          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1439            $self->{state} = 'data';            !!!cp (125);
1440              $self->{state} = DATA_STATE;
1441            ## reconsume            ## reconsume
1442    
1443            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1444    
1445            redo A;            redo A;
1446          } else {          } else {
1447            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1448              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1449            !!!next-input-character;            !!!next-input-character;
1450            redo BC;            redo BC;
1451          }          }
1452        } # BC        } # BC
1453      } elsif ($self->{state} eq 'markup declaration open') {  
1454          die "$0: _get_next_token: unexpected case [BC]";
1455        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1456        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1457    
1458          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1459    
1460        my @next_char;        my @next_char;
1461        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1462                
1463        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1464          !!!next-input-character;          !!!next-input-character;
1465          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1466          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1467            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1468            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1469                                        line => $l, column => $c};
1470              $self->{state} = COMMENT_START_STATE;
1471            !!!next-input-character;            !!!next-input-character;
1472            redo A;            redo A;
1473            } else {
1474              !!!cp (128);
1475          }          }
1476        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1477                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1478          !!!next-input-character;          !!!next-input-character;
1479          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1480          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1481              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1482            !!!next-input-character;            !!!next-input-character;
1483            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1484            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1485                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1486              !!!next-input-character;              !!!next-input-character;
1487              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1488              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1489                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1490                !!!next-input-character;                !!!next-input-character;
1491                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1492                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1493                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1494                  !!!next-input-character;                  !!!next-input-character;
1495                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1496                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1497                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1498                    !!!next-input-character;                    !!!next-input-character;
1499                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1500                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1501                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1502                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1503                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1504                        $self->{state} = DOCTYPE_STATE;
1505                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1506                                                  quirks => 1,
1507                                                  line => $l, column => $c};
1508                      !!!next-input-character;                      !!!next-input-character;
1509                      redo A;                      redo A;
1510                      } else {
1511                        !!!cp (130);
1512                    }                    }
1513                    } else {
1514                      !!!cp (131);
1515                  }                  }
1516                  } else {
1517                    !!!cp (132);
1518                }                }
1519                } else {
1520                  !!!cp (133);
1521              }              }
1522              } else {
1523                !!!cp (134);
1524            }            }
1525            } else {
1526              !!!cp (135);
1527          }          }
1528          } else {
1529            !!!cp (136);
1530        }        }
1531    
1532        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1533        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1534        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1535        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1536          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1537                                    line => $l, column => $c};
1538        redo A;        redo A;
1539                
1540        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1541        ## 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?
1542      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1543        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1544          $self->{state} = 'comment dash';          !!!cp (137);
1545            $self->{state} = COMMENT_START_DASH_STATE;
1546          !!!next-input-character;          !!!next-input-character;
1547          redo A;          redo A;
1548        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1549            !!!cp (138);
1550            !!!parse-error (type => 'bogus comment');
1551            $self->{state} = DATA_STATE;
1552            !!!next-input-character;
1553    
1554            !!!emit ($self->{current_token}); # comment
1555    
1556            redo A;
1557          } elsif ($self->{next_char} == -1) {
1558            !!!cp (139);
1559            !!!parse-error (type => 'unclosed comment');
1560            $self->{state} = DATA_STATE;
1561            ## reconsume
1562    
1563            !!!emit ($self->{current_token}); # comment
1564    
1565            redo A;
1566          } else {
1567            !!!cp (140);
1568            $self->{current_token}->{data} # comment
1569                .= chr ($self->{next_char});
1570            $self->{state} = COMMENT_STATE;
1571            !!!next-input-character;
1572            redo A;
1573          }
1574        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1575          if ($self->{next_char} == 0x002D) { # -
1576            !!!cp (141);
1577            $self->{state} = COMMENT_END_STATE;
1578            !!!next-input-character;
1579            redo A;
1580          } elsif ($self->{next_char} == 0x003E) { # >
1581            !!!cp (142);
1582            !!!parse-error (type => 'bogus comment');
1583            $self->{state} = DATA_STATE;
1584            !!!next-input-character;
1585    
1586            !!!emit ($self->{current_token}); # comment
1587    
1588            redo A;
1589          } elsif ($self->{next_char} == -1) {
1590            !!!cp (143);
1591          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1592          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1593          ## reconsume          ## reconsume
1594    
1595          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1596    
1597          redo A;          redo A;
1598        } else {        } else {
1599          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (144);
1600            $self->{current_token}->{data} # comment
1601                .= '-' . chr ($self->{next_char});
1602            $self->{state} = COMMENT_STATE;
1603            !!!next-input-character;
1604            redo A;
1605          }
1606        } elsif ($self->{state} == COMMENT_STATE) {
1607          if ($self->{next_char} == 0x002D) { # -
1608            !!!cp (145);
1609            $self->{state} = COMMENT_END_DASH_STATE;
1610            !!!next-input-character;
1611            redo A;
1612          } elsif ($self->{next_char} == -1) {
1613            !!!cp (146);
1614            !!!parse-error (type => 'unclosed comment');
1615            $self->{state} = DATA_STATE;
1616            ## reconsume
1617    
1618            !!!emit ($self->{current_token}); # comment
1619    
1620            redo A;
1621          } else {
1622            !!!cp (147);
1623            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1624          ## Stay in the state          ## Stay in the state
1625          !!!next-input-character;          !!!next-input-character;
1626          redo A;          redo A;
1627        }        }
1628      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1629        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1630          $self->{state} = 'comment end';          !!!cp (148);
1631            $self->{state} = COMMENT_END_STATE;
1632          !!!next-input-character;          !!!next-input-character;
1633          redo A;          redo A;
1634        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1635            !!!cp (149);
1636          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1637          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1638          ## reconsume          ## reconsume
1639    
1640          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1641    
1642          redo A;          redo A;
1643        } else {        } else {
1644          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1645          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1646            $self->{state} = COMMENT_STATE;
1647          !!!next-input-character;          !!!next-input-character;
1648          redo A;          redo A;
1649        }        }
1650      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1651        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1652          $self->{state} = 'data';          !!!cp (151);
1653            $self->{state} = DATA_STATE;
1654          !!!next-input-character;          !!!next-input-character;
1655    
1656          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1657    
1658          redo A;          redo A;
1659        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1660          !!!parse-error (type => 'dash in comment');          !!!cp (152);
1661            !!!parse-error (type => 'dash in comment',
1662                            line => $self->{line_prev},
1663                            column => $self->{column_prev});
1664          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1665          ## Stay in the state          ## Stay in the state
1666          !!!next-input-character;          !!!next-input-character;
1667          redo A;          redo A;
1668        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1669            !!!cp (153);
1670          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1671          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1672          ## reconsume          ## reconsume
1673    
1674          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1675    
1676          redo A;          redo A;
1677        } else {        } else {
1678          !!!parse-error (type => 'dash in comment');          !!!cp (154);
1679          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
1680          $self->{state} = 'comment';                          line => $self->{line_prev},
1681                            column => $self->{column_prev});
1682            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1683            $self->{state} = COMMENT_STATE;
1684          !!!next-input-character;          !!!next-input-character;
1685          redo A;          redo A;
1686        }        }
1687      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1688        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1689            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1690            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1691            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1692            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1693          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1694            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1695          !!!next-input-character;          !!!next-input-character;
1696          redo A;          redo A;
1697        } else {        } else {
1698            !!!cp (156);
1699          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1700          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1701          ## reconsume          ## reconsume
1702          redo A;          redo A;
1703        }        }
1704      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1705        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1706            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1707            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1708            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1709            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1710            !!!cp (157);
1711          ## Stay in the state          ## Stay in the state
1712          !!!next-input-character;          !!!next-input-character;
1713          redo A;          redo A;
1714        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
1715                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (158);
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
1716          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1717          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1718          !!!next-input-character;          !!!next-input-character;
1719    
1720          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1721    
1722          redo A;          redo A;
1723        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1724            !!!cp (159);
1725          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1726          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1727          ## reconsume          ## reconsume
1728    
1729          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1730    
1731          redo A;          redo A;
1732        } else {        } else {
1733          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
1734                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name} = chr $self->{next_char};
1735                            error => 1};          delete $self->{current_token}->{quirks};
1736  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1737          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1738            !!!next-input-character;
1739            redo A;
1740          }
1741        } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1742    ## ISSUE: Redundant "First," in the spec.
1743          if ($self->{next_char} == 0x0009 or # HT
1744              $self->{next_char} == 0x000A or # LF
1745              $self->{next_char} == 0x000B or # VT
1746              $self->{next_char} == 0x000C or # FF
1747              $self->{next_char} == 0x0020) { # SP
1748            !!!cp (161);
1749            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1750            !!!next-input-character;
1751            redo A;
1752          } elsif ($self->{next_char} == 0x003E) { # >
1753            !!!cp (162);
1754            $self->{state} = DATA_STATE;
1755            !!!next-input-character;
1756    
1757            !!!emit ($self->{current_token}); # DOCTYPE
1758    
1759            redo A;
1760          } elsif ($self->{next_char} == -1) {
1761            !!!cp (163);
1762            !!!parse-error (type => 'unclosed DOCTYPE');
1763            $self->{state} = DATA_STATE;
1764            ## reconsume
1765    
1766            $self->{current_token}->{quirks} = 1;
1767            !!!emit ($self->{current_token}); # DOCTYPE
1768    
1769            redo A;
1770          } else {
1771            !!!cp (164);
1772            $self->{current_token}->{name}
1773              .= chr ($self->{next_char}); # DOCTYPE
1774            ## Stay in the state
1775          !!!next-input-character;          !!!next-input-character;
1776          redo A;          redo A;
1777        }        }
1778      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1779        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1780            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1781            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1782            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1783            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1784          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (165);
1785          $self->{state} = 'after DOCTYPE name';          ## Stay in the state
1786          !!!next-input-character;          !!!next-input-character;
1787          redo A;          redo A;
1788        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1789          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (166);
1790          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1791          !!!next-input-character;          !!!next-input-character;
1792    
1793          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1794    
1795          redo A;          redo A;
1796        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
1797                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (167);
1798          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
1799          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
1800            ## reconsume
1801    
1802            $self->{current_token}->{quirks} = 1;
1803            !!!emit ($self->{current_token}); # DOCTYPE
1804    
1805            redo A;
1806          } elsif ($self->{next_char} == 0x0050 or # P
1807                   $self->{next_char} == 0x0070) { # p
1808            !!!next-input-character;
1809            if ($self->{next_char} == 0x0055 or # U
1810                $self->{next_char} == 0x0075) { # u
1811              !!!next-input-character;
1812              if ($self->{next_char} == 0x0042 or # B
1813                  $self->{next_char} == 0x0062) { # b
1814                !!!next-input-character;
1815                if ($self->{next_char} == 0x004C or # L
1816                    $self->{next_char} == 0x006C) { # l
1817                  !!!next-input-character;
1818                  if ($self->{next_char} == 0x0049 or # I
1819                      $self->{next_char} == 0x0069) { # i
1820                    !!!next-input-character;
1821                    if ($self->{next_char} == 0x0043 or # C
1822                        $self->{next_char} == 0x0063) { # c
1823                      !!!cp (168);
1824                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1825                      !!!next-input-character;
1826                      redo A;
1827                    } else {
1828                      !!!cp (169);
1829                    }
1830                  } else {
1831                    !!!cp (170);
1832                  }
1833                } else {
1834                  !!!cp (171);
1835                }
1836              } else {
1837                !!!cp (172);
1838              }
1839            } else {
1840              !!!cp (173);
1841            }
1842    
1843            #
1844          } elsif ($self->{next_char} == 0x0053 or # S
1845                   $self->{next_char} == 0x0073) { # s
1846            !!!next-input-character;
1847            if ($self->{next_char} == 0x0059 or # Y
1848                $self->{next_char} == 0x0079) { # y
1849              !!!next-input-character;
1850              if ($self->{next_char} == 0x0053 or # S
1851                  $self->{next_char} == 0x0073) { # s
1852                !!!next-input-character;
1853                if ($self->{next_char} == 0x0054 or # T
1854                    $self->{next_char} == 0x0074) { # t
1855                  !!!next-input-character;
1856                  if ($self->{next_char} == 0x0045 or # E
1857                      $self->{next_char} == 0x0065) { # e
1858                    !!!next-input-character;
1859                    if ($self->{next_char} == 0x004D or # M
1860                        $self->{next_char} == 0x006D) { # m
1861                      !!!cp (174);
1862                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1863                      !!!next-input-character;
1864                      redo A;
1865                    } else {
1866                      !!!cp (175);
1867                    }
1868                  } else {
1869                    !!!cp (176);
1870                  }
1871                } else {
1872                  !!!cp (177);
1873                }
1874              } else {
1875                !!!cp (178);
1876              }
1877            } else {
1878              !!!cp (179);
1879            }
1880    
1881            #
1882          } else {
1883            !!!cp (180);
1884            !!!next-input-character;
1885            #
1886          }
1887    
1888          !!!parse-error (type => 'string after DOCTYPE name');
1889          $self->{current_token}->{quirks} = 1;
1890    
1891          $self->{state} = BOGUS_DOCTYPE_STATE;
1892          # next-input-character is already done
1893          redo A;
1894        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1895          if ({
1896                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1897                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1898              }->{$self->{next_char}}) {
1899            !!!cp (181);
1900          ## Stay in the state          ## Stay in the state
1901          !!!next-input-character;          !!!next-input-character;
1902          redo A;          redo A;
1903        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} eq 0x0022) { # "
1904            !!!cp (182);
1905            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1906            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1907            !!!next-input-character;
1908            redo A;
1909          } elsif ($self->{next_char} eq 0x0027) { # '
1910            !!!cp (183);
1911            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1912            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1913            !!!next-input-character;
1914            redo A;
1915          } elsif ($self->{next_char} eq 0x003E) { # >
1916            !!!cp (184);
1917            !!!parse-error (type => 'no PUBLIC literal');
1918    
1919            $self->{state} = DATA_STATE;
1920            !!!next-input-character;
1921    
1922            $self->{current_token}->{quirks} = 1;
1923            !!!emit ($self->{current_token}); # DOCTYPE
1924    
1925            redo A;
1926          } elsif ($self->{next_char} == -1) {
1927            !!!cp (185);
1928          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1929          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1930          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1931          ## reconsume          ## reconsume
1932    
1933          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
1934          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1935    
1936          redo A;          redo A;
1937        } else {        } else {
1938          $self->{current_token}->{name}          !!!cp (186);
1939            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after PUBLIC');
1940          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{current_token}->{quirks} = 1;
1941    
1942            $self->{state} = BOGUS_DOCTYPE_STATE;
1943            !!!next-input-character;
1944            redo A;
1945          }
1946        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1947          if ($self->{next_char} == 0x0022) { # "
1948            !!!cp (187);
1949            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1950            !!!next-input-character;
1951            redo A;
1952          } elsif ($self->{next_char} == 0x003E) { # >
1953            !!!cp (188);
1954            !!!parse-error (type => 'unclosed PUBLIC literal');
1955    
1956            $self->{state} = DATA_STATE;
1957            !!!next-input-character;
1958    
1959            $self->{current_token}->{quirks} = 1;
1960            !!!emit ($self->{current_token}); # DOCTYPE
1961    
1962            redo A;
1963          } elsif ($self->{next_char} == -1) {
1964            !!!cp (189);
1965            !!!parse-error (type => 'unclosed PUBLIC literal');
1966    
1967            $self->{state} = DATA_STATE;
1968            ## reconsume
1969    
1970            $self->{current_token}->{quirks} = 1;
1971            !!!emit ($self->{current_token}); # DOCTYPE
1972    
1973            redo A;
1974          } else {
1975            !!!cp (190);
1976            $self->{current_token}->{public_identifier} # DOCTYPE
1977                .= chr $self->{next_char};
1978            ## Stay in the state
1979            !!!next-input-character;
1980            redo A;
1981          }
1982        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1983          if ($self->{next_char} == 0x0027) { # '
1984            !!!cp (191);
1985            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1986            !!!next-input-character;
1987            redo A;
1988          } elsif ($self->{next_char} == 0x003E) { # >
1989            !!!cp (192);
1990            !!!parse-error (type => 'unclosed PUBLIC literal');
1991    
1992            $self->{state} = DATA_STATE;
1993            !!!next-input-character;
1994    
1995            $self->{current_token}->{quirks} = 1;
1996            !!!emit ($self->{current_token}); # DOCTYPE
1997    
1998            redo A;
1999          } elsif ($self->{next_char} == -1) {
2000            !!!cp (193);
2001            !!!parse-error (type => 'unclosed PUBLIC literal');
2002    
2003            $self->{state} = DATA_STATE;
2004            ## reconsume
2005    
2006            $self->{current_token}->{quirks} = 1;
2007            !!!emit ($self->{current_token}); # DOCTYPE
2008    
2009            redo A;
2010          } else {
2011            !!!cp (194);
2012            $self->{current_token}->{public_identifier} # DOCTYPE
2013                .= chr $self->{next_char};
2014            ## Stay in the state
2015            !!!next-input-character;
2016            redo A;
2017          }
2018        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2019          if ({
2020                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2021                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2022              }->{$self->{next_char}}) {
2023            !!!cp (195);
2024            ## Stay in the state
2025            !!!next-input-character;
2026            redo A;
2027          } elsif ($self->{next_char} == 0x0022) { # "
2028            !!!cp (196);
2029            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2030            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2031            !!!next-input-character;
2032            redo A;
2033          } elsif ($self->{next_char} == 0x0027) { # '
2034            !!!cp (197);
2035            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2036            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2037            !!!next-input-character;
2038            redo A;
2039          } elsif ($self->{next_char} == 0x003E) { # >
2040            !!!cp (198);
2041            $self->{state} = DATA_STATE;
2042            !!!next-input-character;
2043    
2044            !!!emit ($self->{current_token}); # DOCTYPE
2045    
2046            redo A;
2047          } elsif ($self->{next_char} == -1) {
2048            !!!cp (199);
2049            !!!parse-error (type => 'unclosed DOCTYPE');
2050    
2051            $self->{state} = DATA_STATE;
2052            ## reconsume
2053    
2054            $self->{current_token}->{quirks} = 1;
2055            !!!emit ($self->{current_token}); # DOCTYPE
2056    
2057            redo A;
2058          } else {
2059            !!!cp (200);
2060            !!!parse-error (type => 'string after PUBLIC literal');
2061            $self->{current_token}->{quirks} = 1;
2062    
2063            $self->{state} = BOGUS_DOCTYPE_STATE;
2064            !!!next-input-character;
2065            redo A;
2066          }
2067        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2068          if ({
2069                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2070                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2071              }->{$self->{next_char}}) {
2072            !!!cp (201);
2073            ## Stay in the state
2074            !!!next-input-character;
2075            redo A;
2076          } elsif ($self->{next_char} == 0x0022) { # "
2077            !!!cp (202);
2078            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2079            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2080            !!!next-input-character;
2081            redo A;
2082          } elsif ($self->{next_char} == 0x0027) { # '
2083            !!!cp (203);
2084            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2085            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2086            !!!next-input-character;
2087            redo A;
2088          } elsif ($self->{next_char} == 0x003E) { # >
2089            !!!cp (204);
2090            !!!parse-error (type => 'no SYSTEM literal');
2091            $self->{state} = DATA_STATE;
2092            !!!next-input-character;
2093    
2094            $self->{current_token}->{quirks} = 1;
2095            !!!emit ($self->{current_token}); # DOCTYPE
2096    
2097            redo A;
2098          } elsif ($self->{next_char} == -1) {
2099            !!!cp (205);
2100            !!!parse-error (type => 'unclosed DOCTYPE');
2101    
2102            $self->{state} = DATA_STATE;
2103            ## reconsume
2104    
2105            $self->{current_token}->{quirks} = 1;
2106            !!!emit ($self->{current_token}); # DOCTYPE
2107    
2108            redo A;
2109          } else {
2110            !!!cp (206);
2111            !!!parse-error (type => 'string after SYSTEM');
2112            $self->{current_token}->{quirks} = 1;
2113    
2114            $self->{state} = BOGUS_DOCTYPE_STATE;
2115            !!!next-input-character;
2116            redo A;
2117          }
2118        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2119          if ($self->{next_char} == 0x0022) { # "
2120            !!!cp (207);
2121            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2122            !!!next-input-character;
2123            redo A;
2124          } elsif ($self->{next_char} == 0x003E) { # >
2125            !!!cp (208);
2126            !!!parse-error (type => 'unclosed PUBLIC literal');
2127    
2128            $self->{state} = DATA_STATE;
2129            !!!next-input-character;
2130    
2131            $self->{current_token}->{quirks} = 1;
2132            !!!emit ($self->{current_token}); # DOCTYPE
2133    
2134            redo A;
2135          } elsif ($self->{next_char} == -1) {
2136            !!!cp (209);
2137            !!!parse-error (type => 'unclosed SYSTEM literal');
2138    
2139            $self->{state} = DATA_STATE;
2140            ## reconsume
2141    
2142            $self->{current_token}->{quirks} = 1;
2143            !!!emit ($self->{current_token}); # DOCTYPE
2144    
2145            redo A;
2146          } else {
2147            !!!cp (210);
2148            $self->{current_token}->{system_identifier} # DOCTYPE
2149                .= chr $self->{next_char};
2150          ## Stay in the state          ## Stay in the state
2151          !!!next-input-character;          !!!next-input-character;
2152          redo A;          redo A;
2153        }        }
2154      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2155        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0027) { # '
2156            $self->{next_input_character} == 0x000A or # LF          !!!cp (211);
2157            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2158            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2159            $self->{next_input_character} == 0x0020) { # SP          redo A;
2160          } elsif ($self->{next_char} == 0x003E) { # >
2161            !!!cp (212);
2162            !!!parse-error (type => 'unclosed PUBLIC literal');
2163    
2164            $self->{state} = DATA_STATE;
2165            !!!next-input-character;
2166    
2167            $self->{current_token}->{quirks} = 1;
2168            !!!emit ($self->{current_token}); # DOCTYPE
2169    
2170            redo A;
2171          } elsif ($self->{next_char} == -1) {
2172            !!!cp (213);
2173            !!!parse-error (type => 'unclosed SYSTEM literal');
2174    
2175            $self->{state} = DATA_STATE;
2176            ## reconsume
2177    
2178            $self->{current_token}->{quirks} = 1;
2179            !!!emit ($self->{current_token}); # DOCTYPE
2180    
2181            redo A;
2182          } else {
2183            !!!cp (214);
2184            $self->{current_token}->{system_identifier} # DOCTYPE
2185                .= chr $self->{next_char};
2186          ## Stay in the state          ## Stay in the state
2187          !!!next-input-character;          !!!next-input-character;
2188          redo A;          redo A;
2189        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2190          $self->{state} = 'data';      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2191          if ({
2192                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2193                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2194              }->{$self->{next_char}}) {
2195            !!!cp (215);
2196            ## Stay in the state
2197            !!!next-input-character;
2198            redo A;
2199          } elsif ($self->{next_char} == 0x003E) { # >
2200            !!!cp (216);
2201            $self->{state} = DATA_STATE;
2202          !!!next-input-character;          !!!next-input-character;
2203    
2204          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2205    
2206          redo A;          redo A;
2207        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2208            !!!cp (217);
2209          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2210          $self->{state} = 'data';  
2211            $self->{state} = DATA_STATE;
2212          ## reconsume          ## reconsume
2213    
2214            $self->{current_token}->{quirks} = 1;
2215          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2216    
2217          redo A;          redo A;
2218        } else {        } else {
2219          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (218);
2220          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after SYSTEM literal');
2221          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2222    
2223            $self->{state} = BOGUS_DOCTYPE_STATE;
2224          !!!next-input-character;          !!!next-input-character;
2225          redo A;          redo A;
2226        }        }
2227      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2228        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2229          $self->{state} = 'data';          !!!cp (219);
2230            $self->{state} = DATA_STATE;
2231          !!!next-input-character;          !!!next-input-character;
2232    
2233          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2234    
2235          redo A;          redo A;
2236        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2237            !!!cp (220);
2238          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2239          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2240          ## reconsume          ## reconsume
2241    
2242          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2243    
2244          redo A;          redo A;
2245        } else {        } else {
2246            !!!cp (221);
2247          ## Stay in the state          ## Stay in the state
2248          !!!next-input-character;          !!!next-input-character;
2249          redo A;          redo A;
# Line 1523  sub _get_next_token ($) { Line 2256  sub _get_next_token ($) {
2256    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2257  } # _get_next_token  } # _get_next_token
2258    
2259  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2260    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2261      
2262    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2263    
2264      if ({
2265           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2266           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2267           $additional => 1,
2268          }->{$self->{next_char}}) {
2269        !!!cp (1001);
2270        ## Don't consume
2271        ## No error
2272        return undef;
2273      } elsif ($self->{next_char} == 0x0023) { # #
2274      !!!next-input-character;      !!!next-input-character;
2275      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2276          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2277        my $num;        my $code;
2278        X: {        X: {
2279          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2280          !!!next-input-character;          !!!next-input-character;
2281          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2282              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2283            $num ||= 0;            !!!cp (1002);
2284            $num *= 0x10;            $code ||= 0;
2285            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2286              $code += $self->{next_char} - 0x0030;
2287            redo X;            redo X;
2288          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2289                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2290            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2291            $num ||= 0;            $code ||= 0;
2292            $num *= 0x10;            $code *= 0x10;
2293            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2294            redo X;            redo X;
2295          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2296                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2297            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2298            $num ||= 0;            $code ||= 0;
2299            $num *= 0x10;            $code *= 0x10;
2300            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2301            redo X;            redo X;
2302          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2303            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2304            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2305            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2306              $self->{next_char} = 0x0023; # #
2307            return undef;            return undef;
2308          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2309              !!!cp (1006);
2310            !!!next-input-character;            !!!next-input-character;
2311          } else {          } else {
2312            !!!parse-error (type => 'no refc');            !!!cp (1007);
2313              !!!parse-error (type => 'no refc', line => $l, column => $c);
2314          }          }
2315    
2316          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2317          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!cp (1008);
2318          if ($num > 1114111 or $num == 0) {            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2319            $num = 0xFFFD; # REPLACEMENT CHARACTER            $code = 0xFFFD;
2320            ## ISSUE: Why this is not an error?          } elsif ($code > 0x10FFFF) {
2321          } elsif (0x80 <= $num and $num <= 0x9F) {            !!!cp (1009);
2322            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2323            $num = $c1_entity_char->{$num};            $code = 0xFFFD;
2324            } elsif ($code == 0x000D) {
2325              !!!cp (1010);
2326              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2327              $code = 0x000A;
2328            } elsif (0x80 <= $code and $code <= 0x9F) {
2329              !!!cp (1011);
2330              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2331              $code = $c1_entity_char->{$code};
2332          }          }
2333    
2334          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code,
2335                    has_reference => 1, line => $l, column => $c};
2336        } # X        } # X
2337      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2338               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2339        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2340        !!!next-input-character;        !!!next-input-character;
2341                
2342        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2343                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2344            !!!cp (1012);
2345          $code *= 10;          $code *= 10;
2346          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2347                    
2348          !!!next-input-character;          !!!next-input-character;
2349        }        }
2350    
2351        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2352            !!!cp (1013);
2353          !!!next-input-character;          !!!next-input-character;
2354        } else {        } else {
2355          !!!parse-error (type => 'no refc');          !!!cp (1014);
2356            !!!parse-error (type => 'no refc', line => $l, column => $c);
2357        }        }
2358    
2359        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2360        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
2361          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2362          ## ISSUE: Why this is not an error?          $code = 0xFFFD;
2363          } elsif ($code > 0x10FFFF) {
2364            !!!cp (1016);
2365            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2366            $code = 0xFFFD;
2367          } elsif ($code == 0x000D) {
2368            !!!cp (1017);
2369            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2370            $code = 0x000A;
2371        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2372          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!cp (1018);
2373            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2374          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2375        }        }
2376                
2377        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2378                  line => $l, column => $c};
2379      } else {      } else {
2380        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2381        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2382        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2383          $self->{next_char} = 0x0023; # #
2384        return undef;        return undef;
2385      }      }
2386    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2387              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2388             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2389              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2390      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2391      !!!next-input-character;      !!!next-input-character;
2392    
2393      my $value = $entity_name;      my $value = $entity_name;
2394      my $match;      my $match = 0;
2395        require Whatpm::_NamedEntityList;
2396        our $EntityChar;
2397    
2398      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2399             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2400             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
2401               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
2402              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
2403               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
2404              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
2405               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
2406        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
2407        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
2408          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
2409          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
2410              !!!cp (1020);
2411              $value = $EntityChar->{$entity_name};
2412              $match = 1;
2413              !!!next-input-character;
2414              last;
2415            } else {
2416              !!!cp (1021);
2417              $value = $EntityChar->{$entity_name};
2418              $match = -1;
2419              !!!next-input-character;
2420            }
2421        } else {        } else {
2422          $value .= chr $self->{next_input_character};          !!!cp (1022);
2423            $value .= chr $self->{next_char};
2424            $match *= 2;
2425            !!!next-input-character;
2426        }        }
       !!!next-input-character;  
2427      }      }
2428            
2429      if ($match) {      if ($match > 0) {
2430        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
2431          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2432                  line => $l, column => $c};
2433        } elsif ($match < 0) {
2434          !!!parse-error (type => 'no refc', line => $l, column => $c);
2435          if ($in_attr and $match < -1) {
2436            !!!cp (1024);
2437            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2438                    line => $l, column => $c};
2439        } else {        } else {
2440          !!!parse-error (type => 'refc');          !!!cp (1025);
2441            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2442                    line => $l, column => $c};
2443        }        }
   
       return {type => 'character', data => $value};  
2444      } else {      } else {
2445        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2446        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2447        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
2448        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
2449                  line => $l, column => $c};
2450      }      }
2451    } else {    } else {
2452        !!!cp (1027);
2453      ## no characters are consumed      ## no characters are consumed
2454      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2455      return undef;      return undef;
2456    }    }
2457  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1667  sub _initialize_tree_constructor ($) { Line 2462  sub _initialize_tree_constructor ($) {
2462    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
2463    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
2464    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
2465    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
2466  } # _initialize_tree_constructor  } # _initialize_tree_constructor
2467    
2468  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1694  sub _construct_tree ($) { Line 2489  sub _construct_tree ($) {
2489        
2490    !!!next-token;    !!!next-token;
2491    
   $self->{insertion_mode} = 'before head';  
2492    undef $self->{form_element};    undef $self->{form_element};
2493    undef $self->{head_element};    undef $self->{head_element};
2494    $self->{open_elements} = [];    $self->{open_elements} = [];
2495    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2496    
2497      ## NOTE: The "initial" insertion mode.
2498    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2499    
2500      ## NOTE: The "before html" insertion mode.
2501    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2502      $self->{insertion_mode} = BEFORE_HEAD_IM;
2503    
2504      ## NOTE: The "before head" insertion mode and so on.
2505    $self->_tree_construction_main;    $self->_tree_construction_main;
2506  } # _construct_tree  } # _construct_tree
2507    
2508  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2509    my $self = shift;    my $self = shift;
2510    B: {  
2511        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
2512          if ($token->{error}) {  
2513            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
2514            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
2515          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2516          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
2517            ($token->{name});        ## language.
2518          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
2519          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
2520          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/;
2521          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
2522          return;            defined $token->{public_identifier} or
2523        } elsif ({            defined $token->{system_identifier}) {
2524                  comment => 1,          !!!cp ('t1');
2525                  'start tag' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
2526                  'end tag' => 1,        } elsif ($doctype_name ne 'HTML') {
2527                  'end-of-file' => 1,          !!!cp ('t2');
2528                 }->{$token->{type}}) {          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2529          ## ISSUE: Spec currently left this case undefined.          !!!parse-error (type => 'not HTML5', token => $token);
2530          !!!parse-error (type => 'missing DOCTYPE');        } else {
2531          #$phase = 'root element';          !!!cp ('t3');
2532          ## reprocess        }
2533          #redo B;        
2534          return;        my $doctype = $self->{document}->create_document_type_definition
2535        } elsif ($token->{type} eq 'character') {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2536          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        $doctype->public_id ($token->{public_identifier})
2537            $self->{document}->manakai_append_text ($1);            if defined $token->{public_identifier};
2538            ## ISSUE: DOM3 Core does not allow Document > Text        $doctype->system_id ($token->{system_identifier})
2539            unless (length $token->{data}) {            if defined $token->{system_identifier};
2540              ## Stay in the phase        ## NOTE: Other DocumentType attributes are null or empty lists.
2541              !!!next-token;        ## ISSUE: internalSubset = null??
2542              redo B;        $self->{document}->append_child ($doctype);
2543          
2544          if ($token->{quirks} or $doctype_name ne 'HTML') {
2545            !!!cp ('t4');
2546            $self->{document}->manakai_compat_mode ('quirks');
2547          } elsif (defined $token->{public_identifier}) {
2548            my $pubid = $token->{public_identifier};
2549            $pubid =~ tr/a-z/A-z/;
2550            if ({
2551              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
2552              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2553              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2554              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
2555              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
2556              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
2557              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
2558              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
2559              "-//IETF//DTD HTML 2.0//EN" => 1,
2560              "-//IETF//DTD HTML 2.1E//EN" => 1,
2561              "-//IETF//DTD HTML 3.0//EN" => 1,
2562              "-//IETF//DTD HTML 3.0//EN//" => 1,
2563              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
2564              "-//IETF//DTD HTML 3.2//EN" => 1,
2565              "-//IETF//DTD HTML 3//EN" => 1,
2566              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
2567              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
2568              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
2569              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
2570              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
2571              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
2572              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
2573              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
2574              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
2575              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
2576              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
2577              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
2578              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
2579              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
2580              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
2581              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
2582              "-//IETF//DTD HTML STRICT//EN" => 1,
2583              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
2584              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
2585              "-//IETF//DTD HTML//EN" => 1,
2586              "-//IETF//DTD HTML//EN//2.0" => 1,
2587              "-//IETF//DTD HTML//EN//3.0" => 1,
2588              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
2589              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
2590              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
2591              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
2592              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
2593              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
2594              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
2595              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
2596              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2597              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2598              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2599              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2600              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2601              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2602              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2603              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2604              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
2605              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
2606              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
2607              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
2608              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
2609              "-//W3C//DTD HTML 3.2//EN" => 1,
2610              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
2611              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
2612              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
2613              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
2614              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
2615              "-//W3C//DTD W3 HTML//EN" => 1,
2616              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
2617              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
2618              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
2619              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
2620              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
2621              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2622              "HTML" => 1,
2623            }->{$pubid}) {
2624              !!!cp ('t5');
2625              $self->{document}->manakai_compat_mode ('quirks');
2626            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2627                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2628              if (defined $token->{system_identifier}) {
2629                !!!cp ('t6');
2630                $self->{document}->manakai_compat_mode ('quirks');
2631              } else {
2632                !!!cp ('t7');
2633                $self->{document}->manakai_compat_mode ('limited quirks');
2634            }            }
2635            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2636                     $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2637              !!!cp ('t8');
2638              $self->{document}->manakai_compat_mode ('limited quirks');
2639            } else {
2640              !!!cp ('t9');
2641            }
2642          } else {
2643            !!!cp ('t10');
2644          }
2645          if (defined $token->{system_identifier}) {
2646            my $sysid = $token->{system_identifier};
2647            $sysid =~ tr/A-Z/a-z/;
2648            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2649              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2650              $self->{document}->manakai_compat_mode ('quirks');
2651              !!!cp ('t11');
2652            } else {
2653              !!!cp ('t12');
2654            }
2655          } else {
2656            !!!cp ('t13');
2657          }
2658          
2659          ## Go to the "before html" insertion mode.
2660          !!!next-token;
2661          return;
2662        } elsif ({
2663                  START_TAG_TOKEN, 1,
2664                  END_TAG_TOKEN, 1,
2665                  END_OF_FILE_TOKEN, 1,
2666                 }->{$token->{type}}) {
2667          !!!cp ('t14');
2668          !!!parse-error (type => 'no DOCTYPE', token => $token);
2669          $self->{document}->manakai_compat_mode ('quirks');
2670          ## Go to the "before html" insertion mode.
2671          ## reprocess
2672          return;
2673        } elsif ($token->{type} == CHARACTER_TOKEN) {
2674          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2675            ## Ignore the token
2676    
2677            unless (length $token->{data}) {
2678              !!!cp ('t15');
2679              ## Stay in the insertion mode.
2680              !!!next-token;
2681              redo INITIAL;
2682            } else {
2683              !!!cp ('t16');
2684          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
2685        } else {        } else {
2686          die "$0: $token->{type}: Unknown token";          !!!cp ('t17');
2687        }        }
2688      } # B  
2689          !!!parse-error (type => 'no DOCTYPE', token => $token);
2690          $self->{document}->manakai_compat_mode ('quirks');
2691          ## Go to the "before html" insertion mode.
2692          ## reprocess
2693          return;
2694        } elsif ($token->{type} == COMMENT_TOKEN) {
2695          !!!cp ('t18');
2696          my $comment = $self->{document}->create_comment ($token->{data});
2697          $self->{document}->append_child ($comment);
2698          
2699          ## Stay in the insertion mode.
2700          !!!next-token;
2701          redo INITIAL;
2702        } else {
2703          die "$0: $token->{type}: Unknown token type";
2704        }
2705      } # INITIAL
2706    
2707      die "$0: _tree_construction_initial: This should be never reached";
2708  } # _tree_construction_initial  } # _tree_construction_initial
2709    
2710  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2711    my $self = shift;    my $self = shift;
2712    
2713      ## NOTE: "before html" insertion mode.
2714        
2715    B: {    B: {
2716        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2717          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
2718            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2719          ## Ignore the token          ## Ignore the token
2720          ## Stay in the phase          ## Stay in the insertion mode.
2721          !!!next-token;          !!!next-token;
2722          redo B;          redo B;
2723        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2724            !!!cp ('t20');
2725          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2726          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2727          ## Stay in the phase          ## Stay in the insertion mode.
2728          !!!next-token;          !!!next-token;
2729          redo B;          redo B;
2730        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2731          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2732            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2733            ## ISSUE: DOM3 Core does not allow Document > Text  
2734            unless (length $token->{data}) {            unless (length $token->{data}) {
2735              ## Stay in the phase              !!!cp ('t21');
2736                ## Stay in the insertion mode.
2737              !!!next-token;              !!!next-token;
2738              redo B;              redo B;
2739              } else {
2740                !!!cp ('t22');
2741            }            }
2742            } else {
2743              !!!cp ('t23');
2744          }          }
2745    
2746            $self->{application_cache_selection}->(undef);
2747    
2748          #          #
2749          } elsif ($token->{type} == START_TAG_TOKEN) {
2750            if ($token->{tag_name} eq 'html') {
2751              my $root_element;
2752              !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2753              $self->{document}->append_child ($root_element);
2754              push @{$self->{open_elements}}, [$root_element, 'html'];
2755    
2756              if ($token->{attributes}->{manifest}) {
2757                !!!cp ('t24');
2758                $self->{application_cache_selection}
2759                    ->($token->{attributes}->{manifest}->{value});
2760                ## ISSUE: No relative reference resolution?
2761              } else {
2762                !!!cp ('t25');
2763                $self->{application_cache_selection}->(undef);
2764              }
2765    
2766              !!!next-token;
2767              return; ## Go to the "before head" insertion mode.
2768            } else {
2769              !!!cp ('t25.1');
2770              #
2771            }
2772        } elsif ({        } elsif ({
2773                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2774                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2775                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2776          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
2777          #          #
2778        } else {        } else {
2779          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2780        }        }
2781        my $root_element; !!!create-element ($root_element, 'html');  
2782        $self->{document}->append_child ($root_element);      my $root_element; !!!create-element ($root_element, 'html',, $token);
2783        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
2784        #$phase = 'main';      push @{$self->{open_elements}}, [$root_element, 'html'];
2785        ## reprocess  
2786        #redo B;      $self->{application_cache_selection}->(undef);
2787        return;  
2788        ## NOTE: Reprocess the token.
2789        return; ## Go to the "before head" insertion mode.
2790    
2791        ## ISSUE: There is an issue in the spec
2792    } # B    } # B
2793    
2794      die "$0: _tree_construction_root_element: This should never be reached";
2795  } # _tree_construction_root_element  } # _tree_construction_root_element
2796    
2797  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1813  sub _reset_insertion_mode ($) { Line 2806  sub _reset_insertion_mode ($) {
2806            
2807      ## Step 3      ## Step 3
2808      S3: {      S3: {
2809        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2810        if (defined $self->{inner_html_node}) {          $last = 1;
2811          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
2812              $self->{inner_html_node}->[1] eq 'th') {            if ($self->{inner_html_node}->[1] eq 'td' or
2813            #                $self->{inner_html_node}->[1] eq 'th') {
2814          } else {              !!!cp ('t27');
2815            $node = $self->{inner_html_node};              #
2816              } else {
2817                !!!cp ('t28');
2818                $node = $self->{inner_html_node};
2819              }
2820          }          }
2821        }        }
2822            
2823        ## Step 4..13        ## Step 4..13
2824        my $new_mode = {        my $new_mode = {
2825                        select => 'in select',                        select => IN_SELECT_IM,
2826                        td => 'in cell',                        ## NOTE: |option| and |optgroup| do not set
2827                        th => 'in cell',                        ## insertion mode to "in select" by themselves.
2828                        tr => 'in row',                        td => IN_CELL_IM,
2829                        tbody => 'in table body',                        th => IN_CELL_IM,
2830                        thead => 'in table head',                        tr => IN_ROW_IM,
2831                        tfoot => 'in table foot',                        tbody => IN_TABLE_BODY_IM,
2832                        caption => 'in caption',                        thead => IN_TABLE_BODY_IM,
2833                        colgroup => 'in column group',                        tfoot => IN_TABLE_BODY_IM,
2834                        table => 'in table',                        caption => IN_CAPTION_IM,
2835                        head => 'in body', # not in head!                        colgroup => IN_COLUMN_GROUP_IM,
2836                        body => 'in body',                        table => IN_TABLE_IM,
2837                        frameset => 'in frameset',                        head => IN_BODY_IM, # not in head!
2838                          body => IN_BODY_IM,
2839                          frameset => IN_FRAMESET_IM,
2840                       }->{$node->[1]};                       }->{$node->[1]};
2841        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2842                
2843        ## Step 14        ## Step 14
2844        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2845          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2846            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
2847              $self->{insertion_mode} = BEFORE_HEAD_IM;
2848          } else {          } else {
2849            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
2850              !!!cp ('t30');
2851              $self->{insertion_mode} = AFTER_HEAD_IM;
2852          }          }
2853          return;          return;
2854          } else {
2855            !!!cp ('t31');
2856        }        }
2857                
2858        ## Step 15        ## Step 15
2859        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2860                
2861        ## Step 16        ## Step 16
2862        $i--;        $i--;
# Line 1861  sub _reset_insertion_mode ($) { Line 2865  sub _reset_insertion_mode ($) {
2865        ## Step 17        ## Step 17
2866        redo S3;        redo S3;
2867      } # S3      } # S3
2868    
2869      die "$0: _reset_insertion_mode: This line should never be reached";
2870  } # _reset_insertion_mode  } # _reset_insertion_mode
2871    
2872  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2873    my $self = shift;    my $self = shift;
2874    
   my $phase = 'main';  
   
2875    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2876    
2877    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1884  sub _tree_construction_main ($) { Line 2888  sub _tree_construction_main ($) {
2888      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2889      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2890        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2891            !!!cp ('t32');
2892          return;          return;
2893        }        }
2894      }      }
# Line 1898  sub _tree_construction_main ($) { Line 2903  sub _tree_construction_main ($) {
2903    
2904        ## Step 6        ## Step 6
2905        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2906            !!!cp ('t33_1');
2907          #          #
2908        } else {        } else {
2909          my $in_open_elements;          my $in_open_elements;
2910          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2911            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2912                !!!cp ('t33');
2913              $in_open_elements = 1;              $in_open_elements = 1;
2914              last OE;              last OE;
2915            }            }
2916          }          }
2917          if ($in_open_elements) {          if ($in_open_elements) {
2918              !!!cp ('t34');
2919            #            #
2920          } else {          } else {
2921              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2922              !!!cp ('t35');
2923            redo S4;            redo S4;
2924          }          }
2925        }        }
# Line 1932  sub _tree_construction_main ($) { Line 2942  sub _tree_construction_main ($) {
2942    
2943        ## Step 11        ## Step 11
2944        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2945            !!!cp ('t36');
2946          ## Step 7'          ## Step 7'
2947          $i++;          $i++;
2948          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2949                    
2950          redo S7;          redo S7;
2951        }        }
2952    
2953          !!!cp ('t37');
2954      } # S7      } # S7
2955    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2956    
2957    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2958      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2959        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2960            !!!cp ('t38');
2961          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2962          return;          return;
2963        }        }
2964      }      }
2965    
2966        !!!cp ('t39');
2967    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2968    
2969    my $style_start_tag = sub {    my $insert;
2970      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
2971      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
2972      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
2973       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
2974        ->append_child ($style_el);      ## Step 1
2975      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
2976        my $el;
2977        !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);
2978    
2979        ## Step 2
2980        $insert->($el);
2981    
2982        ## Step 3
2983        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2984      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2985                  
2986        ## Step 4
2987      my $text = '';      my $text = '';
2988      !!!next-token;      !!!next-token;
2989      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2990          !!!cp ('t40');
2991        $text .= $token->{data};        $text .= $token->{data};
2992        !!!next-token;        !!!next-token;
2993      } # stop if non-character token or tokenizer stops tokenising      }
2994    
2995        ## Step 5
2996      if (length $text) {      if (length $text) {
2997        $style_el->manakai_append_text ($text);        !!!cp ('t41');
2998          my $text = $self->{document}->create_text_node ($text);
2999          $el->append_child ($text);
3000      }      }
3001        
3002      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3003                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3004      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3005        ## Step 7
3006        if ($token->{type} == END_TAG_TOKEN and
3007            $token->{tag_name} eq $start_tag_name) {
3008          !!!cp ('t42');
3009        ## Ignore the token        ## Ignore the token
3010      } else {      } else {
3011        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3012        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3013            !!!cp ('t43');
3014            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3015          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3016            !!!cp ('t44');
3017            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3018          } else {
3019            die "$0: $content_model_flag in parse_rcdata";
3020          }
3021      }      }
3022      !!!next-token;      !!!next-token;
3023    }; # $style_start_tag    }; # $parse_rcdata
3024    
3025    my $script_start_tag = sub {    my $script_start_tag = sub () {
3026      my $script_el;      my $script_el;
3027      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes}, $token);
3028      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3029    
3030      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3031      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3032            
3033      my $text = '';      my $text = '';
3034      !!!next-token;      !!!next-token;
3035      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3036          !!!cp ('t45');
3037        $text .= $token->{data};        $text .= $token->{data};
3038        !!!next-token;        !!!next-token;
3039      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3040      if (length $text) {      if (length $text) {
3041          !!!cp ('t46');
3042        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3043      }      }
3044                                
3045      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3046    
3047      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3048          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3049          !!!cp ('t47');
3050        ## Ignore the token        ## Ignore the token
3051      } else {      } else {
3052        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3053          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3054        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3055        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3056      }      }
3057            
3058      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3059          !!!cp ('t49');
3060        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3061      } else {      } else {
3062          !!!cp ('t50');
3063        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3064        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3065          
3066        (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})        $insert->($script_el);
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
3067                
3068        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3069                
# Line 2026  sub _tree_construction_main ($) { Line 3073  sub _tree_construction_main ($) {
3073      !!!next-token;      !!!next-token;
3074    }; # $script_start_tag    }; # $script_start_tag
3075    
3076      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3077      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3078      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3079    
3080    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3081      my $tag_name = shift;      my $end_tag_token = shift;
3082        my $tag_name = $end_tag_token->{tag_name};
3083    
3084        ## NOTE: The adoption agency algorithm (AAA).
3085    
3086      FET: {      FET: {
3087        ## Step 1        ## Step 1
# Line 2035  sub _tree_construction_main ($) { Line 3089  sub _tree_construction_main ($) {
3089        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3090        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3091          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3092              !!!cp ('t51');
3093            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3094            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3095            last AFE;            last AFE;
3096          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3097              !!!cp ('t52');
3098            last AFE;            last AFE;
3099          }          }
3100        } # AFE        } # AFE
3101        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3102          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3103            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3104          ## Ignore the token          ## Ignore the token
3105          !!!next-token;          !!!next-token;
3106          return;          return;
# Line 2055  sub _tree_construction_main ($) { Line 3112  sub _tree_construction_main ($) {
3112          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3113          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3114            if ($in_scope) {            if ($in_scope) {
3115                !!!cp ('t54');
3116              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3117              last INSCOPE;              last INSCOPE;
3118            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3119              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3120                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3121                                token => $end_tag_token);
3122              ## Ignore the token              ## Ignore the token
3123              !!!next-token;              !!!next-token;
3124              return;              return;
3125            }            }
3126          } elsif ({          } elsif ({
3127                    table => 1, caption => 1, td => 1, th => 1,                    applet => 1, table => 1, caption => 1, td => 1, th => 1,
3128                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3129                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3130              !!!cp ('t56');
3131            $in_scope = 0;            $in_scope = 0;
3132          }          }
3133        } # INSCOPE        } # INSCOPE
3134        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3135          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3136            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3137                            token => $end_tag_token);
3138          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3139          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3140          return;          return;
3141        }        }
3142        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3143          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3144            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],
3145                            token => $end_tag_token);
3146        }        }
3147                
3148        ## Step 2        ## Step 2
# Line 2088  sub _tree_construction_main ($) { Line 3153  sub _tree_construction_main ($) {
3153          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
3154              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3155              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3156               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe
3157              !!!cp ('t59');
3158            $furthest_block = $node;            $furthest_block = $node;
3159            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3160          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3161              !!!cp ('t60');
3162            last OE;            last OE;
3163          }          }
3164        } # OE        } # OE
3165                
3166        ## Step 3        ## Step 3
3167        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3168            !!!cp ('t61');
3169          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3170          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3171          !!!next-token;          !!!next-token;
# Line 2110  sub _tree_construction_main ($) { Line 3178  sub _tree_construction_main ($) {
3178        ## Step 5        ## Step 5
3179        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3180        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3181            !!!cp ('t62');
3182          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3183        }        }
3184                
# Line 2132  sub _tree_construction_main ($) { Line 3201  sub _tree_construction_main ($) {
3201          S7S2: {          S7S2: {
3202            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3203              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3204                  !!!cp ('t63');
3205                $node_i_in_active = $_;                $node_i_in_active = $_;
3206                last S7S2;                last S7S2;
3207              }              }
# Line 2145  sub _tree_construction_main ($) { Line 3215  sub _tree_construction_main ($) {
3215                    
3216          ## Step 4          ## Step 4
3217          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3218              !!!cp ('t64');
3219            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3220          }          }
3221                    
3222          ## Step 5          ## Step 5
3223          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3224              !!!cp ('t65');
3225            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3226            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3227            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2167  sub _tree_construction_main ($) { Line 3239  sub _tree_construction_main ($) {
3239        } # S7          } # S7  
3240                
3241        ## Step 8        ## Step 8
3242        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ({
3243               table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3244              }->{$common_ancestor_node->[1]}) {
3245            my $foster_parent_element;
3246            my $next_sibling;
3247                             OE: for (reverse 0..$#{$self->{open_elements}}) {
3248                               if ($self->{open_elements}->[$_]->[1] eq 'table') {
3249                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3250                                 if (defined $parent and $parent->node_type == 1) {
3251                                   !!!cp ('t65.1');
3252                                   $foster_parent_element = $parent;
3253                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3254                                 } else {
3255                                   !!!cp ('t65.2');
3256                                   $foster_parent_element
3257                                     = $self->{open_elements}->[$_ - 1]->[0];
3258                                 }
3259                                 last OE;
3260                               }
3261                             } # OE
3262                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3263                               unless defined $foster_parent_element;
3264            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3265            $open_tables->[-1]->[1] = 1; # tainted
3266          } else {
3267            !!!cp ('t65.3');
3268            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3269          }
3270                
3271        ## Step 9        ## Step 9
3272        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2184  sub _tree_construction_main ($) { Line 3283  sub _tree_construction_main ($) {
3283        my $i;        my $i;
3284        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3285          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3286              !!!cp ('t66');
3287            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3288            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3289          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3290              !!!cp ('t67');
3291            $i = $_;            $i = $_;
3292          }          }
3293        } # AFE        } # AFE
# Line 2196  sub _tree_construction_main ($) { Line 3297  sub _tree_construction_main ($) {
3297        undef $i;        undef $i;
3298        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3299          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3300              !!!cp ('t68');
3301            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3302            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3303          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3304              !!!cp ('t69');
3305            $i = $_;            $i = $_;
3306          }          }
3307        } # OE        } # OE
# Line 2209  sub _tree_construction_main ($) { Line 3312  sub _tree_construction_main ($) {
3312      } # FET      } # FET
3313    }; # $formatting_end_tag    }; # $formatting_end_tag
3314    
3315    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3316      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3317    }; # $insert_to_current    }; # $insert_to_current
3318    
3319    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3320                         my $child = shift;      my $child = shift;
3321                         if ({      if ({
3322                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3323                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3324                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3325                           # MUST        my $foster_parent_element;
3326                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3327                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3328                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3329                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3330                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3331                                   !!!cp ('t70');
3332                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3333                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3334                               } else {                               } else {
3335                                   !!!cp ('t71');
3336                                 $foster_parent_element                                 $foster_parent_element
3337                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3338                               }                               }
# Line 2239  sub _tree_construction_main ($) { Line 3343  sub _tree_construction_main ($) {
3343                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3344                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3345                             ($child, $next_sibling);                             ($child, $next_sibling);
3346                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3347                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3348                         }        !!!cp ('t72');
3349          $self->{open_elements}->[-1]->[0]->append_child ($child);
3350        }
3351    }; # $insert_to_foster    }; # $insert_to_foster
3352    
3353    my $in_body = sub {    B: {
3354      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3355      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3356        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3357          $script_start_tag->();        ## Ignore the token
3358          return;        ## Stay in the phase
3359        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
3360          $style_start_tag->();        redo B;
3361          return;      } elsif ($token->{type} == START_TAG_TOKEN and
3362        } elsif ({               $token->{tag_name} eq 'html') {
3363                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3364                 }->{$token->{tag_name}}) {          !!!cp ('t79');
3365          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html:html', token => $token);
3366          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
3367          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3368          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3369          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html:html', token => $token);
3370            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3371          } else {        } else {
3372            $insert->($el);          !!!cp ('t81');
3373          }        }
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: There is an "as if in head" code clone  
         my $title_el;  
         !!!create-element ($title_el, 'title', $token->{attributes});  
         (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
           ->append_child ($title_el);  
         $self->{content_model_flag} = 'RCDATA';  
         delete $self->{escape}; # MUST  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
         }  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
3374    
3375          !!!next-token;        !!!cp ('t82');
3376          return;        !!!parse-error (type => 'not first start tag', token => $token);
3377        } elsif ($token->{tag_name} eq 'marquee' or        my $top_el = $self->{open_elements}->[0]->[0];
3378                 $token->{tag_name} eq 'object') {        for my $attr_name (keys %{$token->{attributes}}) {
3379          $reconstruct_active_formatting_elements->($insert_to_current);          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3380                      !!!cp ('t84');
3381          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            $top_el->set_attribute_ns
3382          push @$active_formatting_elements, ['#marker', ''];              (undef, [undef, $attr_name],
3383                         $token->{attributes}->{$attr_name}->{value});
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
3384          }          }
3385          delete $self->{escape}; # MUST        }
3386                  !!!next-token;
3387          $insert->($el);        redo B;
3388                } elsif ($token->{type} == COMMENT_TOKEN) {
3389          my $text = '';        my $comment = $self->{document}->create_comment ($token->{data});
3390          if ($token->{tag_name} eq 'textarea') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3391            !!!next-token;          !!!cp ('t85');
3392            if ($token->{type} eq 'character') {          $self->{document}->append_child ($comment);
3393              $token->{data} =~ s/^\x0A//;        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3394              unless (length $token->{data}) {          !!!cp ('t86');
3395                !!!next-token;          $self->{open_elements}->[0]->[0]->append_child ($comment);
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
3396        } else {        } else {
3397          $reconstruct_active_formatting_elements->($insert_to_current);          !!!cp ('t87');
3398                    $self->{open_elements}->[-1]->[0]->append_child ($comment);
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
3399        }        }
3400      } elsif ($token->{type} eq 'end tag') {        !!!next-token;
3401        if ($token->{tag_name} eq 'body') {        redo B;
3402          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3403            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
3404            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3405              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3406            }              !!!cp ('t88.2');
3407            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3408            !!!next-token;            } else {
3409            return;              !!!cp ('t88.1');
3410          } else {              ## Ignore the token.
3411            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
3412            ## Ignore the token              redo B;
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
3413            }            }
3414          } # INSCOPE            unless (length $token->{data}) {
3415                        !!!cp ('t88');
3416          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
3417            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              redo B;
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
3418            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3419          }          }
3420    
3421          undef $self->{form_element};          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3422          !!!next-token;            !!!cp ('t89');
3423          return;            ## As if <head>
3424        } elsif ({            !!!create-element ($self->{head_element}, 'head',, $token);
3425                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3426                 }->{$token->{tag_name}}) {            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
3427    
3428          ## Step 2            ## Reprocess in the "in head" insertion mode...
3429          S2: {            pop @{$self->{open_elements}};
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
3430    
3431              !!!next-token;            ## Reprocess in the "after head" insertion mode...
3432              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3433            } else {            !!!cp ('t90');
3434              ## Step 3            ## As if </noscript>
3435              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
3436                  #not $phrasing_category->{$node->[1]} and            !!!parse-error (type => 'in noscript:#character', token => $token);
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
3437                        
3438            ## Step 5;            ## Reprocess in the "in head" insertion mode...
3439            redo S2;            ## As if </head>
3440          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
3441    
3442    B: {            ## Reprocess in the "after head" insertion mode...
3443      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3444        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
3445          !!!parse-error (type => 'in html:#DOCTYPE');            pop @{$self->{open_elements}};
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
3446    
3447          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
3448          last B;          } else {
3449              !!!cp ('t92');
3450            }
3451    
3452          ## ISSUE: There is an issue in the spec.              ## "after head" insertion mode
3453        } else {              ## As if <body>
3454          if ($self->{insertion_mode} eq 'before head') {              !!!insert-element ('body',, $token);
3455            if ($token->{type} eq 'character') {              $self->{insertion_mode} = IN_BODY_IM;
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
3456              ## reprocess              ## reprocess
3457              redo B;              redo B;
3458            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
3459              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3460                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3461              #} elsif ({                  !!!cp ('t93');
3462              #          base => 1, link => 1, meta => 1,                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);
3463              #          script => 1, style => 1, title => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3464              #         }->{$token->{tag_name}}) {                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3465              #  ## reprocess                  $self->{insertion_mode} = IN_HEAD_IM;
3466              } else {                  !!!next-token;
3467                ## reprocess                  redo B;
3468              }                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3469              redo B;                  !!!cp ('t94');
3470            } elsif ($token->{type} eq 'end tag') {                  #
3471              if ($token->{tag_name} eq 'html') {                } else {
3472                    !!!cp ('t95');
3473                    !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3474                    ## Ignore the token
3475                    !!!next-token;
3476                    redo B;
3477                  }
3478                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3479                  !!!cp ('t96');
3480                ## As if <head>                ## As if <head>
3481                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head',, $token);
3482                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3483                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3484                $self->{insertion_mode} = 'in head';  
3485                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
3486                redo B;                ## Reprocess in the "in head" insertion mode...
3487              } else {              } else {
3488                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t97');
               ## Ignore the token  
               !!!next-token;  
               redo B;  
3489              }              }
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'title') {  
               ## NOTE: There is an "as if in head" code clone  
               my $title_el;  
               !!!create-element ($title_el, 'title', $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($title_el);  
               $self->{content_model_flag} = 'RCDATA';  
               delete $self->{escape}; # MUST  
3490    
3491                my $text = '';              if ($token->{tag_name} eq 'base') {
3492                !!!next-token;                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3493                while ($token->{type} eq 'character') {                  !!!cp ('t98');
3494                  $text .= $token->{data};                  ## As if </noscript>
3495                  !!!next-token;                  pop @{$self->{open_elements}};
3496                }                  !!!parse-error (type => 'in noscript:base', token => $token);
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
3497                                
3498                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
3499                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
3500                } else {                } else {
3501                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
3502                }                }
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
3503    
3504                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
3505                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3506              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
3507                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3508                ## Ignore the token                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{open_elements}->[-1]->[1] eq 'head') {  
                 pop @{$self->{open_elements}};  
3509                } else {                } else {
3510                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
3511                }                }
3512                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3513                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3514                  pop @{$self->{open_elements}} # <head>
3515                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3516                !!!next-token;                !!!next-token;
3517                redo B;                redo B;
3518              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'link') {
3519                #                ## NOTE: There is a "as if in head" code clone.
3520              } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3521                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t102');
3522                ## Ignore the token                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3523                !!!next-token;                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3524                redo B;                } else {
3525              }                  !!!cp ('t103');
           } else {  
             #  
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
3526                }                }
3527              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3528                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3529              #                pop @{$self->{open_elements}} # <head>
3530            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
3531                !!!next-token;                !!!next-token;
3532                redo B;                redo B;
3533              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
3534                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
3535                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3536                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
3537                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3538                $self->{insertion_mode} = 'in head';                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3539                ## reprocess                } else {
3540                redo B;                  !!!cp ('t105');
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
3541                }                }
3542              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3543                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
3544    
3545              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
3546              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3547              ## into the current node" while characters might not be                    !!!cp ('t106');
3548              ## result in a new Text node.                    $self->{change_encoding}
3549              $reconstruct_active_formatting_elements->($insert_to_foster);                        ->($self, $token->{attributes}->{charset}->{value},
3550                                         $token);
3551              if ({                    
3552                   table => 1, tbody => 1, tfoot => 1,                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3553                   thead => 1, tr => 1,                        ->set_user_data (manakai_has_reference =>
3554                  }->{$self->{open_elements}->[-1]->[1]}) {                                             $token->{attributes}->{charset}
3555                # MUST                                                 ->{has_reference});
3556                my $foster_parent_element;                  } elsif ($token->{attributes}->{content}) {
3557                my $next_sibling;                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3558                my $prev_sibling;                    if ($token->{attributes}->{content}->{value}
3559                OE: for (reverse 0..$#{$self->{open_elements}}) {                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3560                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                            [\x09-\x0D\x20]*=
3561                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3562                    if (defined $parent and $parent->node_type == 1) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3563                      $foster_parent_element = $parent;                      !!!cp ('t107');
3564                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $self->{change_encoding}
3565                      $prev_sibling = $next_sibling->previous_sibling;                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3566                               $token);
3567                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3568                            ->set_user_data (manakai_has_reference =>
3569                                                 $token->{attributes}->{content}
3570                                                       ->{has_reference});
3571                    } else {                    } else {
3572                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
3573                    }                    }
                   last OE;  
3574                  }                  }
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
3575                } else {                } else {
3576                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
3577                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
3578                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3579                }                        ->set_user_data (manakai_has_reference =>
3580              } else {                                             $token->{attributes}->{charset}
3581                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
3582              }                  }
3583                                if ($token->{attributes}->{content}) {
3584              !!!next-token;                    !!!cp ('t110');
3585              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3586            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
3587              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
3588              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
3589              !!!next-token;                  }
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
3590                }                }
3591    
3592                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
3593                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
3594                !!!next-token;                !!!next-token;
3595                redo B;                redo B;
3596              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
3597                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3598                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
3599                       }->{$token->{tag_name}}) {                  ## As if </noscript>
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3600                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3601                    !!!parse-error (type => 'in noscript:title', token => $token);
3602                  
3603                    $self->{insertion_mode} = IN_HEAD_IM;
3604                    ## Reprocess in the "in head" insertion mode...
3605                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3606                    !!!cp ('t112');
3607                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3608                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3609                  } else {
3610                    !!!cp ('t113');
3611                }                }
3612    
3613                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
3614                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
3615                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
3616                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3617                  pop @{$self->{open_elements}} # <head>
3618                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3619                redo B;                redo B;
3620              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'style') {
3621                ## NOTE: There are code clones for this "table in table"                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3622                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## insertion mode IN_HEAD_IM)
3623                  ## NOTE: There is a "as if in head" code clone.
3624                ## As if </table>                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3625                ## have a table element in table scope                  !!!cp ('t114');
3626                my $i;                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3627                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3628                  my $node = $self->{open_elements}->[$_];                } else {
3629                  if ($node->[1] eq 'table') {                  !!!cp ('t115');
3630                    $i = $_;                }
3631                    last INSCOPE;                $parse_rcdata->(CDATA_CONTENT_MODEL);
3632                  } elsif ({                pop @{$self->{open_elements}} # <head>
3633                            table => 1, html => 1,                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3634                           }->{$node->[1]}) {                redo B;
3635                    last INSCOPE;              } elsif ($token->{tag_name} eq 'noscript') {
3636                  }                if ($self->{insertion_mode} == IN_HEAD_IM) {
3637                } # INSCOPE                  !!!cp ('t116');
3638                unless (defined $i) {                  ## NOTE: and scripting is disalbed
3639                  !!!parse-error (type => 'unmatched end tag:table');                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3640                  ## Ignore tokens </table><table>                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3641                    !!!next-token;
3642                    redo B;
3643                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3644                    !!!cp ('t117');
3645                    !!!parse-error (type => 'in noscript:noscript', token => $token);
3646                    ## Ignore the token
3647                  !!!next-token;                  !!!next-token;
3648                  redo B;                  redo B;
3649                  } else {
3650                    !!!cp ('t118');
3651                    #
3652                }                }
3653                } elsif ($token->{tag_name} eq 'script') {
3654                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3655                    !!!cp ('t119');
3656                    ## As if </noscript>
3657                    pop @{$self->{open_elements}};
3658                    !!!parse-error (type => 'in noscript:script', token => $token);
3659                                
3660                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
3661                if ({                  ## Reprocess in the "in head" insertion mode...
3662                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3663                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
3664                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3665                  !!!back-token; # <table>                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3666                  $token = {type => 'end tag', tag_name => 'table'};                } else {
3667                  !!!back-token;                  !!!cp ('t121');
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
3668                }                }
3669    
3670                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
3671                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
3672                  pop @{$self->{open_elements}} # <head>
3673                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3674                  redo B;
3675                } elsif ($token->{tag_name} eq 'body' or
3676                         $token->{tag_name} eq 'frameset') {
3677                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3678                    !!!cp ('t122');
3679                    ## As if </noscript>
3680                    pop @{$self->{open_elements}};
3681                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3682                    
3683                    ## Reprocess in the "in head" insertion mode...
3684                    ## As if </head>
3685                    pop @{$self->{open_elements}};
3686                    
3687                    ## Reprocess in the "after head" insertion mode...
3688                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3689                    !!!cp ('t124');
3690                    pop @{$self->{open_elements}};
3691                    
3692                    ## Reprocess in the "after head" insertion mode...
3693                  } else {
3694                    !!!cp ('t125');
3695                }                }
3696    
3697                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
3698                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3699                $self->_reset_insertion_mode;                if ($token->{tag_name} eq 'body') {
3700                    !!!cp ('t126');
3701                ## reprocess                  $self->{insertion_mode} = IN_BODY_IM;
3702                  } elsif ($token->{tag_name} eq 'frameset') {
3703                    !!!cp ('t127');
3704                    $self->{insertion_mode} = IN_FRAMESET_IM;
3705                  } else {
3706                    die "$0: tag name: $self->{tag_name}";
3707                  }
3708                  !!!next-token;
3709                redo B;                redo B;
3710              } else {              } else {
3711                  !!!cp ('t128');
3712                #                #
3713              }              }
3714            } elsif ($token->{type} eq 'end tag') {  
3715              if ($token->{tag_name} eq 'table') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3716                ## have a table element in table scope                !!!cp ('t129');
3717                my $i;                ## As if </noscript>
3718                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                pop @{$self->{open_elements}};
3719                  my $node = $self->{open_elements}->[$_];                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3720                  if ($node->[1] eq $token->{tag_name}) {                
3721                    $i = $_;                ## Reprocess in the "in head" insertion mode...
3722                    last INSCOPE;                ## As if </head>
3723                  } elsif ({                pop @{$self->{open_elements}};
3724                            table => 1, html => 1,  
3725                           }->{$node->[1]}) {                ## Reprocess in the "after head" insertion mode...
3726                    last INSCOPE;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3727                  }                !!!cp ('t130');
3728                } # INSCOPE                ## As if </head>
3729                unless (defined $i) {                pop @{$self->{open_elements}};
3730                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
3731                  ## Ignore the token                ## Reprocess in the "after head" insertion mode...
3732                } else {
3733                  !!!cp ('t131');
3734                }
3735    
3736                ## "after head" insertion mode
3737                ## As if <body>
3738                !!!insert-element ('body',, $token);
3739                $self->{insertion_mode} = IN_BODY_IM;
3740                ## reprocess
3741                redo B;
3742              } elsif ($token->{type} == END_TAG_TOKEN) {
3743                if ($token->{tag_name} eq 'head') {
3744                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3745                    !!!cp ('t132');
3746                    ## As if <head>
3747                    !!!create-element ($self->{head_element}, 'head',, $token);
3748                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3749                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3750    
3751                    ## Reprocess in the "in head" insertion mode...
3752                    pop @{$self->{open_elements}};
3753                    $self->{insertion_mode} = AFTER_HEAD_IM;
3754                    !!!next-token;
3755                    redo B;
3756                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3757                    !!!cp ('t133');
3758                    ## As if </noscript>
3759                    pop @{$self->{open_elements}};
3760                    !!!parse-error (type => 'in noscript:/head', token => $token);
3761                    
3762                    ## Reprocess in the "in head" insertion mode...
3763                    pop @{$self->{open_elements}};
3764                    $self->{insertion_mode} = AFTER_HEAD_IM;
3765                    !!!next-token;
3766                    redo B;
3767                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3768                    !!!cp ('t134');
3769                    pop @{$self->{open_elements}};
3770                    $self->{insertion_mode} = AFTER_HEAD_IM;
3771                  !!!next-token;                  !!!next-token;
3772                  redo B;                  redo B;
3773                  } else {
3774                    !!!cp ('t135');
3775                    #
3776                }                }
3777                              } elsif ($token->{tag_name} eq 'noscript') {
3778                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3779                if ({                  !!!cp ('t136');
3780                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
3781                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
3782                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
3783                  !!!back-token;                  redo B;
3784                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3785                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t137');
3786                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3787                    ## Ignore the token ## ISSUE: An issue in the spec.
3788                    !!!next-token;
3789                  redo B;                  redo B;
3790                  } else {
3791                    !!!cp ('t138');
3792                    #
3793                }                }
3794                } elsif ({
3795                          body => 1, html => 1,
3796                         }->{$token->{tag_name}}) {
3797                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3798                    !!!cp ('t139');
3799                    ## As if <head>
3800                    !!!create-element ($self->{head_element}, 'head',, $token);
3801                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3802                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3803    
3804                    $self->{insertion_mode} = IN_HEAD_IM;
3805                    ## Reprocess in the "in head" insertion mode...
3806                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3807                    !!!cp ('t140');
3808                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3809                    ## Ignore the token
3810                    !!!next-token;
3811                    redo B;
3812                  } else {
3813                    !!!cp ('t141');
3814                  }
3815                  
3816                  #
3817                } elsif ({
3818                          p => 1, br => 1,
3819                         }->{$token->{tag_name}}) {
3820                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3821                    !!!cp ('t142');
3822                    ## As if <head>
3823                    !!!create-element ($self->{head_element}, 'head',, $token);
3824                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3825                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3826    
3827                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3828                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3829                  } else {
3830                    !!!cp ('t143');
3831                }                }
3832    
3833                splice @{$self->{open_elements}}, $i;                #
3834                } else {
3835                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3836                    !!!cp ('t144');
3837                    #
3838                  } else {
3839                    !!!cp ('t145');
3840                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3841                    ## Ignore the token
3842                    !!!next-token;
3843                    redo B;
3844                  }
3845                }
3846    
3847                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3848                  !!!cp ('t146');
3849                  ## As if </noscript>
3850                  pop @{$self->{open_elements}};
3851                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3852                  
3853                  ## Reprocess in the "in head" insertion mode...
3854                  ## As if </head>
3855                  pop @{$self->{open_elements}};
3856    
3857                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3858                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3859              } elsif ({                !!!cp ('t147');
3860                        body => 1, caption => 1, col => 1, colgroup => 1,                ## As if </head>
3861                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                pop @{$self->{open_elements}};
3862                        thead => 1, tr => 1,  
3863                       }->{$token->{tag_name}}) {                ## Reprocess in the "after head" insertion mode...
3864                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3865                ## Ignore the token  ## ISSUE: This case cannot be reached?
3866                  !!!cp ('t148');
3867                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3868                  ## Ignore the token ## ISSUE: An issue in the spec.
3869                !!!next-token;                !!!next-token;
3870                redo B;                redo B;
3871              } else {              } else {
3872                #                !!!cp ('t149');
3873              }              }
           } else {  
             #  
           }  
3874    
3875            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
3876            $in_body->($insert_to_foster);              ## As if <body>
3877            redo B;              !!!insert-element ('body',, $token);
3878          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
3879            if ($token->{type} eq 'character') {              ## reprocess
3880              ## NOTE: This is a code clone of "character in body".              redo B;
3881          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3882            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3883              !!!cp ('t149.1');
3884    
3885              ## NOTE: As if <head>
3886              !!!create-element ($self->{head_element}, 'head',, $token);
3887              $self->{open_elements}->[-1]->[0]->append_child
3888                  ($self->{head_element});
3889              #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3890              #$self->{insertion_mode} = IN_HEAD_IM;
3891              ## NOTE: Reprocess.
3892    
3893              ## NOTE: As if </head>
3894              #pop @{$self->{open_elements}};
3895              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3896              ## NOTE: Reprocess.
3897              
3898              #
3899            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3900              !!!cp ('t149.2');
3901    
3902              ## NOTE: As if </head>
3903              pop @{$self->{open_elements}};
3904              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3905              ## NOTE: Reprocess.
3906    
3907              #
3908            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3909              !!!cp ('t149.3');
3910    
3911              !!!parse-error (type => 'in noscript:#eof', token => $token);
3912    
3913              ## As if </noscript>
3914              pop @{$self->{open_elements}};
3915              #$self->{insertion_mode} = IN_HEAD_IM;
3916              ## NOTE: Reprocess.
3917    
3918              ## NOTE: As if </head>
3919              pop @{$self->{open_elements}};
3920              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3921              ## NOTE: Reprocess.
3922    
3923              #
3924            } else {
3925              !!!cp ('t149.4');
3926              #
3927            }
3928    
3929            ## NOTE: As if <body>
3930            !!!insert-element ('body',, $token);
3931            $self->{insertion_mode} = IN_BODY_IM;
3932            ## NOTE: Reprocess.
3933            redo B;
3934          } else {
3935            die "$0: $token->{type}: Unknown token type";
3936          }
3937    
3938              ## ISSUE: An issue in the spec.
3939        } elsif ($self->{insertion_mode} & BODY_IMS) {
3940              if ($token->{type} == CHARACTER_TOKEN) {
3941                !!!cp ('t150');
3942                ## NOTE: There is a code clone of "character in body".
3943              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3944                            
3945              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3946    
3947              !!!next-token;              !!!next-token;
3948              redo B;              redo B;
3949            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
3950              if ({              if ({
3951                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3952                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3953                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3954                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3955                    ## have an element in table scope
3956                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
3957                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
3958                my $i;                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3959                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
3960                  my $node = $self->{open_elements}->[$_];  
3961                  if ($node->[1] eq 'caption') {                      ## Close the cell
3962                    $i = $_;                      !!!back-token; # <?>
3963                    last INSCOPE;                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],
3964                  } elsif ({                                line => $token->{line},
3965                            table => 1, html => 1,                                column => $token->{column}};
3966                           }->{$node->[1]}) {                      redo B;
3967                    last INSCOPE;                    } elsif ({
3968                                table => 1, html => 1,
3969                               }->{$node->[1]}) {
3970                        !!!cp ('t152');
3971                        ## ISSUE: This case can never be reached, maybe.
3972                        last;
3973                      }
3974                  }                  }
3975                } # INSCOPE  
3976                unless (defined $i) {                  !!!cp ('t153');
3977                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
3978                        value => $token->{tag_name}, token => $token);
3979                  ## Ignore the token                  ## Ignore the token
3980                  !!!next-token;                  !!!next-token;
3981                  redo B;                  redo B;
3982                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3983                                  !!!parse-error (type => 'not closed:caption', token => $token);
3984                ## generate implied end tags                  
3985                if ({                  ## NOTE: As if </caption>.
3986                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
3987                     td => 1, th => 1, tr => 1,                  my $i;
3988                    }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: {
3989                  !!!back-token; # <?>                    for (reverse 0..$#{$self->{open_elements}}) {
3990                  $token = {type => 'end tag', tag_name => 'caption'};                      my $node = $self->{open_elements}->[$_];
3991                  !!!back-token;                      if ($node->[1] eq 'caption') {
3992                  $token = {type => 'end tag',                        !!!cp ('t155');
3993                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        $i = $_;
3994                  redo B;                        last INSCOPE;
3995                }                      } elsif ({
3996                                  table => 1, html => 1,
3997                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                               }->{$node->[1]}) {
3998                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        !!!cp ('t156');
3999                }                        last;
4000                        }
4001                splice @{$self->{open_elements}}, $i;                    }
   
               $clear_up_to_marker->();  
4002    
4003                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4004                      !!!parse-error (type => 'start tag not allowed',
4005                                      value => $token->{tag_name}, token => $token);
4006                      ## Ignore the token
4007                      !!!next-token;
4008                      redo B;
4009                    } # INSCOPE
4010                    
4011                    ## generate implied end tags
4012                    while ({
4013                            dd => 1, dt => 1, li => 1, p => 1,
4014                           }->{$self->{open_elements}->[-1]->[1]}) {
4015                      !!!cp ('t158');
4016                      pop @{$self->{open_elements}};
4017                    }
4018    
4019                ## reprocess                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4020                redo B;                    !!!cp ('t159');
4021                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4022                    } else {
4023                      !!!cp ('t160');
4024                    }
4025                    
4026                    splice @{$self->{open_elements}}, $i;
4027                    
4028                    $clear_up_to_marker->();
4029                    
4030                    $self->{insertion_mode} = IN_TABLE_IM;
4031                    
4032                    ## reprocess
4033                    redo B;
4034                  } else {
4035                    !!!cp ('t161');
4036                    #
4037                  }
4038              } else {              } else {
4039                  !!!cp ('t162');
4040                #                #
4041              }              }
4042            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4043              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4044                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4045                my $i;                  ## have an element in table scope
4046                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4047                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4048                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4049                    $i = $_;                    if ($node->[1] eq $token->{tag_name}) {
4050                    last INSCOPE;                      !!!cp ('t163');
4051                  } elsif ({                      $i = $_;
4052                            table => 1, html => 1,                      last INSCOPE;
4053                           }->{$node->[1]}) {                    } elsif ({
4054                    last INSCOPE;                              table => 1, html => 1,
4055                               }->{$node->[1]}) {
4056                        !!!cp ('t164');
4057                        last INSCOPE;
4058                      }
4059                    } # INSCOPE
4060                      unless (defined $i) {
4061                        !!!cp ('t165');
4062                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4063                        ## Ignore the token
4064                        !!!next-token;
4065                        redo B;
4066                      }
4067                    
4068                    ## generate implied end tags
4069                    while ({
4070                            dd => 1, dt => 1, li => 1, p => 1,
4071                           }->{$self->{open_elements}->[-1]->[1]}) {
4072                      !!!cp ('t166');
4073                      pop @{$self->{open_elements}};
4074                  }                  }
4075                } # INSCOPE  
4076                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4077                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t167');
4078                  ## Ignore the token                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4079                    } else {
4080                      !!!cp ('t168');
4081                    }
4082                    
4083                    splice @{$self->{open_elements}}, $i;
4084                    
4085                    $clear_up_to_marker->();
4086                    
4087                    $self->{insertion_mode} = IN_ROW_IM;
4088                    
4089                  !!!next-token;                  !!!next-token;
4090                  redo B;                  redo B;
4091                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4092                                  !!!cp ('t169');
4093                ## generate implied end tags                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4094                if ({                  ## Ignore the token
4095                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
4096                  redo B;                  redo B;
4097                  } else {
4098                    !!!cp ('t170');
4099                    #
4100                }                }
4101                } elsif ($token->{tag_name} eq 'caption') {
4102                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4103                    ## have a table element in table scope
4104                    my $i;
4105                    INSCOPE: {
4106                      for (reverse 0..$#{$self->{open_elements}}) {
4107                        my $node = $self->{open_elements}->[$_];
4108                        if ($node->[1] eq $token->{tag_name}) {
4109                          !!!cp ('t171');
4110                          $i = $_;
4111                          last INSCOPE;
4112                        } elsif ({
4113                                  table => 1, html => 1,
4114                                 }->{$node->[1]}) {
4115                          !!!cp ('t172');
4116                          last;
4117                        }
4118                      }
4119    
4120                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4121                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4122                                      value => $token->{tag_name}, token => $token);
4123                      ## Ignore the token
4124                      !!!next-token;
4125                      redo B;
4126                    } # INSCOPE
4127                    
4128                    ## generate implied end tags
4129                    while ({
4130                            dd => 1, dt => 1, li => 1, p => 1,
4131                           }->{$self->{open_elements}->[-1]->[1]}) {
4132                      !!!cp ('t174');
4133                      pop @{$self->{open_elements}};
4134                    }
4135                    
4136                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4137                      !!!cp ('t175');
4138                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4139                    } else {
4140                      !!!cp ('t176');
4141                    }
4142                    
4143                    splice @{$self->{open_elements}}, $i;
4144                    
4145                    $clear_up_to_marker->();
4146                    
4147                    $self->{insertion_mode} = IN_TABLE_IM;
4148                    
4149                    !!!next-token;
4150                    redo B;
4151                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4152                    !!!cp ('t177');
4153                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4154                    ## Ignore the token
4155                    !!!next-token;
4156                    redo B;
4157                  } else {
4158                    !!!cp ('t178');
4159                    #
4160                }                }
4161                } elsif ({
4162                          table => 1, tbody => 1, tfoot => 1,
4163                          thead => 1, tr => 1,
4164                         }->{$token->{tag_name}} and
4165                         $self->{insertion_mode} == IN_CELL_IM) {
4166                  ## have an element in table scope
4167                  my $i;
4168                  my $tn;
4169                  INSCOPE: {
4170                    for (reverse 0..$#{$self->{open_elements}}) {
4171                      my $node = $self->{open_elements}->[$_];
4172                      if ($node->[1] eq $token->{tag_name}) {
4173                        !!!cp ('t179');
4174                        $i = $_;
4175    
4176                        ## Close the cell
4177                        !!!back-token; # </?>
4178                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
4179                                  line => $token->{line},
4180                                  column => $token->{column}};
4181                        redo B;
4182                      } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4183                        !!!cp ('t180');
4184                        $tn = $node->[1];
4185                        ## NOTE: There is exactly one |td| or |th| element
4186                        ## in scope in the stack of open elements by definition.
4187                      } elsif ({
4188                                table => 1, html => 1,
4189                               }->{$node->[1]}) {
4190                        ## ISSUE: Can this be reached?
4191                        !!!cp ('t181');
4192                        last;
4193                      }
4194                    }
4195    
4196                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4197                    !!!parse-error (type => 'unmatched end tag',
4198                $clear_up_to_marker->();                      value => $token->{tag_name}, token => $token);
4199                    ## Ignore the token
4200                $self->{insertion_mode} = 'in table';                  !!!next-token;
4201                    redo B;
4202                !!!next-token;                } # INSCOPE
4203                redo B;              } elsif ($token->{tag_name} eq 'table' and
4204              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4205                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4206    
4207                ## As if </caption>                ## As if </caption>
4208                ## have a table element in table scope                ## have a table element in table scope
# Line 3650  sub _tree_construction_main ($) { Line 4210  sub _tree_construction_main ($) {
4210                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4211                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4212                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4213                      !!!cp ('t184');
4214                    $i = $_;                    $i = $_;
4215                    last INSCOPE;                    last INSCOPE;
4216                  } elsif ({                  } elsif ({
4217                            table => 1, html => 1,                            table => 1, html => 1,
4218                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4219                      !!!cp ('t185');
4220                    last INSCOPE;                    last INSCOPE;
4221                  }                  }
4222                } # INSCOPE                } # INSCOPE
4223                unless (defined $i) {                unless (defined $i) {
4224                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4225                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4226                  ## Ignore the token                  ## Ignore the token
4227                  !!!next-token;                  !!!next-token;
4228                  redo B;                  redo B;
4229                }                }
4230                                
4231                ## generate implied end tags                ## generate implied end tags
4232                if ({                while ({
4233                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4234                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4235                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t187');
4236                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4237                }                }
4238    
4239                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4240                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4241                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4242                  } else {
4243                    !!!cp ('t189');
4244                }                }
4245    
4246                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4247    
4248                $clear_up_to_marker->();                $clear_up_to_marker->();
4249    
4250                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4251    
4252                ## reprocess                ## reprocess
4253                redo B;                redo B;
4254              } elsif ({              } elsif ({
4255                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
4256                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4257                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4258                ## Ignore the token                  !!!cp ('t190');
4259                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
4260                  ## Ignore the token                  ## Ignore the token
4261                  !!!next-token;                  !!!next-token;
4262                  redo B;                  redo B;
4263                } else {                } else {
4264                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4265                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4266                }                }
4267              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4268                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4269                          thead => 1, tr => 1,
4270                         }->{$token->{tag_name}} and
4271                         $self->{insertion_mode} == IN_CAPTION_IM) {
4272                  !!!cp ('t192');
4273                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4274                ## Ignore the token                ## Ignore the token
4275                !!!next-token;                !!!next-token;
4276                redo B;                redo B;
4277              } else {              } else {
4278                #                !!!cp ('t193');
4279                  #
4280              }              }
4281            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4282              #          for my $entry (@{$self->{open_elements}}) {
4283              if (not {
4284                dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,
4285                th => 1, thead => 1, tr => 1, body => 1, html => 1,
4286              }->{$entry->[1]}) {
4287                !!!cp ('t75');
4288                !!!parse-error (type => 'in body:#eof', token => $token);
4289                last;
4290            }            }
4291            }
4292    
4293            ## As if </colgroup>          ## Stop parsing.
4294            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
4295              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
4296              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
4297          }
4298    
4299          $insert = $insert_to_current;
4300          #
4301        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4302          if ($token->{type} == CHARACTER_TOKEN) {
4303            if (not $open_tables->[-1]->[1] and # tainted
4304                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4305              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4306                  
4307              unless (length $token->{data}) {
4308                !!!cp ('t194');
4309              !!!next-token;              !!!next-token;
4310              redo B;              redo B;
4311            } else {            } else {
4312              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
4313            }            }
4314          } elsif ($self->{insertion_mode} eq 'in table body') {          }
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
4315    
4316              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4317    
4318              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4319              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
4320              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4321              ## result in a new Text node.              ## result in a new Text node.
4322              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4323                
4324              if ({              if ({
4325                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4326                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3801  sub _tree_construction_main ($) { Line 4333  sub _tree_construction_main ($) {
4333                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4334                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4335                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4336                        !!!cp ('t196');
4337                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4338                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4339                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4340                    } else {                    } else {
4341                        !!!cp ('t197');
4342                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4343                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4344                    }                    }
# Line 3816  sub _tree_construction_main ($) { Line 4350  sub _tree_construction_main ($) {
4350                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4351                if (defined $prev_sibling and                if (defined $prev_sibling and
4352                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4353                    !!!cp ('t198');
4354                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4355                } else {                } else {
4356                    !!!cp ('t199');
4357                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4358                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4359                     $next_sibling);                     $next_sibling);
4360                }                }
4361              } else {            $open_tables->[-1]->[1] = 1; # tainted
4362                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4363              }            !!!cp ('t200');
4364              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4365            }
4366                            
4367              !!!next-token;          !!!next-token;
4368              redo B;          redo B;
4369            } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == START_TAG_TOKEN) {
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
4370              if ({              if ({
4371                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4372                   th => 1, td => 1,                   th => 1, td => 1,
4373                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4374                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4375                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4376                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4377                           $self->{open_elements}->[-1]->[1] ne 'html') {
4378                      !!!cp ('t201');
4379                      pop @{$self->{open_elements}};
4380                    }
4381                    
4382                    !!!insert-element ('tbody',, $token);
4383                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4384                    ## reprocess in the "in table body" insertion mode...
4385                }                }
4386    
4387                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4388                    unless ($token->{tag_name} eq 'tr') {
4389                      !!!cp ('t202');
4390                      !!!parse-error (type => 'missing start tag:tr', token => $token);
4391                    }
4392                    
4393                    ## Clear back to table body context
4394                    while (not {
4395                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4396                    }->{$self->{open_elements}->[-1]->[1]}) {
4397                      !!!cp ('t203');
4398                      ## ISSUE: Can this case be reached?
4399                      pop @{$self->{open_elements}};
4400                    }
4401                    
4402                    $self->{insertion_mode} = IN_ROW_IM;
4403                    if ($token->{tag_name} eq 'tr') {
4404                      !!!cp ('t204');
4405                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4406                      !!!next-token;
4407                      redo B;
4408                    } else {
4409                      !!!cp ('t205');
4410                      !!!insert-element ('tr',, $token);
4411                      ## reprocess in the "in row" insertion mode
4412                    }
4413                  } else {
4414                    !!!cp ('t206');
4415                  }
4416    
4417                  ## Clear back to table row context
4418                while (not {                while (not {
4419                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4420                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4421                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4423                }                }
4424                                
4425                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4426                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
4427                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
4428                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
4429                } else {                
4430                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
4431                redo B;                redo B;
4432              } elsif ({              } elsif ({
4433                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4434                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4435                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4436                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4437                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
4438                my $i;                  ## As if </tr>
4439                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4440                  my $node = $self->{open_elements}->[$_];                  my $i;
4441                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4442                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4443                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4444                    $i = $_;                      !!!cp ('t208');
4445                    last INSCOPE;                      $i = $_;
4446                  } elsif ({                      last INSCOPE;
4447                            table => 1, html => 1,                    } elsif ({
4448                           }->{$node->[1]}) {                              html => 1,
4449                    last INSCOPE;  
4450                                ## NOTE: This element does not appear here, maybe.
4451                                table => 1,
4452                               }->{$node->[1]}) {
4453                        !!!cp ('t209');
4454                        last INSCOPE;
4455                      }
4456                    } # INSCOPE
4457                    unless (defined $i) {
4458                     !!!cp ('t210');
4459    ## TODO: This type is wrong.
4460                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4461                      ## Ignore the token
4462                      !!!next-token;
4463                      redo B;
4464                    }
4465                    
4466                    ## Clear back to table row context
4467                    while (not {
4468                      tr => 1, html => 1,
4469                    }->{$self->{open_elements}->[-1]->[1]}) {
4470                      !!!cp ('t211');
4471                      ## ISSUE: Can this case be reached?
4472                      pop @{$self->{open_elements}};
4473                    }
4474                    
4475                    pop @{$self->{open_elements}}; # tr
4476                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4477                    if ($token->{tag_name} eq 'tr') {
4478                      !!!cp ('t212');
4479                      ## reprocess
4480                      redo B;
4481                    } else {
4482                      !!!cp ('t213');
4483                      ## reprocess in the "in table body" insertion mode...
4484                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4485                }                }
4486    
4487                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4488                while (not {                  ## have an element in table scope
4489                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4490                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4491                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4492                      if ({
4493                           tbody => 1, thead => 1, tfoot => 1,
4494                          }->{$node->[1]}) {
4495                        !!!cp ('t214');
4496                        $i = $_;
4497                        last INSCOPE;
4498                      } elsif ({
4499                                table => 1, html => 1,
4500                               }->{$node->[1]}) {
4501                        !!!cp ('t215');
4502                        last INSCOPE;
4503                      }
4504                    } # INSCOPE
4505                    unless (defined $i) {
4506                      !!!cp ('t216');
4507    ## TODO: This erorr type ios wrong.
4508                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4509                      ## Ignore the token
4510                      !!!next-token;
4511                      redo B;
4512                    }
4513    
4514                    ## Clear back to table body context
4515                    while (not {
4516                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4517                    }->{$self->{open_elements}->[-1]->[1]}) {
4518                      !!!cp ('t217');
4519                      ## ISSUE: Can this state be reached?
4520                      pop @{$self->{open_elements}};
4521                    }
4522                    
4523                    ## As if <{current node}>
4524                    ## have an element in table scope
4525                    ## true by definition
4526                    
4527                    ## Clear back to table body context
4528                    ## nop by definition
4529                    
4530                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4531                    $self->{insertion_mode} = IN_TABLE_IM;
4532                    ## reprocess in "in table" insertion mode...
4533                  } else {
4534                    !!!cp ('t218');
4535                }                }
4536    
4537                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4538                ## have an element in table scope                  ## Clear back to table context
4539                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4540                           $self->{open_elements}->[-1]->[1] ne 'html') {
4541                ## Clear back to table body context                    !!!cp ('t219');
4542                ## nop by definition                    ## ISSUE: Can this state be reached?
4543                      pop @{$self->{open_elements}};
4544                pop @{$self->{open_elements}};                  }
4545                $self->{insertion_mode} = 'in table';                  
4546                ## reprocess                  !!!insert-element ('colgroup',, $token);
4547                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4548                    ## reprocess
4549                    redo B;
4550                  } elsif ({
4551                            caption => 1,
4552                            colgroup => 1,
4553                            tbody => 1, tfoot => 1, thead => 1,
4554                           }->{$token->{tag_name}}) {
4555                    ## Clear back to table context
4556                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4557                           $self->{open_elements}->[-1]->[1] ne 'html') {
4558                      !!!cp ('t220');
4559                      ## ISSUE: Can this state be reached?
4560                      pop @{$self->{open_elements}};
4561                    }
4562                    
4563                    push @$active_formatting_elements, ['#marker', '']
4564                        if $token->{tag_name} eq 'caption';
4565                    
4566                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4567                    $self->{insertion_mode} = {
4568                                               caption => IN_CAPTION_IM,
4569                                               colgroup => IN_COLUMN_GROUP_IM,
4570                                               tbody => IN_TABLE_BODY_IM,
4571                                               tfoot => IN_TABLE_BODY_IM,
4572                                               thead => IN_TABLE_BODY_IM,
4573                                              }->{$token->{tag_name}};
4574                    !!!next-token;
4575                    redo B;
4576                  } else {
4577                    die "$0: in table: <>: $token->{tag_name}";
4578                  }
4579              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4580                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
               !!!parse-error (type => 'not closed:table');  
4581    
4582                ## As if </table>                ## As if </table>
4583                ## have a table element in table scope                ## have a table element in table scope
# Line 3915  sub _tree_construction_main ($) { Line 4585  sub _tree_construction_main ($) {
4585                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4586                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4587                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4588                      !!!cp ('t221');
4589                    $i = $_;                    $i = $_;
4590                    last INSCOPE;                    last INSCOPE;
4591                  } elsif ({                  } elsif ({
4592                            table => 1, html => 1,                            #table => 1,
4593                              html => 1,
4594                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4595                      !!!cp ('t222');
4596                    last INSCOPE;                    last INSCOPE;
4597                  }                  }
4598                } # INSCOPE                } # INSCOPE
4599                unless (defined $i) {                unless (defined $i) {
4600                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
4601    ## TODO: The following is wrong, maybe.
4602                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
4603                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4604                  !!!next-token;                  !!!next-token;
4605                  redo B;                  redo B;
4606                }                }
4607                                
4608    ## TODO: Followings are removed from the latest spec.
4609                ## generate implied end tags                ## generate implied end tags
4610                if ({                while ({
4611                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4612                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4613                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t224');
4614                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4615                }                }
4616    
4617                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4618                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
4619    ## ISSUE: Can this case be reached?
4620                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4621                  } else {
4622                    !!!cp ('t226');
4623                }                }
4624    
4625                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4626                  pop @{$open_tables};
4627    
4628                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4629    
4630                ## reprocess                ## reprocess
4631                redo B;                redo B;
4632              } else {          } elsif ($token->{tag_name} eq 'style') {
4633                #            if (not $open_tables->[-1]->[1]) { # tainted
4634              }              !!!cp ('t227.8');
4635            } elsif ($token->{type} eq 'end tag') {              ## NOTE: This is a "as if in head" code clone.
4636              if ({              $parse_rcdata->(CDATA_CONTENT_MODEL);
4637                   tbody => 1, tfoot => 1, thead => 1,              redo B;
4638                  }->{$token->{tag_name}}) {            } else {
4639                ## have an element in table scope              !!!cp ('t227.7');
4640                my $i;              #
4641                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
4642                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'script') {
4643                  if ($node->[1] eq $token->{tag_name}) {            if (not $open_tables->[-1]->[1]) { # tainted
4644                    $i = $_;              !!!cp ('t227.6');
4645                    last INSCOPE;              ## NOTE: This is a "as if in head" code clone.
4646                  } elsif ({              $script_start_tag->();
4647                            table => 1, html => 1,              redo B;
4648                           }->{$node->[1]}) {            } else {
4649                    last INSCOPE;              !!!cp ('t227.5');
4650                  }              #
4651                } # INSCOPE            }
4652                unless (defined $i) {          } elsif ($token->{tag_name} eq 'input') {
4653                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4654                  ## Ignore the token              if ($token->{attributes}->{type}) { ## TODO: case
4655                  !!!next-token;                my $type = lc $token->{attributes}->{type}->{value};
4656                  redo B;                if ($type eq 'hidden') {
4657                }                  !!!cp ('t227.3');
4658                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4659    
4660                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4661    
4662                    ## TODO: form element pointer
4663    
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4664                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
4665    
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
4666                  !!!next-token;                  !!!next-token;
4667                  redo B;                  redo B;
4668                  } else {
4669                    !!!cp ('t227.2');
4670                    #
4671                }                }
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
4672              } else {              } else {
4673                  !!!cp ('t227.1');
4674                #                #
4675              }              }
4676            } else {            } else {
4677                !!!cp ('t227.4');
4678              #              #
4679            }            }
4680                      } else {
4681            ## As if in table            !!!cp ('t227');
4682            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4683            $in_body->($insert_to_foster);          }
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
4684    
4685              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4686    
4687              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
4688              ## ISSUE: Spec says that "whenever a node would be inserted          #
4689              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
4690              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
4691              $reconstruct_active_formatting_elements->($insert_to_foster);                  $self->{insertion_mode} == IN_ROW_IM) {
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
4692                ## have an element in table scope                ## have an element in table scope
4693                my $i;                my $i;
4694                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4695                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4696                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4697                      !!!cp ('t228');
4698                    $i = $_;                    $i = $_;
4699                    last INSCOPE;                    last INSCOPE;
4700                  } elsif ({                  } elsif ({
4701                            table => 1, html => 1,                            table => 1, html => 1,
4702                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4703                      !!!cp ('t229');
4704                    last INSCOPE;                    last INSCOPE;
4705                  }                  }
4706                } # INSCOPE                } # INSCOPE
4707                unless (defined $i) {                unless (defined $i) {
4708                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
4709                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4710                  ## Ignore the token                  ## Ignore the token
4711                  !!!next-token;                  !!!next-token;
4712                  redo B;                  redo B;
4713                  } else {
4714                    !!!cp ('t232');
4715                }                }
4716    
4717                ## Clear back to table row context                ## Clear back to table row context
4718                while (not {                while (not {
4719                  tr => 1, html => 1,                  tr => 1, html => 1,
4720                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4721                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4722    ## ISSUE: Can this state be reached?
4723                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4724                }                }
4725    
4726                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
4727                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4728                ## reprocess                !!!next-token;
4729                redo B;                redo B;
4730              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4731                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
4732                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
4733                    ## have an element in table scope
4734                ## As if </table>                  my $i;
4735                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4736                my $i;                    my $node = $self->{open_elements}->[$_];
4737                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'tr') {
4738                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
4739                  if ($node->[1] eq 'table') {                      $i = $_;
4740                    $i = $_;                      last INSCOPE;
4741                    last INSCOPE;                    } elsif ({
4742                  } elsif ({                              table => 1, html => 1,
4743                            table => 1, html => 1,                             }->{$node->[1]}) {
4744                           }->{$node->[1]}) {                      !!!cp ('t234');
4745                    last INSCOPE;                      last INSCOPE;
4746                      }
4747                    } # INSCOPE
4748                    unless (defined $i) {
4749                      !!!cp ('t235');
4750    ## TODO: The following is wrong.
4751                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4752                      ## Ignore the token
4753                      !!!next-token;
4754                      redo B;
4755                  }                  }
4756                } # INSCOPE                  
4757                unless (defined $i) {                  ## Clear back to table row context
4758                  !!!parse-error (type => 'unmatched end tag:table');                  while (not {
4759                  ## Ignore tokens </table><table>                    tr => 1, html => 1,
4760                  !!!next-token;                  }->{$self->{open_elements}->[-1]->[1]}) {
4761                  redo B;                    !!!cp ('t236');
4762                }  ## ISSUE: Can this state be reached?
4763                                    pop @{$self->{open_elements}};
4764                ## generate implied end tags                  }
4765                if ({                  
4766                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}}; # tr
4767                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4768                    }->{$self->{open_elements}->[-1]->[1]}) {                  ## reprocess in the "in table body" insertion mode...
4769                  !!!back-token; # <table>                }
4770                  $token = {type => 'end tag', tag_name => 'table'};  
4771                  !!!back-token;                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4772                  $token = {type => 'end tag',                  ## have an element in table scope
4773                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  my $i;
4774                  redo B;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4775                }                    my $node = $self->{open_elements}->[$_];
4776                      if ({
4777                if ($self->{open_elements}->[-1]->[1] ne 'table') {                         tbody => 1, thead => 1, tfoot => 1,
4778                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        }->{$node->[1]}) {
4779                        !!!cp ('t237');
4780                        $i = $_;
4781                        last INSCOPE;
4782                      } elsif ({
4783                                table => 1, html => 1,
4784                               }->{$node->[1]}) {
4785                        !!!cp ('t238');
4786                        last INSCOPE;
4787                      }
4788                    } # INSCOPE
4789                    unless (defined $i) {
4790                      !!!cp ('t239');
4791                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4792                      ## Ignore the token
4793                      !!!next-token;
4794                      redo B;
4795                    }
4796                    
4797                    ## Clear back to table body context
4798                    while (not {
4799                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4800                    }->{$self->{open_elements}->[-1]->[1]}) {
4801                      !!!cp ('t240');
4802                      pop @{$self->{open_elements}};
4803                    }
4804                    
4805                    ## As if <{current node}>
4806                    ## have an element in table scope
4807                    ## true by definition
4808                    
4809                    ## Clear back to table body context
4810                    ## nop by definition
4811                    
4812                    pop @{$self->{open_elements}};
4813                    $self->{insertion_mode} = IN_TABLE_IM;
4814                    ## reprocess in the "in table" insertion mode...
4815                }                }
4816    
4817                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
4818                  ## When you edit the code fragment below, please ensure that
4819                  ## the code for <table> in the "in table" insertion mode
4820                  ## is synced with it.
4821    
4822                $self->_reset_insertion_mode;                ## have a table element in table scope
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
4823                my $i;                my $i;
4824                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4825                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4826                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4827                      !!!cp ('t241');
4828                    $i = $_;                    $i = $_;
4829                    last INSCOPE;                    last INSCOPE;
4830                  } elsif ({                  } elsif ({
4831                            table => 1, html => 1,                            table => 1, html => 1,
4832                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4833                      !!!cp ('t242');
4834                    last INSCOPE;                    last INSCOPE;
4835                  }                  }
4836                } # INSCOPE                } # INSCOPE
4837                unless (defined $i) {                unless (defined $i) {
4838                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
4839                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4840                  ## Ignore the token                  ## Ignore the token
4841                  !!!next-token;                  !!!next-token;
4842                  redo B;                  redo B;
4843                }                }
4844                    
4845                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
4846                while (not {                pop @{$open_tables};
4847                  tr => 1, html => 1,                
4848                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
4849                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
4850                !!!next-token;                !!!next-token;
4851                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
4852              } elsif ({              } elsif ({
4853                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4854                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4855                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
4856                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
4857                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4858                  my $node = $self->{open_elements}->[$_];                  my $i;
4859                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4860                    $i = $_;                    my $node = $self->{open_elements}->[$_];
4861                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
4862                  } elsif ({                      !!!cp ('t247');
4863                            table => 1, html => 1,                      $i = $_;
4864                           }->{$node->[1]}) {                      last INSCOPE;
4865                    last INSCOPE;                    } elsif ({
4866                                table => 1, html => 1,
4867                               }->{$node->[1]}) {
4868                        !!!cp ('t248');
4869                        last INSCOPE;
4870                      }
4871                    } # INSCOPE
4872                      unless (defined $i) {
4873                        !!!cp ('t249');
4874                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4875                        ## Ignore the token
4876                        !!!next-token;
4877                        redo B;
4878                      }
4879                    
4880                    ## As if </tr>
4881                    ## have an element in table scope
4882                    my $i;
4883                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4884                      my $node = $self->{open_elements}->[$_];
4885                      if ($node->[1] eq 'tr') {
4886                        !!!cp ('t250');
4887                        $i = $_;
4888                        last INSCOPE;
4889                      } elsif ({
4890                                table => 1, html => 1,
4891                               }->{$node->[1]}) {
4892                        !!!cp ('t251');
4893                        last INSCOPE;
4894                      }
4895                    } # INSCOPE
4896                      unless (defined $i) {
4897                        !!!cp ('t252');
4898                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
4899                        ## Ignore the token
4900                        !!!next-token;
4901                        redo B;
4902                      }
4903                    
4904                    ## Clear back to table row context
4905                    while (not {
4906                      tr => 1, html => 1,
4907                    }->{$self->{open_elements}->[-1]->[1]}) {
4908                      !!!cp ('t253');
4909    ## ISSUE: Can this case be reached?
4910                      pop @{$self->{open_elements}};
4911                  }                  }
4912                } # INSCOPE                  
4913                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4914                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4915                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4916                }                }
4917    
               ## As if </tr>  
4918                ## have an element in table scope                ## have an element in table scope
4919                my $i;                my $i;
4920                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4921                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4922                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4923                      !!!cp ('t254');
4924                    $i = $_;                    $i = $_;
4925                    last INSCOPE;                    last INSCOPE;
4926                  } elsif ({                  } elsif ({
4927                            table => 1, html => 1,                            table => 1, html => 1,
4928                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4929                      !!!cp ('t255');
4930                    last INSCOPE;                    last INSCOPE;
4931                  }                  }
4932                } # INSCOPE                } # INSCOPE
4933                unless (defined $i) {                unless (defined $i) {
4934                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
4935                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4936                  ## Ignore the token                  ## Ignore the token
4937                  !!!next-token;                  !!!next-token;
4938                  redo B;                  redo B;
4939                }                }
4940    
4941                ## Clear back to table row context                ## Clear back to table body context
4942                while (not {                while (not {
4943                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4944                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4945                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4946    ## ISSUE: Can this case be reached?
4947                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4948                }                }
4949    
4950                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4951                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4952                ## reprocess                !!!next-token;
4953                redo B;                redo B;
4954              } elsif ({              } elsif ({
4955                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4956                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4957                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4958                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4959                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4960                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t258');
4961                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4962                ## Ignore the token                ## Ignore the token
4963                !!!next-token;                !!!next-token;
4964                redo B;                redo B;
4965              } else {          } else {
4966                #            !!!cp ('t259');
4967              }            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           } else {  
             #  
           }  
4968    
4969            ## As if in table            $insert = $insert_to_foster;
4970            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4971            $in_body->($insert_to_foster);          }
4972            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4973          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4974            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
4975              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
4976              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
4977                          #
4978              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4979              !!!cp ('t259.2');
4980              #
4981            }
4982    
4983              !!!next-token;          ## Stop parsing
4984              redo B;          last B;
4985            } elsif ($token->{type} eq 'comment') {        } else {
4986              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
4987              my $comment = $self->{document}->create_comment ($token->{data});        }
4988              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4989              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
4990              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4991            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4992              if ({                unless (length $token->{data}) {
4993                   caption => 1, col => 1, colgroup => 1,                  !!!cp ('t260');
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
4994                  !!!next-token;                  !!!next-token;
4995                  redo B;                  redo B;
4996                }                }
4997                }
4998                ## Close the cell              
4999                !!!back-token; # <?>              !!!cp ('t261');
5000                $token = {type => 'end tag', tag_name => $tn};              #
5001              } elsif ($token->{type} == START_TAG_TOKEN) {
5002                if ($token->{tag_name} eq 'col') {
5003                  !!!cp ('t262');
5004                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5005                  pop @{$self->{open_elements}};
5006                  !!!next-token;
5007                redo B;                redo B;
5008              } else {              } else {
5009                  !!!cp ('t263');
5010                #                #
5011              }              }
5012            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5013              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5014                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
5015                my $i;                  !!!cp ('t264');
5016                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5017                  ## Ignore the token                  ## Ignore the token
5018                  !!!next-token;                  !!!next-token;
5019                  redo B;                  redo B;
5020                  } else {
5021                    !!!cp ('t265');
5022                    pop @{$self->{open_elements}}; # colgroup
5023                    $self->{insertion_mode} = IN_TABLE_IM;
5024                    !!!next-token;
5025                    redo B;            
5026                }                }
5027                              } elsif ($token->{tag_name} eq 'col') {
5028                ## generate implied end tags                !!!cp ('t266');
5029                if ({                !!!parse-error (type => 'unmatched end tag:col', token => $token);
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5030                ## Ignore the token                ## Ignore the token
5031                !!!next-token;                !!!next-token;
5032                redo B;                redo B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
5033              } else {              } else {
5034                #                !!!cp ('t267');
5035                  #
5036              }              }
5037            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5038              #          if ($self->{open_elements}->[-1]->[1] eq 'html' or
5039            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5040                        !!!cp ('t270.2');
5041            $in_body->($insert_to_current);            ## Stop parsing.
5042              last B;
5043            } else {
5044              ## NOTE: As if </colgroup>.
5045              !!!cp ('t270.1');
5046              pop @{$self->{open_elements}}; # colgroup
5047              $self->{insertion_mode} = IN_TABLE_IM;
5048              ## Reprocess.
5049            redo B;            redo B;
5050          } elsif ($self->{insertion_mode} eq 'in select') {          }
5051            if ($token->{type} eq 'character') {        } else {
5052              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          die "$0: $token->{type}: Unknown token type";
5053          }
5054    
5055              ## As if </colgroup>
5056              if ($self->{open_elements}->[-1]->[1] eq 'html') {
5057                !!!cp ('t269');
5058    ## TODO: Wrong error type?
5059                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5060                ## Ignore the token
5061              !!!next-token;              !!!next-token;
5062              redo B;              redo B;
5063            } elsif ($token->{type} eq 'comment') {            } else {
5064              my $comment = $self->{document}->create_comment ($token->{data});              !!!cp ('t270');
5065              $self->{open_elements}->[-1]->[0]->append_child ($comment);              pop @{$self->{open_elements}}; # colgroup
5066              !!!next-token;              $self->{insertion_mode} = IN_TABLE_IM;
5067                ## reprocess
5068              redo B;              redo B;
5069            } elsif ($token->{type} eq 'start tag') {            }
5070        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5071          if ($token->{type} == CHARACTER_TOKEN) {
5072            !!!cp ('t271');
5073            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5074            !!!next-token;
5075            redo B;
5076          } elsif ($token->{type} == START_TAG_TOKEN) {
5077              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
5078                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5079                    !!!cp ('t272');
5080                  ## As if </option>                  ## As if </option>
5081                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5082                  } else {
5083                    !!!cp ('t273');
5084                }                }
5085    
5086                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5087                !!!next-token;                !!!next-token;
5088                redo B;                redo B;
5089              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
5090                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5091                    !!!cp ('t274');
5092                  ## As if </option>                  ## As if </option>
5093                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5094                  } else {
5095                    !!!cp ('t275');
5096                }                }
5097    
5098                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5099                    !!!cp ('t276');
5100                  ## As if </optgroup>                  ## As if </optgroup>
5101                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5102                  } else {
5103                    !!!cp ('t277');
5104                }                }
5105    
5106                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5107                !!!next-token;                !!!next-token;
5108                redo B;                redo B;
5109              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5110                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5111                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5112                      {
5113                       caption => 1, table => 1,
5114                       tbody => 1, tfoot => 1, thead => 1,
5115                       tr => 1, td => 1, th => 1,
5116                      }->{$token->{tag_name}})) {
5117              ## TODO: The type below is not good - <select> is replaced by </select>
5118              !!!parse-error (type => 'not closed:select', token => $token);
5119              ## NOTE: As if the token were </select> (<select> case) or
5120              ## as if there were </select> (otherwise).
5121                ## have an element in table scope                ## have an element in table scope
5122                my $i;                my $i;
5123                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5124                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5125                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5126                      !!!cp ('t278');
5127                    $i = $_;                    $i = $_;
5128                    last INSCOPE;                    last INSCOPE;
5129                  } elsif ({                  } elsif ({
5130                            table => 1, html => 1,                            table => 1, html => 1,
5131                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5132                      !!!cp ('t279');
5133                    last INSCOPE;                    last INSCOPE;
5134                  }                  }
5135                } # INSCOPE                } # INSCOPE
5136                unless (defined $i) {                unless (defined $i) {
5137                  !!!parse-error (type => 'unmatched end tag:select');                  !!!cp ('t280');
5138                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5139                  ## Ignore the token                  ## Ignore the token
5140                  !!!next-token;                  !!!next-token;
5141                  redo B;                  redo B;
5142                }                }
5143                                
5144                  !!!cp ('t281');
5145                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5146    
5147                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5148    
5149                !!!next-token;            if ($token->{tag_name} eq 'select') {
5150                redo B;              !!!cp ('t281.2');
5151              } else {              !!!next-token;
5152                #              redo B;
5153              }            } else {
5154            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t281.1');
5155                ## Reprocess the token.
5156                redo B;
5157              }
5158            } else {
5159              !!!cp ('t282');
5160              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5161              ## Ignore the token
5162              !!!next-token;
5163              redo B;
5164            }
5165          } elsif ($token->{type} == END_TAG_TOKEN) {
5166              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
5167                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
5168                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
5169                    !!!cp ('t283');
5170                  ## As if </option>                  ## As if </option>
5171                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
5172                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5173                    !!!cp ('t284');
5174                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5175                } else {                } else {
5176                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t285');
5177                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5178                  ## Ignore the token                  ## Ignore the token
5179                }                }
5180                !!!next-token;                !!!next-token;
5181                redo B;                redo B;
5182              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
5183                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5184                    !!!cp ('t286');
5185                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5186                } else {                } else {
5187                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t287');
5188                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5189                  ## Ignore the token                  ## Ignore the token
5190                }                }
5191                !!!next-token;                !!!next-token;
# Line 4607  sub _tree_construction_main ($) { Line 5196  sub _tree_construction_main ($) {
5196                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5197                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5198                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5199                      !!!cp ('t288');
5200                    $i = $_;                    $i = $_;
5201                    last INSCOPE;                    last INSCOPE;
5202                  } elsif ({                  } elsif ({
5203                            table => 1, html => 1,                            table => 1, html => 1,
5204                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5205                      !!!cp ('t289');
5206                    last INSCOPE;                    last INSCOPE;
5207                  }                  }
5208                } # INSCOPE                } # INSCOPE
5209                unless (defined $i) {                unless (defined $i) {
5210                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t290');
5211                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5212                  ## Ignore the token                  ## Ignore the token
5213                  !!!next-token;                  !!!next-token;
5214                  redo B;                  redo B;
5215                }                }
5216                                
5217                  !!!cp ('t291');
5218                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5219    
5220                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5221    
5222                !!!next-token;                !!!next-token;
5223                redo B;                redo B;
5224              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5225                        caption => 1, table => 1, tbody => 1,                   {
5226                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5227                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5228                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                   }->{$token->{tag_name}}) {
5229    ## TODO: The following is wrong?
5230                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5231                                
5232                ## have an element in table scope                ## have an element in table scope
5233                my $i;                my $i;
5234                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5235                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5236                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5237                      !!!cp ('t292');
5238                    $i = $_;                    $i = $_;
5239                    last INSCOPE;                    last INSCOPE;
5240                  } elsif ({                  } elsif ({
5241                            table => 1, html => 1,                            table => 1, html => 1,
5242                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5243                      !!!cp ('t293');
5244                    last INSCOPE;                    last INSCOPE;
5245                  }                  }
5246                } # INSCOPE                } # INSCOPE
5247                unless (defined $i) {                unless (defined $i) {
5248                    !!!cp ('t294');
5249                  ## Ignore the token                  ## Ignore the token
5250                  !!!next-token;                  !!!next-token;
5251                  redo B;                  redo B;
# Line 4659  sub _tree_construction_main ($) { Line 5257  sub _tree_construction_main ($) {
5257                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5258                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5259                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5260                      !!!cp ('t295');
5261                    $i = $_;                    $i = $_;
5262                    last INSCOPE;                    last INSCOPE;
5263                  } elsif ({                  } elsif ({
5264                            table => 1, html => 1,                            table => 1, html => 1,
5265                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5266    ## ISSUE: Can this state be reached?
5267                      !!!cp ('t296');
5268                    last INSCOPE;                    last INSCOPE;
5269                  }                  }
5270                } # INSCOPE                } # INSCOPE
5271                unless (defined $i) {                unless (defined $i) {
5272                  !!!parse-error (type => 'unmatched end tag:select');                  !!!cp ('t297');
5273    ## TODO: The following error type is correct?
5274                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5275                  ## Ignore the </select> token                  ## Ignore the </select> token
5276                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5277                  redo B;                  redo B;
5278                }                }
5279                                
5280                  !!!cp ('t298');
5281                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5282    
5283                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5284    
5285                ## reprocess                ## reprocess
5286                redo B;                redo B;
5287              } else {          } else {
5288                #            !!!cp ('t299');
5289              }            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
5290            ## Ignore the token            ## Ignore the token
5291            !!!next-token;            !!!next-token;
5292            redo B;            redo B;
5293          } elsif ($self->{insertion_mode} eq 'after body') {          }
5294            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5295              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5296                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
5297                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
5298              !!!parse-error (type => 'in body:#eof', token => $token);
5299            } else {
5300              !!!cp ('t299.2');
5301            }
5302    
5303            ## Stop parsing.
5304            last B;
5305          } else {
5306            die "$0: $token->{type}: Unknown token type";
5307          }
5308        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5309          if ($token->{type} == CHARACTER_TOKEN) {
5310            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5311              my $data = $1;
5312              ## As if in body
5313              $reconstruct_active_formatting_elements->($insert_to_current);
5314                                
5315                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5316              
5317              unless (length $token->{data}) {
5318                !!!cp ('t300');
5319                !!!next-token;
5320                redo B;
5321              }
5322            }
5323            
5324            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5325              !!!cp ('t301');
5326              !!!parse-error (type => 'after html:#character', token => $token);
5327    
5328                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
5329                  !!!next-token;          } else {
5330                  redo B;            !!!cp ('t302');
5331                }          }
5332              }          
5333                        ## "after body" insertion mode
5334              #          !!!parse-error (type => 'after body:#character', token => $token);
5335              !!!parse-error (type => 'after body:#'.$token->{type});  
5336            } elsif ($token->{type} eq 'comment') {          $self->{insertion_mode} = IN_BODY_IM;
5337              my $comment = $self->{document}->create_comment ($token->{data});          ## reprocess
5338              $self->{open_elements}->[0]->[0]->append_child ($comment);          redo B;
5339          } elsif ($token->{type} == START_TAG_TOKEN) {
5340            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5341              !!!cp ('t303');
5342              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5343              
5344              ## Reprocess in the "after body" insertion mode.
5345            } else {
5346              !!!cp ('t304');
5347            }
5348    
5349            ## "after body" insertion mode
5350            !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5351    
5352            $self->{insertion_mode} = IN_BODY_IM;
5353            ## reprocess
5354            redo B;
5355          } elsif ($token->{type} == END_TAG_TOKEN) {
5356            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5357              !!!cp ('t305');
5358              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5359              
5360              $self->{insertion_mode} = AFTER_BODY_IM;
5361              ## Reprocess in the "after body" insertion mode.
5362            } else {
5363              !!!cp ('t306');
5364            }
5365    
5366            ## "after body" insertion mode
5367            if ($token->{tag_name} eq 'html') {
5368              if (defined $self->{inner_html_node}) {
5369                !!!cp ('t307');
5370                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5371                ## Ignore the token
5372              !!!next-token;              !!!next-token;
5373              redo B;              redo B;
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
5374            } else {            } else {
5375              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t308');
5376                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5377                !!!next-token;
5378                redo B;
5379            }            }
5380            } else {
5381              !!!cp ('t309');
5382              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5383    
5384            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
5385            ## reprocess            ## reprocess
5386            redo B;            redo B;
5387          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
5388            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5389              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t309.2');
5390                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          ## Stop parsing
5391            last B;
5392                unless (length $token->{data}) {        } else {
5393                  !!!next-token;          die "$0: $token->{type}: Unknown token type";
5394                  redo B;        }
5395                }      } elsif ($self->{insertion_mode} & FRAME_IMS) {
5396              }        if ($token->{type} == CHARACTER_TOKEN) {
5397            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5398              #            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5399            } elsif ($token->{type} eq 'comment') {            
5400              my $comment = $self->{document}->create_comment ($token->{data});            unless (length $token->{data}) {
5401              $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!cp ('t310');
5402              !!!next-token;              !!!next-token;
5403              redo B;              redo B;
5404            } elsif ($token->{type} eq 'start tag') {            }
5405              if ($token->{tag_name} eq 'frameset') {          }
5406                !!!insert-element ($token->{tag_name}, $token->{attributes});          
5407                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5408                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5409              } elsif ($token->{tag_name} eq 'frame') {              !!!cp ('t311');
5410                !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!parse-error (type => 'in frameset:#character', token => $token);
5411                pop @{$self->{open_elements}};            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5412                !!!next-token;              !!!cp ('t312');
5413                redo B;              !!!parse-error (type => 'after frameset:#character', token => $token);
5414              } elsif ($token->{tag_name} eq 'noframes') {            } else { # "after html frameset"
5415                $in_body->($insert_to_current);              !!!cp ('t313');
5416                redo B;              !!!parse-error (type => 'after html:#character', token => $token);
5417              } else {  
5418                #              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5419              }              ## Reprocess in the "after frameset" insertion mode.
5420            } elsif ($token->{type} eq 'end tag') {              !!!parse-error (type => 'after frameset:#character', token => $token);
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
5421            }            }
5422                        
5423            if (defined $token->{tag_name}) {            ## Ignore the token.
5424              !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if (length $token->{data}) {
5425                !!!cp ('t314');
5426                ## reprocess the rest of characters
5427            } else {            } else {
5428              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
5429                !!!next-token;
5430              }
5431              redo B;
5432            }
5433            
5434            die qq[$0: Character "$token->{data}"];
5435          } elsif ($token->{type} == START_TAG_TOKEN) {
5436            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5437              !!!cp ('t316');
5438              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5439    
5440              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5441              ## Process in the "after frameset" insertion mode.
5442            } else {
5443              !!!cp ('t317');
5444            }
5445    
5446            if ($token->{tag_name} eq 'frameset' and
5447                $self->{insertion_mode} == IN_FRAMESET_IM) {
5448              !!!cp ('t318');
5449              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5450              !!!next-token;
5451              redo B;
5452            } elsif ($token->{tag_name} eq 'frame' and
5453                     $self->{insertion_mode} == IN_FRAMESET_IM) {
5454              !!!cp ('t319');
5455              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5456              pop @{$self->{open_elements}};
5457              !!!next-token;
5458              redo B;
5459            } elsif ($token->{tag_name} eq 'noframes') {
5460              !!!cp ('t320');
5461              ## NOTE: As if in body.
5462              $parse_rcdata->(CDATA_CONTENT_MODEL);
5463              redo B;
5464            } else {
5465              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5466                !!!cp ('t321');
5467                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5468              } else {
5469                !!!cp ('t322');
5470                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5471            }            }
5472            ## Ignore the token            ## Ignore the token
5473            !!!next-token;            !!!next-token;
5474            redo B;            redo B;
5475          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
5476            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5477              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5478                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t323');
5479              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5480    
5481              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5482              ## Process in the "after frameset" insertion mode.
5483            } else {
5484              !!!cp ('t324');
5485            }
5486    
5487            if ($token->{tag_name} eq 'frameset' and
5488                $self->{insertion_mode} == IN_FRAMESET_IM) {
5489              if ($self->{open_elements}->[-1]->[1] eq 'html' and
5490                  @{$self->{open_elements}} == 1) {
5491                !!!cp ('t325');
5492                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5493                ## Ignore the token
5494                !!!next-token;
5495              } else {
5496                !!!cp ('t326');
5497                pop @{$self->{open_elements}};
5498                !!!next-token;
5499              }
5500    
5501                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
5502                  !!!next-token;                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5503                  redo B;              !!!cp ('t327');
5504                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5505              } else {
5506                !!!cp ('t328');
5507              }
5508              redo B;
5509            } elsif ($token->{tag_name} eq 'html' and
5510                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5511              !!!cp ('t329');
5512              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5513              !!!next-token;
5514              redo B;
5515            } else {
5516              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5517                !!!cp ('t330');
5518                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5519              } else {
5520                !!!cp ('t331');
5521                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5522              }
5523              ## Ignore the token
5524              !!!next-token;
5525              redo B;
5526            }
5527          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5528            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5529                    @{$self->{open_elements}} == 1) { # redundant, maybe
5530              !!!cp ('t331.1');
5531              !!!parse-error (type => 'in body:#eof', token => $token);
5532            } else {
5533              !!!cp ('t331.2');
5534            }
5535            
5536            ## Stop parsing
5537            last B;
5538          } else {
5539            die "$0: $token->{type}: Unknown token type";
5540          }
5541    
5542          ## ISSUE: An issue in spec here
5543        } else {
5544          die "$0: $self->{insertion_mode}: Unknown insertion mode";
5545        }
5546    
5547        ## "in body" insertion mode
5548        if ($token->{type} == START_TAG_TOKEN) {
5549          if ($token->{tag_name} eq 'script') {
5550            !!!cp ('t332');
5551            ## NOTE: This is an "as if in head" code clone
5552            $script_start_tag->();
5553            redo B;
5554          } elsif ($token->{tag_name} eq 'style') {
5555            !!!cp ('t333');
5556            ## NOTE: This is an "as if in head" code clone
5557            $parse_rcdata->(CDATA_CONTENT_MODEL);
5558            redo B;
5559          } elsif ({
5560                    base => 1, link => 1,
5561                   }->{$token->{tag_name}}) {
5562            !!!cp ('t334');
5563            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5564            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5565            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5566            !!!next-token;
5567            redo B;
5568          } elsif ($token->{tag_name} eq 'meta') {
5569            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5570            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5571            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5572    
5573            unless ($self->{confident}) {
5574              if ($token->{attributes}->{charset}) { ## TODO: And if supported
5575                !!!cp ('t335');
5576                $self->{change_encoding}
5577                    ->($self, $token->{attributes}->{charset}->{value}, $token);
5578                
5579                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5580                    ->set_user_data (manakai_has_reference =>
5581                                         $token->{attributes}->{charset}
5582                                             ->{has_reference});
5583              } elsif ($token->{attributes}->{content}) {
5584                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5585                if ($token->{attributes}->{content}->{value}
5586                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5587                        [\x09-\x0D\x20]*=
5588                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5589                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5590                  !!!cp ('t336');
5591                  $self->{change_encoding}
5592                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5593                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5594                      ->set_user_data (manakai_has_reference =>
5595                                           $token->{attributes}->{content}
5596                                                 ->{has_reference});
5597              }              }
5598              }
5599            } else {
5600              if ($token->{attributes}->{charset}) {
5601                !!!cp ('t337');
5602                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5603                    ->set_user_data (manakai_has_reference =>
5604                                         $token->{attributes}->{charset}
5605                                             ->{has_reference});
5606              }
5607              if ($token->{attributes}->{content}) {
5608                !!!cp ('t338');
5609                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5610                    ->set_user_data (manakai_has_reference =>
5611                                         $token->{attributes}->{content}
5612                                             ->{has_reference});
5613              }
5614            }
5615    
5616              #          !!!next-token;
5617            } elsif ($token->{type} eq 'comment') {          redo B;
5618              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{tag_name} eq 'title') {
5619              $self->{open_elements}->[-1]->[0]->append_child ($comment);          !!!cp ('t341');
5620              !!!next-token;          ## NOTE: This is an "as if in head" code clone
5621              redo B;          $parse_rcdata->(RCDATA_CONTENT_MODEL);
5622            } elsif ($token->{type} eq 'start tag') {          redo B;
5623              if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'body') {
5624                $in_body->($insert_to_current);          !!!parse-error (type => 'in body:body', token => $token);
5625                redo B;                
5626              } else {          if (@{$self->{open_elements}} == 1 or
5627                #              $self->{open_elements}->[1]->[1] ne 'body') {
5628              !!!cp ('t342');
5629              ## Ignore the token
5630            } else {
5631              my $body_el = $self->{open_elements}->[1]->[0];
5632              for my $attr_name (keys %{$token->{attributes}}) {
5633                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5634                  !!!cp ('t343');
5635                  $body_el->set_attribute_ns
5636                    (undef, [undef, $attr_name],
5637                     $token->{attributes}->{$attr_name}->{value});
5638              }              }
5639            } elsif ($token->{type} eq 'end tag') {            }
5640              if ($token->{tag_name} eq 'html') {          }
5641                $phase = 'trailing end';          !!!next-token;
5642            redo B;
5643          } elsif ({
5644                    address => 1, blockquote => 1, center => 1, dir => 1,
5645                    div => 1, dl => 1, fieldset => 1,
5646                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5647                    menu => 1, ol => 1, p => 1, ul => 1,
5648                    pre => 1, listing => 1,
5649                    form => 1,
5650                    table => 1,
5651                    hr => 1,
5652                   }->{$token->{tag_name}}) {
5653            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5654              !!!cp ('t350');
5655              !!!parse-error (type => 'in form:form', token => $token);
5656              ## Ignore the token
5657              !!!next-token;
5658              redo B;
5659            }
5660    
5661            ## has a p element in scope
5662            INSCOPE: for (reverse @{$self->{open_elements}}) {
5663              if ($_->[1] eq 'p') {
5664                !!!cp ('t344');
5665                !!!back-token;
5666                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5667                          line => $token->{line}, column => $token->{column}};
5668                redo B;
5669              } elsif ({
5670                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5671                        button => 1, marquee => 1, object => 1, html => 1,
5672                       }->{$_->[1]}) {
5673                !!!cp ('t345');
5674                last INSCOPE;
5675              }
5676            } # INSCOPE
5677              
5678            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5679            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5680              !!!next-token;
5681              if ($token->{type} == CHARACTER_TOKEN) {
5682                $token->{data} =~ s/^\x0A//;
5683                unless (length $token->{data}) {
5684                  !!!cp ('t346');
5685                !!!next-token;                !!!next-token;
               redo B;  
5686              } else {              } else {
5687                #                !!!cp ('t349');
5688              }              }
5689            } else {            } else {
5690              #              !!!cp ('t348');
5691              }
5692            } elsif ($token->{tag_name} eq 'form') {
5693              !!!cp ('t347.1');
5694              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5695    
5696              !!!next-token;
5697            } elsif ($token->{tag_name} eq 'table') {
5698              !!!cp ('t382');
5699              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5700              
5701              $self->{insertion_mode} = IN_TABLE_IM;
5702    
5703              !!!next-token;
5704            } elsif ($token->{tag_name} eq 'hr') {
5705              !!!cp ('t386');
5706              pop @{$self->{open_elements}};
5707            
5708              !!!next-token;
5709            } else {
5710              !!!cp ('t347');
5711              !!!next-token;
5712            }
5713            redo B;
5714          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5715            ## has a p element in scope
5716            INSCOPE: for (reverse @{$self->{open_elements}}) {
5717              if ($_->[1] eq 'p') {
5718                !!!cp ('t353');
5719                !!!back-token;
5720                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5721                          line => $token->{line}, column => $token->{column}};
5722                redo B;
5723              } elsif ({
5724                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5725                        button => 1, marquee => 1, object => 1, html => 1,
5726                       }->{$_->[1]}) {
5727                !!!cp ('t354');
5728                last INSCOPE;
5729            }            }
5730            } # INSCOPE
5731                        
5732            if (defined $token->{tag_name}) {          ## Step 1
5733              !!!parse-error (type => 'after frameset:'.$token->{tag_name});          my $i = -1;
5734            my $node = $self->{open_elements}->[$i];
5735            my $li_or_dtdd = {li => {li => 1},
5736                              dt => {dt => 1, dd => 1},
5737                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5738            LI: {
5739              ## Step 2
5740              if ($li_or_dtdd->{$node->[1]}) {
5741                if ($i != -1) {
5742                  !!!cp ('t355');
5743                  !!!parse-error (type => 'end tag missing:'.
5744                                  $self->{open_elements}->[-1]->[1], token => $token);
5745                } else {
5746                  !!!cp ('t356');
5747                }
5748                splice @{$self->{open_elements}}, $i;
5749                last LI;
5750            } else {            } else {
5751              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
5752              }
5753              
5754              ## Step 3
5755              if (not $formatting_category->{$node->[1]} and
5756                  #not $phrasing_category->{$node->[1]} and
5757                  ($special_category->{$node->[1]} or
5758                   $scoping_category->{$node->[1]}) and
5759                  $node->[1] ne 'address' and $node->[1] ne 'div') {
5760                !!!cp ('t358');
5761                last LI;
5762              }
5763              
5764              !!!cp ('t359');
5765              ## Step 4
5766              $i--;
5767              $node = $self->{open_elements}->[$i];
5768              redo LI;
5769            } # LI
5770              
5771            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5772            !!!next-token;
5773            redo B;
5774          } elsif ($token->{tag_name} eq 'plaintext') {
5775            ## has a p element in scope
5776            INSCOPE: for (reverse @{$self->{open_elements}}) {
5777              if ($_->[1] eq 'p') {
5778                !!!cp ('t367');
5779                !!!back-token;
5780                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5781                          line => $token->{line}, column => $token->{column}};
5782                redo B;
5783              } elsif ({
5784                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5785                        button => 1, marquee => 1, object => 1, html => 1,
5786                       }->{$_->[1]}) {
5787                !!!cp ('t368');
5788                last INSCOPE;
5789              }
5790            } # INSCOPE
5791              
5792            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5793              
5794            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5795              
5796            !!!next-token;
5797            redo B;
5798          } elsif ($token->{tag_name} eq 'a') {
5799            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5800              my $node = $active_formatting_elements->[$i];
5801              if ($node->[1] eq 'a') {
5802                !!!cp ('t371');
5803                !!!parse-error (type => 'in a:a', token => $token);
5804                
5805                !!!back-token;
5806                $token = {type => END_TAG_TOKEN, tag_name => 'a',
5807                          line => $token->{line}, column => $token->{column}};
5808                $formatting_end_tag->($token);
5809                
5810                AFE2: for (reverse 0..$#$active_formatting_elements) {
5811                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5812                    !!!cp ('t372');
5813                    splice @$active_formatting_elements, $_, 1;
5814                    last AFE2;
5815                  }
5816                } # AFE2
5817                OE: for (reverse 0..$#{$self->{open_elements}}) {
5818                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5819                    !!!cp ('t373');
5820                    splice @{$self->{open_elements}}, $_, 1;
5821                    last OE;
5822                  }
5823                } # OE
5824                last AFE;
5825              } elsif ($node->[0] eq '#marker') {
5826                !!!cp ('t374');
5827                last AFE;
5828              }
5829            } # AFE
5830              
5831            $reconstruct_active_formatting_elements->($insert_to_current);
5832    
5833            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5834            push @$active_formatting_elements, $self->{open_elements}->[-1];
5835    
5836            !!!next-token;
5837            redo B;
5838          } elsif ($token->{tag_name} eq 'nobr') {
5839            $reconstruct_active_formatting_elements->($insert_to_current);
5840    
5841            ## has a |nobr| element in scope
5842            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5843              my $node = $self->{open_elements}->[$_];
5844              if ($node->[1] eq 'nobr') {
5845                !!!cp ('t376');
5846                !!!parse-error (type => 'in nobr:nobr', token => $token);
5847                !!!back-token;
5848                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
5849                          line => $token->{line}, column => $token->{column}};
5850                redo B;
5851              } elsif ({
5852                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5853                        button => 1, marquee => 1, object => 1, html => 1,
5854                       }->{$node->[1]}) {
5855                !!!cp ('t377');
5856                last INSCOPE;
5857              }
5858            } # INSCOPE
5859            
5860            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5861            push @$active_formatting_elements, $self->{open_elements}->[-1];
5862            
5863            !!!next-token;
5864            redo B;
5865          } elsif ($token->{tag_name} eq 'button') {
5866            ## has a button element in scope
5867            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5868              my $node = $self->{open_elements}->[$_];
5869              if ($node->[1] eq 'button') {
5870                !!!cp ('t378');
5871                !!!parse-error (type => 'in button:button', token => $token);
5872                !!!back-token;
5873                $token = {type => END_TAG_TOKEN, tag_name => 'button',
5874                          line => $token->{line}, column => $token->{column}};
5875                redo B;
5876              } elsif ({
5877                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5878                        button => 1, marquee => 1, object => 1, html => 1,
5879                       }->{$node->[1]}) {
5880                !!!cp ('t379');
5881                last INSCOPE;
5882            }            }
5883            } # INSCOPE
5884              
5885            $reconstruct_active_formatting_elements->($insert_to_current);
5886              
5887            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5888    
5889            ## TODO: associate with $self->{form_element} if defined
5890    
5891            push @$active_formatting_elements, ['#marker', ''];
5892    
5893            !!!next-token;
5894            redo B;
5895          } elsif ({
5896                    xmp => 1,
5897                    iframe => 1,
5898                    noembed => 1,
5899                    noframes => 1,
5900                    noscript => 0, ## TODO: 1 if scripting is enabled
5901                   }->{$token->{tag_name}}) {
5902            if ($token->{tag_name} eq 'xmp') {
5903              !!!cp ('t381');
5904              $reconstruct_active_formatting_elements->($insert_to_current);
5905            } else {
5906              !!!cp ('t399');
5907            }
5908            ## NOTE: There is an "as if in body" code clone.
5909            $parse_rcdata->(CDATA_CONTENT_MODEL);
5910            redo B;
5911          } elsif ($token->{tag_name} eq 'isindex') {
5912            !!!parse-error (type => 'isindex', token => $token);
5913            
5914            if (defined $self->{form_element}) {
5915              !!!cp ('t389');
5916            ## Ignore the token            ## Ignore the token
5917            !!!next-token;            !!!next-token;
5918            redo B;            redo B;
5919            } else {
5920              my $at = $token->{attributes};
5921              my $form_attrs;
5922              $form_attrs->{action} = $at->{action} if $at->{action};
5923              my $prompt_attr = $at->{prompt};
5924              $at->{name} = {name => 'name', value => 'isindex'};
5925              delete $at->{action};
5926              delete $at->{prompt};
5927              my @tokens = (
5928                            {type => START_TAG_TOKEN, tag_name => 'form',
5929                             attributes => $form_attrs,
5930                             line => $token->{line}, column => $token->{column}},
5931                            {type => START_TAG_TOKEN, tag_name => 'hr',
5932                             line => $token->{line}, column => $token->{column}},
5933                            {type => START_TAG_TOKEN, tag_name => 'p',
5934                             line => $token->{line}, column => $token->{column}},
5935                            {type => START_TAG_TOKEN, tag_name => 'label',
5936                             line => $token->{line}, column => $token->{column}},
5937                           );
5938              if ($prompt_attr) {
5939                !!!cp ('t390');
5940                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
5941                               line => $token->{line}, column => $token->{column}};
5942              } else {
5943                !!!cp ('t391');
5944                push @tokens, {type => CHARACTER_TOKEN,
5945                               data => 'This is a searchable index. Insert your search keywords here: ',
5946                               line => $token->{line}, column => $token->{column}}; # SHOULD
5947                ## TODO: make this configurable
5948              }
5949              push @tokens,
5950                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
5951                             line => $token->{line}, column => $token->{column}},
5952                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5953                            {type => END_TAG_TOKEN, tag_name => 'label',
5954                             line => $token->{line}, column => $token->{column}},
5955                            {type => END_TAG_TOKEN, tag_name => 'p',
5956                             line => $token->{line}, column => $token->{column}},
5957                            {type => START_TAG_TOKEN, tag_name => 'hr',
5958                             line => $token->{line}, column => $token->{column}},
5959                            {type => END_TAG_TOKEN, tag_name => 'form',
5960                             line => $token->{line}, column => $token->{column}};
5961              $token = shift @tokens;
5962              !!!back-token (@tokens);
5963              redo B;
5964            }
5965          } elsif ($token->{tag_name} eq 'textarea') {
5966            my $tag_name = $token->{tag_name};
5967            my $el;
5968            !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);
5969            
5970            ## TODO: $self->{form_element} if defined
5971            $self->{content_model} = RCDATA_CONTENT_MODEL;
5972            delete $self->{escape}; # MUST
5973            
5974            $insert->($el);
5975            
5976            my $text = '';
5977            !!!next-token;
5978            if ($token->{type} == CHARACTER_TOKEN) {
5979              $token->{data} =~ s/^\x0A//;
5980              unless (length $token->{data}) {
5981                !!!cp ('t392');
5982                !!!next-token;
5983              } else {
5984                !!!cp ('t393');
5985              }
5986            } else {
5987              !!!cp ('t394');
5988            }
5989            while ($token->{type} == CHARACTER_TOKEN) {
5990              !!!cp ('t395');
5991              $text .= $token->{data};
5992              !!!next-token;
5993            }
5994            if (length $text) {
5995              !!!cp ('t396');
5996              $el->manakai_append_text ($text);
5997            }
5998            
5999            $self->{content_model} = PCDATA_CONTENT_MODEL;
6000            
6001            if ($token->{type} == END_TAG_TOKEN and
6002                $token->{tag_name} eq $tag_name) {
6003              !!!cp ('t397');
6004              ## Ignore the token
6005            } else {
6006              !!!cp ('t398');
6007              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6008            }
6009            !!!next-token;
6010            redo B;
6011          } elsif ({
6012                    caption => 1, col => 1, colgroup => 1, frame => 1,
6013                    frameset => 1, head => 1, option => 1, optgroup => 1,
6014                    tbody => 1, td => 1, tfoot => 1, th => 1,
6015                    thead => 1, tr => 1,
6016                   }->{$token->{tag_name}}) {
6017            !!!cp ('t401');
6018            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6019            ## Ignore the token
6020            !!!next-token;
6021            redo B;
6022            
6023            ## ISSUE: An issue on HTML5 new elements in the spec.
6024          } else {
6025            if ($token->{tag_name} eq 'image') {
6026              !!!cp ('t384');
6027              !!!parse-error (type => 'image', token => $token);
6028              $token->{tag_name} = 'img';
6029            } else {
6030              !!!cp ('t385');
6031            }
6032    
6033            ## ISSUE: An issue in spec there          ## NOTE: There is an "as if <br>" code clone.
6034            $reconstruct_active_formatting_elements->($insert_to_current);
6035            
6036            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6037    
6038            if ({
6039                 applet => 1, marquee => 1, object => 1,
6040                }->{$token->{tag_name}}) {
6041              !!!cp ('t380');
6042              push @$active_formatting_elements, ['#marker', ''];
6043            } elsif ({
6044                      b => 1, big => 1, em => 1, font => 1, i => 1,
6045                      s => 1, small => 1, strile => 1,
6046                      strong => 1, tt => 1, u => 1,
6047                     }->{$token->{tag_name}}) {
6048              !!!cp ('t375');
6049              push @$active_formatting_elements, $self->{open_elements}->[-1];
6050            } elsif ($token->{tag_name} eq 'input') {
6051              !!!cp ('t388');
6052              ## TODO: associate with $self->{form_element} if defined
6053              pop @{$self->{open_elements}};
6054            } elsif ({
6055                      area => 1, basefont => 1, bgsound => 1, br => 1,
6056                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6057                      #image => 1,
6058                     }->{$token->{tag_name}}) {
6059              !!!cp ('t388.1');
6060              pop @{$self->{open_elements}};
6061            } elsif ($token->{tag_name} eq 'select') {
6062              ## TODO: associate with $self->{form_element} if defined
6063            
6064              if ($self->{insertion_mode} & TABLE_IMS or
6065                  $self->{insertion_mode} & BODY_TABLE_IMS or
6066                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6067                !!!cp ('t400.1');
6068                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6069              } else {
6070                !!!cp ('t400.2');
6071                $self->{insertion_mode} = IN_SELECT_IM;
6072              }
6073          } else {          } else {
6074            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t402');
6075          }          }
6076            
6077            !!!next-token;
6078            redo B;
6079        }        }
6080      } elsif ($phase eq 'trailing end') {      } elsif ($token->{type} == END_TAG_TOKEN) {
6081        ## states in the main stage is preserved yet # MUST        if ($token->{tag_name} eq 'body') {
6082                  ## has a |body| element in scope
6083        if ($token->{type} eq 'DOCTYPE') {          my $i;
6084          !!!parse-error (type => 'after html:#DOCTYPE');          INSCOPE: {
6085          ## Ignore the token            for (reverse @{$self->{open_elements}}) {
6086                if ($_->[1] eq 'body') {
6087                  !!!cp ('t405');
6088                  $i = $_;
6089                  last INSCOPE;
6090                } elsif ({
6091                          applet => 1, table => 1, caption => 1, td => 1, th => 1,
6092                          button => 1, marquee => 1, object => 1, html => 1,
6093                         }->{$_->[1]}) {
6094                  !!!cp ('t405.1');
6095                  last;
6096                }
6097              }
6098    
6099              !!!parse-error (type => 'start tag not allowed',
6100                              value => $token->{tag_name}, token => $token);
6101              ## NOTE: Ignore the token.
6102              !!!next-token;
6103              redo B;
6104            } # INSCOPE
6105    
6106            for (@{$self->{open_elements}}) {
6107              unless ({
6108                       dd => 1, dt => 1, li => 1, p => 1, td => 1,
6109                       th => 1, tr => 1, body => 1, html => 1,
6110                       tbody => 1, tfoot => 1, thead => 1,
6111                      }->{$_->[1]}) {
6112                !!!cp ('t403');
6113                !!!parse-error (type => 'not closed:'.$_->[1], token => $token);
6114                last;
6115              } else {
6116                !!!cp ('t404');
6117              }
6118            }
6119    
6120            $self->{insertion_mode} = AFTER_BODY_IM;
6121          !!!next-token;          !!!next-token;
6122          redo B;          redo B;
6123        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'html') {
6124          my $comment = $self->{document}->create_comment ($token->{data});          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
6125          $self->{document}->append_child ($comment);            ## ISSUE: There is an issue in the spec.
6126              if ($self->{open_elements}->[-1]->[1] ne 'body') {
6127                !!!cp ('t406');
6128                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);
6129              } else {
6130                !!!cp ('t407');
6131              }
6132              $self->{insertion_mode} = AFTER_BODY_IM;
6133              ## reprocess
6134              redo B;
6135            } else {
6136              !!!cp ('t408');
6137              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6138              ## Ignore the token
6139              !!!next-token;
6140              redo B;
6141            }
6142          } elsif ({
6143                    address => 1, blockquote => 1, center => 1, dir => 1,
6144                    div => 1, dl => 1, fieldset => 1, listing => 1,
6145                    menu => 1, ol => 1, pre => 1, ul => 1,
6146                    dd => 1, dt => 1, li => 1,
6147                    applet => 1, button => 1, marquee => 1, object => 1,
6148                   }->{$token->{tag_name}}) {
6149            ## has an element in scope
6150            my $i;
6151            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6152              my $node = $self->{open_elements}->[$_];
6153              if ($node->[1] eq $token->{tag_name}) {
6154                !!!cp ('t410');
6155                $i = $_;
6156                last INSCOPE;
6157              } elsif ({
6158                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6159                        button => 1, marquee => 1, object => 1, html => 1,
6160                       }->{$node->[1]}) {
6161                !!!cp ('t411');
6162                last INSCOPE;
6163              }
6164            } # INSCOPE
6165    
6166            unless (defined $i) { # has an element in scope
6167              !!!cp ('t413');
6168              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6169            } else {
6170              ## Step 1. generate implied end tags
6171              while ({
6172                      dd => ($token->{tag_name} ne 'dd'),
6173                      dt => ($token->{tag_name} ne 'dt'),
6174                      li => ($token->{tag_name} ne 'li'),
6175                      p => 1,
6176                     }->{$self->{open_elements}->[-1]->[1]}) {
6177                !!!cp ('t409');
6178                pop @{$self->{open_elements}};
6179              }
6180    
6181              ## Step 2.
6182              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6183                !!!cp ('t412');
6184                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6185              } else {
6186                !!!cp ('t414');
6187              }
6188    
6189              ## Step 3.
6190              splice @{$self->{open_elements}}, $i;
6191    
6192              ## Step 4.
6193              $clear_up_to_marker->()
6194                  if {
6195                    applet => 1, button => 1, marquee => 1, object => 1,
6196                  }->{$token->{tag_name}};
6197            }
6198          !!!next-token;          !!!next-token;
6199          redo B;          redo B;
6200        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'form') {
6201          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          undef $self->{form_element};
6202            my $data = $1;  
6203            ## As if in the main phase.          ## has an element in scope
6204            ## NOTE: The insertion mode in the main phase          my $i;
6205            ## just before the phase has been changed to the trailing          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6206            ## end phase is either "after body" or "after frameset".            my $node = $self->{open_elements}->[$_];
6207            $reconstruct_active_formatting_elements->($insert_to_current)            if ($node->[1] eq $token->{tag_name}) {
6208              if $phase eq 'main';              !!!cp ('t418');
6209                $i = $_;
6210                last INSCOPE;
6211              } elsif ({
6212                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6213                        button => 1, marquee => 1, object => 1, html => 1,
6214                       }->{$node->[1]}) {
6215                !!!cp ('t419');
6216                last INSCOPE;
6217              }
6218            } # INSCOPE
6219    
6220            unless (defined $i) { # has an element in scope
6221              !!!cp ('t421');
6222              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6223            } else {
6224              ## Step 1. generate implied end tags
6225              while ({
6226                      dd => 1, dt => 1, li => 1, p => 1,
6227                     }->{$self->{open_elements}->[-1]->[1]}) {
6228                !!!cp ('t417');
6229                pop @{$self->{open_elements}};
6230              }
6231                        
6232            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
6233              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6234                !!!cp ('t417.1');
6235                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6236              } else {
6237                !!!cp ('t420');
6238              }  
6239                        
6240            unless (length $token->{data}) {            ## Step 3.
6241              !!!next-token;            splice @{$self->{open_elements}}, $i;
6242              redo B;          }
6243    
6244            !!!next-token;
6245            redo B;
6246          } elsif ({
6247                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6248                   }->{$token->{tag_name}}) {
6249            ## has an element in scope
6250            my $i;
6251            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6252              my $node = $self->{open_elements}->[$_];
6253              if ({
6254                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6255                  }->{$node->[1]}) {
6256                !!!cp ('t423');
6257                $i = $_;
6258                last INSCOPE;
6259              } elsif ({
6260                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6261                        button => 1, marquee => 1, object => 1, html => 1,
6262                       }->{$node->[1]}) {
6263                !!!cp ('t424');
6264                last INSCOPE;
6265              }
6266            } # INSCOPE
6267    
6268            unless (defined $i) { # has an element in scope
6269              !!!cp ('t425.1');
6270              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6271            } else {
6272              ## Step 1. generate implied end tags
6273              while ({
6274                      dd => 1, dt => 1, li => 1, p => 1,
6275                     }->{$self->{open_elements}->[-1]->[1]}) {
6276                !!!cp ('t422');
6277                pop @{$self->{open_elements}};
6278              }
6279              
6280              ## Step 2.
6281              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6282                !!!cp ('t425');
6283                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6284              } else {
6285                !!!cp ('t426');
6286            }            }
6287    
6288              ## Step 3.
6289              splice @{$self->{open_elements}}, $i;
6290          }          }
6291            
6292            !!!next-token;
6293            redo B;
6294          } elsif ($token->{tag_name} eq 'p') {
6295            ## has an element in scope
6296            my $i;
6297            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6298              my $node = $self->{open_elements}->[$_];
6299              if ($node->[1] eq $token->{tag_name}) {
6300                !!!cp ('t410.1');
6301                $i = $_;
6302                last INSCOPE;
6303              } elsif ({
6304                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6305                        button => 1, marquee => 1, object => 1, html => 1,
6306                       }->{$node->[1]}) {
6307                !!!cp ('t411.1');
6308                last INSCOPE;
6309              }
6310            } # INSCOPE
6311    
6312          !!!parse-error (type => 'after html:#character');          if (defined $i) {
6313          $phase = 'main';            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6314          ## reprocess              !!!cp ('t412.1');
6315                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6316              } else {
6317                !!!cp ('t414.1');
6318              }
6319    
6320              splice @{$self->{open_elements}}, $i;
6321            } else {
6322              !!!cp ('t413.1');
6323              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6324    
6325              !!!cp ('t415.1');
6326              ## As if <p>, then reprocess the current token
6327              my $el;
6328              !!!create-element ($el, 'p',, $token);
6329              $insert->($el);
6330              ## NOTE: Not inserted into |$self->{open_elements}|.
6331            }
6332    
6333            !!!next-token;
6334          redo B;          redo B;
6335        } elsif ($token->{type} eq 'start tag' or        } elsif ({
6336                 $token->{type} eq 'end tag') {                  a => 1,
6337          !!!parse-error (type => 'after html:'.$token->{tag_name});                  b => 1, big => 1, em => 1, font => 1, i => 1,
6338          $phase = 'main';                  nobr => 1, s => 1, small => 1, strile => 1,
6339          ## reprocess                  strong => 1, tt => 1, u => 1,
6340                   }->{$token->{tag_name}}) {
6341            !!!cp ('t427');
6342            $formatting_end_tag->($token);
6343          redo B;          redo B;
6344        } elsif ($token->{type} eq 'end-of-file') {        } elsif ($token->{tag_name} eq 'br') {
6345          ## Stop parsing          !!!cp ('t428');
6346          last B;          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6347    
6348            ## As if <br>
6349            $reconstruct_active_formatting_elements->($insert_to_current);
6350            
6351            my $el;
6352            !!!create-element ($el, 'br',, $token);
6353            $insert->($el);
6354            
6355            ## Ignore the token.
6356            !!!next-token;
6357            redo B;
6358          } elsif ({
6359                    caption => 1, col => 1, colgroup => 1, frame => 1,
6360                    frameset => 1, head => 1, option => 1, optgroup => 1,
6361                    tbody => 1, td => 1, tfoot => 1, th => 1,
6362                    thead => 1, tr => 1,
6363                    area => 1, basefont => 1, bgsound => 1,
6364                    embed => 1, hr => 1, iframe => 1, image => 1,
6365                    img => 1, input => 1, isindex => 1, noembed => 1,
6366                    noframes => 1, param => 1, select => 1, spacer => 1,
6367                    table => 1, textarea => 1, wbr => 1,
6368                    noscript => 0, ## TODO: if scripting is enabled
6369                   }->{$token->{tag_name}}) {
6370            !!!cp ('t429');
6371            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6372            ## Ignore the token
6373            !!!next-token;
6374            redo B;
6375            
6376            ## ISSUE: Issue on HTML5 new elements in spec
6377            
6378        } else {        } else {
6379          die "$0: $token->{type}: Unknown token";          ## Step 1
6380            my $node_i = -1;
6381            my $node = $self->{open_elements}->[$node_i];
6382    
6383            ## Step 2
6384            S2: {
6385              if ($node->[1] eq $token->{tag_name}) {
6386                ## Step 1
6387                ## generate implied end tags
6388                while ({
6389                        dd => 1, dt => 1, li => 1, p => 1,
6390                       }->{$self->{open_elements}->[-1]->[1]}) {
6391                  !!!cp ('t430');
6392                  ## ISSUE: Can this case be reached?
6393                  pop @{$self->{open_elements}};
6394                }
6395            
6396                ## Step 2
6397                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6398                  !!!cp ('t431');
6399                  ## NOTE: <x><y></x>
6400                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6401                } else {
6402                  !!!cp ('t432');
6403                }
6404                
6405                ## Step 3
6406                splice @{$self->{open_elements}}, $node_i;
6407    
6408                !!!next-token;
6409                last S2;
6410              } else {
6411                ## Step 3
6412                if (not $formatting_category->{$node->[1]} and
6413                    #not $phrasing_category->{$node->[1]} and
6414                    ($special_category->{$node->[1]} or
6415                     $scoping_category->{$node->[1]})) {
6416                  !!!cp ('t433');
6417                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6418                  ## Ignore the token
6419                  !!!next-token;
6420                  last S2;
6421                }
6422    
6423                !!!cp ('t434');
6424              }
6425              
6426              ## Step 4
6427              $node_i--;
6428              $node = $self->{open_elements}->[$node_i];
6429              
6430              ## Step 5;
6431              redo S2;
6432            } # S2
6433            redo B;
6434        }        }
6435      }      }
6436        redo B;
6437    } # B    } # B
6438    
6439    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4913  sub set_inner_html ($$$) { Line 6447  sub set_inner_html ($$$) {
6447    my $s = \$_[0];    my $s = \$_[0];
6448    my $onerror = $_[1];    my $onerror = $_[1];
6449    
6450      ## ISSUE: Should {confident} be true?
6451    
6452    my $nt = $node->node_type;    my $nt = $node->node_type;
6453    if ($nt == 9) {    if ($nt == 9) {
6454      # MUST      # MUST
# Line 4937  sub set_inner_html ($$$) { Line 6473  sub set_inner_html ($$$) {
6473      ## Step 1 # MUST      ## Step 1 # MUST
6474      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
6475      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
6476      ## TODO: Mark as HTML document      $doc->manakai_is_html (1);
6477      my $p = $class->new;      my $p = $class->new;
6478      $p->{document} = $doc;      $p->{document} = $doc;
6479    
6480      ## Step 9 # MUST      ## Step 8 # MUST
6481      my $i = 0;      my $i = 0;
6482      my $line = 1;      my $line = 1;
6483      my $column = 0;      my $column = 0;
6484      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6485        my $self = shift;        my $self = shift;
6486    
6487        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6488        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6489    
6490        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6491        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6492        $column++;        $column++;
6493    
6494        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6495          $line++;          $line++;
6496          $column = 0;          $column = 0;
6497        } elsif ($self->{next_input_character} == 0x000D) { # CR          !!!cp ('i1');
6498          if ($i >= length $$s) {        } elsif ($self->{next_char} == 0x000D) { # CR
6499            #          $i++ if substr ($$s, $i, 1) eq "\x0A";
6500          } else {          $self->{next_char} = 0x000A; # LF # MUST
           my $next_char = ord substr $$s, $i++, 1;  
           if ($next_char == 0x000A) { # LF  
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
             ## TODO: This is incorrect, since $next_char might be e.g. CR.  
           }  
         }  
         $self->{next_input_character} = 0x000A; # LF # MUST  
6501          $line++;          $line++;
6502          $column = 0;          $column = 0;
6503        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6504          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6505        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6506            !!!cp ('i3');
6507          } elsif ($self->{next_char} == 0x0000) { # NULL
6508            !!!cp ('i4');
6509          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6510          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6511        }        }
6512      };      };
6513      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6514      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6515            
6516      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6517        my (%opt) = @_;        my (%opt) = @_;
# Line 4995  sub set_inner_html ($$$) { Line 6525  sub set_inner_html ($$$) {
6525      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6526    
6527      ## Step 2      ## Step 2
6528      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6529      $p->{content_model_flag} = {      $p->{content_model} = {
6530        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
6531        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
6532        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
6533        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
6534        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
6535        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
6536        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
6537        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
6538        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
6539        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
6540      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
6541         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
6542            unless defined $p->{content_model};
6543            ## ISSUE: What is "the name of the element"? local name?
6544    
6545      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6546    
6547      ## Step 4      ## Step 3
6548      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6549        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6550    
6551      ## Step 5 # MUST      ## Step 4 # MUST
6552      $doc->append_child ($root);      $doc->append_child ($root);
6553    
6554      ## Step 6 # MUST      ## Step 5 # MUST
6555      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6556    
6557      undef $p->{head_element};      undef $p->{head_element};
6558    
6559      ## Step 7 # MUST      ## Step 6 # MUST
6560      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6561    
6562      ## Step 8 # MUST      ## Step 7 # MUST
6563      my $anode = $node;      my $anode = $node;
6564      AN: while (defined $anode) {      AN: while (defined $anode) {
6565        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6566          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6567          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6568            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6569                !!!cp ('i5');
6570              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6571              last AN;              last AN;
6572            }            }
# Line 5042  sub set_inner_html ($$$) { Line 6575  sub set_inner_html ($$$) {
6575        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6576      } # AN      } # AN
6577            
6578      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6579      {      {
6580        my $self = $p;        my $self = $p;
6581        !!!next-token;        !!!next-token;
6582      }      }
6583      $p->_tree_construction_main;      $p->_tree_construction_main;
6584    
6585      ## Step 11 # MUST      ## Step 10 # MUST
6586      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6587      for (@cn) {      for (@cn) {
6588        $node->remove_child ($_);        $node->remove_child ($_);
6589      }      }
6590      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6591    
6592      ## Step 12 # MUST      ## Step 11 # MUST
6593      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6594      for (@cn) {      for (@cn) {
6595        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5073  sub set_inner_html ($$$) { Line 6605  sub set_inner_html ($$$) {
6605    
6606  } # tree construction stage  } # tree construction stage
6607    
6608  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6609    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"  
       $s .= '<' . $tag_name;  
   
       ## ISSUE: Non-html elements  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
6610    
6611  1;  1;
6612  # $Date$  # $Date$

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.116

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24