/[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.6 by wakaba, Sat May 26 08:12:34 2007 UTC revision 1.114 by wakaba, Sun Mar 16 11:40:19 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  
   
 ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>  
29  my $c1_entity_char = {  my $c1_entity_char = {
30       128, 8364,    0x80 => 0x20AC,
31       129, 65533,    0x81 => 0xFFFD,
32       130, 8218,    0x82 => 0x201A,
33       131, 402,    0x83 => 0x0192,
34       132, 8222,    0x84 => 0x201E,
35       133, 8230,    0x85 => 0x2026,
36       134, 8224,    0x86 => 0x2020,
37       135, 8225,    0x87 => 0x2021,
38       136, 710,    0x88 => 0x02C6,
39       137, 8240,    0x89 => 0x2030,
40       138, 352,    0x8A => 0x0160,
41       139, 8249,    0x8B => 0x2039,
42       140, 338,    0x8C => 0x0152,
43       141, 65533,    0x8D => 0xFFFD,
44       142, 381,    0x8E => 0x017D,
45       143, 65533,    0x8F => 0xFFFD,
46       144, 65533,    0x90 => 0xFFFD,
47       145, 8216,    0x91 => 0x2018,
48       146, 8217,    0x92 => 0x2019,
49       147, 8220,    0x93 => 0x201C,
50       148, 8221,    0x94 => 0x201D,
51       149, 8226,    0x95 => 0x2022,
52       150, 8211,    0x96 => 0x2013,
53       151, 8212,    0x97 => 0x2014,
54       152, 732,    0x98 => 0x02DC,
55       153, 8482,    0x99 => 0x2122,
56       154, 353,    0x9A => 0x0161,
57       155, 8250,    0x9B => 0x203A,
58       156, 339,    0x9C => 0x0153,
59       157, 65533,    0x9D => 0xFFFD,
60       158, 382,    0x9E => 0x017E,
61       159, 376,    0x9F => 0x0178,
62  }; # $c1_entity_char  }; # $c1_entity_char
63    
64  my $special_category = {  my $special_category = {
# Line 329  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 338  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      $self->{next_input_character} = -1 and return if $i >= length $$s;  
186      $self->{next_input_character} = ord substr $$s, $i++, 1;      pop @{$self->{prev_char}};
187      $column++;      unshift @{$self->{prev_char}}, $self->{next_char};
188    
189        $self->{next_char} = -1 and return if $i >= length $$s;
190        $self->{next_char} = ord substr $$s, $i++, 1;
191    
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
207            push @{$self->{char}}, $next_char;        !!!parse-error (type => 'NULL');
208          }        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
       }  
       $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  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
209      }      }
210    };    };
211      $self->{prev_char} = [-1, -1, -1];
212      $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 391  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 444  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 451  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} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
415          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
416            $self->{state} = 'tag open';            unless ($self->{escape}) {
417                if ($self->{prev_char}->[0] == 0x002D and # -
418                    $self->{prev_char}->[1] == 0x0021 and # !
419                    $self->{prev_char}->[2] == 0x003C) { # <
420                  !!!cp (3);
421                  $self->{escape} = 1;
422                } else {
423                  !!!cp (4);
424                }
425              } else {
426                !!!cp (5);
427              }
428            }
429            
430            #
431          } elsif ($self->{next_char} == 0x003C) { # <
432            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
433                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
434                 not $self->{escape})) {
435              !!!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} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
444          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
445                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
446              if ($self->{prev_char}->[0] == 0x002D and # -
447                  $self->{prev_char}->[1] == 0x002D) { # -
448                !!!cp (8);
449                delete $self->{escape};
450              } else {
451                !!!cp (9);
452              }
453            } else {
454              !!!cp (10);
455            }
456            
457            #
458          } elsif ($self->{next_char} == -1) {
459            !!!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              !!!cp (21);
548            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
549            $self->{state} = 'data';            $self->{state} = DATA_STATE;
550            !!!next-input-character;            !!!next-input-character;
551    
552            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
553                        line => $self->{line_prev},
554                        column => $self->{column_prev}});
555    
556            redo A;            redo A;
557          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
558              !!!cp (22);
559            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
560            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
561            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
562                                        line => $self->{line_prev},
563                                        column => $self->{column_prev}};
564              ## $self->{next_char} is intentionally left as is
565            redo A;            redo A;
566          } else {          } else {
567              !!!cp (23);
568            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
569            $self->{state} = 'data';            $self->{state} = DATA_STATE;
570            ## reconsume            ## reconsume
571    
572            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
573                        line => $self->{line_prev},
574                        column => $self->{column_prev}});
575    
576            redo A;            redo A;
577          }          }
578        } else {        } else {
579          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
580        }        }
581      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
582        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
583            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
584          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
585          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
586            push @next_char, $self->{next_input_character};            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
587            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            my @next_char;
588            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
589            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              push @next_char, $self->{next_char};
590              !!!next-input-character;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
591              next TAGNAME;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
592            } else {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
593              !!!parse-error (type => 'unmatched end tag');                !!!cp (24);
594              $self->{next_input_character} = shift @next_char; # reconsume                !!!next-input-character;
595              !!!back-next-input-character (@next_char);                next TAGNAME;
596              $self->{state} = 'data';              } else {
597                  !!!cp (25);
598              !!!emit ({type => 'character', data => '</'});                $self->{next_char} = shift @next_char; # reconsume
599                  !!!back-next-input-character (@next_char);
600                  $self->{state} = DATA_STATE;
601    
602                  !!!emit ({type => CHARACTER_TOKEN, data => '</',
603                            line => $l, column => $c});
604      
605                  redo A;
606                }
607              }
608              push @next_char, $self->{next_char};
609          
610              unless ($self->{next_char} == 0x0009 or # HT
611                      $self->{next_char} == 0x000A or # LF
612                      $self->{next_char} == 0x000B or # VT
613                      $self->{next_char} == 0x000C or # FF
614                      $self->{next_char} == 0x0020 or # SP
615                      $self->{next_char} == 0x003E or # >
616                      $self->{next_char} == 0x002F or # /
617                      $self->{next_char} == -1) {
618                !!!cp (26);
619                $self->{next_char} = shift @next_char; # reconsume
620                !!!back-next-input-character (@next_char);
621                $self->{state} = DATA_STATE;
622                !!!emit ({type => CHARACTER_TOKEN, data => '</',
623                          line => $l, column => $c});
624              redo A;              redo A;
625              } else {
626                !!!cp (27);
627                $self->{next_char} = shift @next_char;
628                !!!back-next-input-character (@next_char);
629                # and consume...
630            }            }
         }  
         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;  
