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

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.122

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24