631          } else {          } else {
632            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
633            !!!back-next-input-character (@next_char);            !!!cp (28);
634            # and consume...            # next-input-character is already done
635              $self->{state} = DATA_STATE;
636              !!!emit ({type => CHARACTER_TOKEN, data => '</',
637                        line => $l, column => $c});
638              redo A;
639          }          }
640        }        }
641                
642        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
643            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
644          $self->{current_token} = {type => 'end tag',          !!!cp (29);
645                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
646          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
647          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
648          redo A;                 line => $l, column => $c};
649        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
650                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
651          $self->{current_token} = {type => 'end tag',          redo A;
652                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
653          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
654            !!!cp (30);
655            $self->{current_token} = {type => END_TAG_TOKEN,
656                                      tag_name => chr ($self->{next_char}),
657                                      line => $l, column => $c};
658            $self->{state} = TAG_NAME_STATE;
659          !!!next-input-character;          !!!next-input-character;
660          redo A;          redo A;
661        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
662            !!!cp (31);
663          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
664          $self->{state} = 'data';          $self->{state} = DATA_STATE;
665          !!!next-input-character;          !!!next-input-character;
666          redo A;          redo A;
667        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
668            !!!cp (32);
669          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
670          $self->{state} = 'data';          $self->{state} = DATA_STATE;
671          # reconsume          # reconsume
672    
673          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
674                      line => $l, column => $c});
675    
676          redo A;          redo A;
677        } else {        } else {
678            !!!cp (33);
679          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
680          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
681          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
682          redo A;                                    line => $self->{line_prev}, # "<" of "</"
683        }                                    column => $self->{column_prev} - 1};
684      } elsif ($self->{state} eq 'tag name') {          ## $self->{next_char} is intentionally left as is
685        if ($self->{next_input_character} == 0x0009 or # HT          redo A;
686            $self->{next_input_character} == 0x000A or # LF        }
687            $self->{next_input_character} == 0x000B or # VT      } elsif ($self->{state} == TAG_NAME_STATE) {
688            $self->{next_input_character} == 0x000C or # FF        if ($self->{next_char} == 0x0009 or # HT
689            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000A or # LF
690          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000B or # VT
691          !!!next-input-character;            $self->{next_char} == 0x000C or # FF
692          redo A;            $self->{next_char} == 0x0020) { # SP
693        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!cp (34);
694          if ($self->{current_token}->{type} eq 'start tag') {          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
695            !!!next-input-character;
696            redo A;
697          } elsif ($self->{next_char} == 0x003E) { # >
698            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
699              !!!cp (35);
700              $self->{current_token}->{first_start_tag}
701                  = not defined $self->{last_emitted_start_tag_name};
702            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
703          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
704            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
705            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
706              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
707            }            #  !!! cp (36);
708              #  !!! parse-error (type => 'end tag attribute');
709              #} else {
710                !!!cp (37);
711              #}
712          } else {          } else {
713            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
714          }          }
715          $self->{state} = 'data';          $self->{state} = DATA_STATE;
716          !!!next-input-character;          !!!next-input-character;
717    
718          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
719    
720          redo A;          redo A;
721        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
722                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
723          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
724            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
725            # start tag or end tag            # start tag or end tag
726          ## Stay in this state          ## Stay in this state
727          !!!next-input-character;          !!!next-input-character;
728          redo A;          redo A;
729        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
730          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
731          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
732              !!!cp (39);
733              $self->{current_token}->{first_start_tag}
734                  = not defined $self->{last_emitted_start_tag_name};
735            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
736          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
737            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
738            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
739              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
740            }            #  !!! cp (40);
741              #  !!! parse-error (type => 'end tag attribute');
742              #} else {
743                !!!cp (41);
744              #}
745          } else {          } else {
746            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
747          }          }
748          $self->{state} = 'data';          $self->{state} = DATA_STATE;
749          # reconsume          # reconsume
750    
751          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
752    
753          redo A;          redo A;
754        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
755          !!!next-input-character;          !!!next-input-character;
756          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
757              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
758              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
759            # permitted slash            # permitted slash
760              !!!cp (42);
761            #            #
762          } else {          } else {
763              !!!cp (43);
764            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
765          }          }
766          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
767          # next-input-character is already done          # next-input-character is already done
768          redo A;          redo A;
769        } else {        } else {
770          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
771            $self->{current_token}->{tag_name} .= chr $self->{next_char};
772            # start tag or end tag            # start tag or end tag
773          ## Stay in the state          ## Stay in the state
774          !!!next-input-character;          !!!next-input-character;
775          redo A;          redo A;
776        }        }
777      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
778        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
779            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
780            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
781            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
782            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
783            !!!cp (45);
784          ## Stay in the state          ## Stay in the state
785          !!!next-input-character;          !!!next-input-character;
786          redo A;          redo A;
787        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
788          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
789              !!!cp (46);
790              $self->{current_token}->{first_start_tag}
791                  = not defined $self->{last_emitted_start_tag_name};
792            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
793          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
794            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
795            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
796                !!!cp (47);
797              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
798              } else {
799                !!!cp (48);
800            }            }
801          } else {          } else {
802            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
803          }          }
804          $self->{state} = 'data';          $self->{state} = DATA_STATE;
805          !!!next-input-character;          !!!next-input-character;
806    
807          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
808    
809          redo A;          redo A;
810        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
811                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
812          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
813            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
814                                value => ''};                                value => ''};
815          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
816          !!!next-input-character;          !!!next-input-character;
817          redo A;          redo A;
818        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
819          !!!next-input-character;          !!!next-input-character;
820          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
821              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
822              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
823            # permitted slash            # permitted slash
824              !!!cp (50);
825            #            #
826          } else {          } else {
827              !!!cp (51);
828            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
829          }          }
830          ## Stay in the state          ## Stay in the state
831          # next-input-character is already done          # next-input-character is already done
832          redo A;          redo A;
833        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
834          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
835          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
836              !!!cp (52);
837              $self->{current_token}->{first_start_tag}
838                  = not defined $self->{last_emitted_start_tag_name};
839            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
840          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
841            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
842            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
843                !!!cp (53);
844              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
845              } else {
846                !!!cp (54);
847            }            }
848          } else {          } else {
849            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
850          }          }
851          $self->{state} = 'data';          $self->{state} = DATA_STATE;
852          # reconsume          # reconsume
853    
854          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
855    
856          redo A;          redo A;
857        } else {        } else {
858          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
859                 0x0022 => 1, # "
860                 0x0027 => 1, # '
861                 0x003D => 1, # =
862                }->{$self->{next_char}}) {
863              !!!cp (55);
864              !!!parse-error (type => 'bad attribute name');
865            } else {
866              !!!cp (56);
867            }
868            $self->{current_attribute} = {name => chr ($self->{next_char}),
869                                value => ''};                                value => ''};
870          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
871          !!!next-input-character;          !!!next-input-character;
872          redo A;          redo A;
873        }        }
874      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
875        my $before_leave = sub {        my $before_leave = sub {
876          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
877              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
878            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
879              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
880            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
881          } else {          } else {
882              !!!cp (58);
883            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
884              = $self->{current_attribute};              = $self->{current_attribute};
885          }          }
886        }; # $before_leave        }; # $before_leave
887    
888        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
889            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
890            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
891            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
892            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
893            !!!cp (59);
894          $before_leave->();          $before_leave->();
895          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
896          !!!next-input-character;          !!!next-input-character;
897          redo A;          redo A;
898        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
899            !!!cp (60);
900          $before_leave->();          $before_leave->();
901          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
902          !!!next-input-character;          !!!next-input-character;
903          redo A;          redo A;
904        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
905          $before_leave->();          $before_leave->();
906          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
907              !!!cp (61);
908              $self->{current_token}->{first_start_tag}
909                  = not defined $self->{last_emitted_start_tag_name};
910            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
911          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
912            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
913              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
914            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
915              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
916            }            }
917          } else {          } else {
918            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
919          }          }
920          $self->{state} = 'data';          $self->{state} = DATA_STATE;
921          !!!next-input-character;          !!!next-input-character;
922    
923          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
924    
925          redo A;          redo A;
926        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
927                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
928          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
929            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
930          ## Stay in the state          ## Stay in the state
931          !!!next-input-character;          !!!next-input-character;
932          redo A;          redo A;
933        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
934          $before_leave->();          $before_leave->();
935          !!!next-input-character;          !!!next-input-character;
936          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
937              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
938              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
939            # permitted slash            # permitted slash
940              !!!cp (64);
941            #            #
942          } else {          } else {
943              !!!cp (65);
944            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
945          }          }
946          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
947          # next-input-character is already done          # next-input-character is already done
948          redo A;          redo A;
949        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
950          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
951          $before_leave->();          $before_leave->();
952          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
953              !!!cp (66);
954              $self->{current_token}->{first_start_tag}
955                  = not defined $self->{last_emitted_start_tag_name};
956            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
957          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
958            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
959            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
960                !!!cp (67);
961              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
962              } else {
963                ## NOTE: This state should never be reached.
964                !!!cp (68);
965            }            }
966          } else {          } else {
967            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
968          }          }
969          $self->{state} = 'data';          $self->{state} = DATA_STATE;
970          # reconsume          # reconsume
971    
972          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
973    
974          redo A;          redo A;
975        } else {        } else {
976          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
977                $self->{next_char} == 0x0027) { # '
978              !!!cp (69);
979              !!!parse-error (type => 'bad attribute name');
980            } else {
981              !!!cp (70);
982            }
983            $self->{current_attribute}->{name} .= chr ($self->{next_char});
984          ## Stay in the state          ## Stay in the state
985          !!!next-input-character;          !!!next-input-character;
986          redo A;          redo A;
987        }        }
988      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
989        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
990            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
991            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
992            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
993            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
994            !!!cp (71);
995          ## Stay in the state          ## Stay in the state
996          !!!next-input-character;          !!!next-input-character;
997          redo A;          redo A;
998        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
999          $self->{state} = 'before attribute value';          !!!cp (72);
1000            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1001          !!!next-input-character;          !!!next-input-character;
1002          redo A;          redo A;
1003        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1004          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1005              !!!cp (73);
1006              $self->{current_token}->{first_start_tag}
1007                  = not defined $self->{last_emitted_start_tag_name};
1008            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1009          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1010            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1011            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1012                !!!cp (74);
1013              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1014              } else {
1015                ## NOTE: This state should never be reached.
1016                !!!cp (75);
1017            }            }
1018          } else {          } else {
1019            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1020          }          }
1021          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1022          !!!next-input-character;          !!!next-input-character;
1023    
1024          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1025    
1026          redo A;          redo A;
1027        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1028                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1029          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1030            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
1031                                value => ''};                                value => ''};
1032          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1033          !!!next-input-character;          !!!next-input-character;
1034          redo A;          redo A;
1035        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1036          !!!next-input-character;          !!!next-input-character;
1037          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1038              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
1039              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1040            # permitted slash            # permitted slash
1041              !!!cp (77);
1042            #            #
1043          } else {          } else {
1044              !!!cp (78);
1045            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1046              ## TODO: Different error type for <aa / bb> than <aa/>
1047          }          }
1048          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1049          # next-input-character is already done          # next-input-character is already done
1050          redo A;          redo A;
1051        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1052          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1053          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1054              !!!cp (79);
1055              $self->{current_token}->{first_start_tag}
1056                  = not defined $self->{last_emitted_start_tag_name};
1057            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1058          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1059            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1060            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1061                !!!cp (80);
1062              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1063              } else {
1064                ## NOTE: This state should never be reached.
1065                !!!cp (81);
1066            }            }
1067          } else {          } else {
1068            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1069          }          }
1070          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1071          # reconsume          # reconsume
1072    
1073          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1074    
1075          redo A;          redo A;
1076        } else {        } else {
1077          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1078            $self->{current_attribute} = {name => chr ($self->{next_char}),
1079                                value => ''};                                value => ''};
1080          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1081          !!!next-input-character;          !!!next-input-character;
1082          redo A;                  redo A;        
1083        }        }
1084      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1085        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1086            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1087            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1088            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1089            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1090            !!!cp (83);
1091          ## Stay in the state          ## Stay in the state
1092          !!!next-input-character;          !!!next-input-character;
1093          redo A;          redo A;
1094        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1095          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1096            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1097          !!!next-input-character;          !!!next-input-character;
1098          redo A;          redo A;
1099        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1100          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1101            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1102          ## reconsume          ## reconsume
1103          redo A;          redo A;
1104        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1105          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1106            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1107          !!!next-input-character;          !!!next-input-character;
1108          redo A;          redo A;
1109        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1110          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1111              !!!cp (87);
1112              $self->{current_token}->{first_start_tag}
1113                  = not defined $self->{last_emitted_start_tag_name};
1114            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1115          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1116            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1117            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1118                !!!cp (88);
1119              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1120              } else {
1121                ## NOTE: This state should never be reached.
1122                !!!cp (89);
1123            }            }
1124          } else {          } else {
1125            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1126          }          }
1127          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1128          !!!next-input-character;          !!!next-input-character;
1129    
1130          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1131    
1132          redo A;          redo A;
1133        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1134          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1135          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1136              !!!cp (90);
1137              $self->{current_token}->{first_start_tag}
1138                  = not defined $self->{last_emitted_start_tag_name};
1139            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1140          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1141            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1142            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1143                !!!cp (91);
1144              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1145              } else {
1146                ## NOTE: This state should never be reached.
1147                !!!cp (92);
1148            }            }
1149          } else {          } else {
1150            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1151          }          }
1152          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1153          ## reconsume          ## reconsume
1154    
1155          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1156    
1157          redo A;          redo A;
1158        } else {        } else {
1159          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1160          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1161              !!!parse-error (type => 'bad attribute value');
1162            } else {
1163              !!!cp (94);
1164            }
1165            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1166            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1167          !!!next-input-character;          !!!next-input-character;
1168          redo A;          redo A;
1169        }        }
1170      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1171        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1172          $self->{state} = 'before attribute name';          !!!cp (95);
1173            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1174          !!!next-input-character;          !!!next-input-character;
1175          redo A;          redo A;
1176        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1177          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1178          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1179            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1180          !!!next-input-character;          !!!next-input-character;
1181          redo A;          redo A;
1182        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1183          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1184          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1185              !!!cp (97);
1186              $self->{current_token}->{first_start_tag}
1187                  = not defined $self->{last_emitted_start_tag_name};
1188            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1189          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1190            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1191            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1192                !!!cp (98);
1193              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1194              } else {
1195                ## NOTE: This state should never be reached.
1196                !!!cp (99);
1197            }            }
1198          } else {          } else {
1199            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1200          }          }
1201          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1202          ## reconsume          ## reconsume
1203    
1204          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1205    
1206          redo A;          redo A;
1207        } else {        } else {
1208          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1209            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1210          ## Stay in the state          ## Stay in the state
1211          !!!next-input-character;          !!!next-input-character;
1212          redo A;          redo A;
1213        }        }
1214      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1215        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1216          $self->{state} = 'before attribute name';          !!!cp (101);
1217            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1218          !!!next-input-character;          !!!next-input-character;
1219          redo A;          redo A;
1220        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1221          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1222          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1223            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1224          !!!next-input-character;          !!!next-input-character;
1225          redo A;          redo A;
1226        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1227          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1228          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1229              !!!cp (103);
1230              $self->{current_token}->{first_start_tag}
1231                  = not defined $self->{last_emitted_start_tag_name};
1232            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1233          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1234            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1235            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1236                !!!cp (104);
1237              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1238              } else {
1239                ## NOTE: This state should never be reached.
1240                !!!cp (105);
1241            }            }
1242          } else {          } else {
1243            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1244          }          }
1245          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1246          ## reconsume          ## reconsume
1247    
1248          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1249    
1250          redo A;          redo A;
1251        } else {        } else {
1252          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1253            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1254          ## Stay in the state          ## Stay in the state
1255          !!!next-input-character;          !!!next-input-character;
1256          redo A;          redo A;
1257        }        }
1258      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1259        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1260            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1261            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1262            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1263            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1264          $self->{state} = 'before attribute name';          !!!cp (107);
1265          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1266          redo A;          !!!next-input-character;
1267        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1268          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1269          $self->{state} = 'entity in attribute value';          !!!cp (108);
1270          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1271          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1272        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1273          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1274          } elsif ($self->{next_char} == 0x003E) { # >
1275            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1276              !!!cp (109);
1277              $self->{current_token}->{first_start_tag}
1278                  = not defined $self->{last_emitted_start_tag_name};
1279            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1280          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1281            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1282            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1283                !!!cp (110);
1284              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1285              } else {
1286                ## NOTE: This state should never be reached.
1287                !!!cp (111);
1288            }            }
1289          } else {          } else {
1290            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1291          }          }
1292          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294    
1295          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1296    
1297          redo A;          redo A;
1298        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1299          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1300          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1301              !!!cp (112);
1302              $self->{current_token}->{first_start_tag}
1303                  = not defined $self->{last_emitted_start_tag_name};
1304            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1305          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1306            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1307            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1308                !!!cp (113);
1309              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1310              } else {
1311                ## NOTE: This state should never be reached.
1312                !!!cp (114);
1313            }            }
1314          } else {          } else {
1315            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1316          }          }
1317          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1318          ## reconsume          ## reconsume
1319    
1320          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1321    
1322          redo A;          redo A;
1323        } else {        } else {
1324          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1325                 0x0022 => 1, # "
1326                 0x0027 => 1, # '
1327                 0x003D => 1, # =
1328                }->{$self->{next_char}}) {
1329              !!!cp (115);
1330              !!!parse-error (type => 'bad attribute value');
1331            } else {
1332              !!!cp (116);
1333            }
1334            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1335          ## Stay in the state          ## Stay in the state
1336          !!!next-input-character;          !!!next-input-character;
1337          redo A;          redo A;
1338        }        }
1339      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1340        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1341              (1,
1342               $self->{last_attribute_value_state}
1343                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1344               $self->{last_attribute_value_state}
1345                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1346               -1);
1347    
1348        unless (defined $token) {        unless (defined $token) {
1349            !!!cp (117);
1350          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1351        } else {        } else {
1352            !!!cp (118);
1353          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1354            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1355          ## 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"
1356        }        }
1357    
1358        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1359        # next-input-character is already done        # next-input-character is already done
1360        redo A;        redo A;
1361      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1362          if ($self->{next_char} == 0x0009 or # HT
1363              $self->{next_char} == 0x000A or # LF
1364              $self->{next_char} == 0x000B or # VT
1365              $self->{next_char} == 0x000C or # FF
1366              $self->{next_char} == 0x0020) { # SP
1367            !!!cp (118);
1368            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1369            !!!next-input-character;
1370            redo A;
1371          } elsif ($self->{next_char} == 0x003E) { # >
1372            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1373              !!!cp (119);
1374              $self->{current_token}->{first_start_tag}
1375                  = not defined $self->{last_emitted_start_tag_name};
1376              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1377            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1378              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1379              if ($self->{current_token}->{attributes}) {
1380                !!!cp (120);
1381                !!!parse-error (type => 'end tag attribute');
1382              } else {
1383                ## NOTE: This state should never be reached.
1384                !!!cp (121);
1385              }
1386            } else {
1387              die "$0: $self->{current_token}->{type}: Unknown token type";
1388            }
1389            $self->{state} = DATA_STATE;
1390            !!!next-input-character;
1391    
1392            !!!emit ($self->{current_token}); # start tag or end tag
1393    
1394            redo A;
1395          } elsif ($self->{next_char} == 0x002F) { # /
1396            !!!next-input-character;
1397            if ($self->{next_char} == 0x003E and # >
1398                $self->{current_token}->{type} == START_TAG_TOKEN and
1399                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1400              # permitted slash
1401              !!!cp (122);
1402              #
1403            } else {
1404              !!!cp (123);
1405              !!!parse-error (type => 'nestc');
1406            }
1407            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1408            # next-input-character is already done
1409            redo A;
1410          } else {
1411            !!!cp (124);
1412            !!!parse-error (type => 'no space between attributes');
1413            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1414            ## reconsume
1415            redo A;
1416          }
1417        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1418        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1419                
1420        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1421          #my $token = {type => COMMENT_TOKEN, data => ''};
1422    
1423        BC: {        BC: {
1424          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1425            $self->{state} = 'data';            !!!cp (124);
1426              $self->{state} = DATA_STATE;
1427            !!!next-input-character;            !!!next-input-character;
1428    
1429            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1430    
1431            redo A;            redo A;
1432          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1433            $self->{state} = 'data';            !!!cp (125);
1434              $self->{state} = DATA_STATE;
1435            ## reconsume            ## reconsume
1436    
1437            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1438    
1439            redo A;            redo A;
1440          } else {          } else {
1441            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1442              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1443            !!!next-input-character;            !!!next-input-character;
1444            redo BC;            redo BC;
1445          }          }
1446        } # BC        } # BC
1447      } elsif ($self->{state} eq 'markup declaration open') {  
1448          die "$0: _get_next_token: unexpected case [BC]";
1449        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1450        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1451    
1452          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1453    
1454        my @next_char;        my @next_char;
1455        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1456                
1457        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1458          !!!next-input-character;          !!!next-input-character;
1459          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1460          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1461            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1462            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1463                                        line => $l, column => $c};
1464              $self->{state} = COMMENT_START_STATE;
1465            !!!next-input-character;            !!!next-input-character;
1466            redo A;            redo A;
1467            } else {
1468              !!!cp (128);
1469          }          }
1470        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1471                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1472          !!!next-input-character;          !!!next-input-character;
1473          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1474          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1475              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1476            !!!next-input-character;            !!!next-input-character;
1477            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1478            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1479                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1480              !!!next-input-character;              !!!next-input-character;
1481              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1482              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1483                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1484                !!!next-input-character;                !!!next-input-character;
1485                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1486                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1487                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1488                  !!!next-input-character;                  !!!next-input-character;
1489                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1490                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1491                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1492                    !!!next-input-character;                    !!!next-input-character;
1493                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1494                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1495                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1496                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1497                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1498                        $self->{state} = DOCTYPE_STATE;
1499                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1500                                                  quirks => 1,
1501                                                  line => $l, column => $c};
1502                      !!!next-input-character;                      !!!next-input-character;
1503                      redo A;                      redo A;
1504                      } else {
1505                        !!!cp (130);
1506                    }                    }
1507                    } else {
1508                      !!!cp (131);
1509                  }                  }
1510                  } else {
1511                    !!!cp (132);
1512                }                }
1513                } else {
1514                  !!!cp (133);
1515              }              }
1516              } else {
1517                !!!cp (134);
1518            }            }
1519            } else {
1520              !!!cp (135);
1521          }          }
1522          } else {
1523            !!!cp (136);
1524        }        }
1525    
1526        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1527        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1528        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1529        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1530          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1531                                    line => $l, column => $c};
1532        redo A;        redo A;
1533                
1534        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1535        ## 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?
1536      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1537        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1538          $self->{state} = 'comment dash';          !!!cp (137);
1539            $self->{state} = COMMENT_START_DASH_STATE;
1540            !!!next-input-character;
1541            redo A;
1542          } elsif ($self->{next_char} == 0x003E) { # >
1543            !!!cp (138);
1544            !!!parse-error (type => 'bogus comment');
1545            $self->{state} = DATA_STATE;
1546            !!!next-input-character;
1547    
1548            !!!emit ($self->{current_token}); # comment
1549    
1550            redo A;
1551          } elsif ($self->{next_char} == -1) {
1552            !!!cp (139);
1553            !!!parse-error (type => 'unclosed comment');
1554            $self->{state} = DATA_STATE;
1555            ## reconsume
1556    
1557            !!!emit ($self->{current_token}); # comment
1558    
1559            redo A;
1560          } else {
1561            !!!cp (140);
1562            $self->{current_token}->{data} # comment
1563                .= chr ($self->{next_char});
1564            $self->{state} = COMMENT_STATE;
1565            !!!next-input-character;
1566            redo A;
1567          }
1568        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1569          if ($self->{next_char} == 0x002D) { # -
1570            !!!cp (141);
1571            $self->{state} = COMMENT_END_STATE;
1572            !!!next-input-character;
1573            redo A;
1574          } elsif ($self->{next_char} == 0x003E) { # >
1575            !!!cp (142);
1576            !!!parse-error (type => 'bogus comment');
1577            $self->{state} = DATA_STATE;
1578            !!!next-input-character;
1579    
1580            !!!emit ($self->{current_token}); # comment
1581    
1582            redo A;
1583          } elsif ($self->{next_char} == -1) {
1584            !!!cp (143);
1585            !!!parse-error (type => 'unclosed comment');
1586            $self->{state} = DATA_STATE;
1587            ## reconsume
1588    
1589            !!!emit ($self->{current_token}); # comment
1590    
1591            redo A;
1592          } else {
1593            !!!cp (144);
1594            $self->{current_token}->{data} # comment
1595                .= '-' . chr ($self->{next_char});
1596            $self->{state} = COMMENT_STATE;
1597          !!!next-input-character;          !!!next-input-character;
1598          redo A;          redo A;
1599        } elsif ($self->{next_input_character} == -1) {        }
1600        } elsif ($self->{state} == COMMENT_STATE) {
1601          if ($self->{next_char} == 0x002D) { # -
1602            !!!cp (145);
1603            $self->{state} = COMMENT_END_DASH_STATE;
1604            !!!next-input-character;
1605            redo A;
1606          } elsif ($self->{next_char} == -1) {
1607            !!!cp (146);
1608          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1609          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1610          ## reconsume          ## reconsume
1611    
1612          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1613    
1614          redo A;          redo A;
1615        } else {        } else {
1616          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1617            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1618          ## Stay in the state          ## Stay in the state
1619          !!!next-input-character;          !!!next-input-character;
1620          redo A;          redo A;
1621        }        }
1622      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1623        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1624          $self->{state} = 'comment end';          !!!cp (148);
1625            $self->{state} = COMMENT_END_STATE;
1626          !!!next-input-character;          !!!next-input-character;
1627          redo A;          redo A;
1628        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1629            !!!cp (149);
1630          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1631          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1632          ## reconsume          ## reconsume
1633    
1634          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1635    
1636          redo A;          redo A;
1637        } else {        } else {
1638          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1639          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1640            $self->{state} = COMMENT_STATE;
1641          !!!next-input-character;          !!!next-input-character;
1642          redo A;          redo A;
1643        }        }
1644      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1645        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1646          $self->{state} = 'data';          !!!cp (151);
1647            $self->{state} = DATA_STATE;
1648          !!!next-input-character;          !!!next-input-character;
1649    
1650          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1651    
1652          redo A;          redo A;
1653        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1654          !!!parse-error (type => 'dash in comment');          !!!cp (152);
1655            !!!parse-error (type => 'dash in comment',
1656                            line => $self->{line_prev},
1657                            column => $self->{column_prev});
1658          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1659          ## Stay in the state          ## Stay in the state
1660          !!!next-input-character;          !!!next-input-character;
1661          redo A;          redo A;
1662        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1663            !!!cp (153);
1664          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1665          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1666          ## reconsume          ## reconsume
1667    
1668          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1669    
1670          redo A;          redo A;
1671        } else {        } else {
1672          !!!parse-error (type => 'dash in comment');          !!!cp (154);
1673          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
1674          $self->{state} = 'comment';                          line => $self->{line_prev},
1675                            column => $self->{column_prev});
1676            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1677            $self->{state} = COMMENT_STATE;
1678          !!!next-input-character;          !!!next-input-character;
1679          redo A;          redo A;
1680        }        }
1681      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1682        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1683            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1684            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1685            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1686            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1687          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1688            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1689          !!!next-input-character;          !!!next-input-character;
1690          redo A;          redo A;
1691        } else {        } else {
1692            !!!cp (156);
1693          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1694          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1695          ## reconsume          ## reconsume
1696          redo A;          redo A;
1697        }        }
1698      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1699        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1700            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1701            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1702            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1703            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1704            !!!cp (157);
1705          ## Stay in the state          ## Stay in the state
1706          !!!next-input-character;          !!!next-input-character;
1707          redo A;          redo A;
1708        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
1709                 $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) { # >  
1710          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1711          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1712          !!!next-input-character;          !!!next-input-character;
1713    
1714          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1715    
1716          redo A;          redo A;
1717        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1718            !!!cp (159);
1719          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1720          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1721          ## reconsume          ## reconsume
1722    
1723          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1724    
1725          redo A;          redo A;
1726        } else {        } else {
1727          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
1728                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name} = chr $self->{next_char};
1729                            error => 1};          delete $self->{current_token}->{quirks};
1730  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1731          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1732          !!!next-input-character;          !!!next-input-character;
1733          redo A;          redo A;
1734        }        }
1735      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1736        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
1737            $self->{next_input_character} == 0x000A or # LF        if ($self->{next_char} == 0x0009 or # HT
1738            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000A or # LF
1739            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000B or # VT
1740            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000C or # FF
1741          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{next_char} == 0x0020) { # SP
1742          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
1743            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1744          !!!next-input-character;          !!!next-input-character;
1745          redo A;          redo A;
1746        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1747          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
1748          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1749          !!!next-input-character;          !!!next-input-character;
1750    
1751          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1752    
1753          redo A;          redo A;
1754        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
1755                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
1756          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
1757          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
1758            ## reconsume
1759    
1760            $self->{current_token}->{quirks} = 1;
1761            !!!emit ($self->{current_token}); # DOCTYPE
1762    
1763            redo A;
1764          } else {
1765            !!!cp (164);
1766            $self->{current_token}->{name}
1767              .= chr ($self->{next_char}); # DOCTYPE
1768          ## Stay in the state          ## Stay in the state
1769          !!!next-input-character;          !!!next-input-character;
1770          redo A;          redo A;
1771        } elsif ($self->{next_input_character} == -1) {        }
1772        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1773          if ($self->{next_char} == 0x0009 or # HT
1774              $self->{next_char} == 0x000A or # LF
1775              $self->{next_char} == 0x000B or # VT
1776              $self->{next_char} == 0x000C or # FF
1777              $self->{next_char} == 0x0020) { # SP
1778            !!!cp (165);
1779            ## Stay in the state
1780            !!!next-input-character;
1781            redo A;
1782          } elsif ($self->{next_char} == 0x003E) { # >
1783            !!!cp (166);
1784            $self->{state} = DATA_STATE;
1785            !!!next-input-character;
1786    
1787            !!!emit ($self->{current_token}); # DOCTYPE
1788    
1789            redo A;
1790          } elsif ($self->{next_char} == -1) {
1791            !!!cp (167);
1792          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1793          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
1794          ## reconsume          ## reconsume
1795    
1796          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
1797          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1798    
1799          redo A;          redo A;
1800          } elsif ($self->{next_char} == 0x0050 or # P
1801                   $self->{next_char} == 0x0070) { # p
1802            !!!next-input-character;
1803            if ($self->{next_char} == 0x0055 or # U
1804                $self->{next_char} == 0x0075) { # u
1805              !!!next-input-character;
1806              if ($self->{next_char} == 0x0042 or # B
1807                  $self->{next_char} == 0x0062) { # b
1808                !!!next-input-character;
1809                if ($self->{next_char} == 0x004C or # L
1810                    $self->{next_char} == 0x006C) { # l
1811                  !!!next-input-character;
1812                  if ($self->{next_char} == 0x0049 or # I
1813                      $self->{next_char} == 0x0069) { # i
1814                    !!!next-input-character;
1815                    if ($self->{next_char} == 0x0043 or # C
1816                        $self->{next_char} == 0x0063) { # c
1817                      !!!cp (168);
1818                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1819                      !!!next-input-character;
1820                      redo A;
1821                    } else {
1822                      !!!cp (169);
1823                    }
1824                  } else {
1825                    !!!cp (170);
1826                  }
1827                } else {
1828                  !!!cp (171);
1829                }
1830              } else {
1831                !!!cp (172);
1832              }
1833            } else {
1834              !!!cp (173);
1835            }
1836    
1837            #
1838          } elsif ($self->{next_char} == 0x0053 or # S
1839                   $self->{next_char} == 0x0073) { # s
1840            !!!next-input-character;
1841            if ($self->{next_char} == 0x0059 or # Y
1842                $self->{next_char} == 0x0079) { # y
1843              !!!next-input-character;
1844              if ($self->{next_char} == 0x0053 or # S
1845                  $self->{next_char} == 0x0073) { # s
1846                !!!next-input-character;
1847                if ($self->{next_char} == 0x0054 or # T
1848                    $self->{next_char} == 0x0074) { # t
1849                  !!!next-input-character;
1850                  if ($self->{next_char} == 0x0045 or # E
1851                      $self->{next_char} == 0x0065) { # e
1852                    !!!next-input-character;
1853                    if ($self->{next_char} == 0x004D or # M
1854                        $self->{next_char} == 0x006D) { # m
1855                      !!!cp (174);
1856                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1857                      !!!next-input-character;
1858                      redo A;
1859                    } else {
1860                      !!!cp (175);
1861                    }
1862                  } else {
1863                    !!!cp (176);
1864                  }
1865                } else {
1866                  !!!cp (177);
1867                }
1868              } else {
1869                !!!cp (178);
1870              }
1871            } else {
1872              !!!cp (179);
1873            }
1874    
1875            #
1876        } else {        } else {
1877          $self->{current_token}->{name}          !!!cp (180);
1878            .= chr ($self->{next_input_character}); # DOCTYPE          !!!next-input-character;
1879          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          #
1880          }
1881    
1882          !!!parse-error (type => 'string after DOCTYPE name');
1883          $self->{current_token}->{quirks} = 1;
1884    
1885          $self->{state} = BOGUS_DOCTYPE_STATE;
1886          # next-input-character is already done
1887          redo A;
1888        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1889          if ({
1890                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1891                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1892              }->{$self->{next_char}}) {
1893            !!!cp (181);
1894            ## Stay in the state
1895            !!!next-input-character;
1896            redo A;
1897          } elsif ($self->{next_char} eq 0x0022) { # "
1898            !!!cp (182);
1899            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1900            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1901            !!!next-input-character;
1902            redo A;
1903          } elsif ($self->{next_char} eq 0x0027) { # '
1904            !!!cp (183);
1905            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1906            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1907            !!!next-input-character;
1908            redo A;
1909          } elsif ($self->{next_char} eq 0x003E) { # >
1910            !!!cp (184);
1911            !!!parse-error (type => 'no PUBLIC literal');
1912    
1913            $self->{state} = DATA_STATE;
1914            !!!next-input-character;
1915    
1916            $self->{current_token}->{quirks} = 1;
1917            !!!emit ($self->{current_token}); # DOCTYPE
1918    
1919            redo A;
1920          } elsif ($self->{next_char} == -1) {
1921            !!!cp (185);
1922            !!!parse-error (type => 'unclosed DOCTYPE');
1923    
1924            $self->{state} = DATA_STATE;
1925            ## reconsume
1926    
1927            $self->{current_token}->{quirks} = 1;
1928            !!!emit ($self->{current_token}); # DOCTYPE
1929    
1930            redo A;
1931          } else {
1932            !!!cp (186);
1933            !!!parse-error (type => 'string after PUBLIC');
1934            $self->{current_token}->{quirks} = 1;
1935    
1936            $self->{state} = BOGUS_DOCTYPE_STATE;
1937            !!!next-input-character;
1938            redo A;
1939          }
1940        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1941          if ($self->{next_char} == 0x0022) { # "
1942            !!!cp (187);
1943            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1944            !!!next-input-character;
1945            redo A;
1946          } elsif ($self->{next_char} == 0x003E) { # >
1947            !!!cp (188);
1948            !!!parse-error (type => 'unclosed PUBLIC literal');
1949    
1950            $self->{state} = DATA_STATE;
1951            !!!next-input-character;
1952    
1953            $self->{current_token}->{quirks} = 1;
1954            !!!emit ($self->{current_token}); # DOCTYPE
1955    
1956            redo A;
1957          } elsif ($self->{next_char} == -1) {
1958            !!!cp (189);
1959            !!!parse-error (type => 'unclosed PUBLIC literal');
1960    
1961            $self->{state} = DATA_STATE;
1962            ## reconsume
1963    
1964            $self->{current_token}->{quirks} = 1;
1965            !!!emit ($self->{current_token}); # DOCTYPE
1966    
1967            redo A;
1968          } else {
1969            !!!cp (190);
1970            $self->{current_token}->{public_identifier} # DOCTYPE
1971                .= chr $self->{next_char};
1972            ## Stay in the state
1973            !!!next-input-character;
1974            redo A;
1975          }
1976        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1977          if ($self->{next_char} == 0x0027) { # '
1978            !!!cp (191);
1979            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1980            !!!next-input-character;
1981            redo A;
1982          } elsif ($self->{next_char} == 0x003E) { # >
1983            !!!cp (192);
1984            !!!parse-error (type => 'unclosed PUBLIC literal');
1985    
1986            $self->{state} = DATA_STATE;
1987            !!!next-input-character;
1988    
1989            $self->{current_token}->{quirks} = 1;
1990            !!!emit ($self->{current_token}); # DOCTYPE
1991    
1992            redo A;
1993          } elsif ($self->{next_char} == -1) {
1994            !!!cp (193);
1995            !!!parse-error (type => 'unclosed PUBLIC literal');
1996    
1997            $self->{state} = DATA_STATE;
1998            ## reconsume
1999    
2000            $self->{current_token}->{quirks} = 1;
2001            !!!emit ($self->{current_token}); # DOCTYPE
2002    
2003            redo A;
2004          } else {
2005            !!!cp (194);
2006            $self->{current_token}->{public_identifier} # DOCTYPE
2007                .= chr $self->{next_char};
2008            ## Stay in the state
2009            !!!next-input-character;
2010            redo A;
2011          }
2012        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2013          if ({
2014                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2015                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2016              }->{$self->{next_char}}) {
2017            !!!cp (195);
2018            ## Stay in the state
2019            !!!next-input-character;
2020            redo A;
2021          } elsif ($self->{next_char} == 0x0022) { # "
2022            !!!cp (196);
2023            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2024            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2025            !!!next-input-character;
2026            redo A;
2027          } elsif ($self->{next_char} == 0x0027) { # '
2028            !!!cp (197);
2029            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2030            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2031            !!!next-input-character;
2032            redo A;
2033          } elsif ($self->{next_char} == 0x003E) { # >
2034            !!!cp (198);
2035            $self->{state} = DATA_STATE;
2036            !!!next-input-character;
2037    
2038            !!!emit ($self->{current_token}); # DOCTYPE
2039    
2040            redo A;
2041          } elsif ($self->{next_char} == -1) {
2042            !!!cp (199);
2043            !!!parse-error (type => 'unclosed DOCTYPE');
2044    
2045            $self->{state} = DATA_STATE;
2046            ## reconsume
2047    
2048            $self->{current_token}->{quirks} = 1;
2049            !!!emit ($self->{current_token}); # DOCTYPE
2050    
2051            redo A;
2052          } else {
2053            !!!cp (200);
2054            !!!parse-error (type => 'string after PUBLIC literal');
2055            $self->{current_token}->{quirks} = 1;
2056    
2057            $self->{state} = BOGUS_DOCTYPE_STATE;
2058            !!!next-input-character;
2059            redo A;
2060          }
2061        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2062          if ({
2063                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2064                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2065              }->{$self->{next_char}}) {
2066            !!!cp (201);
2067            ## Stay in the state
2068            !!!next-input-character;
2069            redo A;
2070          } elsif ($self->{next_char} == 0x0022) { # "
2071            !!!cp (202);
2072            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2073            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2074            !!!next-input-character;
2075            redo A;
2076          } elsif ($self->{next_char} == 0x0027) { # '
2077            !!!cp (203);
2078            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2079            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2080            !!!next-input-character;
2081            redo A;
2082          } elsif ($self->{next_char} == 0x003E) { # >
2083            !!!cp (204);
2084            !!!parse-error (type => 'no SYSTEM literal');
2085            $self->{state} = DATA_STATE;
2086            !!!next-input-character;
2087    
2088            $self->{current_token}->{quirks} = 1;
2089            !!!emit ($self->{current_token}); # DOCTYPE
2090    
2091            redo A;
2092          } elsif ($self->{next_char} == -1) {
2093            !!!cp (205);
2094            !!!parse-error (type => 'unclosed DOCTYPE');
2095    
2096            $self->{state} = DATA_STATE;
2097            ## reconsume
2098    
2099            $self->{current_token}->{quirks} = 1;
2100            !!!emit ($self->{current_token}); # DOCTYPE
2101    
2102            redo A;
2103          } else {
2104            !!!cp (206);
2105            !!!parse-error (type => 'string after SYSTEM');
2106            $self->{current_token}->{quirks} = 1;
2107    
2108            $self->{state} = BOGUS_DOCTYPE_STATE;
2109            !!!next-input-character;
2110            redo A;
2111          }
2112        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2113          if ($self->{next_char} == 0x0022) { # "
2114            !!!cp (207);
2115            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2116            !!!next-input-character;
2117            redo A;
2118          } elsif ($self->{next_char} == 0x003E) { # >
2119            !!!cp (208);
2120            !!!parse-error (type => 'unclosed PUBLIC literal');
2121    
2122            $self->{state} = DATA_STATE;
2123            !!!next-input-character;
2124    
2125            $self->{current_token}->{quirks} = 1;
2126            !!!emit ($self->{current_token}); # DOCTYPE
2127    
2128            redo A;
2129          } elsif ($self->{next_char} == -1) {
2130            !!!cp (209);
2131            !!!parse-error (type => 'unclosed SYSTEM literal');
2132    
2133            $self->{state} = DATA_STATE;
2134            ## reconsume
2135    
2136            $self->{current_token}->{quirks} = 1;
2137            !!!emit ($self->{current_token}); # DOCTYPE
2138    
2139            redo A;
2140          } else {
2141            !!!cp (210);
2142            $self->{current_token}->{system_identifier} # DOCTYPE
2143                .= chr $self->{next_char};
2144          ## Stay in the state          ## Stay in the state
2145          !!!next-input-character;          !!!next-input-character;
2146          redo A;          redo A;
2147        }        }
2148      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2149        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0027) { # '
2150            $self->{next_input_character} == 0x000A or # LF          !!!cp (211);
2151            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2152            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2153            $self->{next_input_character} == 0x0020) { # SP          redo A;
2154          } elsif ($self->{next_char} == 0x003E) { # >
2155            !!!cp (212);
2156            !!!parse-error (type => 'unclosed PUBLIC literal');
2157    
2158            $self->{state} = DATA_STATE;
2159            !!!next-input-character;
2160    
2161            $self->{current_token}->{quirks} = 1;
2162            !!!emit ($self->{current_token}); # DOCTYPE
2163    
2164            redo A;
2165          } elsif ($self->{next_char} == -1) {
2166            !!!cp (213);
2167            !!!parse-error (type => 'unclosed SYSTEM literal');
2168    
2169            $self->{state} = DATA_STATE;
2170            ## reconsume
2171    
2172            $self->{current_token}->{quirks} = 1;
2173            !!!emit ($self->{current_token}); # DOCTYPE
2174    
2175            redo A;
2176          } else {
2177            !!!cp (214);
2178            $self->{current_token}->{system_identifier} # DOCTYPE
2179                .= chr $self->{next_char};
2180            ## Stay in the state
2181            !!!next-input-character;
2182            redo A;
2183          }
2184        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2185          if ({
2186                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2187                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2188              }->{$self->{next_char}}) {
2189            !!!cp (215);
2190          ## Stay in the state          ## Stay in the state
2191          !!!next-input-character;          !!!next-input-character;
2192          redo A;          redo A;
2193        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2194          $self->{state} = 'data';          !!!cp (216);
2195            $self->{state} = DATA_STATE;
2196          !!!next-input-character;          !!!next-input-character;
2197    
2198          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2199    
2200          redo A;          redo A;
2201        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2202            !!!cp (217);
2203          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2204          $self->{state} = 'data';  
2205            $self->{state} = DATA_STATE;
2206          ## reconsume          ## reconsume
2207    
2208            $self->{current_token}->{quirks} = 1;
2209          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2210    
2211          redo A;          redo A;
2212        } else {        } else {
2213          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (218);
2214          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after SYSTEM literal');
2215          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2216    
2217            $self->{state} = BOGUS_DOCTYPE_STATE;
2218          !!!next-input-character;          !!!next-input-character;
2219          redo A;          redo A;
2220        }        }
2221      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2222        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2223          $self->{state} = 'data';          !!!cp (219);
2224            $self->{state} = DATA_STATE;
2225          !!!next-input-character;          !!!next-input-character;
2226    
2227          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2228    
2229          redo A;          redo A;
2230        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2231            !!!cp (220);
2232          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2233          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2234          ## reconsume          ## reconsume
2235    
2236          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2237    
2238          redo A;          redo A;
2239        } else {        } else {
2240            !!!cp (221);
2241          ## Stay in the state          ## Stay in the state
2242          !!!next-input-character;          !!!next-input-character;
2243          redo A;          redo A;
# Line 1490  sub _get_next_token ($) { Line 2250  sub _get_next_token ($) {
2250    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2251  } # _get_next_token  } # _get_next_token
2252    
2253  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2254    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2255      
2256    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2257    
2258      if ({
2259           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2260           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2261           $additional => 1,
2262          }->{$self->{next_char}}) {
2263        !!!cp (1001);
2264        ## Don't consume
2265        ## No error
2266        return undef;
2267      } elsif ($self->{next_char} == 0x0023) { # #
2268      !!!next-input-character;      !!!next-input-character;
2269      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2270          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2271        my $num;        my $code;
2272        X: {        X: {
2273          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2274          !!!next-input-character;          !!!next-input-character;
2275          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2276              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2277            $num ||= 0;            !!!cp (1002);
2278            $num *= 0x10;            $code ||= 0;
2279            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2280              $code += $self->{next_char} - 0x0030;
2281            redo X;            redo X;
2282          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2283                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2284            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2285            $num ||= 0;            $code ||= 0;
2286            $num *= 0x10;            $code *= 0x10;
2287            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2288            redo X;            redo X;
2289          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2290                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2291            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2292            $num ||= 0;            $code ||= 0;
2293            $num *= 0x10;            $code *= 0x10;
2294            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2295            redo X;            redo X;
2296          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2297            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2298            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2299            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2300              $self->{next_char} = 0x0023; # #
2301            return undef;            return undef;
2302          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2303              !!!cp (1006);
2304            !!!next-input-character;            !!!next-input-character;
2305          } else {          } else {
2306            !!!parse-error (type => 'no refc');            !!!cp (1007);
2307              !!!parse-error (type => 'no refc', line => $l, column => $c);
2308          }          }
2309    
2310          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2311          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!cp (1008);
2312          if ($num > 1114111 or $num == 0) {            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2313            $num = 0xFFFD; # REPLACEMENT CHARACTER            $code = 0xFFFD;
2314            ## ISSUE: Why this is not an error?          } elsif ($code > 0x10FFFF) {
2315          } elsif (0x80 <= $num and $num <= 0x9F) {            !!!cp (1009);
2316            ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2317            ## ISSUE: Not in the spec yet; parse error?            $code = 0xFFFD;
2318            $num = $c1_entity_char->{$num};          } elsif ($code == 0x000D) {
2319              !!!cp (1010);
2320              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2321              $code = 0x000A;
2322            } elsif (0x80 <= $code and $code <= 0x9F) {
2323              !!!cp (1011);
2324              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2325              $code = $c1_entity_char->{$code};
2326          }          }
2327    
2328          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code,
2329                    has_reference => 1, line => $l, column => $c};
2330        } # X        } # X
2331      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2332               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2333        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2334        !!!next-input-character;        !!!next-input-character;
2335                
2336        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2337                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2338            !!!cp (1012);
2339          $code *= 10;          $code *= 10;
2340          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2341                    
2342          !!!next-input-character;          !!!next-input-character;
2343        }        }
2344    
2345        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2346            !!!cp (1013);
2347          !!!next-input-character;          !!!next-input-character;
2348        } else {        } else {
2349          !!!parse-error (type => 'no refc');          !!!cp (1014);
2350            !!!parse-error (type => 'no refc', line => $l, column => $c);
2351        }        }
2352    
2353        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2354        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
2355          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2356          ## ISSUE: Why this is not an error?          $code = 0xFFFD;
2357          } elsif ($code > 0x10FFFF) {
2358            !!!cp (1016);
2359            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2360            $code = 0xFFFD;
2361          } elsif ($code == 0x000D) {
2362            !!!cp (1017);
2363            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2364            $code = 0x000A;
2365        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2366          ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>          !!!cp (1018);
2367          ## ISSUE: Not in the spec yet; parse error?          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2368          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2369        }        }
2370                
2371        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2372                  line => $l, column => $c};
2373      } else {      } else {
2374        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2375        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2376        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2377          $self->{next_char} = 0x0023; # #
2378        return undef;        return undef;
2379      }      }
2380    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2381              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2382             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2383              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2384      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2385      !!!next-input-character;      !!!next-input-character;
2386    
2387      my $value = $entity_name;      my $value = $entity_name;
2388      my $match;      my $match = 0;
2389        require Whatpm::_NamedEntityList;
2390        our $EntityChar;
2391    
2392      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2393             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2394             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
2395               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
2396              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
2397               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
2398              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
2399               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
2400        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
2401        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
2402          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
2403          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
2404              !!!cp (1020);
2405              $value = $EntityChar->{$entity_name};
2406              $match = 1;
2407              !!!next-input-character;
2408              last;
2409            } else {
2410              !!!cp (1021);
2411              $value = $EntityChar->{$entity_name};
2412              $match = -1;
2413              !!!next-input-character;
2414            }
2415        } else {        } else {
2416          $value .= chr $self->{next_input_character};          !!!cp (1022);
2417            $value .= chr $self->{next_char};
2418            $match *= 2;
2419            !!!next-input-character;
2420        }        }
       !!!next-input-character;  
2421      }      }
2422            
2423      if ($match) {      if ($match > 0) {
2424        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
2425          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2426                  line => $l, column => $c};
2427        } elsif ($match < 0) {
2428          !!!parse-error (type => 'no refc', line => $l, column => $c);
2429          if ($in_attr and $match < -1) {
2430            !!!cp (1024);
2431            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2432                    line => $l, column => $c};
2433        } else {        } else {
2434          !!!parse-error (type => 'refc');          !!!cp (1025);
2435            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2436                    line => $l, column => $c};
2437        }        }
   
       return {type => 'character', data => $value};  
2438      } else {      } else {
2439        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2440        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2441        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
2442        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
2443                  line => $l, column => $c};
2444      }      }
2445    } else {    } else {
2446        !!!cp (1027);
2447      ## no characters are consumed      ## no characters are consumed
2448      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2449      return undef;      return undef;
2450    }    }
2451  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1636  sub _initialize_tree_constructor ($) { Line 2456  sub _initialize_tree_constructor ($) {
2456    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
2457    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
2458    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
2459    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
2460  } # _initialize_tree_constructor  } # _initialize_tree_constructor
2461    
2462  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1663  sub _construct_tree ($) { Line 2483  sub _construct_tree ($) {
2483        
2484    !!!next-token;    !!!next-token;
2485    
   $self->{insertion_mode} = 'before head';  
2486    undef $self->{form_element};    undef $self->{form_element};
2487    undef $self->{head_element};    undef $self->{head_element};
2488    $self->{open_elements} = [];    $self->{open_elements} = [];
2489    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2490    
2491      ## NOTE: The "initial" insertion mode.
2492    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2493    
2494      ## NOTE: The "before html" insertion mode.
2495    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2496      $self->{insertion_mode} = BEFORE_HEAD_IM;
2497    
2498      ## NOTE: The "before head" insertion mode and so on.
2499    $self->_tree_construction_main;    $self->_tree_construction_main;
2500  } # _construct_tree  } # _construct_tree
2501    
2502  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2503    my $self = shift;    my $self = shift;
2504    B: {  
2505        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
2506          if ($token->{error}) {  
2507            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
2508            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
2509          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2510          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
2511            ($token->{name});        ## language.
2512          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
2513          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
2514          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/;
2515          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
2516          return;            defined $token->{public_identifier} or
2517        } elsif ({            defined $token->{system_identifier}) {
2518                  comment => 1,          !!!cp ('t1');
2519                  'start tag' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
2520                  'end tag' => 1,        } elsif ($doctype_name ne 'HTML') {
2521                  'end-of-file' => 1,          !!!cp ('t2');
2522                 }->{$token->{type}}) {          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2523          ## ISSUE: Spec currently left this case undefined.          !!!parse-error (type => 'not HTML5', token => $token);
2524          !!!parse-error (type => 'missing DOCTYPE');        } else {
2525          #$phase = 'root element';          !!!cp ('t3');
2526          ## reprocess        }
2527          #redo B;        
2528          return;        my $doctype = $self->{document}->create_document_type_definition
2529        } elsif ($token->{type} eq 'character') {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2530          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        $doctype->public_id ($token->{public_identifier})
2531            $self->{document}->manakai_append_text ($1);            if defined $token->{public_identifier};
2532            ## ISSUE: DOM3 Core does not allow Document > Text        $doctype->system_id ($token->{system_identifier})
2533            unless (length $token->{data}) {            if defined $token->{system_identifier};
2534              ## Stay in the phase        ## NOTE: Other DocumentType attributes are null or empty lists.
2535              !!!next-token;        ## ISSUE: internalSubset = null??
2536              redo B;        $self->{document}->append_child ($doctype);
2537          
2538          if ($token->{quirks} or $doctype_name ne 'HTML') {
2539            !!!cp ('t4');
2540            $self->{document}->manakai_compat_mode ('quirks');
2541          } elsif (defined $token->{public_identifier}) {
2542            my $pubid = $token->{public_identifier};
2543            $pubid =~ tr/a-z/A-z/;
2544            if ({
2545              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
2546              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2547              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2548              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
2549              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
2550              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
2551              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
2552              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
2553              "-//IETF//DTD HTML 2.0//EN" => 1,
2554              "-//IETF//DTD HTML 2.1E//EN" => 1,
2555              "-//IETF//DTD HTML 3.0//EN" => 1,
2556              "-//IETF//DTD HTML 3.0//EN//" => 1,
2557              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
2558              "-//IETF//DTD HTML 3.2//EN" => 1,
2559              "-//IETF//DTD HTML 3//EN" => 1,
2560              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
2561              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
2562              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
2563              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
2564              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
2565              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
2566              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
2567              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
2568              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
2569              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
2570              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
2571              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
2572              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
2573              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
2574              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
2575              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
2576              "-//IETF//DTD HTML STRICT//EN" => 1,
2577              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
2578              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
2579              "-//IETF//DTD HTML//EN" => 1,
2580              "-//IETF//DTD HTML//EN//2.0" => 1,
2581              "-//IETF//DTD HTML//EN//3.0" => 1,
2582              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
2583              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
2584              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
2585              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
2586              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
2587              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
2588              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
2589              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
2590              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2591              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2592              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2593              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2594              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2595              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2596              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2597              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2598              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
2599              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
2600              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
2601              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
2602              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
2603              "-//W3C//DTD HTML 3.2//EN" => 1,
2604              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
2605              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
2606              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
2607              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
2608              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
2609              "-//W3C//DTD W3 HTML//EN" => 1,
2610              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
2611              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
2612              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
2613              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
2614              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
2615              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2616              "HTML" => 1,
2617            }->{$pubid}) {
2618              !!!cp ('t5');
2619              $self->{document}->manakai_compat_mode ('quirks');
2620            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2621                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2622              if (defined $token->{system_identifier}) {
2623                !!!cp ('t6');
2624                $self->{document}->manakai_compat_mode ('quirks');
2625              } else {
2626                !!!cp ('t7');
2627                $self->{document}->manakai_compat_mode ('limited quirks');
2628            }            }
2629            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2630                     $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2631              !!!cp ('t8');
2632              $self->{document}->manakai_compat_mode ('limited quirks');
2633            } else {
2634              !!!cp ('t9');
2635            }
2636          } else {
2637            !!!cp ('t10');
2638          }
2639          if (defined $token->{system_identifier}) {
2640            my $sysid = $token->{system_identifier};
2641            $sysid =~ tr/A-Z/a-z/;
2642            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2643              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2644              $self->{document}->manakai_compat_mode ('quirks');
2645              !!!cp ('t11');
2646            } else {
2647              !!!cp ('t12');
2648            }
2649          } else {
2650            !!!cp ('t13');
2651          }
2652          
2653          ## Go to the "before html" insertion mode.
2654          !!!next-token;
2655          return;
2656        } elsif ({
2657                  START_TAG_TOKEN, 1,
2658                  END_TAG_TOKEN, 1,
2659                  END_OF_FILE_TOKEN, 1,
2660                 }->{$token->{type}}) {
2661          !!!cp ('t14');
2662          !!!parse-error (type => 'no DOCTYPE', token => $token);
2663          $self->{document}->manakai_compat_mode ('quirks');
2664          ## Go to the "before html" insertion mode.
2665          ## reprocess
2666          return;
2667        } elsif ($token->{type} == CHARACTER_TOKEN) {
2668          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2669            ## Ignore the token
2670    
2671            unless (length $token->{data}) {
2672              !!!cp ('t15');
2673              ## Stay in the insertion mode.
2674              !!!next-token;
2675              redo INITIAL;
2676            } else {
2677              !!!cp ('t16');
2678          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
2679        } else {        } else {
2680          die "$0: $token->{type}: Unknown token";          !!!cp ('t17');
2681        }        }
2682      } # B  
2683          !!!parse-error (type => 'no DOCTYPE', token => $token);
2684          $self->{document}->manakai_compat_mode ('quirks');
2685          ## Go to the "before html" insertion mode.
2686          ## reprocess
2687          return;
2688        } elsif ($token->{type} == COMMENT_TOKEN) {
2689          !!!cp ('t18');
2690          my $comment = $self->{document}->create_comment ($token->{data});
2691          $self->{document}->append_child ($comment);
2692          
2693          ## Stay in the insertion mode.
2694          !!!next-token;
2695          redo INITIAL;
2696        } else {
2697          die "$0: $token->{type}: Unknown token type";
2698        }
2699      } # INITIAL
2700    
2701      die "$0: _tree_construction_initial: This should be never reached";
2702  } # _tree_construction_initial  } # _tree_construction_initial
2703    
2704  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2705    my $self = shift;    my $self = shift;
2706    
2707      ## NOTE: "before html" insertion mode.
2708        
2709    B: {    B: {
2710        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2711          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
2712            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2713          ## Ignore the token          ## Ignore the token
2714          ## Stay in the phase          ## Stay in the insertion mode.
2715          !!!next-token;          !!!next-token;
2716          redo B;          redo B;
2717        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2718            !!!cp ('t20');
2719          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2720          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2721          ## Stay in the phase          ## Stay in the insertion mode.
2722          !!!next-token;          !!!next-token;
2723          redo B;          redo B;
2724        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2725          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2726            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2727            ## ISSUE: DOM3 Core does not allow Document > Text  
2728            unless (length $token->{data}) {            unless (length $token->{data}) {
2729              ## Stay in the phase              !!!cp ('t21');
2730                ## Stay in the insertion mode.
2731              !!!next-token;              !!!next-token;
2732              redo B;              redo B;
2733              } else {
2734                !!!cp ('t22');
2735            }            }
2736            } else {
2737              !!!cp ('t23');
2738          }          }
2739    
2740            $self->{application_cache_selection}->(undef);
2741    
2742          #          #
2743          } elsif ($token->{type} == START_TAG_TOKEN) {
2744            if ($token->{tag_name} eq 'html') {
2745              my $root_element;
2746              !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});
2747              $self->{document}->append_child ($root_element);
2748              push @{$self->{open_elements}}, [$root_element, 'html'];
2749    
2750              if ($token->{attributes}->{manifest}) {
2751                !!!cp ('t24');
2752                $self->{application_cache_selection}
2753                    ->($token->{attributes}->{manifest}->{value});
2754                ## ISSUE: No relative reference resolution?
2755              } else {
2756                !!!cp ('t25');
2757                $self->{application_cache_selection}->(undef);
2758              }
2759    
2760              !!!next-token;
2761              return; ## Go to the "before head" insertion mode.
2762            } else {
2763              !!!cp ('t25.1');
2764              #
2765            }
2766        } elsif ({        } elsif ({
2767                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2768                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2769                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2770          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
2771          #          #
2772        } else {        } else {
2773          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2774        }        }
2775        my $root_element; !!!create-element ($root_element, 'html');  
2776        $self->{document}->append_child ($root_element);      my $root_element; !!!create-element ($root_element, 'html');
2777        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
2778        #$phase = 'main';      push @{$self->{open_elements}}, [$root_element, 'html'];
2779        ## reprocess  
2780        #redo B;      $self->{application_cache_selection}->(undef);
2781        return;  
2782        ## NOTE: Reprocess the token.
2783        return; ## Go to the "before head" insertion mode.
2784    
2785        ## ISSUE: There is an issue in the spec
2786    } # B    } # B
2787    
2788      die "$0: _tree_construction_root_element: This should never be reached";
2789  } # _tree_construction_root_element  } # _tree_construction_root_element
2790    
2791  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1782  sub _reset_insertion_mode ($) { Line 2800  sub _reset_insertion_mode ($) {
2800            
2801      ## Step 3      ## Step 3
2802      S3: {      S3: {
2803        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2804        if (defined $self->{inner_html_node}) {          $last = 1;
2805          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
2806              $self->{inner_html_node}->[1] eq 'th') {            if ($self->{inner_html_node}->[1] eq 'td' or
2807            #                $self->{inner_html_node}->[1] eq 'th') {
2808          } else {              !!!cp ('t27');
2809            $node = $self->{inner_html_node};              #
2810              } else {
2811                !!!cp ('t28');
2812                $node = $self->{inner_html_node};
2813              }
2814          }          }
2815        }        }
2816            
2817        ## Step 4..13        ## Step 4..13
2818        my $new_mode = {        my $new_mode = {
2819                        select => 'in select',                        select => IN_SELECT_IM,
2820                        td => 'in cell',                        ## NOTE: |option| and |optgroup| do not set
2821                        th => 'in cell',                        ## insertion mode to "in select" by themselves.
2822                        tr => 'in row',                        td => IN_CELL_IM,
2823                        tbody => 'in table body',                        th => IN_CELL_IM,
2824                        thead => 'in table head',                        tr => IN_ROW_IM,
2825                        tfoot => 'in table foot',                        tbody => IN_TABLE_BODY_IM,
2826                        caption => 'in caption',                        thead => IN_TABLE_BODY_IM,
2827                        colgroup => 'in column group',                        tfoot => IN_TABLE_BODY_IM,
2828                        table => 'in table',                        caption => IN_CAPTION_IM,
2829                        head => 'in body', # not in head!                        colgroup => IN_COLUMN_GROUP_IM,
2830                        body => 'in body',                        table => IN_TABLE_IM,
2831                        frameset => 'in frameset',                        head => IN_BODY_IM, # not in head!
2832                          body => IN_BODY_IM,
2833                          frameset => IN_FRAMESET_IM,
2834                       }->{$node->[1]};                       }->{$node->[1]};
2835        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2836                
2837        ## Step 14        ## Step 14
2838        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2839          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2840            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
2841              $self->{insertion_mode} = BEFORE_HEAD_IM;
2842          } else {          } else {
2843            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
2844              !!!cp ('t30');
2845              $self->{insertion_mode} = AFTER_HEAD_IM;
2846          }          }
2847          return;          return;
2848          } else {
2849            !!!cp ('t31');
2850        }        }
2851                
2852        ## Step 15        ## Step 15
2853        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2854                
2855        ## Step 16        ## Step 16
2856        $i--;        $i--;
# Line 1830  sub _reset_insertion_mode ($) { Line 2859  sub _reset_insertion_mode ($) {
2859        ## Step 17        ## Step 17
2860        redo S3;        redo S3;
2861      } # S3      } # S3
2862    
2863      die "$0: _reset_insertion_mode: This line should never be reached";
2864  } # _reset_insertion_mode  } # _reset_insertion_mode
2865    
2866  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2867    my $self = shift;    my $self = shift;
2868    
   my $phase = 'main';  
   
2869    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2870    
2871    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1853  sub _tree_construction_main ($) { Line 2882  sub _tree_construction_main ($) {
2882      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2883      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2884        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2885            !!!cp ('t32');
2886          return;          return;
2887        }        }
2888      }      }
# Line 1867  sub _tree_construction_main ($) { Line 2897  sub _tree_construction_main ($) {
2897    
2898        ## Step 6        ## Step 6
2899        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2900            !!!cp ('t33_1');
2901          #          #
2902        } else {        } else {
2903          my $in_open_elements;          my $in_open_elements;
2904          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2905            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2906                !!!cp ('t33');
2907              $in_open_elements = 1;              $in_open_elements = 1;
2908              last OE;              last OE;
2909            }            }
2910          }          }
2911          if ($in_open_elements) {          if ($in_open_elements) {
2912              !!!cp ('t34');
2913            #            #
2914          } else {          } else {
2915              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2916              !!!cp ('t35');
2917            redo S4;            redo S4;
2918          }          }
2919        }        }
# Line 1901  sub _tree_construction_main ($) { Line 2936  sub _tree_construction_main ($) {
2936    
2937        ## Step 11        ## Step 11
2938        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2939            !!!cp ('t36');
2940          ## Step 7'          ## Step 7'
2941          $i++;          $i++;
2942          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2943                    
2944          redo S7;          redo S7;
2945        }        }
2946    
2947          !!!cp ('t37');
2948      } # S7      } # S7
2949    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2950    
2951    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2952      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2953        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2954            !!!cp ('t38');
2955          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2956          return;          return;
2957        }        }
2958      }      }
2959    
2960        !!!cp ('t39');
2961    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2962    
2963    my $style_start_tag = sub {    my $insert;
2964      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
2965      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
2966      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
2967       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
2968        ->append_child ($style_el);      ## Step 1
2969      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
2970                      my $el;
2971        !!!create-element ($el, $start_tag_name, $token->{attributes});
2972    
2973        ## Step 2
2974        $insert->($el);
2975    
2976        ## Step 3
2977        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2978        delete $self->{escape}; # MUST
2979    
2980        ## Step 4
2981      my $text = '';      my $text = '';
2982      !!!next-token;      !!!next-token;
2983      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2984          !!!cp ('t40');
2985        $text .= $token->{data};        $text .= $token->{data};
2986        !!!next-token;        !!!next-token;
2987      } # stop if non-character token or tokenizer stops tokenising      }
2988    
2989        ## Step 5
2990      if (length $text) {      if (length $text) {
2991        $style_el->manakai_append_text ($text);        !!!cp ('t41');
2992          my $text = $self->{document}->create_text_node ($text);
2993          $el->append_child ($text);
2994      }      }
2995        
2996      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2997                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2998      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2999        ## Step 7
3000        if ($token->{type} == END_TAG_TOKEN and
3001            $token->{tag_name} eq $start_tag_name) {
3002          !!!cp ('t42');
3003        ## Ignore the token        ## Ignore the token
3004      } else {      } else {
3005        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3006        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3007            !!!cp ('t43');
3008            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3009          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3010            !!!cp ('t44');
3011            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3012          } else {
3013            die "$0: $content_model_flag in parse_rcdata";
3014          }
3015      }      }
3016      !!!next-token;      !!!next-token;
3017    }; # $style_start_tag    }; # $parse_rcdata
3018    
3019    my $script_start_tag = sub {    my $script_start_tag = sub () {
3020      my $script_el;      my $script_el;
3021      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
3022      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3023    
3024      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3025        delete $self->{escape}; # MUST
3026            
3027      my $text = '';      my $text = '';
3028      !!!next-token;      !!!next-token;
3029      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3030          !!!cp ('t45');
3031        $text .= $token->{data};        $text .= $token->{data};
3032        !!!next-token;        !!!next-token;
3033      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3034      if (length $text) {      if (length $text) {
3035          !!!cp ('t46');
3036        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3037      }      }
3038                                
3039      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3040    
3041      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3042          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3043          !!!cp ('t47');
3044        ## Ignore the token        ## Ignore the token
3045      } else {      } else {
3046        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3047          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3048        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3049        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3050      }      }
3051            
3052      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3053          !!!cp ('t49');
3054        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3055      } else {      } else {
3056          !!!cp ('t50');
3057        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3058        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3059          
3060        (($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);  
3061                
3062        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3063                
# Line 1993  sub _tree_construction_main ($) { Line 3067  sub _tree_construction_main ($) {
3067      !!!next-token;      !!!next-token;
3068    }; # $script_start_tag    }; # $script_start_tag
3069    
3070      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3071      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3072      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3073    
3074    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3075      my $tag_name = shift;      my $end_tag_token = shift;
3076        my $tag_name = $end_tag_token->{tag_name};
3077    
3078        ## NOTE: The adoption agency algorithm (AAA).
3079    
3080      FET: {      FET: {
3081        ## Step 1        ## Step 1
# Line 2002  sub _tree_construction_main ($) { Line 3083  sub _tree_construction_main ($) {
3083        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3084        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3085          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3086              !!!cp ('t51');
3087            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3088            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3089            last AFE;            last AFE;
3090          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3091              !!!cp ('t52');
3092            last AFE;            last AFE;
3093          }          }
3094        } # AFE        } # AFE
3095        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3096          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3097            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3098          ## Ignore the token          ## Ignore the token
3099          !!!next-token;          !!!next-token;
3100          return;          return;
# Line 2022  sub _tree_construction_main ($) { Line 3106  sub _tree_construction_main ($) {
3106          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3107          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3108            if ($in_scope) {            if ($in_scope) {
3109                !!!cp ('t54');
3110              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3111              last INSCOPE;              last INSCOPE;
3112            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3113              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3114                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3115                                token => $end_tag_token);
3116              ## Ignore the token              ## Ignore the token
3117              !!!next-token;              !!!next-token;
3118              return;              return;
3119            }            }
3120          } elsif ({          } elsif ({
3121                    table => 1, caption => 1, td => 1, th => 1,                    applet => 1, table => 1, caption => 1, td => 1, th => 1,
3122                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3123                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3124              !!!cp ('t56');
3125            $in_scope = 0;            $in_scope = 0;
3126          }          }
3127        } # INSCOPE        } # INSCOPE
3128        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3129          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3130            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3131                            token => $end_tag_token);
3132          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3133          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3134          return;          return;
3135        }        }
3136        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3137          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3138            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],
3139                            token => $end_tag_token);
3140        }        }
3141                
3142        ## Step 2        ## Step 2
# Line 2055  sub _tree_construction_main ($) { Line 3147  sub _tree_construction_main ($) {
3147          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
3148              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3149              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3150               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe
3151              !!!cp ('t59');
3152            $furthest_block = $node;            $furthest_block = $node;
3153            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3154          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3155              !!!cp ('t60');
3156            last OE;            last OE;
3157          }          }
3158        } # OE        } # OE
3159                
3160        ## Step 3        ## Step 3
3161        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3162            !!!cp ('t61');
3163          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3164          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3165          !!!next-token;          !!!next-token;
# Line 2077  sub _tree_construction_main ($) { Line 3172  sub _tree_construction_main ($) {
3172        ## Step 5        ## Step 5
3173        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3174        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3175            !!!cp ('t62');
3176          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3177        }        }
3178                
# Line 2099  sub _tree_construction_main ($) { Line 3195  sub _tree_construction_main ($) {
3195          S7S2: {          S7S2: {
3196            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3197              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3198                  !!!cp ('t63');
3199                $node_i_in_active = $_;                $node_i_in_active = $_;
3200                last S7S2;                last S7S2;
3201              }              }
# Line 2112  sub _tree_construction_main ($) { Line 3209  sub _tree_construction_main ($) {
3209                    
3210          ## Step 4          ## Step 4
3211          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3212              !!!cp ('t64');
3213            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3214          }          }
3215                    
3216          ## Step 5          ## Step 5
3217          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3218              !!!cp ('t65');
3219            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3220            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3221            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2134  sub _tree_construction_main ($) { Line 3233  sub _tree_construction_main ($) {
3233        } # S7          } # S7  
3234                
3235        ## Step 8        ## Step 8
3236        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ({
3237               table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3238              }->{$common_ancestor_node->[1]}) {
3239            my $foster_parent_element;
3240            my $next_sibling;
3241                             OE: for (reverse 0..$#{$self->{open_elements}}) {
3242                               if ($self->{open_elements}->[$_]->[1] eq 'table') {
3243                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3244                                 if (defined $parent and $parent->node_type == 1) {
3245                                   !!!cp ('t65.1');
3246                                   $foster_parent_element = $parent;
3247                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3248                                 } else {
3249                                   !!!cp ('t65.2');
3250                                   $foster_parent_element
3251                                     = $self->{open_elements}->[$_ - 1]->[0];
3252                                 }
3253                                 last OE;
3254                               }
3255                             } # OE
3256                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3257                               unless defined $foster_parent_element;
3258            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3259            $open_tables->[-1]->[1] = 1; # tainted
3260          } else {
3261            !!!cp ('t65.3');
3262            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3263          }
3264                
3265        ## Step 9        ## Step 9
3266        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2151  sub _tree_construction_main ($) { Line 3277  sub _tree_construction_main ($) {
3277        my $i;        my $i;
3278        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3279          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3280              !!!cp ('t66');
3281            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3282            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3283          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3284              !!!cp ('t67');
3285            $i = $_;            $i = $_;
3286          }          }
3287        } # AFE        } # AFE
# Line 2163  sub _tree_construction_main ($) { Line 3291  sub _tree_construction_main ($) {
3291        undef $i;        undef $i;
3292        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3293          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3294              !!!cp ('t68');
3295            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3296            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3297          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3298              !!!cp ('t69');
3299            $i = $_;            $i = $_;
3300          }          }
3301        } # OE        } # OE
# Line 2176  sub _tree_construction_main ($) { Line 3306  sub _tree_construction_main ($) {
3306      } # FET      } # FET
3307    }; # $formatting_end_tag    }; # $formatting_end_tag
3308    
3309    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3310      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3311    }; # $insert_to_current    }; # $insert_to_current
3312    
3313    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3314                         my $child = shift;      my $child = shift;
3315                         if ({      if ({
3316                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3317                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3318                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3319                           # MUST        my $foster_parent_element;
3320                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3321                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3322                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3323                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3324                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3325                                   !!!cp ('t70');
3326                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3327                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3328                               } else {                               } else {
3329                                   !!!cp ('t71');
3330                                 $foster_parent_element                                 $foster_parent_element
3331                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3332                               }                               }
# Line 2206  sub _tree_construction_main ($) { Line 3337  sub _tree_construction_main ($) {
3337                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3338                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3339                             ($child, $next_sibling);                             ($child, $next_sibling);
3340                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3341                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3342                         }        !!!cp ('t72');
3343          $self->{open_elements}->[-1]->[0]->append_child ($child);
3344        }
3345    }; # $insert_to_foster    }; # $insert_to_foster
3346    
3347    my $in_body = sub {    B: {
3348      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3349      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3350        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3351          $script_start_tag->();        ## Ignore the token
3352          return;        ## Stay in the phase
3353        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
3354          $style_start_tag->();        redo B;
3355          return;      } elsif ($token->{type} == START_TAG_TOKEN and
3356        } elsif ({               $token->{tag_name} eq 'html') {
3357                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3358                 }->{$token->{tag_name}}) {          !!!cp ('t79');
3359          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html:html', token => $token);
3360          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
3361          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3362          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3363          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html:html', token => $token);
3364            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3365          } else {        } else {
3366            $insert->($el);          !!!cp ('t81');
3367          }        }
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: There is an "as if in head" code clone  
         my $title_el;  
         !!!create-element ($title_el, 'title', $token->{attributes});  
         (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
           ->append_child ($title_el);  
         $self->{content_model_flag} = 'RCDATA';  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
         }  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
3368    
3369          !!!next-token;        !!!cp ('t82');
3370          return;        !!!parse-error (type => 'not first start tag', token => $token);
3371        } elsif ($token->{tag_name} eq 'marquee' or        my $top_el = $self->{open_elements}->[0]->[0];
3372                 $token->{tag_name} eq 'object') {        for my $attr_name (keys %{$token->{attributes}}) {
3373          $reconstruct_active_formatting_elements->($insert_to_current);          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3374                      !!!cp ('t84');
3375          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            $top_el->set_attribute_ns
3376          push @$active_formatting_elements, ['#marker', ''];              (undef, [undef, $attr_name],
3377                         $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';  
           
         !!!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;  
3378          }          }
3379        } elsif ({        }
3380                  textarea => 1,        !!!next-token;
3381                  iframe => 1,        redo B;
3382                  noembed => 1,      } elsif ($token->{type} == COMMENT_TOKEN) {
3383                  noframes => 1,        my $comment = $self->{document}->create_comment ($token->{data});
3384                  noscript => 0, ## TODO: 1 if scripting is enabled        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3385                 }->{$token->{tag_name}}) {          !!!cp ('t85');
3386          my $tag_name = $token->{tag_name};          $self->{document}->append_child ($comment);
3387          my $el;        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3388          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t86');
3389                    $self->{open_elements}->[0]->[0]->append_child ($comment);
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
3390        } else {        } else {
3391          $reconstruct_active_formatting_elements->($insert_to_current);          !!!cp ('t87');
3392                    $self->{open_elements}->[-1]->[0]->append_child ($comment);
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
3393        }        }
3394      } elsif ($token->{type} eq 'end tag') {        !!!next-token;
3395        if ($token->{tag_name} eq 'body') {        redo B;
3396          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3397            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
3398            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3399              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3400            }              !!!cp ('t88.2');
3401            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3402            !!!next-token;            } else {
3403            return;              !!!cp ('t88.1');
3404          } else {              ## Ignore the token.
3405            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
3406            ## 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,  
                 form => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
3407            }            }
3408          } # INSCOPE            unless (length $token->{data}) {
3409                        !!!cp ('t88');
3410          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
3411            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              redo B;
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         undef $self->{form_element} if $token->{tag_name} eq 'form';  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
3412            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3413          }          }
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
3414    
3415              !!!next-token;          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3416              last S2;            !!!cp ('t89');
3417            } else {            ## As if <head>
3418              ## Step 3            !!!create-element ($self->{head_element}, 'head');
3419              if (not $formatting_category->{$node->[1]} and            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3420                  #not $phrasing_category->{$node->[1]} and            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3421                  ($special_category->{$node->[1]} or  
3422                   $scoping_category->{$node->[1]})) {            ## Reprocess in the "in head" insertion mode...
3423                !!!parse-error (type => 'not closed:'.$node->[1]);            pop @{$self->{open_elements}};
3424                ## Ignore the token  
3425                !!!next-token;            ## Reprocess in the "after head" insertion mode...
3426                last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3427              }            !!!cp ('t90');
3428            }            ## As if </noscript>
3429              pop @{$self->{open_elements}};
3430              !!!parse-error (type => 'in noscript:#character', token => $token);
3431                        
3432            ## Step 4            ## Reprocess in the "in head" insertion mode...
3433            $node_i--;            ## As if </head>
3434            $node = $self->{open_elements}->[$node_i];            pop @{$self->{open_elements}};
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
3435    
3436    B: {            ## Reprocess in the "after head" insertion mode...
3437      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3438        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
3439          !!!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]);  
         }  
3440    
3441          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
3442          last B;          } else {
3443              !!!cp ('t92');
3444            }
3445    
3446          ## ISSUE: There is an issue in the spec.              ## "after head" insertion mode
3447        } else {              ## As if <body>
3448          if ($self->{insertion_mode} eq 'before head') {              !!!insert-element ('body');
3449            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';  
3450              ## reprocess              ## reprocess
3451              redo B;              redo B;
3452            } 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';  
3453              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3454                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3455              #} elsif ({                  !!!cp ('t93');
3456              #          base => 1, link => 1, meta => 1,                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3457              #          script => 1, style => 1, title => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3458              #         }->{$token->{tag_name}}) {                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3459              #  ## reprocess                  $self->{insertion_mode} = IN_HEAD_IM;
3460              } else {                  !!!next-token;
3461                ## reprocess                  redo B;
3462              }                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3463              redo B;                  !!!cp ('t94');
3464            } elsif ($token->{type} eq 'end tag') {                  #
3465              if ($token->{tag_name} eq 'html') {                } else {
3466                    !!!cp ('t95');
3467                    !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3468                    ## Ignore the token
3469                    !!!next-token;
3470                    redo B;
3471                  }
3472                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3473                  !!!cp ('t96');
3474                ## As if <head>                ## As if <head>
3475                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3476                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3477                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3478                $self->{insertion_mode} = 'in head';  
3479                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
3480                redo B;                ## Reprocess in the "in head" insertion mode...
3481              } else {              } else {
3482                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t97');
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
3483              }              }
               
             #  
           } 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';  
3484    
3485                my $text = '';              if ($token->{tag_name} eq 'base') {
3486                !!!next-token;                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3487                while ($token->{type} eq 'character') {                  !!!cp ('t98');
3488                  $text .= $token->{data};                  ## As if </noscript>
3489                  !!!next-token;                  pop @{$self->{open_elements}};
3490                }                  !!!parse-error (type => 'in noscript:base', token => $token);
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
3491                                
3492                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
3493                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
3494                } else {                } else {
3495                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
3496                }                }
               !!!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);  
3497    
3498                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
3499                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3500              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
3501                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3502                ## 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}};  
3503                } else {                } else {
3504                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
3505                }                }
3506                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3507                !!!next-token;                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3508                redo B;                pop @{$self->{open_elements}} # <head>
3509              } elsif ($token->{tag_name} eq 'html') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
               #  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
3510                !!!next-token;                !!!next-token;
3511                redo B;                redo B;
3512              }              } elsif ($token->{tag_name} eq 'link') {
3513            } else {                ## NOTE: There is a "as if in head" code clone.
3514              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3515            }                  !!!cp ('t102');
3516                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3517            if ($self->{open_elements}->[-1]->[1] eq 'head') {                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3518              ## As if </head>                } else {
3519              pop @{$self->{open_elements}};                  !!!cp ('t103');
           }  
           $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;  
3520                }                }
3521              }                !!!insert-element ($token->{tag_name}, $token->{attributes});
3522                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3523              #                pop @{$self->{open_elements}} # <head>
3524            } 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';  
3525                !!!next-token;                !!!next-token;
3526                redo B;                redo B;
3527              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
3528                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
3529                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3530                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
3531                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3532                $self->{insertion_mode} = 'in head';                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3533                ## reprocess                } else {
3534                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;  
3535                }                }
3536              }                !!!insert-element ($token->{tag_name}, $token->{attributes});
3537                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
3538    
3539              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
3540              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3541              ## into the current node" while characters might not be                    !!!cp ('t106');
3542              ## result in a new Text node.                    $self->{change_encoding}
3543              $reconstruct_active_formatting_elements->($insert_to_foster);                        ->($self, $token->{attributes}->{charset}->{value},
3544                                         $token);
3545              if ({                    
3546                   table => 1, tbody => 1, tfoot => 1,                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3547                   thead => 1, tr => 1,                        ->set_user_data (manakai_has_reference =>
3548                  }->{$self->{open_elements}->[-1]->[1]}) {                                             $token->{attributes}->{charset}
3549                # MUST                                                 ->{has_reference});
3550                my $foster_parent_element;                  } elsif ($token->{attributes}->{content}) {
3551                my $next_sibling;                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3552                my $prev_sibling;                    if ($token->{attributes}->{content}->{value}
3553                OE: for (reverse 0..$#{$self->{open_elements}}) {                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3554                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                            [\x09-\x0D\x20]*=
3555                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3556                    if (defined $parent and $parent->node_type == 1) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3557                      $foster_parent_element = $parent;                      !!!cp ('t107');
3558                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $self->{change_encoding}
3559                      $prev_sibling = $next_sibling->previous_sibling;                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3560                               $token);
3561                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3562                            ->set_user_data (manakai_has_reference =>
3563                                                 $token->{attributes}->{content}
3564                                                       ->{has_reference});
3565                    } else {                    } else {
3566                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
3567                    }                    }
                   last OE;  
3568                  }                  }
               } # 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});  
3569                } else {                } else {
3570                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
3571                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
3572                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3573                }                        ->set_user_data (manakai_has_reference =>
3574              } else {                                             $token->{attributes}->{charset}
3575                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
3576              }                  }
3577                                if ($token->{attributes}->{content}) {
3578              !!!next-token;                    !!!cp ('t110');
3579              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3580            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
3581              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
3582              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
3583              !!!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}};  
3584                }                }
3585    
3586                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
3587                  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}};  
3588                !!!next-token;                !!!next-token;
3589                redo B;                redo B;
3590              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
3591                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3592                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
3593                       }->{$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]);  
3594                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3595                    !!!parse-error (type => 'in noscript:title', token => $token);
3596                  
3597                    $self->{insertion_mode} = IN_HEAD_IM;
3598                    ## Reprocess in the "in head" insertion mode...
3599                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3600                    !!!cp ('t112');
3601                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3602                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3603                  } else {
3604                    !!!cp ('t113');
3605                }                }
3606    
3607                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
3608                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
3609                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
3610                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3611                  pop @{$self->{open_elements}} # <head>
3612                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3613                redo B;                redo B;
3614              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'style') {
3615                ## NOTE: There are code clones for this "table in table"                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3616                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## insertion mode IN_HEAD_IM)
3617                  ## NOTE: There is a "as if in head" code clone.
3618                ## As if </table>                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3619                ## have a table element in table scope                  !!!cp ('t114');
3620                my $i;                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3621                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3622                  my $node = $self->{open_elements}->[$_];                } else {
3623                  if ($node->[1] eq 'table') {                  !!!cp ('t115');
3624                    $i = $_;                }
3625                    last INSCOPE;                $parse_rcdata->(CDATA_CONTENT_MODEL);
3626                  } elsif ({                pop @{$self->{open_elements}} # <head>
3627                            table => 1, html => 1,                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3628                           }->{$node->[1]}) {                redo B;
3629                    last INSCOPE;              } elsif ($token->{tag_name} eq 'noscript') {
3630                  }                if ($self->{insertion_mode} == IN_HEAD_IM) {
3631                } # INSCOPE                  !!!cp ('t116');
3632                unless (defined $i) {                  ## NOTE: and scripting is disalbed
3633                  !!!parse-error (type => 'unmatched end tag:table');                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3634                  ## Ignore tokens </table><table>                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3635                    !!!next-token;
3636                    redo B;
3637                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3638                    !!!cp ('t117');
3639                    !!!parse-error (type => 'in noscript:noscript', token => $token);
3640                    ## Ignore the token
3641                  !!!next-token;                  !!!next-token;
3642                  redo B;                  redo B;
3643                  } else {
3644                    !!!cp ('t118');
3645                    #
3646                }                }
3647                } elsif ($token->{tag_name} eq 'script') {
3648                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3649                    !!!cp ('t119');
3650                    ## As if </noscript>
3651                    pop @{$self->{open_elements}};
3652                    !!!parse-error (type => 'in noscript:script', token => $token);
3653                                
3654                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
3655                if ({                  ## Reprocess in the "in head" insertion mode...
3656                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3657                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
3658                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3659                  !!!back-token; # <table>                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3660                  $token = {type => 'end tag', tag_name => 'table'};                } else {
3661                  !!!back-token;                  !!!cp ('t121');
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
3662                }                }
3663    
3664                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
3665                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
3666                  pop @{$self->{open_elements}} # <head>
3667                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3668                  redo B;
3669                } elsif ($token->{tag_name} eq 'body' or
3670                         $token->{tag_name} eq 'frameset') {
3671                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3672                    !!!cp ('t122');
3673                    ## As if </noscript>
3674                    pop @{$self->{open_elements}};
3675                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3676                    
3677                    ## Reprocess in the "in head" insertion mode...
3678                    ## As if </head>
3679                    pop @{$self->{open_elements}};
3680                    
3681                    ## Reprocess in the "after head" insertion mode...
3682                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3683                    !!!cp ('t124');
3684                    pop @{$self->{open_elements}};
3685                    
3686                    ## Reprocess in the "after head" insertion mode...
3687                  } else {
3688                    !!!cp ('t125');
3689                }                }
3690    
3691                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
3692                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3693                $self->_reset_insertion_mode;                if ($token->{tag_name} eq 'body') {
3694                    !!!cp ('t126');
3695                ## reprocess                  $self->{insertion_mode} = IN_BODY_IM;
3696                  } elsif ($token->{tag_name} eq 'frameset') {
3697                    !!!cp ('t127');
3698                    $self->{insertion_mode} = IN_FRAMESET_IM;
3699                  } else {
3700                    die "$0: tag name: $self->{tag_name}";
3701                  }
3702                  !!!next-token;
3703                redo B;                redo B;
3704              } else {              } else {
3705                  !!!cp ('t128');
3706                #                #
3707              }              }
3708            } elsif ($token->{type} eq 'end tag') {  
3709              if ($token->{tag_name} eq 'table') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3710                ## have a table element in table scope                !!!cp ('t129');
3711                my $i;                ## As if </noscript>
3712                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                pop @{$self->{open_elements}};
3713                  my $node = $self->{open_elements}->[$_];                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3714                  if ($node->[1] eq $token->{tag_name}) {                
3715                    $i = $_;                ## Reprocess in the "in head" insertion mode...
3716                    last INSCOPE;                ## As if </head>
3717                  } elsif ({                pop @{$self->{open_elements}};
3718                            table => 1, html => 1,  
3719                           }->{$node->[1]}) {                ## Reprocess in the "after head" insertion mode...
3720                    last INSCOPE;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3721                  }                !!!cp ('t130');
3722                } # INSCOPE                ## As if </head>
3723                unless (defined $i) {                pop @{$self->{open_elements}};
3724                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
3725                  ## Ignore the token                ## Reprocess in the "after head" insertion mode...
3726                } else {
3727                  !!!cp ('t131');
3728                }
3729    
3730                ## "after head" insertion mode
3731                ## As if <body>
3732                !!!insert-element ('body');
3733                $self->{insertion_mode} = IN_BODY_IM;
3734                ## reprocess
3735                redo B;
3736              } elsif ($token->{type} == END_TAG_TOKEN) {
3737                if ($token->{tag_name} eq 'head') {
3738                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3739                    !!!cp ('t132');
3740                    ## As if <head>
3741                    !!!create-element ($self->{head_element}, 'head');
3742                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3743                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3744    
3745                    ## Reprocess in the "in head" insertion mode...
3746                    pop @{$self->{open_elements}};
3747                    $self->{insertion_mode} = AFTER_HEAD_IM;
3748                  !!!next-token;                  !!!next-token;
3749                  redo B;                  redo B;
3750                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3751                    !!!cp ('t133');
3752                    ## As if </noscript>
3753                    pop @{$self->{open_elements}};
3754                    !!!parse-error (type => 'in noscript:/head', token => $token);
3755                    
3756                    ## Reprocess in the "in head" insertion mode...
3757                    pop @{$self->{open_elements}};
3758                    $self->{insertion_mode} = AFTER_HEAD_IM;
3759                    !!!next-token;
3760                    redo B;
3761                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3762                    !!!cp ('t134');
3763                    pop @{$self->{open_elements}};
3764                    $self->{insertion_mode} = AFTER_HEAD_IM;
3765                    !!!next-token;
3766                    redo B;
3767                  } else {
3768                    !!!cp ('t135');
3769                    #
3770                }                }
3771                              } elsif ($token->{tag_name} eq 'noscript') {
3772                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3773                if ({                  !!!cp ('t136');
3774                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
3775                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
3776                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
3777                  !!!back-token;                  redo B;
3778                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3779                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t137');
3780                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3781                    ## Ignore the token ## ISSUE: An issue in the spec.
3782                    !!!next-token;
3783                  redo B;                  redo B;
3784                  } else {
3785                    !!!cp ('t138');
3786                    #
3787                }                }
3788                } elsif ({
3789                          body => 1, html => 1,
3790                         }->{$token->{tag_name}}) {
3791                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3792                    !!!cp ('t139');
3793                    ## As if <head>
3794                    !!!create-element ($self->{head_element}, 'head');
3795                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3796                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3797    
3798                    $self->{insertion_mode} = IN_HEAD_IM;
3799                    ## Reprocess in the "in head" insertion mode...
3800                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3801                    !!!cp ('t140');
3802                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3803                    ## Ignore the token
3804                    !!!next-token;
3805                    redo B;
3806                  } else {
3807                    !!!cp ('t141');
3808                  }
3809                  
3810                  #
3811                } elsif ({
3812                          p => 1, br => 1,
3813                         }->{$token->{tag_name}}) {
3814                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3815                    !!!cp ('t142');
3816                    ## As if <head>
3817                    !!!create-element ($self->{head_element}, 'head');
3818                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3819                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3820    
3821                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3822                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3823                  } else {
3824                    !!!cp ('t143');
3825                }                }
3826    
3827                splice @{$self->{open_elements}}, $i;                #
3828                } else {
3829                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3830                    !!!cp ('t144');
3831                    #
3832                  } else {
3833                    !!!cp ('t145');
3834                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3835                    ## Ignore the token
3836                    !!!next-token;
3837                    redo B;
3838                  }
3839                }
3840    
3841                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3842                  !!!cp ('t146');
3843                  ## As if </noscript>
3844                  pop @{$self->{open_elements}};
3845                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3846                  
3847                  ## Reprocess in the "in head" insertion mode...
3848                  ## As if </head>
3849                  pop @{$self->{open_elements}};
3850    
3851                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3852                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3853              } elsif ({                !!!cp ('t147');
3854                        body => 1, caption => 1, col => 1, colgroup => 1,                ## As if </head>
3855                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                pop @{$self->{open_elements}};
3856                        thead => 1, tr => 1,  
3857                       }->{$token->{tag_name}}) {                ## Reprocess in the "after head" insertion mode...
3858                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3859                ## Ignore the token  ## ISSUE: This case cannot be reached?
3860                  !!!cp ('t148');
3861                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3862                  ## Ignore the token ## ISSUE: An issue in the spec.
3863                !!!next-token;                !!!next-token;
3864                redo B;                redo B;
3865              } else {              } else {
3866                #                !!!cp ('t149');
3867              }              }
           } else {  
             #  
           }  
3868    
3869            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
3870            $in_body->($insert_to_foster);              ## As if <body>
3871            redo B;              !!!insert-element ('body');
3872          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
3873            if ($token->{type} eq 'character') {              ## reprocess
3874              ## NOTE: This is a code clone of "character in body".              redo B;
3875          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3876            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3877              !!!cp ('t149.1');
3878    
3879              ## NOTE: As if <head>
3880              !!!create-element ($self->{head_element}, 'head');
3881              $self->{open_elements}->[-1]->[0]->append_child
3882                  ($self->{head_element});
3883              #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3884              #$self->{insertion_mode} = IN_HEAD_IM;
3885              ## NOTE: Reprocess.
3886    
3887              ## NOTE: As if </head>
3888              #pop @{$self->{open_elements}};
3889              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3890              ## NOTE: Reprocess.
3891              
3892              #
3893            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3894              !!!cp ('t149.2');
3895    
3896              ## NOTE: As if </head>
3897              pop @{$self->{open_elements}};
3898              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3899              ## NOTE: Reprocess.
3900    
3901              #
3902            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3903              !!!cp ('t149.3');
3904    
3905              !!!parse-error (type => 'in noscript:#eof', token => $token);
3906    
3907              ## As if </noscript>
3908              pop @{$self->{open_elements}};
3909              #$self->{insertion_mode} = IN_HEAD_IM;
3910              ## NOTE: Reprocess.
3911    
3912              ## NOTE: As if </head>
3913              pop @{$self->{open_elements}};
3914              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3915              ## NOTE: Reprocess.
3916    
3917              #
3918            } else {
3919              !!!cp ('t149.4');
3920              #
3921            }
3922    
3923            ## NOTE: As if <body>
3924            !!!insert-element ('body');
3925            $self->{insertion_mode} = IN_BODY_IM;
3926            ## NOTE: Reprocess.
3927            redo B;
3928          } else {
3929            die "$0: $token->{type}: Unknown token type";
3930          }
3931    
3932              ## ISSUE: An issue in the spec.
3933        } elsif ($self->{insertion_mode} & BODY_IMS) {
3934              if ($token->{type} == CHARACTER_TOKEN) {
3935                !!!cp ('t150');
3936                ## NOTE: There is a code clone of "character in body".
3937              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3938                            
3939              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3940    
3941              !!!next-token;              !!!next-token;
3942              redo B;              redo B;
3943            } 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') {  
3944              if ({              if ({
3945                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3946                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3947                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3948                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3949                    ## have an element in table scope
3950                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
3951                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
3952                my $i;                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3953                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
3954                  my $node = $self->{open_elements}->[$_];  
3955                  if ($node->[1] eq 'caption') {                      ## Close the cell
3956                    $i = $_;                      !!!back-token; # <?>
3957                    last INSCOPE;                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],
3958                  } elsif ({                                line => $token->{line},
3959                            table => 1, html => 1,                                column => $token->{column}};
3960                           }->{$node->[1]}) {                      redo B;
3961                    last INSCOPE;                    } elsif ({
3962                                table => 1, html => 1,
3963                               }->{$node->[1]}) {
3964                        !!!cp ('t152');
3965                        ## ISSUE: This case can never be reached, maybe.
3966                        last;
3967                      }
3968                  }                  }
3969                } # INSCOPE  
3970                unless (defined $i) {                  !!!cp ('t153');
3971                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
3972                        value => $token->{tag_name}, token => $token);
3973                  ## Ignore the token                  ## Ignore the token
3974                  !!!next-token;                  !!!next-token;
3975                  redo B;                  redo B;
3976                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3977                                  !!!parse-error (type => 'not closed:caption', token => $token);
3978                ## generate implied end tags                  
3979                if ({                  ## NOTE: As if </caption>.
3980                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
3981                     td => 1, th => 1, tr => 1,                  my $i;
3982                    }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: {
3983                  !!!back-token; # <?>                    for (reverse 0..$#{$self->{open_elements}}) {
3984                  $token = {type => 'end tag', tag_name => 'caption'};                      my $node = $self->{open_elements}->[$_];
3985                  !!!back-token;                      if ($node->[1] eq 'caption') {
3986                  $token = {type => 'end tag',                        !!!cp ('t155');
3987                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        $i = $_;
3988                  redo B;                        last INSCOPE;
3989                }                      } elsif ({
3990                                  table => 1, html => 1,
3991                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                               }->{$node->[1]}) {
3992                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        !!!cp ('t156');
3993                }                        last;
3994                        }
3995                splice @{$self->{open_elements}}, $i;                    }
   
               $clear_up_to_marker->();  
3996    
3997                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
3998                      !!!parse-error (type => 'start tag not allowed',
3999                                      value => $token->{tag_name}, token => $token);
4000                      ## Ignore the token
4001                      !!!next-token;
4002                      redo B;
4003                    } # INSCOPE
4004                    
4005                    ## generate implied end tags
4006                    while ({
4007                            dd => 1, dt => 1, li => 1, p => 1,
4008                           }->{$self->{open_elements}->[-1]->[1]}) {
4009                      !!!cp ('t158');
4010                      pop @{$self->{open_elements}};
4011                    }
4012    
4013                ## reprocess                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4014                redo B;                    !!!cp ('t159');
4015                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4016                    } else {
4017                      !!!cp ('t160');
4018                    }
4019                    
4020                    splice @{$self->{open_elements}}, $i;
4021                    
4022                    $clear_up_to_marker->();
4023                    
4024                    $self->{insertion_mode} = IN_TABLE_IM;
4025                    
4026                    ## reprocess
4027                    redo B;
4028                  } else {
4029                    !!!cp ('t161');
4030                    #
4031                  }
4032              } else {              } else {
4033                  !!!cp ('t162');
4034                #                #
4035              }              }
4036            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4037              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4038                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4039                my $i;                  ## have an element in table scope
4040                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4041                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4042                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4043                    $i = $_;                    if ($node->[1] eq $token->{tag_name}) {
4044                    last INSCOPE;                      !!!cp ('t163');
4045                  } elsif ({                      $i = $_;
4046                            table => 1, html => 1,                      last INSCOPE;
4047                           }->{$node->[1]}) {                    } elsif ({
4048                    last INSCOPE;                              table => 1, html => 1,
4049                               }->{$node->[1]}) {
4050                        !!!cp ('t164');
4051                        last INSCOPE;
4052                      }
4053                    } # INSCOPE
4054                      unless (defined $i) {
4055                        !!!cp ('t165');
4056                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4057                        ## Ignore the token
4058                        !!!next-token;
4059                        redo B;
4060                      }
4061                    
4062                    ## generate implied end tags
4063                    while ({
4064                            dd => 1, dt => 1, li => 1, p => 1,
4065                           }->{$self->{open_elements}->[-1]->[1]}) {
4066                      !!!cp ('t166');
4067                      pop @{$self->{open_elements}};
4068                  }                  }
4069                } # INSCOPE  
4070                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4071                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t167');
4072                  ## Ignore the token                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4073                    } else {
4074                      !!!cp ('t168');
4075                    }
4076                    
4077                    splice @{$self->{open_elements}}, $i;
4078                    
4079                    $clear_up_to_marker->();
4080                    
4081                    $self->{insertion_mode} = IN_ROW_IM;
4082                    
4083                  !!!next-token;                  !!!next-token;
4084                  redo B;                  redo B;
4085                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4086                                  !!!cp ('t169');
4087                ## generate implied end tags                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4088                if ({                  ## Ignore the token
4089                     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  
4090                  redo B;                  redo B;
4091                  } else {
4092                    !!!cp ('t170');
4093                    #
4094                }                }
4095                } elsif ($token->{tag_name} eq 'caption') {
4096                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4097                    ## have a table element in table scope
4098                    my $i;
4099                    INSCOPE: {
4100                      for (reverse 0..$#{$self->{open_elements}}) {
4101                        my $node = $self->{open_elements}->[$_];
4102                        if ($node->[1] eq $token->{tag_name}) {
4103                          !!!cp ('t171');
4104                          $i = $_;
4105                          last INSCOPE;
4106                        } elsif ({
4107                                  table => 1, html => 1,
4108                                 }->{$node->[1]}) {
4109                          !!!cp ('t172');
4110                          last;
4111                        }
4112                      }
4113    
4114                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4115                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4116                                      value => $token->{tag_name}, token => $token);
4117                      ## Ignore the token
4118                      !!!next-token;
4119                      redo B;
4120                    } # INSCOPE
4121                    
4122                    ## generate implied end tags
4123                    while ({
4124                            dd => 1, dt => 1, li => 1, p => 1,
4125                           }->{$self->{open_elements}->[-1]->[1]}) {
4126                      !!!cp ('t174');
4127                      pop @{$self->{open_elements}};
4128                    }
4129                    
4130                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4131                      !!!cp ('t175');
4132                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4133                    } else {
4134                      !!!cp ('t176');
4135                    }
4136                    
4137                    splice @{$self->{open_elements}}, $i;
4138                    
4139                    $clear_up_to_marker->();
4140                    
4141                    $self->{insertion_mode} = IN_TABLE_IM;
4142                    
4143                    !!!next-token;
4144                    redo B;
4145                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4146                    !!!cp ('t177');
4147                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4148                    ## Ignore the token
4149                    !!!next-token;
4150                    redo B;
4151                  } else {
4152                    !!!cp ('t178');
4153                    #
4154                }                }
4155                } elsif ({
4156                          table => 1, tbody => 1, tfoot => 1,
4157                          thead => 1, tr => 1,
4158                         }->{$token->{tag_name}} and
4159                         $self->{insertion_mode} == IN_CELL_IM) {
4160                  ## have an element in table scope
4161                  my $i;
4162                  my $tn;
4163                  INSCOPE: {
4164                    for (reverse 0..$#{$self->{open_elements}}) {
4165                      my $node = $self->{open_elements}->[$_];
4166                      if ($node->[1] eq $token->{tag_name}) {
4167                        !!!cp ('t179');
4168                        $i = $_;
4169    
4170                        ## Close the cell
4171                        !!!back-token; # </?>
4172                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
4173                                  line => $token->{line},
4174                                  column => $token->{column}};
4175                        redo B;
4176                      } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4177                        !!!cp ('t180');
4178                        $tn = $node->[1];
4179                        ## NOTE: There is exactly one |td| or |th| element
4180                        ## in scope in the stack of open elements by definition.
4181                      } elsif ({
4182                                table => 1, html => 1,
4183                               }->{$node->[1]}) {
4184                        ## ISSUE: Can this be reached?
4185                        !!!cp ('t181');
4186                        last;
4187                      }
4188                    }
4189    
4190                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4191                    !!!parse-error (type => 'unmatched end tag',
4192                $clear_up_to_marker->();                      value => $token->{tag_name}, token => $token);
4193                    ## Ignore the token
4194                $self->{insertion_mode} = 'in table';                  !!!next-token;
4195                    redo B;
4196                !!!next-token;                } # INSCOPE
4197                redo B;              } elsif ($token->{tag_name} eq 'table' and
4198              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4199                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4200    
4201                ## As if </caption>                ## As if </caption>
4202                ## have a table element in table scope                ## have a table element in table scope
# Line 3559  sub _tree_construction_main ($) { Line 4204  sub _tree_construction_main ($) {
4204                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4205                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4206                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4207                      !!!cp ('t184');
4208                    $i = $_;                    $i = $_;
4209                    last INSCOPE;                    last INSCOPE;
4210                  } elsif ({                  } elsif ({
4211                            table => 1, html => 1,                            table => 1, html => 1,
4212                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4213                      !!!cp ('t185');
4214                    last INSCOPE;                    last INSCOPE;
4215                  }                  }
4216                } # INSCOPE                } # INSCOPE
4217                unless (defined $i) {                unless (defined $i) {
4218                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4219                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4220                  ## Ignore the token                  ## Ignore the token
4221                  !!!next-token;                  !!!next-token;
4222                  redo B;                  redo B;
4223                }                }
4224                                
4225                ## generate implied end tags                ## generate implied end tags
4226                if ({                while ({
4227                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4228                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4229                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t187');
4230                  !!!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;  
4231                }                }
4232    
4233                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4234                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4235                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4236                  } else {
4237                    !!!cp ('t189');
4238                }                }
4239    
4240                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4241    
4242                $clear_up_to_marker->();                $clear_up_to_marker->();
4243    
4244                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4245    
4246                ## reprocess                ## reprocess
4247                redo B;                redo B;
4248              } elsif ({              } elsif ({
4249                        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,  
4250                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4251                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4252                ## Ignore the token                  !!!cp ('t190');
4253                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');  
4254                  ## Ignore the token                  ## Ignore the token
4255                  !!!next-token;                  !!!next-token;
4256                  redo B;                  redo B;
4257                } else {                } else {
4258                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4259                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4260                }                }
4261              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4262                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4263                          thead => 1, tr => 1,
4264                         }->{$token->{tag_name}} and
4265                         $self->{insertion_mode} == IN_CAPTION_IM) {
4266                  !!!cp ('t192');
4267                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4268                ## Ignore the token                ## Ignore the token
4269                !!!next-token;                !!!next-token;
4270                redo B;                redo B;
4271              } else {              } else {
4272                #                !!!cp ('t193');
4273                  #
4274              }              }
4275            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4276              #          for my $entry (@{$self->{open_elements}}) {
4277              if (not {
4278                dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,
4279                th => 1, thead => 1, tr => 1, body => 1, html => 1,
4280              }->{$entry->[1]}) {
4281                !!!cp ('t75');
4282                !!!parse-error (type => 'in body:#eof', token => $token);
4283                last;
4284            }            }
4285            }
4286    
4287            ## As if </colgroup>          ## Stop parsing.
4288            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
4289              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
4290              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
4291          }
4292    
4293          $insert = $insert_to_current;
4294          #
4295        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4296          if ($token->{type} == CHARACTER_TOKEN) {
4297            if (not $open_tables->[-1]->[1] and # tainted
4298                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4299              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4300                  
4301              unless (length $token->{data}) {
4302                !!!cp ('t194');
4303              !!!next-token;              !!!next-token;
4304              redo B;              redo B;
4305            } else {            } else {
4306              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
4307            }            }
4308          } 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;  
               }  
             }  
4309    
4310              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4311    
4312              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4313              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
4314              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4315              ## result in a new Text node.              ## result in a new Text node.
4316              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4317                
4318              if ({              if ({
4319                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4320                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3710  sub _tree_construction_main ($) { Line 4327  sub _tree_construction_main ($) {
4327                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4328                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4329                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4330                        !!!cp ('t196');
4331                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4332                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4333                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4334                    } else {                    } else {
4335                        !!!cp ('t197');
4336                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4337                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4338                    }                    }
# Line 3725  sub _tree_construction_main ($) { Line 4344  sub _tree_construction_main ($) {
4344                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4345                if (defined $prev_sibling and                if (defined $prev_sibling and
4346                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4347                    !!!cp ('t198');
4348                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4349                } else {                } else {
4350                    !!!cp ('t199');
4351                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4352                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4353                     $next_sibling);                     $next_sibling);
4354                }                }
4355              } else {            $open_tables->[-1]->[1] = 1; # tainted
4356                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4357              }            !!!cp ('t200');
4358              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4359            }
4360                            
4361              !!!next-token;          !!!next-token;
4362              redo B;          redo B;
4363            } 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') {  
4364              if ({              if ({
4365                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4366                   th => 1, td => 1,                   th => 1, td => 1,
4367                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4368                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4369                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4370                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4371                           $self->{open_elements}->[-1]->[1] ne 'html') {
4372                      !!!cp ('t201');
4373                      pop @{$self->{open_elements}};
4374                    }
4375                    
4376                    !!!insert-element ('tbody');
4377                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4378                    ## reprocess in the "in table body" insertion mode...
4379                }                }
4380    
4381                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4382                    unless ($token->{tag_name} eq 'tr') {
4383                      !!!cp ('t202');
4384                      !!!parse-error (type => 'missing start tag:tr', token => $token);
4385                    }
4386                    
4387                    ## Clear back to table body context
4388                    while (not {
4389                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4390                    }->{$self->{open_elements}->[-1]->[1]}) {
4391                      !!!cp ('t203');
4392                      ## ISSUE: Can this case be reached?
4393                      pop @{$self->{open_elements}};
4394                    }
4395                    
4396                    $self->{insertion_mode} = IN_ROW_IM;
4397                    if ($token->{tag_name} eq 'tr') {
4398                      !!!cp ('t204');
4399                      !!!insert-element ($token->{tag_name}, $token->{attributes});
4400                      !!!next-token;
4401                      redo B;
4402                    } else {
4403                      !!!cp ('t205');
4404                      !!!insert-element ('tr');
4405                      ## reprocess in the "in row" insertion mode
4406                    }
4407                  } else {
4408                    !!!cp ('t206');
4409                  }
4410    
4411                  ## Clear back to table row context
4412                while (not {                while (not {
4413                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4414                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4415                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4416                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4417                }                }
4418                                
4419                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
4420                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
4421                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
4422                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
4423                } else {                
4424                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
4425                redo B;                redo B;
4426              } elsif ({              } elsif ({
4427                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4428                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4429                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4430                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4431                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
4432                my $i;                  ## As if </tr>
4433                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4434                  my $node = $self->{open_elements}->[$_];                  my $i;
4435                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4436                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4437                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4438                    $i = $_;                      !!!cp ('t208');
4439                    last INSCOPE;                      $i = $_;
4440                  } elsif ({                      last INSCOPE;
4441                            table => 1, html => 1,                    } elsif ({
4442                           }->{$node->[1]}) {                              html => 1,
4443                    last INSCOPE;  
4444                                ## NOTE: This element does not appear here, maybe.
4445                                table => 1,
4446                               }->{$node->[1]}) {
4447                        !!!cp ('t209');
4448                        last INSCOPE;
4449                      }
4450                    } # INSCOPE
4451                    unless (defined $i) {
4452                     !!!cp ('t210');
4453    ## TODO: This type is wrong.
4454                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4455                      ## Ignore the token
4456                      !!!next-token;
4457                      redo B;
4458                    }
4459                    
4460                    ## Clear back to table row context
4461                    while (not {
4462                      tr => 1, html => 1,
4463                    }->{$self->{open_elements}->[-1]->[1]}) {
4464                      !!!cp ('t211');
4465                      ## ISSUE: Can this case be reached?
4466                      pop @{$self->{open_elements}};
4467                    }
4468                    
4469                    pop @{$self->{open_elements}}; # tr
4470                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4471                    if ($token->{tag_name} eq 'tr') {
4472                      !!!cp ('t212');
4473                      ## reprocess
4474                      redo B;
4475                    } else {
4476                      !!!cp ('t213');
4477                      ## reprocess in the "in table body" insertion mode...
4478                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4479                }                }
4480    
4481                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4482                while (not {                  ## have an element in table scope
4483                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4484                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4485                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4486                      if ({
4487                           tbody => 1, thead => 1, tfoot => 1,
4488                          }->{$node->[1]}) {
4489                        !!!cp ('t214');
4490                        $i = $_;
4491                        last INSCOPE;
4492                      } elsif ({
4493                                table => 1, html => 1,
4494                               }->{$node->[1]}) {
4495                        !!!cp ('t215');
4496                        last INSCOPE;
4497                      }
4498                    } # INSCOPE
4499                    unless (defined $i) {
4500                      !!!cp ('t216');
4501    ## TODO: This erorr type ios wrong.
4502                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4503                      ## Ignore the token
4504                      !!!next-token;
4505                      redo B;
4506                    }
4507    
4508                    ## Clear back to table body context
4509                    while (not {
4510                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4511                    }->{$self->{open_elements}->[-1]->[1]}) {
4512                      !!!cp ('t217');
4513                      ## ISSUE: Can this state be reached?
4514                      pop @{$self->{open_elements}};
4515                    }
4516                    
4517                    ## As if <{current node}>
4518                    ## have an element in table scope
4519                    ## true by definition
4520                    
4521                    ## Clear back to table body context
4522                    ## nop by definition
4523                    
4524                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4525                    $self->{insertion_mode} = IN_TABLE_IM;
4526                    ## reprocess in "in table" insertion mode...
4527                  } else {
4528                    !!!cp ('t218');
4529                }                }
4530    
4531                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4532                ## have an element in table scope                  ## Clear back to table context
4533                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4534                           $self->{open_elements}->[-1]->[1] ne 'html') {
4535                ## Clear back to table body context                    !!!cp ('t219');
4536                ## nop by definition                    ## ISSUE: Can this state be reached?
4537                      pop @{$self->{open_elements}};
4538                pop @{$self->{open_elements}};                  }
4539                $self->{insertion_mode} = 'in table';                  
4540                ## reprocess                  !!!insert-element ('colgroup');
4541                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4542                    ## reprocess
4543                    redo B;
4544                  } elsif ({
4545                            caption => 1,
4546                            colgroup => 1,
4547                            tbody => 1, tfoot => 1, thead => 1,
4548                           }->{$token->{tag_name}}) {
4549                    ## Clear back to table context
4550                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4551                           $self->{open_elements}->[-1]->[1] ne 'html') {
4552                      !!!cp ('t220');
4553                      ## ISSUE: Can this state be reached?
4554                      pop @{$self->{open_elements}};
4555                    }
4556                    
4557                    push @$active_formatting_elements, ['#marker', '']
4558                        if $token->{tag_name} eq 'caption';
4559                    
4560                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4561                    $self->{insertion_mode} = {
4562                                               caption => IN_CAPTION_IM,
4563                                               colgroup => IN_COLUMN_GROUP_IM,
4564                                               tbody => IN_TABLE_BODY_IM,
4565                                               tfoot => IN_TABLE_BODY_IM,
4566                                               thead => IN_TABLE_BODY_IM,
4567                                              }->{$token->{tag_name}};
4568                    !!!next-token;
4569                    redo B;
4570                  } else {
4571                    die "$0: in table: <>: $token->{tag_name}";
4572                  }
4573              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4574                ## 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');  
4575    
4576                ## As if </table>                ## As if </table>
4577                ## have a table element in table scope                ## have a table element in table scope
# Line 3824  sub _tree_construction_main ($) { Line 4579  sub _tree_construction_main ($) {
4579                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4580                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4581                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4582                      !!!cp ('t221');
4583                    $i = $_;                    $i = $_;
4584                    last INSCOPE;                    last INSCOPE;
4585                  } elsif ({                  } elsif ({
4586                            table => 1, html => 1,                            #table => 1,
4587                              html => 1,
4588                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4589                      !!!cp ('t222');
4590                    last INSCOPE;                    last INSCOPE;
4591                  }                  }
4592                } # INSCOPE                } # INSCOPE
4593                unless (defined $i) {                unless (defined $i) {
4594                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
4595    ## TODO: The following is wrong, maybe.
4596                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
4597                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4598                  !!!next-token;                  !!!next-token;
4599                  redo B;                  redo B;
4600                }                }
4601                                
4602    ## TODO: Followings are removed from the latest spec.
4603                ## generate implied end tags                ## generate implied end tags
4604                if ({                while ({
4605                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4606                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4607                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t224');
4608                  !!!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;  
4609                }                }
4610    
4611                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4612                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
4613    ## ISSUE: Can this case be reached?
4614                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4615                  } else {
4616                    !!!cp ('t226');
4617                }                }
4618    
4619                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4620                  pop @{$open_tables};
4621    
4622                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4623    
4624                ## reprocess                ## reprocess
4625                redo B;                redo B;
4626              } else {          } elsif ($token->{tag_name} eq 'style') {
4627                #            if (not $open_tables->[-1]->[1]) { # tainted
4628              }              !!!cp ('t227.8');
4629            } elsif ($token->{type} eq 'end tag') {              ## NOTE: This is a "as if in head" code clone.
4630              if ({              $parse_rcdata->(CDATA_CONTENT_MODEL);
4631                   tbody => 1, tfoot => 1, thead => 1,              redo B;
4632                  }->{$token->{tag_name}}) {            } else {
4633                ## have an element in table scope              !!!cp ('t227.7');
4634                my $i;              #
4635                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
4636                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'script') {
4637                  if ($node->[1] eq $token->{tag_name}) {            if (not $open_tables->[-1]->[1]) { # tainted
4638                    $i = $_;              !!!cp ('t227.6');
4639                    last INSCOPE;              ## NOTE: This is a "as if in head" code clone.
4640                  } elsif ({              $script_start_tag->();
4641                            table => 1, html => 1,              redo B;
4642                           }->{$node->[1]}) {            } else {
4643                    last INSCOPE;              !!!cp ('t227.5');
4644                  }              #
4645                } # INSCOPE            }
4646                unless (defined $i) {          } elsif ($token->{tag_name} eq 'input') {
4647                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4648                  ## Ignore the token              if ($token->{attributes}->{type}) { ## TODO: case
4649                  !!!next-token;                my $type = lc $token->{attributes}->{type}->{value};
4650                  redo B;                if ($type eq 'hidden') {
4651                }                  !!!cp ('t227.3');
4652                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4653    
4654                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4655    
4656                    ## TODO: form element pointer
4657    
               ## 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]);  
4658                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
4659    
               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  
4660                  !!!next-token;                  !!!next-token;
4661                  redo B;                  redo B;
4662                  } else {
4663                    !!!cp ('t227.2');
4664                    #
4665                }                }
   
               ## 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;  
4666              } else {              } else {
4667                  !!!cp ('t227.1');
4668                #                #
4669              }              }
4670            } else {            } else {
4671                !!!cp ('t227.4');
4672              #              #
4673            }            }
4674                      } else {
4675            ## As if in table            !!!cp ('t227');
4676            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4677            $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;  
               }  
             }  
4678    
4679              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4680    
4681              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
4682              ## ISSUE: Spec says that "whenever a node would be inserted          #
4683              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
4684              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
4685              $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>  
4686                ## have an element in table scope                ## have an element in table scope
4687                my $i;                my $i;
4688                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4689                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4690                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4691                      !!!cp ('t228');
4692                    $i = $_;                    $i = $_;
4693                    last INSCOPE;                    last INSCOPE;
4694                  } elsif ({                  } elsif ({
4695                            table => 1, html => 1,                            table => 1, html => 1,
4696                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4697                      !!!cp ('t229');
4698                    last INSCOPE;                    last INSCOPE;
4699                  }                  }
4700                } # INSCOPE                } # INSCOPE
4701                unless (defined $i) {                unless (defined $i) {
4702                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
4703                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4704                  ## Ignore the token                  ## Ignore the token
4705                  !!!next-token;                  !!!next-token;
4706                  redo B;                  redo B;
4707                  } else {
4708                    !!!cp ('t232');
4709                }                }
4710    
4711                ## Clear back to table row context                ## Clear back to table row context
4712                while (not {                while (not {
4713                  tr => 1, html => 1,                  tr => 1, html => 1,
4714                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4715                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4716    ## ISSUE: Can this state be reached?
4717                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4718                }                }
4719    
4720                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
4721                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4722                ## reprocess                !!!next-token;
4723                redo B;                redo B;
4724              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4725                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
4726                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
4727                    ## have an element in table scope
4728                ## As if </table>                  my $i;
4729                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4730                my $i;                    my $node = $self->{open_elements}->[$_];
4731                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'tr') {
4732                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
4733                  if ($node->[1] eq 'table') {                      $i = $_;
4734                    $i = $_;                      last INSCOPE;
4735                    last INSCOPE;                    } elsif ({
4736                  } elsif ({                              table => 1, html => 1,
4737                            table => 1, html => 1,                             }->{$node->[1]}) {
4738                           }->{$node->[1]}) {                      !!!cp ('t234');
4739                    last INSCOPE;                      last INSCOPE;
4740                      }
4741                    } # INSCOPE
4742                    unless (defined $i) {
4743                      !!!cp ('t235');
4744    ## TODO: The following is wrong.
4745                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4746                      ## Ignore the token
4747                      !!!next-token;
4748                      redo B;
4749                  }                  }
4750                } # INSCOPE                  
4751                unless (defined $i) {                  ## Clear back to table row context
4752                  !!!parse-error (type => 'unmatched end tag:table');                  while (not {
4753                  ## Ignore tokens </table><table>                    tr => 1, html => 1,
4754                  !!!next-token;                  }->{$self->{open_elements}->[-1]->[1]}) {
4755                  redo B;                    !!!cp ('t236');
4756                }  ## ISSUE: Can this state be reached?
4757                                    pop @{$self->{open_elements}};
4758                ## generate implied end tags                  }
4759                if ({                  
4760                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}}; # tr
4761                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4762                    }->{$self->{open_elements}->[-1]->[1]}) {                  ## reprocess in the "in table body" insertion mode...
4763                  !!!back-token; # <table>                }
4764                  $token = {type => 'end tag', tag_name => 'table'};  
4765                  !!!back-token;                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4766                  $token = {type => 'end tag',                  ## have an element in table scope
4767                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  my $i;
4768                  redo B;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4769                }                    my $node = $self->{open_elements}->[$_];
4770                      if ({
4771                if ($self->{open_elements}->[-1]->[1] ne 'table') {                         tbody => 1, thead => 1, tfoot => 1,
4772                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        }->{$node->[1]}) {
4773                        !!!cp ('t237');
4774                        $i = $_;
4775                        last INSCOPE;
4776                      } elsif ({
4777                                table => 1, html => 1,
4778                               }->{$node->[1]}) {
4779                        !!!cp ('t238');
4780                        last INSCOPE;
4781                      }
4782                    } # INSCOPE
4783                    unless (defined $i) {
4784                      !!!cp ('t239');
4785                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4786                      ## Ignore the token
4787                      !!!next-token;
4788                      redo B;
4789                    }
4790                    
4791                    ## Clear back to table body context
4792                    while (not {
4793                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4794                    }->{$self->{open_elements}->[-1]->[1]}) {
4795                      !!!cp ('t240');
4796                      pop @{$self->{open_elements}};
4797                    }
4798                    
4799                    ## As if <{current node}>
4800                    ## have an element in table scope
4801                    ## true by definition
4802                    
4803                    ## Clear back to table body context
4804                    ## nop by definition
4805                    
4806                    pop @{$self->{open_elements}};
4807                    $self->{insertion_mode} = IN_TABLE_IM;
4808                    ## reprocess in the "in table" insertion mode...
4809                }                }
4810    
4811                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
4812                  ## When you edit the code fragment below, please ensure that
4813                  ## the code for <table> in the "in table" insertion mode
4814                  ## is synced with it.
4815    
4816                $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  
4817                my $i;                my $i;
4818                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4819                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4820                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4821                      !!!cp ('t241');
4822                    $i = $_;                    $i = $_;
4823                    last INSCOPE;                    last INSCOPE;
4824                  } elsif ({                  } elsif ({
4825                            table => 1, html => 1,                            table => 1, html => 1,
4826                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4827                      !!!cp ('t242');
4828                    last INSCOPE;                    last INSCOPE;
4829                  }                  }
4830                } # INSCOPE                } # INSCOPE
4831                unless (defined $i) {                unless (defined $i) {
4832                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
4833                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4834                  ## Ignore the token                  ## Ignore the token
4835                  !!!next-token;                  !!!next-token;
4836                  redo B;                  redo B;
4837                }                }
4838                    
4839                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
4840                while (not {                pop @{$open_tables};
4841                  tr => 1, html => 1,                
4842                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
4843                  !!!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';  
4844                !!!next-token;                !!!next-token;
4845                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;  
4846              } elsif ({              } elsif ({
4847                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4848                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4849                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
4850                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
4851                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4852                  my $node = $self->{open_elements}->[$_];                  my $i;
4853                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4854                    $i = $_;                    my $node = $self->{open_elements}->[$_];
4855                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
4856                  } elsif ({                      !!!cp ('t247');
4857                            table => 1, html => 1,                      $i = $_;
4858                           }->{$node->[1]}) {                      last INSCOPE;
4859                    last INSCOPE;                    } elsif ({
4860                                table => 1, html => 1,
4861                               }->{$node->[1]}) {
4862                        !!!cp ('t248');
4863                        last INSCOPE;
4864                      }
4865                    } # INSCOPE
4866                      unless (defined $i) {
4867                        !!!cp ('t249');
4868                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4869                        ## Ignore the token
4870                        !!!next-token;
4871                        redo B;
4872                      }
4873                    
4874                    ## As if </tr>
4875                    ## have an element in table scope
4876                    my $i;
4877                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4878                      my $node = $self->{open_elements}->[$_];
4879                      if ($node->[1] eq 'tr') {
4880                        !!!cp ('t250');
4881                        $i = $_;
4882                        last INSCOPE;
4883                      } elsif ({
4884                                table => 1, html => 1,
4885                               }->{$node->[1]}) {
4886                        !!!cp ('t251');
4887                        last INSCOPE;
4888                      }
4889                    } # INSCOPE
4890                      unless (defined $i) {
4891                        !!!cp ('t252');
4892                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
4893                        ## Ignore the token
4894                        !!!next-token;
4895                        redo B;
4896                      }
4897                    
4898                    ## Clear back to table row context
4899                    while (not {
4900                      tr => 1, html => 1,
4901                    }->{$self->{open_elements}->[-1]->[1]}) {
4902                      !!!cp ('t253');
4903    ## ISSUE: Can this case be reached?
4904                      pop @{$self->{open_elements}};
4905                  }                  }
4906                } # INSCOPE                  
4907                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4908                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4909                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4910                }                }
4911    
               ## As if </tr>  
4912                ## have an element in table scope                ## have an element in table scope
4913                my $i;                my $i;
4914                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4915                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4916                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4917                      !!!cp ('t254');
4918                    $i = $_;                    $i = $_;
4919                    last INSCOPE;                    last INSCOPE;
4920                  } elsif ({                  } elsif ({
4921                            table => 1, html => 1,                            table => 1, html => 1,
4922                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4923                      !!!cp ('t255');
4924                    last INSCOPE;                    last INSCOPE;
4925                  }                  }
4926                } # INSCOPE                } # INSCOPE
4927                unless (defined $i) {                unless (defined $i) {
4928                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
4929                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4930                  ## Ignore the token                  ## Ignore the token
4931                  !!!next-token;                  !!!next-token;
4932                  redo B;                  redo B;
4933                }                }
4934    
4935                ## Clear back to table row context                ## Clear back to table body context
4936                while (not {                while (not {
4937                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4938                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4939                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4940    ## ISSUE: Can this case be reached?
4941                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4942                }                }
4943    
4944                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4945                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4946                ## reprocess                !!!next-token;
4947                redo B;                redo B;
4948              } elsif ({              } elsif ({
4949                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4950                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4951                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4952                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4953                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4954                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t258');
4955                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4956                ## Ignore the token                ## Ignore the token
4957                !!!next-token;                !!!next-token;
4958                redo B;                redo B;
4959              } else {          } else {
4960                #            !!!cp ('t259');
4961              }            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           } else {  
             #  
           }  
4962    
4963            ## As if in table            $insert = $insert_to_foster;
4964            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4965            $in_body->($insert_to_foster);          }
4966            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4967          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4968            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
4969              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
4970              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
4971                          #
4972              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4973              !!!cp ('t259.2');
4974              #
4975            }
4976    
4977              !!!next-token;          ## Stop parsing
4978              redo B;          last B;
4979            } elsif ($token->{type} eq 'comment') {        } else {
4980              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
4981              my $comment = $self->{document}->create_comment ($token->{data});        }
4982              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4983              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
4984              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4985            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4986              if ({                unless (length $token->{data}) {
4987                   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  
4988                  !!!next-token;                  !!!next-token;
4989                  redo B;                  redo B;
4990                }                }
4991                }
4992                ## Close the cell              
4993                !!!back-token; # <?>              !!!cp ('t261');
4994                $token = {type => 'end tag', tag_name => $tn};              #
4995              } elsif ($token->{type} == START_TAG_TOKEN) {
4996                if ($token->{tag_name} eq 'col') {
4997                  !!!cp ('t262');
4998                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4999                  pop @{$self->{open_elements}};
5000                  !!!next-token;
5001                redo B;                redo B;
5002              } else {              } else {
5003                  !!!cp ('t263');
5004                #                #
5005              }              }
5006            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5007              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5008                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
5009                my $i;                  !!!cp ('t264');
5010                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});  
5011                  ## Ignore the token                  ## Ignore the token
5012                  !!!next-token;                  !!!next-token;
5013                  redo B;                  redo B;
5014                  } else {
5015                    !!!cp ('t265');
5016                    pop @{$self->{open_elements}}; # colgroup
5017                    $self->{insertion_mode} = IN_TABLE_IM;
5018                    !!!next-token;
5019                    redo B;            
5020                }                }
5021                              } elsif ($token->{tag_name} eq 'col') {
5022                ## generate implied end tags                !!!cp ('t266');
5023                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});  
5024                ## Ignore the token                ## Ignore the token
5025                !!!next-token;                !!!next-token;
5026                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;  
5027              } else {              } else {
5028                #                !!!cp ('t267');
5029                  #
5030              }              }
5031            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5032              #          if ($self->{open_elements}->[-1]->[1] eq 'html' or
5033            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5034                        !!!cp ('t270.2');
5035            $in_body->($insert_to_current);            ## Stop parsing.
5036              last B;
5037            } else {
5038              ## NOTE: As if </colgroup>.
5039              !!!cp ('t270.1');
5040              pop @{$self->{open_elements}}; # colgroup
5041              $self->{insertion_mode} = IN_TABLE_IM;
5042              ## Reprocess.
5043            redo B;            redo B;
5044          } elsif ($self->{insertion_mode} eq 'in select') {          }
5045            if ($token->{type} eq 'character') {        } else {
5046              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          die "$0: $token->{type}: Unknown token type";
5047          }
5048    
5049              ## As if </colgroup>
5050              if ($self->{open_elements}->[-1]->[1] eq 'html') {
5051                !!!cp ('t269');
5052    ## TODO: Wrong error type?
5053                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5054                ## Ignore the token
5055              !!!next-token;              !!!next-token;
5056              redo B;              redo B;
5057            } elsif ($token->{type} eq 'comment') {            } else {
5058              my $comment = $self->{document}->create_comment ($token->{data});              !!!cp ('t270');
5059              $self->{open_elements}->[-1]->[0]->append_child ($comment);              pop @{$self->{open_elements}}; # colgroup
5060              !!!next-token;              $self->{insertion_mode} = IN_TABLE_IM;
5061                ## reprocess
5062              redo B;              redo B;
5063            } elsif ($token->{type} eq 'start tag') {            }
5064        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5065          if ($token->{type} == CHARACTER_TOKEN) {
5066            !!!cp ('t271');
5067            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5068            !!!next-token;
5069            redo B;
5070          } elsif ($token->{type} == START_TAG_TOKEN) {
5071              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
5072                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5073                    !!!cp ('t272');
5074                  ## As if </option>                  ## As if </option>
5075                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5076                  } else {
5077                    !!!cp ('t273');
5078                }                }
5079    
5080                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 4443  sub _tree_construction_main ($) { Line 5082  sub _tree_construction_main ($) {
5082                redo B;                redo B;
5083              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
5084                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5085                    !!!cp ('t274');
5086                  ## As if </option>                  ## As if </option>
5087                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5088                  } else {
5089                    !!!cp ('t275');
5090                }                }
5091    
5092                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5093                    !!!cp ('t276');
5094                  ## As if </optgroup>                  ## As if </optgroup>
5095                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5096                  } else {
5097                    !!!cp ('t277');
5098                }                }
5099    
5100                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
5101                !!!next-token;                !!!next-token;
5102                redo B;                redo B;
5103              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5104                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5105                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5106                      {
5107                       caption => 1, table => 1,
5108                       tbody => 1, tfoot => 1, thead => 1,
5109                       tr => 1, td => 1, th => 1,
5110                      }->{$token->{tag_name}})) {
5111              ## TODO: The type below is not good - <select> is replaced by </select>
5112              !!!parse-error (type => 'not closed:select', token => $token);
5113              ## NOTE: As if the token were </select> (<select> case) or
5114              ## as if there were </select> (otherwise).
5115                ## have an element in table scope                ## have an element in table scope
5116                my $i;                my $i;
5117                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5118                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5119                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5120                      !!!cp ('t278');
5121                    $i = $_;                    $i = $_;
5122                    last INSCOPE;                    last INSCOPE;
5123                  } elsif ({                  } elsif ({
5124                            table => 1, html => 1,                            table => 1, html => 1,
5125                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5126                      !!!cp ('t279');
5127                    last INSCOPE;                    last INSCOPE;
5128                  }                  }
5129                } # INSCOPE                } # INSCOPE
5130                unless (defined $i) {                unless (defined $i) {
5131                  !!!parse-error (type => 'unmatched end tag:select');                  !!!cp ('t280');
5132                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5133                  ## Ignore the token                  ## Ignore the token
5134                  !!!next-token;                  !!!next-token;
5135                  redo B;                  redo B;
5136                }                }
5137                                
5138                  !!!cp ('t281');
5139                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5140    
5141                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5142    
5143                !!!next-token;            if ($token->{tag_name} eq 'select') {
5144                redo B;              !!!cp ('t281.2');
5145              } else {              !!!next-token;
5146                #              redo B;
5147              }            } else {
5148            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t281.1');
5149                ## Reprocess the token.
5150                redo B;
5151              }
5152            } else {
5153              !!!cp ('t282');
5154              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5155              ## Ignore the token
5156              !!!next-token;
5157              redo B;
5158            }
5159          } elsif ($token->{type} == END_TAG_TOKEN) {
5160              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
5161                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
5162                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
5163                    !!!cp ('t283');
5164                  ## As if </option>                  ## As if </option>
5165                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
5166                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5167                    !!!cp ('t284');
5168                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5169                } else {                } else {
5170                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t285');
5171                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5172                  ## Ignore the token                  ## Ignore the token
5173                }                }
5174                !!!next-token;                !!!next-token;
5175                redo B;                redo B;
5176              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
5177                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5178                    !!!cp ('t286');
5179                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5180                } else {                } else {
5181                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t287');
5182                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5183                  ## Ignore the token                  ## Ignore the token
5184                }                }
5185                !!!next-token;                !!!next-token;
# Line 4516  sub _tree_construction_main ($) { Line 5190  sub _tree_construction_main ($) {
5190                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5191                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5192                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5193                      !!!cp ('t288');
5194                    $i = $_;                    $i = $_;
5195                    last INSCOPE;                    last INSCOPE;
5196                  } elsif ({                  } elsif ({
5197                            table => 1, html => 1,                            table => 1, html => 1,
5198                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5199                      !!!cp ('t289');
5200                    last INSCOPE;                    last INSCOPE;
5201                  }                  }
5202                } # INSCOPE                } # INSCOPE
5203                unless (defined $i) {                unless (defined $i) {
5204                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t290');
5205                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5206                  ## Ignore the token                  ## Ignore the token
5207                  !!!next-token;                  !!!next-token;
5208                  redo B;                  redo B;
5209                }                }
5210                                
5211                  !!!cp ('t291');
5212                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5213    
5214                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5215    
5216                !!!next-token;                !!!next-token;
5217                redo B;                redo B;
5218              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5219                        caption => 1, table => 1, tbody => 1,                   {
5220                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5221                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5222                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                   }->{$token->{tag_name}}) {
5223    ## TODO: The following is wrong?
5224                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5225                                
5226                ## have an element in table scope                ## have an element in table scope
5227                my $i;                my $i;
5228                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5229                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5230                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5231                      !!!cp ('t292');
5232                    $i = $_;                    $i = $_;
5233                    last INSCOPE;                    last INSCOPE;
5234                  } elsif ({                  } elsif ({
5235                            table => 1, html => 1,                            table => 1, html => 1,
5236                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5237                      !!!cp ('t293');
5238                    last INSCOPE;                    last INSCOPE;
5239                  }                  }
5240                } # INSCOPE                } # INSCOPE
5241                unless (defined $i) {                unless (defined $i) {
5242                    !!!cp ('t294');
5243                  ## Ignore the token                  ## Ignore the token
5244                  !!!next-token;                  !!!next-token;
5245                  redo B;                  redo B;
# Line 4568  sub _tree_construction_main ($) { Line 5251  sub _tree_construction_main ($) {
5251                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5252                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5253                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5254                      !!!cp ('t295');
5255                    $i = $_;                    $i = $_;
5256                    last INSCOPE;                    last INSCOPE;
5257                  } elsif ({                  } elsif ({
5258                            table => 1, html => 1,                            table => 1, html => 1,
5259                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5260    ## ISSUE: Can this state be reached?
5261                      !!!cp ('t296');
5262                    last INSCOPE;                    last INSCOPE;
5263                  }                  }
5264                } # INSCOPE                } # INSCOPE
5265                unless (defined $i) {                unless (defined $i) {
5266                  !!!parse-error (type => 'unmatched end tag:select');                  !!!cp ('t297');
5267    ## TODO: The following error type is correct?
5268                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5269                  ## Ignore the </select> token                  ## Ignore the </select> token
5270                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5271                  redo B;                  redo B;
5272                }                }
5273                                
5274                  !!!cp ('t298');
5275                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5276    
5277                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5278    
5279                ## reprocess                ## reprocess
5280                redo B;                redo B;
5281              } else {          } else {
5282                #            !!!cp ('t299');
5283              }            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
5284            ## Ignore the token            ## Ignore the token
5285            !!!next-token;            !!!next-token;
5286            redo B;            redo B;
5287          } elsif ($self->{insertion_mode} eq 'after body') {          }
5288            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5289              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5290                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
5291                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
5292              !!!parse-error (type => 'in body:#eof', token => $token);
5293            } else {
5294              !!!cp ('t299.2');
5295            }
5296    
5297            ## Stop parsing.
5298            last B;
5299          } else {
5300            die "$0: $token->{type}: Unknown token type";
5301          }
5302        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5303          if ($token->{type} == CHARACTER_TOKEN) {
5304            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5305              my $data = $1;
5306              ## As if in body
5307              $reconstruct_active_formatting_elements->($insert_to_current);
5308                                
5309                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5310              
5311              unless (length $token->{data}) {
5312                !!!cp ('t300');
5313                !!!next-token;
5314                redo B;
5315              }
5316            }
5317            
5318            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5319              !!!cp ('t301');
5320              !!!parse-error (type => 'after html:#character', token => $token);
5321    
5322                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
5323                  !!!next-token;          } else {
5324                  redo B;            !!!cp ('t302');
5325                }          }
5326              }          
5327                        ## "after body" insertion mode
5328              #          !!!parse-error (type => 'after body:#character', token => $token);
5329              !!!parse-error (type => 'after body:#'.$token->{type});  
5330            } elsif ($token->{type} eq 'comment') {          $self->{insertion_mode} = IN_BODY_IM;
5331              my $comment = $self->{document}->create_comment ($token->{data});          ## reprocess
5332              $self->{open_elements}->[0]->[0]->append_child ($comment);          redo B;
5333          } elsif ($token->{type} == START_TAG_TOKEN) {
5334            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5335              !!!cp ('t303');
5336              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5337              
5338              ## Reprocess in the "after body" insertion mode.
5339            } else {
5340              !!!cp ('t304');
5341            }
5342    
5343            ## "after body" insertion mode
5344            !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5345    
5346            $self->{insertion_mode} = IN_BODY_IM;
5347            ## reprocess
5348            redo B;
5349          } elsif ($token->{type} == END_TAG_TOKEN) {
5350            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5351              !!!cp ('t305');
5352              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5353              
5354              $self->{insertion_mode} = AFTER_BODY_IM;
5355              ## Reprocess in the "after body" insertion mode.
5356            } else {
5357              !!!cp ('t306');
5358            }
5359    
5360            ## "after body" insertion mode
5361            if ($token->{tag_name} eq 'html') {
5362              if (defined $self->{inner_html_node}) {
5363                !!!cp ('t307');
5364                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5365                ## Ignore the token
5366              !!!next-token;              !!!next-token;
5367              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});  
             }  
5368            } else {            } else {
5369              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t308');
5370                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5371                !!!next-token;
5372                redo B;
5373            }            }
5374            } else {
5375              !!!cp ('t309');
5376              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5377    
5378            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
5379            ## reprocess            ## reprocess
5380            redo B;            redo B;
5381          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
5382            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5383              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t309.2');
5384                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          ## Stop parsing
5385            last B;
5386                unless (length $token->{data}) {        } else {
5387                  !!!next-token;          die "$0: $token->{type}: Unknown token type";
5388                  redo B;        }
5389                }      } elsif ($self->{insertion_mode} & FRAME_IMS) {
5390              }        if ($token->{type} == CHARACTER_TOKEN) {
5391            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5392              #            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5393            } elsif ($token->{type} eq 'comment') {            
5394              my $comment = $self->{document}->create_comment ($token->{data});            unless (length $token->{data}) {
5395              $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!cp ('t310');
5396              !!!next-token;              !!!next-token;
5397              redo B;              redo B;
5398            } elsif ($token->{type} eq 'start tag') {            }
5399              if ($token->{tag_name} eq 'frameset') {          }
5400                !!!insert-element ($token->{tag_name}, $token->{attributes});          
5401                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5402                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5403              } elsif ($token->{tag_name} eq 'frame') {              !!!cp ('t311');
5404                !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!parse-error (type => 'in frameset:#character', token => $token);
5405                pop @{$self->{open_elements}};            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5406                !!!next-token;              !!!cp ('t312');
5407                redo B;              !!!parse-error (type => 'after frameset:#character', token => $token);
5408              } elsif ($token->{tag_name} eq 'noframes') {            } else { # "after html frameset"
5409                $in_body->($insert_to_current);              !!!cp ('t313');
5410                redo B;              !!!parse-error (type => 'after html:#character', token => $token);
5411              } else {  
5412                #              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5413              }              ## Reprocess in the "after frameset" insertion mode.
5414            } 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 {  
             #  
5415            }            }
5416                        
5417            if (defined $token->{tag_name}) {            ## Ignore the token.
5418              !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if (length $token->{data}) {
5419                !!!cp ('t314');
5420                ## reprocess the rest of characters
5421            } else {            } else {
5422              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
5423                !!!next-token;
5424              }
5425              redo B;
5426            }
5427            
5428            die qq[$0: Character "$token->{data}"];
5429          } elsif ($token->{type} == START_TAG_TOKEN) {
5430            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5431              !!!cp ('t316');
5432              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5433    
5434              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5435              ## Process in the "after frameset" insertion mode.
5436            } else {
5437              !!!cp ('t317');
5438            }
5439    
5440            if ($token->{tag_name} eq 'frameset' and
5441                $self->{insertion_mode} == IN_FRAMESET_IM) {
5442              !!!cp ('t318');
5443              !!!insert-element ($token->{tag_name}, $token->{attributes});
5444              !!!next-token;
5445              redo B;
5446            } elsif ($token->{tag_name} eq 'frame' and
5447                     $self->{insertion_mode} == IN_FRAMESET_IM) {
5448              !!!cp ('t319');
5449              !!!insert-element ($token->{tag_name}, $token->{attributes});
5450              pop @{$self->{open_elements}};
5451              !!!next-token;
5452              redo B;
5453            } elsif ($token->{tag_name} eq 'noframes') {
5454              !!!cp ('t320');
5455              ## NOTE: As if in body.
5456              $parse_rcdata->(CDATA_CONTENT_MODEL);
5457              redo B;
5458            } else {
5459              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5460                !!!cp ('t321');
5461                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5462              } else {
5463                !!!cp ('t322');
5464                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5465            }            }
5466            ## Ignore the token            ## Ignore the token
5467            !!!next-token;            !!!next-token;
5468            redo B;            redo B;
5469          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
5470            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5471              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5472                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t323');
5473              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5474    
5475              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5476              ## Process in the "after frameset" insertion mode.
5477            } else {
5478              !!!cp ('t324');
5479            }
5480    
5481            if ($token->{tag_name} eq 'frameset' and
5482                $self->{insertion_mode} == IN_FRAMESET_IM) {
5483              if ($self->{open_elements}->[-1]->[1] eq 'html' and
5484                  @{$self->{open_elements}} == 1) {
5485                !!!cp ('t325');
5486                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5487                ## Ignore the token
5488                !!!next-token;
5489              } else {
5490                !!!cp ('t326');
5491                pop @{$self->{open_elements}};
5492                !!!next-token;
5493              }
5494    
5495                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
5496                  !!!next-token;                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5497                  redo B;              !!!cp ('t327');
5498                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5499              } else {
5500                !!!cp ('t328');
5501              }
5502              redo B;
5503            } elsif ($token->{tag_name} eq 'html' and
5504                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5505              !!!cp ('t329');
5506              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5507              !!!next-token;
5508              redo B;
5509            } else {
5510              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5511                !!!cp ('t330');
5512                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5513              } else {
5514                !!!cp ('t331');
5515                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5516              }
5517              ## Ignore the token
5518              !!!next-token;
5519              redo B;
5520            }
5521          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5522            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5523                    @{$self->{open_elements}} == 1) { # redundant, maybe
5524              !!!cp ('t331.1');
5525              !!!parse-error (type => 'in body:#eof', token => $token);
5526            } else {
5527              !!!cp ('t331.2');
5528            }
5529            
5530            ## Stop parsing
5531            last B;
5532          } else {
5533            die "$0: $token->{type}: Unknown token type";
5534          }
5535    
5536          ## ISSUE: An issue in spec here
5537        } else {
5538          die "$0: $self->{insertion_mode}: Unknown insertion mode";
5539        }
5540    
5541        ## "in body" insertion mode
5542        if ($token->{type} == START_TAG_TOKEN) {
5543          if ($token->{tag_name} eq 'script') {
5544            !!!cp ('t332');
5545            ## NOTE: This is an "as if in head" code clone
5546            $script_start_tag->();
5547            redo B;
5548          } elsif ($token->{tag_name} eq 'style') {
5549            !!!cp ('t333');
5550            ## NOTE: This is an "as if in head" code clone
5551            $parse_rcdata->(CDATA_CONTENT_MODEL);
5552            redo B;
5553          } elsif ({
5554                    base => 1, link => 1,
5555                   }->{$token->{tag_name}}) {
5556            !!!cp ('t334');
5557            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5558            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5559            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5560            !!!next-token;
5561            redo B;
5562          } elsif ($token->{tag_name} eq 'meta') {
5563            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5564            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5565            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5566    
5567            unless ($self->{confident}) {
5568              if ($token->{attributes}->{charset}) { ## TODO: And if supported
5569                !!!cp ('t335');
5570                $self->{change_encoding}
5571                    ->($self, $token->{attributes}->{charset}->{value}, $token);
5572                
5573                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5574                    ->set_user_data (manakai_has_reference =>
5575                                         $token->{attributes}->{charset}
5576                                             ->{has_reference});
5577              } elsif ($token->{attributes}->{content}) {
5578                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5579                if ($token->{attributes}->{content}->{value}
5580                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5581                        [\x09-\x0D\x20]*=
5582                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5583                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5584                  !!!cp ('t336');
5585                  $self->{change_encoding}
5586                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5587                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5588                      ->set_user_data (manakai_has_reference =>
5589                                           $token->{attributes}->{content}
5590                                                 ->{has_reference});
5591              }              }
5592              }
5593            } else {
5594              if ($token->{attributes}->{charset}) {
5595                !!!cp ('t337');
5596                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5597                    ->set_user_data (manakai_has_reference =>
5598                                         $token->{attributes}->{charset}
5599                                             ->{has_reference});
5600              }
5601              if ($token->{attributes}->{content}) {
5602                !!!cp ('t338');
5603                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5604                    ->set_user_data (manakai_has_reference =>
5605                                         $token->{attributes}->{content}
5606                                             ->{has_reference});
5607              }
5608            }
5609    
5610              #          !!!next-token;
5611            } elsif ($token->{type} eq 'comment') {          redo B;
5612              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{tag_name} eq 'title') {
5613              $self->{open_elements}->[-1]->[0]->append_child ($comment);          !!!cp ('t341');
5614              !!!next-token;          ## NOTE: This is an "as if in head" code clone
5615              redo B;          $parse_rcdata->(RCDATA_CONTENT_MODEL);
5616            } elsif ($token->{type} eq 'start tag') {          redo B;
5617              if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'body') {
5618                $in_body->($insert_to_current);          !!!parse-error (type => 'in body:body', token => $token);
5619                redo B;                
5620              } else {          if (@{$self->{open_elements}} == 1 or
5621                #              $self->{open_elements}->[1]->[1] ne 'body') {
5622              !!!cp ('t342');
5623              ## Ignore the token
5624            } else {
5625              my $body_el = $self->{open_elements}->[1]->[0];
5626              for my $attr_name (keys %{$token->{attributes}}) {
5627                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5628                  !!!cp ('t343');
5629                  $body_el->set_attribute_ns
5630                    (undef, [undef, $attr_name],
5631                     $token->{attributes}->{$attr_name}->{value});
5632              }              }
5633            } elsif ($token->{type} eq 'end tag') {            }
5634              if ($token->{tag_name} eq 'html') {          }
5635                $phase = 'trailing end';          !!!next-token;
5636            redo B;
5637          } elsif ({
5638                    address => 1, blockquote => 1, center => 1, dir => 1,
5639                    div => 1, dl => 1, fieldset => 1,
5640                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5641                    menu => 1, ol => 1, p => 1, ul => 1,
5642                    pre => 1, listing => 1,
5643                    form => 1,
5644                    table => 1,
5645                    hr => 1,
5646                   }->{$token->{tag_name}}) {
5647            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5648              !!!cp ('t350');
5649              !!!parse-error (type => 'in form:form', token => $token);
5650              ## Ignore the token
5651              !!!next-token;
5652              redo B;
5653            }
5654    
5655            ## has a p element in scope
5656            INSCOPE: for (reverse @{$self->{open_elements}}) {
5657              if ($_->[1] eq 'p') {
5658                !!!cp ('t344');
5659                !!!back-token;
5660                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5661                          line => $token->{line}, column => $token->{column}};
5662                redo B;
5663              } elsif ({
5664                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5665                        button => 1, marquee => 1, object => 1, html => 1,
5666                       }->{$_->[1]}) {
5667                !!!cp ('t345');
5668                last INSCOPE;
5669              }
5670            } # INSCOPE
5671              
5672            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5673            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5674              !!!next-token;
5675              if ($token->{type} == CHARACTER_TOKEN) {
5676                $token->{data} =~ s/^\x0A//;
5677                unless (length $token->{data}) {
5678                  !!!cp ('t346');
5679                !!!next-token;                !!!next-token;
               redo B;  
5680              } else {              } else {
5681                #                !!!cp ('t349');
5682              }              }
5683            } else {            } else {
5684              #              !!!cp ('t348');
5685              }
5686            } elsif ($token->{tag_name} eq 'form') {
5687              !!!cp ('t347.1');
5688              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5689    
5690              !!!next-token;
5691            } elsif ($token->{tag_name} eq 'table') {
5692              !!!cp ('t382');
5693              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5694              
5695              $self->{insertion_mode} = IN_TABLE_IM;
5696    
5697              !!!next-token;
5698            } elsif ($token->{tag_name} eq 'hr') {
5699              !!!cp ('t386');
5700              pop @{$self->{open_elements}};
5701            
5702              !!!next-token;
5703            } else {
5704              !!!cp ('t347');
5705              !!!next-token;
5706            }
5707            redo B;
5708          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5709            ## has a p element in scope
5710            INSCOPE: for (reverse @{$self->{open_elements}}) {
5711              if ($_->[1] eq 'p') {
5712                !!!cp ('t353');
5713                !!!back-token;
5714                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5715                          line => $token->{line}, column => $token->{column}};
5716                redo B;
5717              } elsif ({
5718                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5719                        button => 1, marquee => 1, object => 1, html => 1,
5720                       }->{$_->[1]}) {
5721                !!!cp ('t354');
5722                last INSCOPE;
5723            }            }
5724            } # INSCOPE
5725                        
5726            if (defined $token->{tag_name}) {          ## Step 1
5727              !!!parse-error (type => 'after frameset:'.$token->{tag_name});          my $i = -1;
5728            my $node = $self->{open_elements}->[$i];
5729            my $li_or_dtdd = {li => {li => 1},
5730                              dt => {dt => 1, dd => 1},
5731                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5732            LI: {
5733              ## Step 2
5734              if ($li_or_dtdd->{$node->[1]}) {
5735                if ($i != -1) {
5736                  !!!cp ('t355');
5737                  !!!parse-error (type => 'end tag missing:'.
5738                                  $self->{open_elements}->[-1]->[1], token => $token);
5739                } else {
5740                  !!!cp ('t356');
5741                }
5742                splice @{$self->{open_elements}}, $i;
5743                last LI;
5744            } else {            } else {
5745              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
5746              }
5747              
5748              ## Step 3
5749              if (not $formatting_category->{$node->[1]} and
5750                  #not $phrasing_category->{$node->[1]} and
5751                  ($special_category->{$node->[1]} or
5752                   $scoping_category->{$node->[1]}) and
5753                  $node->[1] ne 'address' and $node->[1] ne 'div') {
5754                !!!cp ('t358');
5755                last LI;
5756              }
5757              
5758              !!!cp ('t359');
5759              ## Step 4
5760              $i--;
5761              $node = $self->{open_elements}->[$i];
5762              redo LI;
5763            } # LI
5764              
5765            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5766            !!!next-token;
5767            redo B;
5768          } elsif ($token->{tag_name} eq 'plaintext') {
5769            ## has a p element in scope
5770            INSCOPE: for (reverse @{$self->{open_elements}}) {
5771              if ($_->[1] eq 'p') {
5772                !!!cp ('t367');
5773                !!!back-token;
5774                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5775                          line => $token->{line}, column => $token->{column}};
5776                redo B;
5777              } elsif ({
5778                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5779                        button => 1, marquee => 1, object => 1, html => 1,
5780                       }->{$_->[1]}) {
5781                !!!cp ('t368');
5782                last INSCOPE;
5783              }
5784            } # INSCOPE
5785              
5786            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5787              
5788            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5789              
5790            !!!next-token;
5791            redo B;
5792          } elsif ($token->{tag_name} eq 'a') {
5793            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5794              my $node = $active_formatting_elements->[$i];
5795              if ($node->[1] eq 'a') {
5796                !!!cp ('t371');
5797                !!!parse-error (type => 'in a:a', token => $token);
5798                
5799                !!!back-token;
5800                $token = {type => END_TAG_TOKEN, tag_name => 'a',
5801                          line => $token->{line}, column => $token->{column}};
5802                $formatting_end_tag->($token);
5803                
5804                AFE2: for (reverse 0..$#$active_formatting_elements) {
5805                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5806                    !!!cp ('t372');
5807                    splice @$active_formatting_elements, $_, 1;
5808                    last AFE2;
5809                  }
5810                } # AFE2
5811                OE: for (reverse 0..$#{$self->{open_elements}}) {
5812                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5813                    !!!cp ('t373');
5814                    splice @{$self->{open_elements}}, $_, 1;
5815                    last OE;
5816                  }
5817                } # OE
5818                last AFE;
5819              } elsif ($node->[0] eq '#marker') {
5820                !!!cp ('t374');
5821                last AFE;
5822              }
5823            } # AFE
5824              
5825            $reconstruct_active_formatting_elements->($insert_to_current);
5826    
5827            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5828            push @$active_formatting_elements, $self->{open_elements}->[-1];
5829    
5830            !!!next-token;
5831            redo B;
5832          } elsif ($token->{tag_name} eq 'nobr') {
5833            $reconstruct_active_formatting_elements->($insert_to_current);
5834    
5835            ## has a |nobr| element in scope
5836            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5837              my $node = $self->{open_elements}->[$_];
5838              if ($node->[1] eq 'nobr') {
5839                !!!cp ('t376');
5840                !!!parse-error (type => 'in nobr:nobr', token => $token);
5841                !!!back-token;
5842                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
5843                          line => $token->{line}, column => $token->{column}};
5844                redo B;
5845              } elsif ({
5846                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5847                        button => 1, marquee => 1, object => 1, html => 1,
5848                       }->{$node->[1]}) {
5849                !!!cp ('t377');
5850                last INSCOPE;
5851              }
5852            } # INSCOPE
5853            
5854            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5855            push @$active_formatting_elements, $self->{open_elements}->[-1];
5856            
5857            !!!next-token;
5858            redo B;
5859          } elsif ($token->{tag_name} eq 'button') {
5860            ## has a button element in scope
5861            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5862              my $node = $self->{open_elements}->[$_];
5863              if ($node->[1] eq 'button') {
5864                !!!cp ('t378');
5865                !!!parse-error (type => 'in button:button', token => $token);
5866                !!!back-token;
5867                $token = {type => END_TAG_TOKEN, tag_name => 'button',
5868                          line => $token->{line}, column => $token->{column}};
5869                redo B;
5870              } elsif ({
5871                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5872                        button => 1, marquee => 1, object => 1, html => 1,
5873                       }->{$node->[1]}) {
5874                !!!cp ('t379');
5875                last INSCOPE;
5876            }            }
5877            } # INSCOPE
5878              
5879            $reconstruct_active_formatting_elements->($insert_to_current);
5880              
5881            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5882    
5883            ## TODO: associate with $self->{form_element} if defined
5884    
5885            push @$active_formatting_elements, ['#marker', ''];
5886    
5887            !!!next-token;
5888            redo B;
5889          } elsif ({
5890                    xmp => 1,
5891                    iframe => 1,
5892                    noembed => 1,
5893                    noframes => 1,
5894                    noscript => 0, ## TODO: 1 if scripting is enabled
5895                   }->{$token->{tag_name}}) {
5896            if ($token->{tag_name} eq 'xmp') {
5897              !!!cp ('t381');
5898              $reconstruct_active_formatting_elements->($insert_to_current);
5899            } else {
5900              !!!cp ('t399');
5901            }
5902            ## NOTE: There is an "as if in body" code clone.
5903            $parse_rcdata->(CDATA_CONTENT_MODEL);
5904            redo B;
5905          } elsif ($token->{tag_name} eq 'isindex') {
5906            !!!parse-error (type => 'isindex', token => $token);
5907            
5908            if (defined $self->{form_element}) {
5909              !!!cp ('t389');
5910            ## Ignore the token            ## Ignore the token
5911            !!!next-token;            !!!next-token;
5912            redo B;            redo B;
5913            } else {
5914              my $at = $token->{attributes};
5915              my $form_attrs;
5916              $form_attrs->{action} = $at->{action} if $at->{action};
5917              my $prompt_attr = $at->{prompt};
5918              $at->{name} = {name => 'name', value => 'isindex'};
5919              delete $at->{action};
5920              delete $at->{prompt};
5921              my @tokens = (
5922                            {type => START_TAG_TOKEN, tag_name => 'form',
5923                             attributes => $form_attrs,
5924                             line => $token->{line}, column => $token->{column}},
5925                            {type => START_TAG_TOKEN, tag_name => 'hr',
5926                             line => $token->{line}, column => $token->{column}},
5927                            {type => START_TAG_TOKEN, tag_name => 'p',
5928                             line => $token->{line}, column => $token->{column}},
5929                            {type => START_TAG_TOKEN, tag_name => 'label',
5930                             line => $token->{line}, column => $token->{column}},
5931                           );
5932              if ($prompt_attr) {
5933                !!!cp ('t390');
5934                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
5935                               line => $token->{line}, column => $token->{column}};
5936              } else {
5937                !!!cp ('t391');
5938                push @tokens, {type => CHARACTER_TOKEN,
5939                               data => 'This is a searchable index. Insert your search keywords here: ',
5940                               line => $token->{line}, column => $token->{column}}; # SHOULD
5941                ## TODO: make this configurable
5942              }
5943              push @tokens,
5944                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
5945                             line => $token->{line}, column => $token->{column}},
5946                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5947                            {type => END_TAG_TOKEN, tag_name => 'label',
5948                             line => $token->{line}, column => $token->{column}},
5949                            {type => END_TAG_TOKEN, tag_name => 'p',
5950                             line => $token->{line}, column => $token->{column}},
5951                            {type => START_TAG_TOKEN, tag_name => 'hr',
5952                             line => $token->{line}, column => $token->{column}},
5953                            {type => END_TAG_TOKEN, tag_name => 'form',
5954                             line => $token->{line}, column => $token->{column}};
5955              $token = shift @tokens;
5956              !!!back-token (@tokens);
5957              redo B;
5958            }
5959          } elsif ($token->{tag_name} eq 'textarea') {
5960            my $tag_name = $token->{tag_name};
5961            my $el;
5962            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5963            
5964            ## TODO: $self->{form_element} if defined
5965            $self->{content_model} = RCDATA_CONTENT_MODEL;
5966            delete $self->{escape}; # MUST
5967            
5968            $insert->($el);
5969            
5970            my $text = '';
5971            !!!next-token;
5972            if ($token->{type} == CHARACTER_TOKEN) {
5973              $token->{data} =~ s/^\x0A//;
5974              unless (length $token->{data}) {
5975                !!!cp ('t392');
5976                !!!next-token;
5977              } else {
5978                !!!cp ('t393');
5979              }
5980            } else {
5981              !!!cp ('t394');
5982            }
5983            while ($token->{type} == CHARACTER_TOKEN) {
5984              !!!cp ('t395');
5985              $text .= $token->{data};
5986              !!!next-token;
5987            }
5988            if (length $text) {
5989              !!!cp ('t396');
5990              $el->manakai_append_text ($text);
5991            }
5992            
5993            $self->{content_model} = PCDATA_CONTENT_MODEL;
5994            
5995            if ($token->{type} == END_TAG_TOKEN and
5996                $token->{tag_name} eq $tag_name) {
5997              !!!cp ('t397');
5998              ## Ignore the token
5999            } else {
6000              !!!cp ('t398');
6001              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6002            }
6003            !!!next-token;
6004            redo B;
6005          } elsif ({
6006                    caption => 1, col => 1, colgroup => 1, frame => 1,
6007                    frameset => 1, head => 1, option => 1, optgroup => 1,
6008                    tbody => 1, td => 1, tfoot => 1, th => 1,
6009                    thead => 1, tr => 1,
6010                   }->{$token->{tag_name}}) {
6011            !!!cp ('t401');
6012            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6013            ## Ignore the token
6014            !!!next-token;
6015            redo B;
6016            
6017            ## ISSUE: An issue on HTML5 new elements in the spec.
6018          } else {
6019            if ($token->{tag_name} eq 'image') {
6020              !!!cp ('t384');
6021              !!!parse-error (type => 'image', token => $token);
6022              $token->{tag_name} = 'img';
6023            } else {
6024              !!!cp ('t385');
6025            }
6026    
6027            ## ISSUE: An issue in spec there          ## NOTE: There is an "as if <br>" code clone.
6028            $reconstruct_active_formatting_elements->($insert_to_current);
6029            
6030            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
6031    
6032            if ({
6033                 applet => 1, marquee => 1, object => 1,
6034                }->{$token->{tag_name}}) {
6035              !!!cp ('t380');
6036              push @$active_formatting_elements, ['#marker', ''];
6037            } elsif ({
6038                      b => 1, big => 1, em => 1, font => 1, i => 1,
6039                      s => 1, small => 1, strile => 1,
6040                      strong => 1, tt => 1, u => 1,
6041                     }->{$token->{tag_name}}) {
6042              !!!cp ('t375');
6043              push @$active_formatting_elements, $self->{open_elements}->[-1];
6044            } elsif ($token->{tag_name} eq 'input') {
6045              !!!cp ('t388');
6046              ## TODO: associate with $self->{form_element} if defined
6047              pop @{$self->{open_elements}};
6048            } elsif ({
6049                      area => 1, basefont => 1, bgsound => 1, br => 1,
6050                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6051                      #image => 1,
6052                     }->{$token->{tag_name}}) {
6053              !!!cp ('t388.1');
6054              pop @{$self->{open_elements}};
6055            } elsif ($token->{tag_name} eq 'select') {
6056              ## TODO: associate with $self->{form_element} if defined
6057            
6058              if ($self->{insertion_mode} & TABLE_IMS or
6059                  $self->{insertion_mode} & BODY_TABLE_IMS or
6060                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6061                !!!cp ('t400.1');
6062                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6063              } else {
6064                !!!cp ('t400.2');
6065                $self->{insertion_mode} = IN_SELECT_IM;
6066              }
6067          } else {          } else {
6068            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t402');
6069          }          }
6070            
6071            !!!next-token;
6072            redo B;
6073        }        }
6074      } elsif ($phase eq 'trailing end') {      } elsif ($token->{type} == END_TAG_TOKEN) {
6075        ## states in the main stage is preserved yet # MUST        if ($token->{tag_name} eq 'body') {
6076                  ## has a |body| element in scope
6077        if ($token->{type} eq 'DOCTYPE') {          my $i;
6078          !!!parse-error (type => 'after html:#DOCTYPE');          INSCOPE: {
6079          ## Ignore the token            for (reverse @{$self->{open_elements}}) {
6080                if ($_->[1] eq 'body') {
6081                  !!!cp ('t405');
6082                  $i = $_;
6083                  last INSCOPE;
6084                } elsif ({
6085                          applet => 1, table => 1, caption => 1, td => 1, th => 1,
6086                          button => 1, marquee => 1, object => 1, html => 1,
6087                         }->{$_->[1]}) {
6088                  !!!cp ('t405.1');
6089                  last;
6090                }
6091              }
6092    
6093              !!!parse-error (type => 'start tag not allowed',
6094                              value => $token->{tag_name}, token => $token);
6095              ## NOTE: Ignore the token.
6096              !!!next-token;
6097              redo B;
6098            } # INSCOPE
6099    
6100            for (@{$self->{open_elements}}) {
6101              unless ({
6102                       dd => 1, dt => 1, li => 1, p => 1, td => 1,
6103                       th => 1, tr => 1, body => 1, html => 1,
6104                       tbody => 1, tfoot => 1, thead => 1,
6105                      }->{$_->[1]}) {
6106                !!!cp ('t403');
6107                !!!parse-error (type => 'not closed:'.$_->[1], token => $token);
6108                last;
6109              } else {
6110                !!!cp ('t404');
6111              }
6112            }
6113    
6114            $self->{insertion_mode} = AFTER_BODY_IM;
6115          !!!next-token;          !!!next-token;
6116          redo B;          redo B;
6117        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'html') {
6118          my $comment = $self->{document}->create_comment ($token->{data});          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
6119          $self->{document}->append_child ($comment);            ## ISSUE: There is an issue in the spec.
6120              if ($self->{open_elements}->[-1]->[1] ne 'body') {
6121                !!!cp ('t406');
6122                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);
6123              } else {
6124                !!!cp ('t407');
6125              }
6126              $self->{insertion_mode} = AFTER_BODY_IM;
6127              ## reprocess
6128              redo B;
6129            } else {
6130              !!!cp ('t408');
6131              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6132              ## Ignore the token
6133              !!!next-token;
6134              redo B;
6135            }
6136          } elsif ({
6137                    address => 1, blockquote => 1, center => 1, dir => 1,
6138                    div => 1, dl => 1, fieldset => 1, listing => 1,
6139                    menu => 1, ol => 1, pre => 1, ul => 1,
6140                    dd => 1, dt => 1, li => 1,
6141                    applet => 1, button => 1, marquee => 1, object => 1,
6142                   }->{$token->{tag_name}}) {
6143            ## has an element in scope
6144            my $i;
6145            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6146              my $node = $self->{open_elements}->[$_];
6147              if ($node->[1] eq $token->{tag_name}) {
6148                !!!cp ('t410');
6149                $i = $_;
6150                last INSCOPE;
6151              } elsif ({
6152                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6153                        button => 1, marquee => 1, object => 1, html => 1,
6154                       }->{$node->[1]}) {
6155                !!!cp ('t411');
6156                last INSCOPE;
6157              }
6158            } # INSCOPE
6159    
6160            unless (defined $i) { # has an element in scope
6161              !!!cp ('t413');
6162              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6163            } else {
6164              ## Step 1. generate implied end tags
6165              while ({
6166                      dd => ($token->{tag_name} ne 'dd'),
6167                      dt => ($token->{tag_name} ne 'dt'),
6168                      li => ($token->{tag_name} ne 'li'),
6169                      p => 1,
6170                     }->{$self->{open_elements}->[-1]->[1]}) {
6171                !!!cp ('t409');
6172                pop @{$self->{open_elements}};
6173              }
6174    
6175              ## Step 2.
6176              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6177                !!!cp ('t412');
6178                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6179              } else {
6180                !!!cp ('t414');
6181              }
6182    
6183              ## Step 3.
6184              splice @{$self->{open_elements}}, $i;
6185    
6186              ## Step 4.
6187              $clear_up_to_marker->()
6188                  if {
6189                    applet => 1, button => 1, marquee => 1, object => 1,
6190                  }->{$token->{tag_name}};
6191            }
6192          !!!next-token;          !!!next-token;
6193          redo B;          redo B;
6194        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'form') {
6195          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          undef $self->{form_element};
6196            my $data = $1;  
6197            ## As if in the main phase.          ## has an element in scope
6198            ## NOTE: The insertion mode in the main phase          my $i;
6199            ## just before the phase has been changed to the trailing          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6200            ## end phase is either "after body" or "after frameset".            my $node = $self->{open_elements}->[$_];
6201            $reconstruct_active_formatting_elements->($insert_to_current)            if ($node->[1] eq $token->{tag_name}) {
6202              if $phase eq 'main';              !!!cp ('t418');
6203                $i = $_;
6204                last INSCOPE;
6205              } elsif ({
6206                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6207                        button => 1, marquee => 1, object => 1, html => 1,
6208                       }->{$node->[1]}) {
6209                !!!cp ('t419');
6210                last INSCOPE;
6211              }
6212            } # INSCOPE
6213    
6214            unless (defined $i) { # has an element in scope
6215              !!!cp ('t421');
6216              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6217            } else {
6218              ## Step 1. generate implied end tags
6219              while ({
6220                      dd => 1, dt => 1, li => 1, p => 1,
6221                     }->{$self->{open_elements}->[-1]->[1]}) {
6222                !!!cp ('t417');
6223                pop @{$self->{open_elements}};
6224              }
6225                        
6226            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
6227              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6228                !!!cp ('t417.1');
6229                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6230              } else {
6231                !!!cp ('t420');
6232              }  
6233                        
6234            unless (length $token->{data}) {            ## Step 3.
6235              !!!next-token;            splice @{$self->{open_elements}}, $i;
6236              redo B;          }
6237    
6238            !!!next-token;
6239            redo B;
6240          } elsif ({
6241                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6242                   }->{$token->{tag_name}}) {
6243            ## has an element in scope
6244            my $i;
6245            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6246              my $node = $self->{open_elements}->[$_];
6247              if ({
6248                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6249                  }->{$node->[1]}) {
6250                !!!cp ('t423');
6251                $i = $_;
6252                last INSCOPE;
6253              } elsif ({
6254                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6255                        button => 1, marquee => 1, object => 1, html => 1,
6256                       }->{$node->[1]}) {
6257                !!!cp ('t424');
6258                last INSCOPE;
6259            }            }
6260            } # INSCOPE
6261    
6262            unless (defined $i) { # has an element in scope
6263              !!!cp ('t425.1');
6264              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6265            } else {
6266              ## Step 1. generate implied end tags
6267              while ({
6268                      dd => 1, dt => 1, li => 1, p => 1,
6269                     }->{$self->{open_elements}->[-1]->[1]}) {
6270                !!!cp ('t422');
6271                pop @{$self->{open_elements}};
6272              }
6273              
6274              ## Step 2.
6275              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6276                !!!cp ('t425');
6277                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6278              } else {
6279                !!!cp ('t426');
6280              }
6281    
6282              ## Step 3.
6283              splice @{$self->{open_elements}}, $i;
6284          }          }
6285            
6286            !!!next-token;
6287            redo B;
6288          } elsif ($token->{tag_name} eq 'p') {
6289            ## has an element in scope
6290            my $i;
6291            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6292              my $node = $self->{open_elements}->[$_];
6293              if ($node->[1] eq $token->{tag_name}) {
6294                !!!cp ('t410.1');
6295                $i = $_;
6296                last INSCOPE;
6297              } elsif ({
6298                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6299                        button => 1, marquee => 1, object => 1, html => 1,
6300                       }->{$node->[1]}) {
6301                !!!cp ('t411.1');
6302                last INSCOPE;
6303              }
6304            } # INSCOPE
6305    
6306          !!!parse-error (type => 'after html:#character');          if (defined $i) {
6307          $phase = 'main';            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6308          ## reprocess              !!!cp ('t412.1');
6309                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6310              } else {
6311                !!!cp ('t414.1');
6312              }
6313    
6314              splice @{$self->{open_elements}}, $i;
6315            } else {
6316              !!!cp ('t413.1');
6317              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6318    
6319              !!!cp ('t415.1');
6320              ## As if <p>, then reprocess the current token
6321              my $el;
6322              !!!create-element ($el, 'p');
6323              $insert->($el);
6324              ## NOTE: Not inserted into |$self->{open_elements}|.
6325            }
6326    
6327            !!!next-token;
6328          redo B;          redo B;
6329        } elsif ($token->{type} eq 'start tag' or        } elsif ({
6330                 $token->{type} eq 'end tag') {                  a => 1,
6331          !!!parse-error (type => 'after html:'.$token->{tag_name});                  b => 1, big => 1, em => 1, font => 1, i => 1,
6332          $phase = 'main';                  nobr => 1, s => 1, small => 1, strile => 1,
6333          ## reprocess                  strong => 1, tt => 1, u => 1,
6334                   }->{$token->{tag_name}}) {
6335            !!!cp ('t427');
6336            $formatting_end_tag->($token);
6337          redo B;          redo B;
6338        } elsif ($token->{type} eq 'end-of-file') {        } elsif ($token->{tag_name} eq 'br') {
6339          ## Stop parsing          !!!cp ('t428');
6340          last B;          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6341    
6342            ## As if <br>
6343            $reconstruct_active_formatting_elements->($insert_to_current);
6344            
6345            my $el;
6346            !!!create-element ($el, 'br');
6347            $insert->($el);
6348            
6349            ## Ignore the token.
6350            !!!next-token;
6351            redo B;
6352          } elsif ({
6353                    caption => 1, col => 1, colgroup => 1, frame => 1,
6354                    frameset => 1, head => 1, option => 1, optgroup => 1,
6355                    tbody => 1, td => 1, tfoot => 1, th => 1,
6356                    thead => 1, tr => 1,
6357                    area => 1, basefont => 1, bgsound => 1,
6358                    embed => 1, hr => 1, iframe => 1, image => 1,
6359                    img => 1, input => 1, isindex => 1, noembed => 1,
6360                    noframes => 1, param => 1, select => 1, spacer => 1,
6361                    table => 1, textarea => 1, wbr => 1,
6362                    noscript => 0, ## TODO: if scripting is enabled
6363                   }->{$token->{tag_name}}) {
6364            !!!cp ('t429');
6365            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6366            ## Ignore the token
6367            !!!next-token;
6368            redo B;
6369            
6370            ## ISSUE: Issue on HTML5 new elements in spec
6371            
6372        } else {        } else {
6373          die "$0: $token->{type}: Unknown token";          ## Step 1
6374            my $node_i = -1;
6375            my $node = $self->{open_elements}->[$node_i];
6376    
6377            ## Step 2
6378            S2: {
6379              if ($node->[1] eq $token->{tag_name}) {
6380                ## Step 1
6381                ## generate implied end tags
6382                while ({
6383                        dd => 1, dt => 1, li => 1, p => 1,
6384                       }->{$self->{open_elements}->[-1]->[1]}) {
6385                  !!!cp ('t430');
6386                  ## ISSUE: Can this case be reached?
6387                  pop @{$self->{open_elements}};
6388                }
6389            
6390                ## Step 2
6391                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6392                  !!!cp ('t431');
6393                  ## NOTE: <x><y></x>
6394                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6395                } else {
6396                  !!!cp ('t432');
6397                }
6398                
6399                ## Step 3
6400                splice @{$self->{open_elements}}, $node_i;
6401    
6402                !!!next-token;
6403                last S2;
6404              } else {
6405                ## Step 3
6406                if (not $formatting_category->{$node->[1]} and
6407                    #not $phrasing_category->{$node->[1]} and
6408                    ($special_category->{$node->[1]} or
6409                     $scoping_category->{$node->[1]})) {
6410                  !!!cp ('t433');
6411                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6412                  ## Ignore the token
6413                  !!!next-token;
6414                  last S2;
6415                }
6416    
6417                !!!cp ('t434');
6418              }
6419              
6420              ## Step 4
6421              $node_i--;
6422              $node = $self->{open_elements}->[$node_i];
6423              
6424              ## Step 5;
6425              redo S2;
6426            } # S2
6427            redo B;
6428        }        }
6429      }      }
6430        redo B;
6431    } # B    } # B
6432    
6433    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4822  sub set_inner_html ($$$) { Line 6441  sub set_inner_html ($$$) {
6441    my $s = \$_[0];    my $s = \$_[0];
6442    my $onerror = $_[1];    my $onerror = $_[1];
6443    
6444      ## ISSUE: Should {confident} be true?
6445    
6446    my $nt = $node->node_type;    my $nt = $node->node_type;
6447    if ($nt == 9) {    if ($nt == 9) {
6448      # MUST      # MUST
# Line 4844  sub set_inner_html ($$$) { Line 6465  sub set_inner_html ($$$) {
6465      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
6466    
6467      ## Step 1 # MUST      ## Step 1 # MUST
6468      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
6469      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
6470        $doc->manakai_is_html (1);
6471      my $p = $class->new;      my $p = $class->new;
6472      $p->{document} = $doc;      $p->{document} = $doc;
6473    
6474      ## Step 9 # MUST      ## Step 8 # MUST
6475      my $i = 0;      my $i = 0;
6476      my $line = 1;      my $line = 1;
6477      my $column = 0;      my $column = 0;
6478      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6479        my $self = shift;        my $self = shift;
6480        $self->{next_input_character} = -1 and return if $i >= length $$s;  
6481        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
6482          unshift @{$self->{prev_char}}, $self->{next_char};
6483    
6484          $self->{next_char} = -1 and return if $i >= length $$s;
6485          $self->{next_char} = ord substr $$s, $i++, 1;
6486        $column++;        $column++;
6487    
6488        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6489          $line++;          $line++;
6490          $column = 0;          $column = 0;
6491        } elsif ($self->{next_input_character} == 0x000D) { # CR          !!!cp ('i1');
6492          if ($i >= length $$s) {        } elsif ($self->{next_char} == 0x000D) { # CR
6493            #          $i++ if substr ($$s, $i, 1) eq "\x0A";
6494          } 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;  
           }  
         }  
         $self->{next_input_character} = 0x000A; # LF # MUST  
6495          $line++;          $line++;
6496          $column = 0;          $column = 0;
6497        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6498          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6499        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6500          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i3');
6501          } elsif ($self->{next_char} == 0x0000) { # NULL
6502            !!!cp ('i4');
6503            !!!parse-error (type => 'NULL');
6504            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6505        }        }
6506      };      };
6507        $p->{prev_char} = [-1, -1, -1];
6508        $p->{next_char} = -1;
6509            
6510      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6511        my (%opt) = @_;        my (%opt) = @_;
# Line 4895  sub set_inner_html ($$$) { Line 6519  sub set_inner_html ($$$) {
6519      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6520    
6521      ## Step 2      ## Step 2
6522      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6523      $p->{content_model_flag} = {      $p->{content_model} = {
6524        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
6525        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
6526        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
6527        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
6528        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
6529        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
6530        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
6531        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
6532        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
6533        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
6534      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
6535         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
6536            unless defined $p->{content_model};
6537            ## ISSUE: What is "the name of the element"? local name?
6538    
6539      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6540    
6541      ## Step 4      ## Step 3
6542      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6543        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6544    
6545      ## Step 5 # MUST      ## Step 4 # MUST
6546      $doc->append_child ($root);      $doc->append_child ($root);
6547    
6548      ## Step 6 # MUST      ## Step 5 # MUST
6549      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6550    
6551      undef $p->{head_element};      undef $p->{head_element};
6552    
6553      ## Step 7 # MUST      ## Step 6 # MUST
6554      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6555    
6556      ## Step 8 # MUST      ## Step 7 # MUST
6557      my $anode = $node;      my $anode = $node;
6558      AN: while (defined $anode) {      AN: while (defined $anode) {
6559        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6560          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6561          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6562            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6563                !!!cp ('i5');
6564              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6565              last AN;              last AN;
6566            }            }
# Line 4942  sub set_inner_html ($$$) { Line 6569  sub set_inner_html ($$$) {
6569        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6570      } # AN      } # AN
6571            
6572      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6573      {      {
6574        my $self = $p;        my $self = $p;
6575        !!!next-token;        !!!next-token;
6576      }      }
6577      $p->_tree_construction_main;      $p->_tree_construction_main;
6578    
6579      ## Step 11 # MUST      ## Step 10 # MUST
6580      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6581      for (@cn) {      for (@cn) {
6582        $node->remove_child ($_);        $node->remove_child ($_);
6583      }      }
6584      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6585    
6586      ## Step 12 # MUST      ## Step 11 # MUST
6587      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6588      for (@cn) {      for (@cn) {
6589          $this_doc->adopt_node ($_);
6590        $node->append_child ($_);        $node->append_child ($_);
6591      }      }
6592      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
6593    
6594      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6595    } else {    } else {
# Line 4972  sub set_inner_html ($$$) { Line 6599  sub set_inner_html ($$$) {
6599    
6600  } # tree construction stage  } # tree construction stage
6601    
6602  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6603    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  
6604    
6605  1;  1;
6606  # $Date$  # $Date$

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.114

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24