/[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.3 by wakaba, Wed May 2 13:44:34 2007 UTC revision 1.108 by wakaba, Mon Mar 10 10:55:57 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    
29  my $entity_char = {  my $c1_entity_char = {
30    AElig => "\x{00C6}",    0x80 => 0x20AC,
31    Aacute => "\x{00C1}",    0x81 => 0xFFFD,
32    Acirc => "\x{00C2}",    0x82 => 0x201A,
33    Agrave => "\x{00C0}",    0x83 => 0x0192,
34    Alpha => "\x{0391}",    0x84 => 0x201E,
35    Aring => "\x{00C5}",    0x85 => 0x2026,
36    Atilde => "\x{00C3}",    0x86 => 0x2020,
37    Auml => "\x{00C4}",    0x87 => 0x2021,
38    Beta => "\x{0392}",    0x88 => 0x02C6,
39    Ccedil => "\x{00C7}",    0x89 => 0x2030,
40    Chi => "\x{03A7}",    0x8A => 0x0160,
41    Dagger => "\x{2021}",    0x8B => 0x2039,
42    Delta => "\x{0394}",    0x8C => 0x0152,
43    ETH => "\x{00D0}",    0x8D => 0xFFFD,
44    Eacute => "\x{00C9}",    0x8E => 0x017D,
45    Ecirc => "\x{00CA}",    0x8F => 0xFFFD,
46    Egrave => "\x{00C8}",    0x90 => 0xFFFD,
47    Epsilon => "\x{0395}",    0x91 => 0x2018,
48    Eta => "\x{0397}",    0x92 => 0x2019,
49    Euml => "\x{00CB}",    0x93 => 0x201C,
50    Gamma => "\x{0393}",    0x94 => 0x201D,
51    Iacute => "\x{00CD}",    0x95 => 0x2022,
52    Icirc => "\x{00CE}",    0x96 => 0x2013,
53    Igrave => "\x{00CC}",    0x97 => 0x2014,
54    Iota => "\x{0399}",    0x98 => 0x02DC,
55    Iuml => "\x{00CF}",    0x99 => 0x2122,
56    Kappa => "\x{039A}",    0x9A => 0x0161,
57    Lambda => "\x{039B}",    0x9B => 0x203A,
58    Mu => "\x{039C}",    0x9C => 0x0153,
59    Ntilde => "\x{00D1}",    0x9D => 0xFFFD,
60    Nu => "\x{039D}",    0x9E => 0x017E,
61    OElig => "\x{0152}",    0x9F => 0x0178,
62    Oacute => "\x{00D3}",  }; # $c1_entity_char
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 };  
63    
64  my $special_category = {  my $special_category = {
65    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,
# Line 293  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 302  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        ## TODO: if $charset is supported
112        ## TODO: normalize charset name
113    
114        ## "Change the encoding" algorithm:
115    
116        ## Step 1    
117        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
118          $charset = 'utf-8';
119        }
120    
121        ## Step 2
122        if (defined $self->{input_encoding} and
123            $self->{input_encoding} eq $charset) {
124          $self->{confident} = 1;
125          return;
126        }
127    
128        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
129            ':'.$charset, level => 'w');
130    
131        ## Step 3
132        # if (can) {
133          ## change the encoding on the fly.
134          #$self->{confident} = 1;
135          #return;
136        # }
137    
138        ## Step 4
139        throw Whatpm::HTML::RestartParser (charset => $charset);
140      }; # $self->{change_encoding}
141    
142      my @args = @_; shift @args; # $s
143      my $return;
144      try {
145        $return = $self->parse_char_string ($s, @args);  
146      } catch Whatpm::HTML::RestartParser with {
147        my $charset = shift->{charset};
148        $s = \ (Encode::decode ($charset, $$bytes_s));    
149        $self->{input_encoding} = $charset; ## TODO: normalize
150        $self->{confident} = 1;
151        $return = $self->parse_char_string ($s, @args);
152      };
153      return $return;
154    } # parse_byte_string
155    
156    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
157    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
158    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
159    ## because the core part of our HTML parser expects a string of character,
160    ## not a string of bytes or code units or anything which might contain a BOM.
161    ## Therefore, any parser interface that accepts a string of bytes,
162    ## such as |parse_byte_string| in this module, must ensure that it does
163    ## strip the BOM and never strip any ZWNBSP.
164    
165    *parse_char_string = \&parse_string;
166    
167  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
168    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
169    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
170    $self->{document} = $_[1];    $self->{document} = $_[1];
171      @{$self->{document}->child_nodes} = ();
172    
173    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
174    
175      $self->{confident} = 1 unless exists $self->{confident};
176      $self->{document}->input_encoding ($self->{input_encoding})
177          if defined $self->{input_encoding};
178    
179    my $i = 0;    my $i = 0;
180    my $line = 1;    my $line = 1;
181    my $column = 0;    my $column = 0;
182    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
183      my $self = shift;      my $self = shift;
184      $self->{next_input_character} = -1 and return if $i >= length $$s;  
185      $self->{next_input_character} = ord substr $$s, $i++, 1;      pop @{$self->{prev_char}};
186        unshift @{$self->{prev_char}}, $self->{next_char};
187    
188        $self->{next_char} = -1 and return if $i >= length $$s;
189        $self->{next_char} = ord substr $$s, $i++, 1;
190      $column++;      $column++;
191            
192      if ($self->{next_input_character} == 0x000D) { # CR      if ($self->{next_char} == 0x000A) { # LF
       if ($i >= length $$s) {  
         #  
       } else {  
         my $next_char = ord substr $$s, $i++, 1;  
         if ($next_char == 0x000A) { # LF  
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
       }  
       $self->{next_input_character} = 0x000A; # LF # MUST  
193        $line++;        $line++;
194        $column = -1;        $column = 0;
195      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} == 0x000D) { # CR
196        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $i++ if substr ($$s, $i, 1) eq "\x0A";
197      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
198        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $line++;
199          $column = 0;
200        } elsif ($self->{next_char} > 0x10FFFF) {
201          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
202        } elsif ($self->{next_char} == 0x0000) { # NULL
203          !!!parse-error (type => 'NULL');
204          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
205      }      }
206    };    };
207      $self->{prev_char} = [-1, -1, -1];
208      $self->{next_char} = -1;
209    
210    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
211      my (%opt) = @_;      my (%opt) = @_;
# Line 358  sub parse_string ($$$;$) { Line 226  sub parse_string ($$$;$) {
226  sub new ($) {  sub new ($) {
227    my $class = shift;    my $class = shift;
228    my $self = bless {}, $class;    my $self = bless {}, $class;
229    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
230      $self->{next_input_character} = -1;      $self->{next_char} = -1;
231    };    };
232    $self->{parse_error} = sub {    $self->{parse_error} = sub {
233      #      #
234    };    };
235      $self->{change_encoding} = sub {
236        # if ($_[0] is a supported encoding) {
237        #   run "change the encoding" algorithm;
238        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
239        # }
240      };
241      $self->{application_cache_selection} = sub {
242        #
243      };
244    return $self;    return $self;
245  } # new  } # new
246    
247    sub CM_ENTITY () { 0b001 } # & markup in data
248    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
249    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
250    
251    sub PLAINTEXT_CONTENT_MODEL () { 0 }
252    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
253    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
254    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
255    
256    sub DATA_STATE () { 0 }
257    sub ENTITY_DATA_STATE () { 1 }
258    sub TAG_OPEN_STATE () { 2 }
259    sub CLOSE_TAG_OPEN_STATE () { 3 }
260    sub TAG_NAME_STATE () { 4 }
261    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
262    sub ATTRIBUTE_NAME_STATE () { 6 }
263    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
264    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
265    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
266    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
267    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
268    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
269    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
270    sub COMMENT_START_STATE () { 14 }
271    sub COMMENT_START_DASH_STATE () { 15 }
272    sub COMMENT_STATE () { 16 }
273    sub COMMENT_END_STATE () { 17 }
274    sub COMMENT_END_DASH_STATE () { 18 }
275    sub BOGUS_COMMENT_STATE () { 19 }
276    sub DOCTYPE_STATE () { 20 }
277    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
278    sub DOCTYPE_NAME_STATE () { 22 }
279    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
280    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
281    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
282    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
283    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
284    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
285    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
286    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
287    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
288    sub BOGUS_DOCTYPE_STATE () { 32 }
289    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
290    
291    sub DOCTYPE_TOKEN () { 1 }
292    sub COMMENT_TOKEN () { 2 }
293    sub START_TAG_TOKEN () { 3 }
294    sub END_TAG_TOKEN () { 4 }
295    sub END_OF_FILE_TOKEN () { 5 }
296    sub CHARACTER_TOKEN () { 6 }
297    
298    sub AFTER_HTML_IMS () { 0b100 }
299    sub HEAD_IMS ()       { 0b1000 }
300    sub BODY_IMS ()       { 0b10000 }
301    sub BODY_TABLE_IMS () { 0b100000 }
302    sub TABLE_IMS ()      { 0b1000000 }
303    sub ROW_IMS ()        { 0b10000000 }
304    sub BODY_AFTER_IMS () { 0b100000000 }
305    sub FRAME_IMS ()      { 0b1000000000 }
306    sub SELECT_IMS ()     { 0b10000000000 }
307    
308    ## NOTE: "initial" and "before html" insertion modes have no constants.
309    
310    ## NOTE: "after after body" insertion mode.
311    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
312    
313    ## NOTE: "after after frameset" insertion mode.
314    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
315    
316    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
317    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
318    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
319    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
320    sub IN_BODY_IM () { BODY_IMS }
321    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
322    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
323    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
324    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
325    sub IN_TABLE_IM () { TABLE_IMS }
326    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
327    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
328    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
329    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
330    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
331    sub IN_COLUMN_GROUP_IM () { 0b10 }
332    
333  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
334    
335  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
336    my $self = shift;    my $self = shift;
337    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
338    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
339    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
340    undef $self->{current_attribute};    undef $self->{current_attribute};
341    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
342    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
343    $self->{char} = [];    $self->{char} = [];
344    # $self->{next_input_character}    # $self->{next_char}
345    !!!next-input-character;    !!!next-input-character;
346    $self->{token} = [];    $self->{token} = [];
347      # $self->{escape}
348  } # _initialize_tokenizer  } # _initialize_tokenizer
349    
350  ## A token has:  ## A token has:
351  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
352  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
353  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
354      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
355  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
356  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
357  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
358    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
359  ## Macros  ##        ->{name}
360  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
361  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
362  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
363    
364  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
365    
# Line 405  sub _initialize_tokenizer ($) { Line 369  sub _initialize_tokenizer ($) {
369  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
370  ## and removed from the list.  ## and removed from the list.
371    
372    ## NOTE: HTML5 "Writing HTML documents" section, applied to
373    ## documents and not to user agents and conformance checkers,
374    ## contains some requirements that are not detected by the
375    ## parsing algorithm:
376    ## - Some requirements on character encoding declarations. ## TODO
377    ## - "Elements MUST NOT contain content that their content model disallows."
378    ##   ... Some are parse error, some are not (will be reported by c.c.).
379    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
380    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
381    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
382    
383    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
384    ## be detected by the HTML5 parsing algorithm:
385    ## - Text,
386    
387  sub _get_next_token ($) {  sub _get_next_token ($) {
388    my $self = shift;    my $self = shift;
389    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 412  sub _get_next_token ($) { Line 391  sub _get_next_token ($) {
391    }    }
392    
393    A: {    A: {
394      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
395        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
396          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
397              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
398            $self->{state} = 'entity data';            !!!cp (1);
399              $self->{state} = ENTITY_DATA_STATE;
400            !!!next-input-character;            !!!next-input-character;
401            redo A;            redo A;
402          } else {          } else {
403              !!!cp (2);
404            #            #
405          }          }
406        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
407          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
408            $self->{state} = 'tag open';            unless ($self->{escape}) {
409                if ($self->{prev_char}->[0] == 0x002D and # -
410                    $self->{prev_char}->[1] == 0x0021 and # !
411                    $self->{prev_char}->[2] == 0x003C) { # <
412                  !!!cp (3);
413                  $self->{escape} = 1;
414                } else {
415                  !!!cp (4);
416                }
417              } else {
418                !!!cp (5);
419              }
420            }
421            
422            #
423          } elsif ($self->{next_char} == 0x003C) { # <
424            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
425                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
426                 not $self->{escape})) {
427              !!!cp (6);
428              $self->{state} = TAG_OPEN_STATE;
429            !!!next-input-character;            !!!next-input-character;
430            redo A;            redo A;
431          } else {          } else {
432              !!!cp (7);
433            #            #
434          }          }
435        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
436          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
437                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
438              if ($self->{prev_char}->[0] == 0x002D and # -
439                  $self->{prev_char}->[1] == 0x002D) { # -
440                !!!cp (8);
441                delete $self->{escape};
442              } else {
443                !!!cp (9);
444              }
445            } else {
446              !!!cp (10);
447            }
448            
449            #
450          } elsif ($self->{next_char} == -1) {
451            !!!cp (11);
452            !!!emit ({type => END_OF_FILE_TOKEN});
453          last A; ## TODO: ok?          last A; ## TODO: ok?
454          } else {
455            !!!cp (12);
456        }        }
457        # Anything else        # Anything else
458        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
459                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char}};
460        ## Stay in the data state        ## Stay in the data state
461        !!!next-input-character;        !!!next-input-character;
462    
463        !!!emit ($token);        !!!emit ($token);
464    
465        redo A;        redo A;
466      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
467        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
468                
469        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
470    
471        $self->{state} = 'data';        $self->{state} = DATA_STATE;
472        # next-input-character is already done        # next-input-character is already done
473    
474        unless (defined $token) {        unless (defined $token) {
475          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
476            !!!emit ({type => CHARACTER_TOKEN, data => '&'});
477        } else {        } else {
478            !!!cp (14);
479          !!!emit ($token);          !!!emit ($token);
480        }        }
481    
482        redo A;        redo A;
483      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
484        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
485            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
486          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
487            !!!next-input-character;            !!!next-input-character;
488            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
489            redo A;            redo A;
490          } else {          } else {
491              !!!cp (16);
492            ## reconsume            ## reconsume
493            $self->{state} = 'data';            $self->{state} = DATA_STATE;
494    
495            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
496    
497            redo A;            redo A;
498          }          }
499        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
500          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
501            $self->{state} = 'markup declaration open';            !!!cp (17);
502              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
503            !!!next-input-character;            !!!next-input-character;
504            redo A;            redo A;
505          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
506            $self->{state} = 'close tag open';            !!!cp (18);
507              $self->{state} = CLOSE_TAG_OPEN_STATE;
508            !!!next-input-character;            !!!next-input-character;
509            redo A;            redo A;
510          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
511                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
512              !!!cp (19);
513            $self->{current_token}            $self->{current_token}
514              = {type => 'start tag',              = {type => START_TAG_TOKEN,
515                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020)};
516            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
517            !!!next-input-character;            !!!next-input-character;
518            redo A;            redo A;
519          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
520                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
521            $self->{current_token} = {type => 'start tag',            !!!cp (20);
522                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
523            $self->{state} = 'tag name';                              tag_name => chr ($self->{next_char})};
524              $self->{state} = TAG_NAME_STATE;
525            !!!next-input-character;            !!!next-input-character;
526            redo A;            redo A;
527          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
528              !!!cp (21);
529            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
530            $self->{state} = 'data';            $self->{state} = DATA_STATE;
531            !!!next-input-character;            !!!next-input-character;
532    
533            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
534    
535            redo A;            redo A;
536          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
537              !!!cp (22);
538            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
539            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
540            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_char} is intentionally left as is
541            redo A;            redo A;
542          } else {          } else {
543              !!!cp (23);
544            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
545            $self->{state} = 'data';            $self->{state} = DATA_STATE;
546            ## reconsume            ## reconsume
547    
548            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
549    
550            redo A;            redo A;
551          }          }
552        } else {        } else {
553          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
554        }        }
555      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
556        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
557            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
558          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
559          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
560            push @next_char, $self->{next_input_character};            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
561            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              push @next_char, $self->{next_char};
562            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
563            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
564              !!!next-input-character;              if ($self->{next_char} == $c or $self->{next_char} == $C) {
565              next TAGNAME;                !!!cp (24);
566            } else {                !!!next-input-character;
567              !!!parse-error (type => 'unmatched end tag');                next TAGNAME;
568              $self->{next_input_character} = shift @next_char; # reconsume              } else {
569              !!!back-next-input-character (@next_char);                !!!cp (25);
570              $self->{state} = 'data';                $self->{next_char} = shift @next_char; # reconsume
571                  !!!back-next-input-character (@next_char);
572              !!!emit ({type => 'character', data => '</'});                $self->{state} = DATA_STATE;
573    
574                  !!!emit ({type => CHARACTER_TOKEN, data => '</'});
575      
576                  redo A;
577                }
578              }
579              push @next_char, $self->{next_char};
580          
581              unless ($self->{next_char} == 0x0009 or # HT
582                      $self->{next_char} == 0x000A or # LF
583                      $self->{next_char} == 0x000B or # VT
584                      $self->{next_char} == 0x000C or # FF
585                      $self->{next_char} == 0x0020 or # SP
586                      $self->{next_char} == 0x003E or # >
587                      $self->{next_char} == 0x002F or # /
588                      $self->{next_char} == -1) {
589                !!!cp (26);
590                $self->{next_char} = shift @next_char; # reconsume
591                !!!back-next-input-character (@next_char);
592                $self->{state} = DATA_STATE;
593                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
594              redo A;              redo A;
595              } else {
596                !!!cp (27);
597                $self->{next_char} = shift @next_char;
598                !!!back-next-input-character (@next_char);
599                # and consume...
600            }            }
         }  
         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;  
601          } else {          } else {
602            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
603            !!!back-next-input-character (@next_char);            !!!cp (28);
604            # and consume...            # next-input-character is already done
605              $self->{state} = DATA_STATE;
606              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
607              redo A;
608          }          }
609        }        }
610                
611        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
612            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
613          $self->{current_token} = {type => 'end tag',          !!!cp (29);
614                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token} = {type => END_TAG_TOKEN,
615          $self->{state} = 'tag name';                            tag_name => chr ($self->{next_char} + 0x0020)};
616          !!!next-input-character;          $self->{state} = TAG_NAME_STATE;
617          redo A;          !!!next-input-character;
618        } elsif (0x0061 <= $self->{next_input_character} and          redo A;
619                 $self->{next_input_character} <= 0x007A) { # a..z        } elsif (0x0061 <= $self->{next_char} and
620          $self->{current_token} = {type => 'end tag',                 $self->{next_char} <= 0x007A) { # a..z
621                            tag_name => chr ($self->{next_input_character})};          !!!cp (30);
622          $self->{state} = 'tag name';          $self->{current_token} = {type => END_TAG_TOKEN,
623                              tag_name => chr ($self->{next_char})};
624            $self->{state} = TAG_NAME_STATE;
625          !!!next-input-character;          !!!next-input-character;
626          redo A;          redo A;
627        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
628            !!!cp (31);
629          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
630          $self->{state} = 'data';          $self->{state} = DATA_STATE;
631          !!!next-input-character;          !!!next-input-character;
632          redo A;          redo A;
633        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
634            !!!cp (32);
635          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
636          $self->{state} = 'data';          $self->{state} = DATA_STATE;
637          # reconsume          # reconsume
638    
639          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
640    
641          redo A;          redo A;
642        } else {        } else {
643            !!!cp (33);
644          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
645          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
646          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_char} is intentionally left as is
647          redo A;          redo A;
648        }        }
649      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
650        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
651            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
652            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
653            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
654            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
655          $self->{state} = 'before attribute name';          !!!cp (34);
656          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
657          redo A;          !!!next-input-character;
658        } elsif ($self->{next_input_character} == 0x003E) { # >          redo A;
659          if ($self->{current_token}->{type} eq 'start tag') {        } elsif ($self->{next_char} == 0x003E) { # >
660            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
661              !!!cp (35);
662              $self->{current_token}->{first_start_tag}
663                  = not defined $self->{last_emitted_start_tag_name};
664            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
665          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
666            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
667            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
668              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
669            }            #  !!! cp (36);
670              #  !!! parse-error (type => 'end tag attribute');
671              #} else {
672                !!!cp (37);
673              #}
674          } else {          } else {
675            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
676          }          }
677          $self->{state} = 'data';          $self->{state} = DATA_STATE;
678          !!!next-input-character;          !!!next-input-character;
679    
680          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
681    
682          redo A;          redo A;
683        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
684                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
685          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
686            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
687            # start tag or end tag            # start tag or end tag
688          ## Stay in this state          ## Stay in this state
689          !!!next-input-character;          !!!next-input-character;
690          redo A;          redo A;
691        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
692          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
693          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
694              !!!cp (39);
695              $self->{current_token}->{first_start_tag}
696                  = not defined $self->{last_emitted_start_tag_name};
697            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
698          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
699            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
700            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
701              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
702            }            #  !!! cp (40);
703              #  !!! parse-error (type => 'end tag attribute');
704              #} else {
705                !!!cp (41);
706              #}
707          } else {          } else {
708            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
709          }          }
710          $self->{state} = 'data';          $self->{state} = DATA_STATE;
711          # reconsume          # reconsume
712    
713          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
714    
715          redo A;          redo A;
716        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
717          !!!next-input-character;          !!!next-input-character;
718          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
719              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
720              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
721            # permitted slash            # permitted slash
722              !!!cp (42);
723            #            #
724          } else {          } else {
725              !!!cp (43);
726            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
727          }          }
728          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
729          # next-input-character is already done          # next-input-character is already done
730          redo A;          redo A;
731        } else {        } else {
732          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
733            $self->{current_token}->{tag_name} .= chr $self->{next_char};
734            # start tag or end tag            # start tag or end tag
735          ## Stay in the state          ## Stay in the state
736          !!!next-input-character;          !!!next-input-character;
737          redo A;          redo A;
738        }        }
739      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
740        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
741            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
742            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
743            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
744            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
745            !!!cp (45);
746          ## Stay in the state          ## Stay in the state
747          !!!next-input-character;          !!!next-input-character;
748          redo A;          redo A;
749        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
750          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
751              !!!cp (46);
752              $self->{current_token}->{first_start_tag}
753                  = not defined $self->{last_emitted_start_tag_name};
754            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
755          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
756            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
757            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
758                !!!cp (47);
759              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
760              } else {
761                !!!cp (48);
762            }            }
763          } else {          } else {
764            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
765          }          }
766          $self->{state} = 'data';          $self->{state} = DATA_STATE;
767          !!!next-input-character;          !!!next-input-character;
768    
769          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
770    
771          redo A;          redo A;
772        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
773                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
774          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
775            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
776                                value => ''};                                value => ''};
777          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
778          !!!next-input-character;          !!!next-input-character;
779          redo A;          redo A;
780        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
781          !!!next-input-character;          !!!next-input-character;
782          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
783              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
784              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
785            # permitted slash            # permitted slash
786              !!!cp (50);
787            #            #
788          } else {          } else {
789              !!!cp (51);
790            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
791          }          }
792          ## Stay in the state          ## Stay in the state
793          # next-input-character is already done          # next-input-character is already done
794          redo A;          redo A;
795        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
796          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
797          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
798              !!!cp (52);
799              $self->{current_token}->{first_start_tag}
800                  = not defined $self->{last_emitted_start_tag_name};
801            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
802          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
803            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
804            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
805                !!!cp (53);
806              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
807              } else {
808                !!!cp (54);
809            }            }
810          } else {          } else {
811            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
812          }          }
813          $self->{state} = 'data';          $self->{state} = DATA_STATE;
814          # reconsume          # reconsume
815    
816          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
817    
818          redo A;          redo A;
819        } else {        } else {
820          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
821                 0x0022 => 1, # "
822                 0x0027 => 1, # '
823                 0x003D => 1, # =
824                }->{$self->{next_char}}) {
825              !!!cp (55);
826              !!!parse-error (type => 'bad attribute name');
827            } else {
828              !!!cp (56);
829            }
830            $self->{current_attribute} = {name => chr ($self->{next_char}),
831                                value => ''};                                value => ''};
832          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
833          !!!next-input-character;          !!!next-input-character;
834          redo A;          redo A;
835        }        }
836      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
837        my $before_leave = sub {        my $before_leave = sub {
838          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
839              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
840            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
841              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
842            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
843          } else {          } else {
844              !!!cp (58);
845            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
846              = $self->{current_attribute};              = $self->{current_attribute};
847          }          }
848        }; # $before_leave        }; # $before_leave
849    
850        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
851            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
852            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
853            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
854            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
855            !!!cp (59);
856          $before_leave->();          $before_leave->();
857          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
858          !!!next-input-character;          !!!next-input-character;
859          redo A;          redo A;
860        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
861            !!!cp (60);
862          $before_leave->();          $before_leave->();
863          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
864          !!!next-input-character;          !!!next-input-character;
865          redo A;          redo A;
866        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
867          $before_leave->();          $before_leave->();
868          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
869              !!!cp (61);
870              $self->{current_token}->{first_start_tag}
871                  = not defined $self->{last_emitted_start_tag_name};
872            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
873          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
874            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
875              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
876            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
877              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
878            }            }
879          } else {          } else {
880            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
881          }          }
882          $self->{state} = 'data';          $self->{state} = DATA_STATE;
883          !!!next-input-character;          !!!next-input-character;
884    
885          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
886    
887          redo A;          redo A;
888        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
889                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
890          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
891            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
892          ## Stay in the state          ## Stay in the state
893          !!!next-input-character;          !!!next-input-character;
894          redo A;          redo A;
895        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
896          $before_leave->();          $before_leave->();
897          !!!next-input-character;          !!!next-input-character;
898          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
899              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
900              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
901            # permitted slash            # permitted slash
902              !!!cp (64);
903            #            #
904          } else {          } else {
905              !!!cp (65);
906            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
907          }          }
908          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
909          # next-input-character is already done          # next-input-character is already done
910          redo A;          redo A;
911        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
912          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
913          $before_leave->();          $before_leave->();
914          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
915              !!!cp (66);
916              $self->{current_token}->{first_start_tag}
917                  = not defined $self->{last_emitted_start_tag_name};
918            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
919          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
920            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
921            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
922                !!!cp (67);
923              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
924              } else {
925                ## NOTE: This state should never be reached.
926                !!!cp (68);
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          # reconsume          # reconsume
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        } else {        } else {
938          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
939                $self->{next_char} == 0x0027) { # '
940              !!!cp (69);
941              !!!parse-error (type => 'bad attribute name');
942            } else {
943              !!!cp (70);
944            }
945            $self->{current_attribute}->{name} .= chr ($self->{next_char});
946          ## Stay in the state          ## Stay in the state
947          !!!next-input-character;          !!!next-input-character;
948          redo A;          redo A;
949        }        }
950      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
951        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
952            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
953            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
954            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
955            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
956            !!!cp (71);
957          ## Stay in the state          ## Stay in the state
958          !!!next-input-character;          !!!next-input-character;
959          redo A;          redo A;
960        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
961          $self->{state} = 'before attribute value';          !!!cp (72);
962            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
963          !!!next-input-character;          !!!next-input-character;
964          redo A;          redo A;
965        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
966          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
967              !!!cp (73);
968              $self->{current_token}->{first_start_tag}
969                  = not defined $self->{last_emitted_start_tag_name};
970            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
971          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
972            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
973            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
974                !!!cp (74);
975              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
976              } else {
977                ## NOTE: This state should never be reached.
978                !!!cp (75);
979            }            }
980          } else {          } else {
981            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
982          }          }
983          $self->{state} = 'data';          $self->{state} = DATA_STATE;
984          !!!next-input-character;          !!!next-input-character;
985    
986          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
987    
988          redo A;          redo A;
989        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
990                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
991          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
992            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
993                                value => ''};                                value => ''};
994          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
995          !!!next-input-character;          !!!next-input-character;
996          redo A;          redo A;
997        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
998          !!!next-input-character;          !!!next-input-character;
999          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1000              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
1001              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1002            # permitted slash            # permitted slash
1003              !!!cp (77);
1004            #            #
1005          } else {          } else {
1006              !!!cp (78);
1007            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1008              ## TODO: Different error type for <aa / bb> than <aa/>
1009          }          }
1010          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1011          # next-input-character is already done          # next-input-character is already done
1012          redo A;          redo A;
1013        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1014          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1015          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1016              !!!cp (79);
1017              $self->{current_token}->{first_start_tag}
1018                  = not defined $self->{last_emitted_start_tag_name};
1019            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1020          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1021            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1022            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1023                !!!cp (80);
1024              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1025              } else {
1026                ## NOTE: This state should never be reached.
1027                !!!cp (81);
1028            }            }
1029          } else {          } else {
1030            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1031          }          }
1032          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1033          # reconsume          # reconsume
1034    
1035          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1036    
1037          redo A;          redo A;
1038        } else {        } else {
1039          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1040            $self->{current_attribute} = {name => chr ($self->{next_char}),
1041                                value => ''};                                value => ''};
1042          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1043          !!!next-input-character;          !!!next-input-character;
1044          redo A;                  redo A;        
1045        }        }
1046      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1047        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1048            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1049            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1050            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1051            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1052            !!!cp (83);
1053          ## Stay in the state          ## Stay in the state
1054          !!!next-input-character;          !!!next-input-character;
1055          redo A;          redo A;
1056        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1057          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1058            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1059          !!!next-input-character;          !!!next-input-character;
1060          redo A;          redo A;
1061        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1062          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1063            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1064          ## reconsume          ## reconsume
1065          redo A;          redo A;
1066        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1067          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1068            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1069          !!!next-input-character;          !!!next-input-character;
1070          redo A;          redo A;
1071        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1072          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1073              !!!cp (87);
1074              $self->{current_token}->{first_start_tag}
1075                  = not defined $self->{last_emitted_start_tag_name};
1076            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1077          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1078            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1079            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1080                !!!cp (88);
1081              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1082              } else {
1083                ## NOTE: This state should never be reached.
1084                !!!cp (89);
1085            }            }
1086          } else {          } else {
1087            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1088          }          }
1089          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1090          !!!next-input-character;          !!!next-input-character;
1091    
1092          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1093    
1094          redo A;          redo A;
1095        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1096          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1097          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1098              !!!cp (90);
1099              $self->{current_token}->{first_start_tag}
1100                  = not defined $self->{last_emitted_start_tag_name};
1101            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1102          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1103            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1104            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1105                !!!cp (91);
1106              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1107              } else {
1108                ## NOTE: This state should never be reached.
1109                !!!cp (92);
1110            }            }
1111          } else {          } else {
1112            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1113          }          }
1114          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1115          ## reconsume          ## reconsume
1116    
1117          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1118    
1119          redo A;          redo A;
1120        } else {        } else {
1121          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1122          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1123              !!!parse-error (type => 'bad attribute value');
1124            } else {
1125              !!!cp (94);
1126            }
1127            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1128            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1129          !!!next-input-character;          !!!next-input-character;
1130          redo A;          redo A;
1131        }        }
1132      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1133        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1134          $self->{state} = 'before attribute name';          !!!cp (95);
1135            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1136          !!!next-input-character;          !!!next-input-character;
1137          redo A;          redo A;
1138        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1139          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1140          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1141            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1142          !!!next-input-character;          !!!next-input-character;
1143          redo A;          redo A;
1144        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1145          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1146          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1147              !!!cp (97);
1148              $self->{current_token}->{first_start_tag}
1149                  = not defined $self->{last_emitted_start_tag_name};
1150            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1151          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1152            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1153            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1154                !!!cp (98);
1155              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1156              } else {
1157                ## NOTE: This state should never be reached.
1158                !!!cp (99);
1159            }            }
1160          } else {          } else {
1161            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1162          }          }
1163          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1164          ## reconsume          ## reconsume
1165    
1166          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1167    
1168          redo A;          redo A;
1169        } else {        } else {
1170          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1171            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1172          ## Stay in the state          ## Stay in the state
1173          !!!next-input-character;          !!!next-input-character;
1174          redo A;          redo A;
1175        }        }
1176      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1177        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1178          $self->{state} = 'before attribute name';          !!!cp (101);
1179            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1180          !!!next-input-character;          !!!next-input-character;
1181          redo A;          redo A;
1182        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1183          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1184          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1185            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1186          !!!next-input-character;          !!!next-input-character;
1187          redo A;          redo A;
1188        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1189          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1190          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1191              !!!cp (103);
1192              $self->{current_token}->{first_start_tag}
1193                  = not defined $self->{last_emitted_start_tag_name};
1194            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1195          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1196            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1197            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1198                !!!cp (104);
1199              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1200              } else {
1201                ## NOTE: This state should never be reached.
1202                !!!cp (105);
1203            }            }
1204          } else {          } else {
1205            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1206          }          }
1207          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1208          ## reconsume          ## reconsume
1209    
1210          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1211    
1212          redo A;          redo A;
1213        } else {        } else {
1214          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1215            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1216          ## Stay in the state          ## Stay in the state
1217          !!!next-input-character;          !!!next-input-character;
1218          redo A;          redo A;
1219        }        }
1220      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1221        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1222            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1223            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1224            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1225            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1226          $self->{state} = 'before attribute name';          !!!cp (107);
1227          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1228          redo A;          !!!next-input-character;
1229        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1230          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1231          $self->{state} = 'entity in attribute value';          !!!cp (108);
1232          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1233          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1234        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1235          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1236          } elsif ($self->{next_char} == 0x003E) { # >
1237            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1238              !!!cp (109);
1239              $self->{current_token}->{first_start_tag}
1240                  = not defined $self->{last_emitted_start_tag_name};
1241            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1242          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1243            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1244            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1245                !!!cp (110);
1246              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1247              } else {
1248                ## NOTE: This state should never be reached.
1249                !!!cp (111);
1250            }            }
1251          } else {          } else {
1252            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1253          }          }
1254          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1255          !!!next-input-character;          !!!next-input-character;
1256    
1257          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1258    
1259          redo A;          redo A;
1260        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1261          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1262          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1263              !!!cp (112);
1264              $self->{current_token}->{first_start_tag}
1265                  = not defined $self->{last_emitted_start_tag_name};
1266            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1267          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1268            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1269            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1270                !!!cp (113);
1271              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1272              } else {
1273                ## NOTE: This state should never be reached.
1274                !!!cp (114);
1275            }            }
1276          } else {          } else {
1277            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1278          }          }
1279          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1280          ## reconsume          ## reconsume
1281    
1282          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1283    
1284          redo A;          redo A;
1285        } else {        } else {
1286          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1287                 0x0022 => 1, # "
1288                 0x0027 => 1, # '
1289                 0x003D => 1, # =
1290                }->{$self->{next_char}}) {
1291              !!!cp (115);
1292              !!!parse-error (type => 'bad attribute value');
1293            } else {
1294              !!!cp (116);
1295            }
1296            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1297          ## Stay in the state          ## Stay in the state
1298          !!!next-input-character;          !!!next-input-character;
1299          redo A;          redo A;
1300        }        }
1301      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1302        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1303              (1,
1304               $self->{last_attribute_value_state}
1305                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1306               $self->{last_attribute_value_state}
1307                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1308               -1);
1309    
1310        unless (defined $token) {        unless (defined $token) {
1311            !!!cp (117);
1312          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1313        } else {        } else {
1314            !!!cp (118);
1315          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1316            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1317          ## 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"
1318        }        }
1319    
1320        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1321        # next-input-character is already done        # next-input-character is already done
1322        redo A;        redo A;
1323      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1324          if ($self->{next_char} == 0x0009 or # HT
1325              $self->{next_char} == 0x000A or # LF
1326              $self->{next_char} == 0x000B or # VT
1327              $self->{next_char} == 0x000C or # FF
1328              $self->{next_char} == 0x0020) { # SP
1329            !!!cp (118);
1330            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1331            !!!next-input-character;
1332            redo A;
1333          } elsif ($self->{next_char} == 0x003E) { # >
1334            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1335              !!!cp (119);
1336              $self->{current_token}->{first_start_tag}
1337                  = not defined $self->{last_emitted_start_tag_name};
1338              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1339            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1340              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1341              if ($self->{current_token}->{attributes}) {
1342                !!!cp (120);
1343                !!!parse-error (type => 'end tag attribute');
1344              } else {
1345                ## NOTE: This state should never be reached.
1346                !!!cp (121);
1347              }
1348            } else {
1349              die "$0: $self->{current_token}->{type}: Unknown token type";
1350            }
1351            $self->{state} = DATA_STATE;
1352            !!!next-input-character;
1353    
1354            !!!emit ($self->{current_token}); # start tag or end tag
1355    
1356            redo A;
1357          } elsif ($self->{next_char} == 0x002F) { # /
1358            !!!next-input-character;
1359            if ($self->{next_char} == 0x003E and # >
1360                $self->{current_token}->{type} == START_TAG_TOKEN and
1361                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1362              # permitted slash
1363              !!!cp (122);
1364              #
1365            } else {
1366              !!!cp (123);
1367              !!!parse-error (type => 'nestc');
1368            }
1369            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1370            # next-input-character is already done
1371            redo A;
1372          } else {
1373            !!!cp (124);
1374            !!!parse-error (type => 'no space between attributes');
1375            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1376            ## reconsume
1377            redo A;
1378          }
1379        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1380        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1381                
1382        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1383    
1384        BC: {        BC: {
1385          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1386            $self->{state} = 'data';            !!!cp (124);
1387              $self->{state} = DATA_STATE;
1388            !!!next-input-character;            !!!next-input-character;
1389    
1390            !!!emit ($token);            !!!emit ($token);
1391    
1392            redo A;            redo A;
1393          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1394            $self->{state} = 'data';            !!!cp (125);
1395              $self->{state} = DATA_STATE;
1396            ## reconsume            ## reconsume
1397    
1398            !!!emit ($token);            !!!emit ($token);
1399    
1400            redo A;            redo A;
1401          } else {          } else {
1402            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1403              $token->{data} .= chr ($self->{next_char});
1404            !!!next-input-character;            !!!next-input-character;
1405            redo BC;            redo BC;
1406          }          }
1407        } # BC        } # BC
1408      } elsif ($self->{state} eq 'markup declaration open') {  
1409          die "$0: _get_next_token: unexpected case [BC]";
1410        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1411        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1412    
1413        my @next_char;        my @next_char;
1414        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1415                
1416        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1417          !!!next-input-character;          !!!next-input-character;
1418          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1419          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1420            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1421            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1422              $self->{state} = COMMENT_START_STATE;
1423            !!!next-input-character;            !!!next-input-character;
1424            redo A;            redo A;
1425            } else {
1426              !!!cp (128);
1427          }          }
1428        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1429                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1430          !!!next-input-character;          !!!next-input-character;
1431          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1432          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1433              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1434            !!!next-input-character;            !!!next-input-character;
1435            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1436            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1437                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1438              !!!next-input-character;              !!!next-input-character;
1439              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1440              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1441                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1442                !!!next-input-character;                !!!next-input-character;
1443                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1444                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1445                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1446                  !!!next-input-character;                  !!!next-input-character;
1447                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1448                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1449                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1450                    !!!next-input-character;                    !!!next-input-character;
1451                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1452                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1453                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1454                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1455                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1456                        $self->{state} = DOCTYPE_STATE;
1457                      !!!next-input-character;                      !!!next-input-character;
1458                      redo A;                      redo A;
1459                      } else {
1460                        !!!cp (130);
1461                    }                    }
1462                    } else {
1463                      !!!cp (131);
1464                  }                  }
1465                  } else {
1466                    !!!cp (132);
1467                }                }
1468                } else {
1469                  !!!cp (133);
1470              }              }
1471              } else {
1472                !!!cp (134);
1473            }            }
1474            } else {
1475              !!!cp (135);
1476          }          }
1477          } else {
1478            !!!cp (136);
1479        }        }
1480    
1481        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1482        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1483        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1484        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1485        redo A;        redo A;
1486                
1487        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1488        ## 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?
1489      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1490        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1491          $self->{state} = 'comment dash';          !!!cp (137);
1492            $self->{state} = COMMENT_START_DASH_STATE;
1493            !!!next-input-character;
1494            redo A;
1495          } elsif ($self->{next_char} == 0x003E) { # >
1496            !!!cp (138);
1497            !!!parse-error (type => 'bogus comment');
1498            $self->{state} = DATA_STATE;
1499          !!!next-input-character;          !!!next-input-character;
1500    
1501            !!!emit ($self->{current_token}); # comment
1502    
1503          redo A;          redo A;
1504        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1505            !!!cp (139);
1506          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1507          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1508          ## reconsume          ## reconsume
1509    
1510          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1511    
1512          redo A;          redo A;
1513        } else {        } else {
1514          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (140);
1515            $self->{current_token}->{data} # comment
1516                .= chr ($self->{next_char});
1517            $self->{state} = COMMENT_STATE;
1518            !!!next-input-character;
1519            redo A;
1520          }
1521        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1522          if ($self->{next_char} == 0x002D) { # -
1523            !!!cp (141);
1524            $self->{state} = COMMENT_END_STATE;
1525            !!!next-input-character;
1526            redo A;
1527          } elsif ($self->{next_char} == 0x003E) { # >
1528            !!!cp (142);
1529            !!!parse-error (type => 'bogus comment');
1530            $self->{state} = DATA_STATE;
1531            !!!next-input-character;
1532    
1533            !!!emit ($self->{current_token}); # comment
1534    
1535            redo A;
1536          } elsif ($self->{next_char} == -1) {
1537            !!!cp (143);
1538            !!!parse-error (type => 'unclosed comment');
1539            $self->{state} = DATA_STATE;
1540            ## reconsume
1541    
1542            !!!emit ($self->{current_token}); # comment
1543    
1544            redo A;
1545          } else {
1546            !!!cp (144);
1547            $self->{current_token}->{data} # comment
1548                .= '-' . chr ($self->{next_char});
1549            $self->{state} = COMMENT_STATE;
1550            !!!next-input-character;
1551            redo A;
1552          }
1553        } elsif ($self->{state} == COMMENT_STATE) {
1554          if ($self->{next_char} == 0x002D) { # -
1555            !!!cp (145);
1556            $self->{state} = COMMENT_END_DASH_STATE;
1557            !!!next-input-character;
1558            redo A;
1559          } elsif ($self->{next_char} == -1) {
1560            !!!cp (146);
1561            !!!parse-error (type => 'unclosed comment');
1562            $self->{state} = DATA_STATE;
1563            ## reconsume
1564    
1565            !!!emit ($self->{current_token}); # comment
1566    
1567            redo A;
1568          } else {
1569            !!!cp (147);
1570            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1571          ## Stay in the state          ## Stay in the state
1572          !!!next-input-character;          !!!next-input-character;
1573          redo A;          redo A;
1574        }        }
1575      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1576        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1577          $self->{state} = 'comment end';          !!!cp (148);
1578            $self->{state} = COMMENT_END_STATE;
1579          !!!next-input-character;          !!!next-input-character;
1580          redo A;          redo A;
1581        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1582            !!!cp (149);
1583          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1584          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1585          ## reconsume          ## reconsume
1586    
1587          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1588    
1589          redo A;          redo A;
1590        } else {        } else {
1591          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1592          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1593            $self->{state} = COMMENT_STATE;
1594          !!!next-input-character;          !!!next-input-character;
1595          redo A;          redo A;
1596        }        }
1597      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1598        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1599          $self->{state} = 'data';          !!!cp (151);
1600            $self->{state} = DATA_STATE;
1601          !!!next-input-character;          !!!next-input-character;
1602    
1603          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1604    
1605          redo A;          redo A;
1606        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1607            !!!cp (152);
1608          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1609          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1610          ## Stay in the state          ## Stay in the state
1611          !!!next-input-character;          !!!next-input-character;
1612          redo A;          redo A;
1613        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1614            !!!cp (153);
1615          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1616          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1617          ## reconsume          ## reconsume
1618    
1619          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1620    
1621          redo A;          redo A;
1622        } else {        } else {
1623            !!!cp (154);
1624          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1625          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1626          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1627          !!!next-input-character;          !!!next-input-character;
1628          redo A;          redo A;
1629        }        }
1630      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1631        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1632            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1633            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1634            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1635            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1636          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1637            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1638          !!!next-input-character;          !!!next-input-character;
1639          redo A;          redo A;
1640        } else {        } else {
1641            !!!cp (156);
1642          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1643          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1644          ## reconsume          ## reconsume
1645          redo A;          redo A;
1646        }        }
1647      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1648        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1649            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1650            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1651            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1652            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1653            !!!cp (157);
1654          ## Stay in the state          ## Stay in the state
1655          !!!next-input-character;          !!!next-input-character;
1656          redo A;          redo A;
1657        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
1658                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (158);
1659          $self->{current_token} = {type => 'DOCTYPE',          !!!parse-error (type => 'no DOCTYPE name');
1660                            name => chr ($self->{next_input_character} - 0x0020),          $self->{state} = DATA_STATE;
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
1661          !!!next-input-character;          !!!next-input-character;
1662    
1663            !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1664    
1665          redo A;          redo A;
1666        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == -1) {
1667            !!!cp (159);
1668          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1669          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1670            ## reconsume
1671    
1672            !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1673    
1674            redo A;
1675          } else {
1676            !!!cp (160);
1677            $self->{current_token}
1678                = {type => DOCTYPE_TOKEN,
1679                   name => chr ($self->{next_char}),
1680                   #quirks => 0,
1681                  };
1682    ## ISSUE: "Set the token's name name to the" in the spec
1683            $self->{state} = DOCTYPE_NAME_STATE;
1684            !!!next-input-character;
1685            redo A;
1686          }
1687        } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1688    ## ISSUE: Redundant "First," in the spec.
1689          if ($self->{next_char} == 0x0009 or # HT
1690              $self->{next_char} == 0x000A or # LF
1691              $self->{next_char} == 0x000B or # VT
1692              $self->{next_char} == 0x000C or # FF
1693              $self->{next_char} == 0x0020) { # SP
1694            !!!cp (161);
1695            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1696            !!!next-input-character;
1697            redo A;
1698          } elsif ($self->{next_char} == 0x003E) { # >
1699            !!!cp (162);
1700            $self->{state} = DATA_STATE;
1701          !!!next-input-character;          !!!next-input-character;
1702    
1703          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE
1704    
1705          redo A;          redo A;
1706        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1707          !!!parse-error (type => 'no DOCTYPE name');          !!!cp (163);
1708          $self->{state} = 'data';          !!!parse-error (type => 'unclosed DOCTYPE');
1709            $self->{state} = DATA_STATE;
1710          ## reconsume          ## reconsume
1711    
1712          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          $self->{current_token}->{quirks} = 1;
1713            !!!emit ($self->{current_token}); # DOCTYPE
1714    
1715          redo A;          redo A;
1716        } else {        } else {
1717          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (164);
1718                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name}
1719                            error => 1};            .= chr ($self->{next_char}); # DOCTYPE
1720          $self->{state} = 'DOCTYPE name';          ## Stay in the state
1721          !!!next-input-character;          !!!next-input-character;
1722          redo A;          redo A;
1723        }        }
1724      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1725        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1726            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1727            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1728            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1729            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1730          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (165);
1731          $self->{state} = 'after DOCTYPE name';          ## Stay in the state
1732          !!!next-input-character;          !!!next-input-character;
1733          redo A;          redo A;
1734        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1735          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (166);
1736          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1737          !!!next-input-character;          !!!next-input-character;
1738    
1739          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1740    
1741          redo A;          redo A;
1742        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
1743                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (167);
1744          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
1745          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
1746            ## reconsume
1747    
1748            $self->{current_token}->{quirks} = 1;
1749            !!!emit ($self->{current_token}); # DOCTYPE
1750    
1751            redo A;
1752          } elsif ($self->{next_char} == 0x0050 or # P
1753                   $self->{next_char} == 0x0070) { # p
1754            !!!next-input-character;
1755            if ($self->{next_char} == 0x0055 or # U
1756                $self->{next_char} == 0x0075) { # u
1757              !!!next-input-character;
1758              if ($self->{next_char} == 0x0042 or # B
1759                  $self->{next_char} == 0x0062) { # b
1760                !!!next-input-character;
1761                if ($self->{next_char} == 0x004C or # L
1762                    $self->{next_char} == 0x006C) { # l
1763                  !!!next-input-character;
1764                  if ($self->{next_char} == 0x0049 or # I
1765                      $self->{next_char} == 0x0069) { # i
1766                    !!!next-input-character;
1767                    if ($self->{next_char} == 0x0043 or # C
1768                        $self->{next_char} == 0x0063) { # c
1769                      !!!cp (168);
1770                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1771                      !!!next-input-character;
1772                      redo A;
1773                    } else {
1774                      !!!cp (169);
1775                    }
1776                  } else {
1777                    !!!cp (170);
1778                  }
1779                } else {
1780                  !!!cp (171);
1781                }
1782              } else {
1783                !!!cp (172);
1784              }
1785            } else {
1786              !!!cp (173);
1787            }
1788    
1789            #
1790          } elsif ($self->{next_char} == 0x0053 or # S
1791                   $self->{next_char} == 0x0073) { # s
1792            !!!next-input-character;
1793            if ($self->{next_char} == 0x0059 or # Y
1794                $self->{next_char} == 0x0079) { # y
1795              !!!next-input-character;
1796              if ($self->{next_char} == 0x0053 or # S
1797                  $self->{next_char} == 0x0073) { # s
1798                !!!next-input-character;
1799                if ($self->{next_char} == 0x0054 or # T
1800                    $self->{next_char} == 0x0074) { # t
1801                  !!!next-input-character;
1802                  if ($self->{next_char} == 0x0045 or # E
1803                      $self->{next_char} == 0x0065) { # e
1804                    !!!next-input-character;
1805                    if ($self->{next_char} == 0x004D or # M
1806                        $self->{next_char} == 0x006D) { # m
1807                      !!!cp (174);
1808                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1809                      !!!next-input-character;
1810                      redo A;
1811                    } else {
1812                      !!!cp (175);
1813                    }
1814                  } else {
1815                    !!!cp (176);
1816                  }
1817                } else {
1818                  !!!cp (177);
1819                }
1820              } else {
1821                !!!cp (178);
1822              }
1823            } else {
1824              !!!cp (179);
1825            }
1826    
1827            #
1828          } else {
1829            !!!cp (180);
1830            !!!next-input-character;
1831            #
1832          }
1833    
1834          !!!parse-error (type => 'string after DOCTYPE name');
1835          $self->{current_token}->{quirks} = 1;
1836    
1837          $self->{state} = BOGUS_DOCTYPE_STATE;
1838          # next-input-character is already done
1839          redo A;
1840        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1841          if ({
1842                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1843                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1844              }->{$self->{next_char}}) {
1845            !!!cp (181);
1846          ## Stay in the state          ## Stay in the state
1847          !!!next-input-character;          !!!next-input-character;
1848          redo A;          redo A;
1849        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} eq 0x0022) { # "
1850            !!!cp (182);
1851            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1852            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1853            !!!next-input-character;
1854            redo A;
1855          } elsif ($self->{next_char} eq 0x0027) { # '
1856            !!!cp (183);
1857            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1858            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1859            !!!next-input-character;
1860            redo A;
1861          } elsif ($self->{next_char} eq 0x003E) { # >
1862            !!!cp (184);
1863            !!!parse-error (type => 'no PUBLIC literal');
1864    
1865            $self->{state} = DATA_STATE;
1866            !!!next-input-character;
1867    
1868            $self->{current_token}->{quirks} = 1;
1869            !!!emit ($self->{current_token}); # DOCTYPE
1870    
1871            redo A;
1872          } elsif ($self->{next_char} == -1) {
1873            !!!cp (185);
1874          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1875          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1876          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1877          ## reconsume          ## reconsume
1878    
1879          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
1880          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1881    
1882          redo A;          redo A;
1883        } else {        } else {
1884          $self->{current_token}->{name}          !!!cp (186);
1885            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after PUBLIC');
1886          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{current_token}->{quirks} = 1;
1887    
1888            $self->{state} = BOGUS_DOCTYPE_STATE;
1889            !!!next-input-character;
1890            redo A;
1891          }
1892        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1893          if ($self->{next_char} == 0x0022) { # "
1894            !!!cp (187);
1895            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1896            !!!next-input-character;
1897            redo A;
1898          } elsif ($self->{next_char} == 0x003E) { # >
1899            !!!cp (188);
1900            !!!parse-error (type => 'unclosed PUBLIC literal');
1901    
1902            $self->{state} = DATA_STATE;
1903            !!!next-input-character;
1904    
1905            $self->{current_token}->{quirks} = 1;
1906            !!!emit ($self->{current_token}); # DOCTYPE
1907    
1908            redo A;
1909          } elsif ($self->{next_char} == -1) {
1910            !!!cp (189);
1911            !!!parse-error (type => 'unclosed PUBLIC literal');
1912    
1913            $self->{state} = DATA_STATE;
1914            ## reconsume
1915    
1916            $self->{current_token}->{quirks} = 1;
1917            !!!emit ($self->{current_token}); # DOCTYPE
1918    
1919            redo A;
1920          } else {
1921            !!!cp (190);
1922            $self->{current_token}->{public_identifier} # DOCTYPE
1923                .= chr $self->{next_char};
1924            ## Stay in the state
1925            !!!next-input-character;
1926            redo A;
1927          }
1928        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1929          if ($self->{next_char} == 0x0027) { # '
1930            !!!cp (191);
1931            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1932            !!!next-input-character;
1933            redo A;
1934          } elsif ($self->{next_char} == 0x003E) { # >
1935            !!!cp (192);
1936            !!!parse-error (type => 'unclosed PUBLIC literal');
1937    
1938            $self->{state} = DATA_STATE;
1939            !!!next-input-character;
1940    
1941            $self->{current_token}->{quirks} = 1;
1942            !!!emit ($self->{current_token}); # DOCTYPE
1943    
1944            redo A;
1945          } elsif ($self->{next_char} == -1) {
1946            !!!cp (193);
1947            !!!parse-error (type => 'unclosed PUBLIC literal');
1948    
1949            $self->{state} = DATA_STATE;
1950            ## reconsume
1951    
1952            $self->{current_token}->{quirks} = 1;
1953            !!!emit ($self->{current_token}); # DOCTYPE
1954    
1955            redo A;
1956          } else {
1957            !!!cp (194);
1958            $self->{current_token}->{public_identifier} # DOCTYPE
1959                .= chr $self->{next_char};
1960          ## Stay in the state          ## Stay in the state
1961          !!!next-input-character;          !!!next-input-character;
1962          redo A;          redo A;
1963        }        }
1964      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1965        if ($self->{next_input_character} == 0x0009 or # HT        if ({
1966            $self->{next_input_character} == 0x000A or # LF              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1967            $self->{next_input_character} == 0x000B or # VT              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1968            $self->{next_input_character} == 0x000C or # FF            }->{$self->{next_char}}) {
1969            $self->{next_input_character} == 0x0020) { # SP          !!!cp (195);
1970          ## Stay in the state          ## Stay in the state
1971          !!!next-input-character;          !!!next-input-character;
1972          redo A;          redo A;
1973        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x0022) { # "
1974          $self->{state} = 'data';          !!!cp (196);
1975            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1976            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1977            !!!next-input-character;
1978            redo A;
1979          } elsif ($self->{next_char} == 0x0027) { # '
1980            !!!cp (197);
1981            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1982            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1983            !!!next-input-character;
1984            redo A;
1985          } elsif ($self->{next_char} == 0x003E) { # >
1986            !!!cp (198);
1987            $self->{state} = DATA_STATE;
1988            !!!next-input-character;
1989    
1990            !!!emit ($self->{current_token}); # DOCTYPE
1991    
1992            redo A;
1993          } elsif ($self->{next_char} == -1) {
1994            !!!cp (199);
1995            !!!parse-error (type => 'unclosed DOCTYPE');
1996    
1997            $self->{state} = DATA_STATE;
1998            ## reconsume
1999    
2000            $self->{current_token}->{quirks} = 1;
2001            !!!emit ($self->{current_token}); # DOCTYPE
2002    
2003            redo A;
2004          } else {
2005            !!!cp (200);
2006            !!!parse-error (type => 'string after PUBLIC literal');
2007            $self->{current_token}->{quirks} = 1;
2008    
2009            $self->{state} = BOGUS_DOCTYPE_STATE;
2010            !!!next-input-character;
2011            redo A;
2012          }
2013        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2014          if ({
2015                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2016                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2017              }->{$self->{next_char}}) {
2018            !!!cp (201);
2019            ## Stay in the state
2020            !!!next-input-character;
2021            redo A;
2022          } elsif ($self->{next_char} == 0x0022) { # "
2023            !!!cp (202);
2024            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2025            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2026            !!!next-input-character;
2027            redo A;
2028          } elsif ($self->{next_char} == 0x0027) { # '
2029            !!!cp (203);
2030            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2031            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2032            !!!next-input-character;
2033            redo A;
2034          } elsif ($self->{next_char} == 0x003E) { # >
2035            !!!cp (204);
2036            !!!parse-error (type => 'no SYSTEM literal');
2037            $self->{state} = DATA_STATE;
2038            !!!next-input-character;
2039    
2040            $self->{current_token}->{quirks} = 1;
2041            !!!emit ($self->{current_token}); # DOCTYPE
2042    
2043            redo A;
2044          } elsif ($self->{next_char} == -1) {
2045            !!!cp (205);
2046            !!!parse-error (type => 'unclosed DOCTYPE');
2047    
2048            $self->{state} = DATA_STATE;
2049            ## reconsume
2050    
2051            $self->{current_token}->{quirks} = 1;
2052            !!!emit ($self->{current_token}); # DOCTYPE
2053    
2054            redo A;
2055          } else {
2056            !!!cp (206);
2057            !!!parse-error (type => 'string after SYSTEM');
2058            $self->{current_token}->{quirks} = 1;
2059    
2060            $self->{state} = BOGUS_DOCTYPE_STATE;
2061            !!!next-input-character;
2062            redo A;
2063          }
2064        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2065          if ($self->{next_char} == 0x0022) { # "
2066            !!!cp (207);
2067            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2068            !!!next-input-character;
2069            redo A;
2070          } elsif ($self->{next_char} == 0x003E) { # >
2071            !!!cp (208);
2072            !!!parse-error (type => 'unclosed PUBLIC literal');
2073    
2074            $self->{state} = DATA_STATE;
2075            !!!next-input-character;
2076    
2077            $self->{current_token}->{quirks} = 1;
2078            !!!emit ($self->{current_token}); # DOCTYPE
2079    
2080            redo A;
2081          } elsif ($self->{next_char} == -1) {
2082            !!!cp (209);
2083            !!!parse-error (type => 'unclosed SYSTEM literal');
2084    
2085            $self->{state} = DATA_STATE;
2086            ## reconsume
2087    
2088            $self->{current_token}->{quirks} = 1;
2089            !!!emit ($self->{current_token}); # DOCTYPE
2090    
2091            redo A;
2092          } else {
2093            !!!cp (210);
2094            $self->{current_token}->{system_identifier} # DOCTYPE
2095                .= chr $self->{next_char};
2096            ## Stay in the state
2097            !!!next-input-character;
2098            redo A;
2099          }
2100        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2101          if ($self->{next_char} == 0x0027) { # '
2102            !!!cp (211);
2103            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2104            !!!next-input-character;
2105            redo A;
2106          } elsif ($self->{next_char} == 0x003E) { # >
2107            !!!cp (212);
2108            !!!parse-error (type => 'unclosed PUBLIC literal');
2109    
2110            $self->{state} = DATA_STATE;
2111            !!!next-input-character;
2112    
2113            $self->{current_token}->{quirks} = 1;
2114            !!!emit ($self->{current_token}); # DOCTYPE
2115    
2116            redo A;
2117          } elsif ($self->{next_char} == -1) {
2118            !!!cp (213);
2119            !!!parse-error (type => 'unclosed SYSTEM literal');
2120    
2121            $self->{state} = DATA_STATE;
2122            ## reconsume
2123    
2124            $self->{current_token}->{quirks} = 1;
2125            !!!emit ($self->{current_token}); # DOCTYPE
2126    
2127            redo A;
2128          } else {
2129            !!!cp (214);
2130            $self->{current_token}->{system_identifier} # DOCTYPE
2131                .= chr $self->{next_char};
2132            ## Stay in the state
2133            !!!next-input-character;
2134            redo A;
2135          }
2136        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2137          if ({
2138                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2139                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2140              }->{$self->{next_char}}) {
2141            !!!cp (215);
2142            ## Stay in the state
2143            !!!next-input-character;
2144            redo A;
2145          } elsif ($self->{next_char} == 0x003E) { # >
2146            !!!cp (216);
2147            $self->{state} = DATA_STATE;
2148          !!!next-input-character;          !!!next-input-character;
2149    
2150          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2151    
2152          redo A;          redo A;
2153        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2154            !!!cp (217);
2155          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2156          $self->{state} = 'data';  
2157            $self->{state} = DATA_STATE;
2158          ## reconsume          ## reconsume
2159    
2160            $self->{current_token}->{quirks} = 1;
2161          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2162    
2163          redo A;          redo A;
2164        } else {        } else {
2165          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (218);
2166          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after SYSTEM literal');
2167          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2168    
2169            $self->{state} = BOGUS_DOCTYPE_STATE;
2170          !!!next-input-character;          !!!next-input-character;
2171          redo A;          redo A;
2172        }        }
2173      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2174        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2175          $self->{state} = 'data';          !!!cp (219);
2176            $self->{state} = DATA_STATE;
2177          !!!next-input-character;          !!!next-input-character;
2178    
2179          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2180    
2181          redo A;          redo A;
2182        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2183            !!!cp (220);
2184          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2185          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2186          ## reconsume          ## reconsume
2187    
2188          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2189    
2190          redo A;          redo A;
2191        } else {        } else {
2192            !!!cp (221);
2193          ## Stay in the state          ## Stay in the state
2194          !!!next-input-character;          !!!next-input-character;
2195          redo A;          redo A;
# Line 1449  sub _get_next_token ($) { Line 2202  sub _get_next_token ($) {
2202    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2203  } # _get_next_token  } # _get_next_token
2204    
2205  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2206    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2207      
2208    if ($self->{next_input_character} == 0x0023) { # #    if ({
2209           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2210           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2211           $additional => 1,
2212          }->{$self->{next_char}}) {
2213        !!!cp (1001);
2214        ## Don't consume
2215        ## No error
2216        return undef;
2217      } elsif ($self->{next_char} == 0x0023) { # #
2218      !!!next-input-character;      !!!next-input-character;
2219      my $num;      if ($self->{next_char} == 0x0078 or # x
2220      if ($self->{next_input_character} == 0x0078 or # x          $self->{next_char} == 0x0058) { # X
2221          $self->{next_input_character} == 0x0058) { # X        my $code;
2222        X: {        X: {
2223          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2224          !!!next-input-character;          !!!next-input-character;
2225          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2226              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2227            $num ||= 0;            !!!cp (1002);
2228            $num *= 0x10;            $code ||= 0;
2229            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2230              $code += $self->{next_char} - 0x0030;
2231            redo X;            redo X;
2232          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2233                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2234            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2235            $num ||= 0;            $code ||= 0;
2236            $num *= 0x10;            $code *= 0x10;
2237            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2238            redo X;            redo X;
2239          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2240                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2241            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2242            $num ||= 0;            $code ||= 0;
2243            $num *= 0x10;            $code *= 0x10;
2244            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2245            redo X;            redo X;
2246          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2247              !!!cp (1005);
2248            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
2249            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2250            !!!back-next-input-character ($x_char);            $self->{next_char} = 0x0023; # #
2251            return undef;            return undef;
2252          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2253              !!!cp (1006);
2254            !!!next-input-character;            !!!next-input-character;
2255          } else {          } else {
2256              !!!cp (1007);
2257            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
2258          }          }
2259    
2260          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2261          if ($num > 1114111 or $num == 0) {            !!!cp (1008);
2262            $num = 0xFFFD; # REPLACEMENT CHARACTER            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2263            ## ISSUE: Why this is not an error?            $code = 0xFFFD;
2264            } elsif ($code > 0x10FFFF) {
2265              !!!cp (1009);
2266              !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2267              $code = 0xFFFD;
2268            } elsif ($code == 0x000D) {
2269              !!!cp (1010);
2270              !!!parse-error (type => 'CR character reference');
2271              $code = 0x000A;
2272            } elsif (0x80 <= $code and $code <= 0x9F) {
2273              !!!cp (1011);
2274              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2275              $code = $c1_entity_char->{$code};
2276          }          }
2277    
2278          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code,
2279                    has_reference => 1};
2280        } # X        } # X
2281      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2282               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2283        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2284        !!!next-input-character;        !!!next-input-character;
2285                
2286        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2287                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2288            !!!cp (1012);
2289          $code *= 10;          $code *= 10;
2290          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2291                    
2292          !!!next-input-character;          !!!next-input-character;
2293        }        }
2294    
2295        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2296            !!!cp (1013);
2297          !!!next-input-character;          !!!next-input-character;
2298        } else {        } else {
2299            !!!cp (1014);
2300          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
2301        }        }
2302    
2303        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2304        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
2305          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2306          ## ISSUE: Why this is not an error?          $code = 0xFFFD;
2307          } elsif ($code > 0x10FFFF) {
2308            !!!cp (1016);
2309            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2310            $code = 0xFFFD;
2311          } elsif ($code == 0x000D) {
2312            !!!cp (1017);
2313            !!!parse-error (type => 'CR character reference');
2314            $code = 0x000A;
2315          } elsif (0x80 <= $code and $code <= 0x9F) {
2316            !!!cp (1018);
2317            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2318            $code = $c1_entity_char->{$code};
2319        }        }
2320                
2321        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2322      } else {      } else {
2323          !!!cp (1019);
2324        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2325        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_char});
2326        $self->{next_input_character} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2327        return undef;        return undef;
2328      }      }
2329    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2330              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2331             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2332              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2333      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2334      !!!next-input-character;      !!!next-input-character;
2335    
2336      my $value = $entity_name;      my $value = $entity_name;
2337      my $match;      my $match = 0;
2338        require Whatpm::_NamedEntityList;
2339        our $EntityChar;
2340    
2341      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2342             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2343             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
2344               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
2345              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
2346               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
2347              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
2348               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
2349        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
2350        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
2351          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
2352          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
2353              !!!cp (1020);
2354              $value = $EntityChar->{$entity_name};
2355              $match = 1;
2356              !!!next-input-character;
2357              last;
2358            } else {
2359              !!!cp (1021);
2360              $value = $EntityChar->{$entity_name};
2361              $match = -1;
2362              !!!next-input-character;
2363            }
2364        } else {        } else {
2365          $value .= chr $self->{next_input_character};          !!!cp (1022);
2366            $value .= chr $self->{next_char};
2367            $match *= 2;
2368            !!!next-input-character;
2369        }        }
       !!!next-input-character;  
2370      }      }
2371            
2372      if ($match) {      if ($match > 0) {
2373        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
2374          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2375        } elsif ($match < 0) {
2376          !!!parse-error (type => 'no refc');
2377          if ($in_attr and $match < -1) {
2378            !!!cp (1024);
2379            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2380        } else {        } else {
2381          !!!parse-error (type => 'refc');          !!!cp (1025);
2382            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2383        }        }
   
       return {type => 'character', data => $value};  
2384      } else {      } else {
2385          !!!cp (1026);
2386        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2387        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
2388        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
2389      }      }
2390    } else {    } else {
2391        !!!cp (1027);
2392      ## no characters are consumed      ## no characters are consumed
2393      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero');
2394      return undef;      return undef;
# Line 1586  sub _initialize_tree_constructor ($) { Line 2401  sub _initialize_tree_constructor ($) {
2401    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
2402    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
2403    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
2404    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
2405  } # _initialize_tree_constructor  } # _initialize_tree_constructor
2406    
2407  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1613  sub _construct_tree ($) { Line 2428  sub _construct_tree ($) {
2428        
2429    !!!next-token;    !!!next-token;
2430    
   $self->{insertion_mode} = 'before head';  
2431    undef $self->{form_element};    undef $self->{form_element};
2432    undef $self->{head_element};    undef $self->{head_element};
2433    $self->{open_elements} = [];    $self->{open_elements} = [];
2434    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2435    
2436      ## NOTE: The "initial" insertion mode.
2437    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2438    
2439      ## NOTE: The "before html" insertion mode.
2440    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2441      $self->{insertion_mode} = BEFORE_HEAD_IM;
2442    
2443      ## NOTE: The "before head" insertion mode and so on.
2444    $self->_tree_construction_main;    $self->_tree_construction_main;
2445  } # _construct_tree  } # _construct_tree
2446    
2447  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2448    my $self = shift;    my $self = shift;
2449    B: {  
2450        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
2451          if ($token->{error}) {  
2452            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
2453            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
2454          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2455          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
2456            ($token->{name});        ## language.
2457          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
2458          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
2459          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/;
2460          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
2461          return;            defined $token->{public_identifier} or
2462        } elsif ({            defined $token->{system_identifier}) {
2463                  comment => 1,          !!!cp ('t1');
2464                  'start tag' => 1,          !!!parse-error (type => 'not HTML5');
2465                  'end tag' => 1,        } elsif ($doctype_name ne 'HTML') {
2466                  'end-of-file' => 1,          !!!cp ('t2');
2467                 }->{$token->{type}}) {          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2468          ## ISSUE: Spec currently left this case undefined.          !!!parse-error (type => 'not HTML5');
2469          !!!parse-error (type => 'missing DOCTYPE');        } else {
2470          #$phase = 'root element';          !!!cp ('t3');
2471          ## reprocess        }
2472          #redo B;        
2473          return;        my $doctype = $self->{document}->create_document_type_definition
2474        } elsif ($token->{type} eq 'character') {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2475          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        $doctype->public_id ($token->{public_identifier})
2476            $self->{document}->manakai_append_text ($1);            if defined $token->{public_identifier};
2477            ## ISSUE: DOM3 Core does not allow Document > Text        $doctype->system_id ($token->{system_identifier})
2478            unless (length $token->{data}) {            if defined $token->{system_identifier};
2479              ## Stay in the phase        ## NOTE: Other DocumentType attributes are null or empty lists.
2480              !!!next-token;        ## ISSUE: internalSubset = null??
2481              redo B;        $self->{document}->append_child ($doctype);
2482          
2483          if ($token->{quirks} or $doctype_name ne 'HTML') {
2484            !!!cp ('t4');
2485            $self->{document}->manakai_compat_mode ('quirks');
2486          } elsif (defined $token->{public_identifier}) {
2487            my $pubid = $token->{public_identifier};
2488            $pubid =~ tr/a-z/A-z/;
2489            if ({
2490              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
2491              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2492              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2493              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
2494              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
2495              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
2496              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
2497              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
2498              "-//IETF//DTD HTML 2.0//EN" => 1,
2499              "-//IETF//DTD HTML 2.1E//EN" => 1,
2500              "-//IETF//DTD HTML 3.0//EN" => 1,
2501              "-//IETF//DTD HTML 3.0//EN//" => 1,
2502              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
2503              "-//IETF//DTD HTML 3.2//EN" => 1,
2504              "-//IETF//DTD HTML 3//EN" => 1,
2505              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
2506              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
2507              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
2508              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
2509              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
2510              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
2511              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
2512              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
2513              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
2514              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
2515              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
2516              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
2517              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
2518              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
2519              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
2520              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
2521              "-//IETF//DTD HTML STRICT//EN" => 1,
2522              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
2523              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
2524              "-//IETF//DTD HTML//EN" => 1,
2525              "-//IETF//DTD HTML//EN//2.0" => 1,
2526              "-//IETF//DTD HTML//EN//3.0" => 1,
2527              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
2528              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
2529              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
2530              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
2531              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
2532              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
2533              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
2534              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
2535              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2536              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2537              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2538              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2539              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2540              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2541              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2542              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2543              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
2544              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
2545              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
2546              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
2547              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
2548              "-//W3C//DTD HTML 3.2//EN" => 1,
2549              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
2550              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
2551              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
2552              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
2553              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
2554              "-//W3C//DTD W3 HTML//EN" => 1,
2555              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
2556              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
2557              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
2558              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
2559              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
2560              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2561              "HTML" => 1,
2562            }->{$pubid}) {
2563              !!!cp ('t5');
2564              $self->{document}->manakai_compat_mode ('quirks');
2565            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2566                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2567              if (defined $token->{system_identifier}) {
2568                !!!cp ('t6');
2569                $self->{document}->manakai_compat_mode ('quirks');
2570              } else {
2571                !!!cp ('t7');
2572                $self->{document}->manakai_compat_mode ('limited quirks');
2573            }            }
2574            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2575                     $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2576              !!!cp ('t8');
2577              $self->{document}->manakai_compat_mode ('limited quirks');
2578            } else {
2579              !!!cp ('t9');
2580            }
2581          } else {
2582            !!!cp ('t10');
2583          }
2584          if (defined $token->{system_identifier}) {
2585            my $sysid = $token->{system_identifier};
2586            $sysid =~ tr/A-Z/a-z/;
2587            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2588              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2589              $self->{document}->manakai_compat_mode ('quirks');
2590              !!!cp ('t11');
2591            } else {
2592              !!!cp ('t12');
2593            }
2594          } else {
2595            !!!cp ('t13');
2596          }
2597          
2598          ## Go to the "before html" insertion mode.
2599          !!!next-token;
2600          return;
2601        } elsif ({
2602                  START_TAG_TOKEN, 1,
2603                  END_TAG_TOKEN, 1,
2604                  END_OF_FILE_TOKEN, 1,
2605                 }->{$token->{type}}) {
2606          !!!cp ('t14');
2607          !!!parse-error (type => 'no DOCTYPE');
2608          $self->{document}->manakai_compat_mode ('quirks');
2609          ## Go to the "before html" insertion mode.
2610          ## reprocess
2611          return;
2612        } elsif ($token->{type} == CHARACTER_TOKEN) {
2613          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2614            ## Ignore the token
2615    
2616            unless (length $token->{data}) {
2617              !!!cp ('t15');
2618              ## Stay in the insertion mode.
2619              !!!next-token;
2620              redo INITIAL;
2621            } else {
2622              !!!cp ('t16');
2623          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
2624        } else {        } else {
2625          die "$0: $token->{type}: Unknown token";          !!!cp ('t17');
2626        }        }
2627      } # B  
2628          !!!parse-error (type => 'no DOCTYPE');
2629          $self->{document}->manakai_compat_mode ('quirks');
2630          ## Go to the "before html" insertion mode.
2631          ## reprocess
2632          return;
2633        } elsif ($token->{type} == COMMENT_TOKEN) {
2634          !!!cp ('t18');
2635          my $comment = $self->{document}->create_comment ($token->{data});
2636          $self->{document}->append_child ($comment);
2637          
2638          ## Stay in the insertion mode.
2639          !!!next-token;
2640          redo INITIAL;
2641        } else {
2642          die "$0: $token->{type}: Unknown token type";
2643        }
2644      } # INITIAL
2645    
2646      die "$0: _tree_construction_initial: This should be never reached";
2647  } # _tree_construction_initial  } # _tree_construction_initial
2648    
2649  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2650    my $self = shift;    my $self = shift;
2651    
2652      ## NOTE: "before html" insertion mode.
2653        
2654    B: {    B: {
2655        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2656            !!!cp ('t19');
2657          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2658          ## Ignore the token          ## Ignore the token
2659          ## Stay in the phase          ## Stay in the insertion mode.
2660          !!!next-token;          !!!next-token;
2661          redo B;          redo B;
2662        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2663            !!!cp ('t20');
2664          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2665          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2666          ## Stay in the phase          ## Stay in the insertion mode.
2667          !!!next-token;          !!!next-token;
2668          redo B;          redo B;
2669        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2670          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2671            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2672            ## ISSUE: DOM3 Core does not allow Document > Text  
2673            unless (length $token->{data}) {            unless (length $token->{data}) {
2674              ## Stay in the phase              !!!cp ('t21');
2675                ## Stay in the insertion mode.
2676              !!!next-token;              !!!next-token;
2677              redo B;              redo B;
2678              } else {
2679                !!!cp ('t22');
2680            }            }
2681            } else {
2682              !!!cp ('t23');
2683          }          }
2684    
2685            $self->{application_cache_selection}->(undef);
2686    
2687          #          #
2688          } elsif ($token->{type} == START_TAG_TOKEN) {
2689            if ($token->{tag_name} eq 'html') {
2690              my $root_element;
2691              !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});
2692              $self->{document}->append_child ($root_element);
2693              push @{$self->{open_elements}}, [$root_element, 'html'];
2694    
2695              if ($token->{attributes}->{manifest}) {
2696                !!!cp ('t24');
2697                $self->{application_cache_selection}
2698                    ->($token->{attributes}->{manifest}->{value});
2699                ## ISSUE: No relative reference resolution?
2700              } else {
2701                !!!cp ('t25');
2702                $self->{application_cache_selection}->(undef);
2703              }
2704    
2705              !!!next-token;
2706              return; ## Go to the "before head" insertion mode.
2707            } else {
2708              !!!cp ('t25.1');
2709              #
2710            }
2711        } elsif ({        } elsif ({
2712                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2713                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2714                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2715          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
2716          #          #
2717        } else {        } else {
2718          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2719        }        }
2720        my $root_element; !!!create-element ($root_element, 'html');  
2721        $self->{document}->append_child ($root_element);      my $root_element; !!!create-element ($root_element, 'html');
2722        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
2723        #$phase = 'main';      push @{$self->{open_elements}}, [$root_element, 'html'];
2724        ## reprocess  
2725        #redo B;      $self->{application_cache_selection}->(undef);
2726        return;  
2727        ## NOTE: Reprocess the token.
2728        return; ## Go to the "before head" insertion mode.
2729    
2730        ## ISSUE: There is an issue in the spec
2731    } # B    } # B
2732    
2733      die "$0: _tree_construction_root_element: This should never be reached";
2734  } # _tree_construction_root_element  } # _tree_construction_root_element
2735    
2736  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1732  sub _reset_insertion_mode ($) { Line 2745  sub _reset_insertion_mode ($) {
2745            
2746      ## Step 3      ## Step 3
2747      S3: {      S3: {
2748        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2749        if (defined $self->{inner_html_node}) {          $last = 1;
2750          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
2751              $self->{inner_html_node}->[1] eq 'th') {            if ($self->{inner_html_node}->[1] eq 'td' or
2752            #                $self->{inner_html_node}->[1] eq 'th') {
2753          } else {              !!!cp ('t27');
2754            $node = $self->{inner_html_node};              #
2755              } else {
2756                !!!cp ('t28');
2757                $node = $self->{inner_html_node};
2758              }
2759          }          }
2760        }        }
2761            
2762        ## Step 4..13        ## Step 4..13
2763        my $new_mode = {        my $new_mode = {
2764                        select => 'in select',                        select => IN_SELECT_IM,
2765                        td => 'in cell',                        ## NOTE: |option| and |optgroup| do not set
2766                        th => 'in cell',                        ## insertion mode to "in select" by themselves.
2767                        tr => 'in row',                        td => IN_CELL_IM,
2768                        tbody => 'in table body',                        th => IN_CELL_IM,
2769                        thead => 'in table head',                        tr => IN_ROW_IM,
2770                        tfoot => 'in table foot',                        tbody => IN_TABLE_BODY_IM,
2771                        caption => 'in caption',                        thead => IN_TABLE_BODY_IM,
2772                        colgroup => 'in column group',                        tfoot => IN_TABLE_BODY_IM,
2773                        table => 'in table',                        caption => IN_CAPTION_IM,
2774                        head => 'in body', # not in head!                        colgroup => IN_COLUMN_GROUP_IM,
2775                        body => 'in body',                        table => IN_TABLE_IM,
2776                        frameset => 'in frameset',                        head => IN_BODY_IM, # not in head!
2777                          body => IN_BODY_IM,
2778                          frameset => IN_FRAMESET_IM,
2779                       }->{$node->[1]};                       }->{$node->[1]};
2780        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2781                
2782        ## Step 14        ## Step 14
2783        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2784          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2785            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
2786              $self->{insertion_mode} = BEFORE_HEAD_IM;
2787          } else {          } else {
2788            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
2789              !!!cp ('t30');
2790              $self->{insertion_mode} = AFTER_HEAD_IM;
2791          }          }
2792          return;          return;
2793          } else {
2794            !!!cp ('t31');
2795        }        }
2796                
2797        ## Step 15        ## Step 15
2798        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2799                
2800        ## Step 16        ## Step 16
2801        $i--;        $i--;
# Line 1780  sub _reset_insertion_mode ($) { Line 2804  sub _reset_insertion_mode ($) {
2804        ## Step 17        ## Step 17
2805        redo S3;        redo S3;
2806      } # S3      } # S3
2807    
2808      die "$0: _reset_insertion_mode: This line should never be reached";
2809  } # _reset_insertion_mode  } # _reset_insertion_mode
2810    
2811  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2812    my $self = shift;    my $self = shift;
2813    
   my $phase = 'main';  
   
2814    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2815    
2816    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1803  sub _tree_construction_main ($) { Line 2827  sub _tree_construction_main ($) {
2827      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2828      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2829        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2830            !!!cp ('t32');
2831          return;          return;
2832        }        }
2833      }      }
# Line 1817  sub _tree_construction_main ($) { Line 2842  sub _tree_construction_main ($) {
2842    
2843        ## Step 6        ## Step 6
2844        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2845            !!!cp ('t33_1');
2846          #          #
2847        } else {        } else {
2848          my $in_open_elements;          my $in_open_elements;
2849          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2850            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2851                !!!cp ('t33');
2852              $in_open_elements = 1;              $in_open_elements = 1;
2853              last OE;              last OE;
2854            }            }
2855          }          }
2856          if ($in_open_elements) {          if ($in_open_elements) {
2857              !!!cp ('t34');
2858            #            #
2859          } else {          } else {
2860              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2861              !!!cp ('t35');
2862            redo S4;            redo S4;
2863          }          }
2864        }        }
# Line 1851  sub _tree_construction_main ($) { Line 2881  sub _tree_construction_main ($) {
2881    
2882        ## Step 11        ## Step 11
2883        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2884            !!!cp ('t36');
2885          ## Step 7'          ## Step 7'
2886          $i++;          $i++;
2887          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2888                    
2889          redo S7;          redo S7;
2890        }        }
2891    
2892          !!!cp ('t37');
2893      } # S7      } # S7
2894    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2895    
2896    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2897      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2898        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2899            !!!cp ('t38');
2900          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2901          return;          return;
2902        }        }
2903      }      }
2904    
2905        !!!cp ('t39');
2906    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2907    
2908    my $style_start_tag = sub {    my $insert;
2909      my $style_el; !!!create-element ($style_el, 'style');  
2910      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
2911      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
2912       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
2913        ->append_child ($style_el);      ## Step 1
2914      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
2915                      my $el;
2916        !!!create-element ($el, $start_tag_name, $token->{attributes});
2917    
2918        ## Step 2
2919        $insert->($el);
2920    
2921        ## Step 3
2922        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2923        delete $self->{escape}; # MUST
2924    
2925        ## Step 4
2926      my $text = '';      my $text = '';
2927      !!!next-token;      !!!next-token;
2928      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2929          !!!cp ('t40');
2930        $text .= $token->{data};        $text .= $token->{data};
2931        !!!next-token;        !!!next-token;
2932      } # stop if non-character token or tokenizer stops tokenising      }
2933    
2934        ## Step 5
2935      if (length $text) {      if (length $text) {
2936        $style_el->manakai_append_text ($text);        !!!cp ('t41');
2937          my $text = $self->{document}->create_text_node ($text);
2938          $el->append_child ($text);
2939      }      }
2940        
2941      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2942                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2943      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2944        ## Step 7
2945        if ($token->{type} == END_TAG_TOKEN and
2946            $token->{tag_name} eq $start_tag_name) {
2947          !!!cp ('t42');
2948        ## Ignore the token        ## Ignore the token
2949      } else {      } else {
2950        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
2951        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
2952            !!!cp ('t43');
2953            !!!parse-error (type => 'in CDATA:#'.$token->{type});
2954          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2955            !!!cp ('t44');
2956            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2957          } else {
2958            die "$0: $content_model_flag in parse_rcdata";
2959          }
2960      }      }
2961      !!!next-token;      !!!next-token;
2962    }; # $style_start_tag    }; # $parse_rcdata
2963    
2964    my $script_start_tag = sub {    my $script_start_tag = sub () {
2965      my $script_el;      my $script_el;
2966      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2967      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2968    
2969      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2970        delete $self->{escape}; # MUST
2971            
2972      my $text = '';      my $text = '';
2973      !!!next-token;      !!!next-token;
2974      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2975          !!!cp ('t45');
2976        $text .= $token->{data};        $text .= $token->{data};
2977        !!!next-token;        !!!next-token;
2978      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
2979      if (length $text) {      if (length $text) {
2980          !!!cp ('t46');
2981        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2982      }      }
2983                                
2984      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2985    
2986      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2987          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2988          !!!cp ('t47');
2989        ## Ignore the token        ## Ignore the token
2990      } else {      } else {
2991          !!!cp ('t48');
2992        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2993        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2994        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2995      }      }
2996            
2997      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
2998          !!!cp ('t49');
2999        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3000      } else {      } else {
3001          !!!cp ('t50');
3002        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3003        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3004          
3005        (($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);  
3006                
3007        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3008                
# Line 1943  sub _tree_construction_main ($) { Line 3012  sub _tree_construction_main ($) {
3012      !!!next-token;      !!!next-token;
3013    }; # $script_start_tag    }; # $script_start_tag
3014    
3015      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3016      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3017      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3018    
3019    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3020      my $tag_name = shift;      my $tag_name = shift;
3021    
3022        ## NOTE: The adoption agency algorithm (AAA).
3023    
3024      FET: {      FET: {
3025        ## Step 1        ## Step 1
3026        my $formatting_element;        my $formatting_element;
3027        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3028        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3029          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3030              !!!cp ('t51');
3031            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3032            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3033            last AFE;            last AFE;
3034          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3035              !!!cp ('t52');
3036            last AFE;            last AFE;
3037          }          }
3038        } # AFE        } # AFE
3039        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3040            !!!cp ('t53');
3041          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
3042          ## Ignore the token          ## Ignore the token
3043          !!!next-token;          !!!next-token;
# Line 1972  sub _tree_construction_main ($) { Line 3050  sub _tree_construction_main ($) {
3050          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3051          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3052            if ($in_scope) {            if ($in_scope) {
3053                !!!cp ('t54');
3054              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3055              last INSCOPE;              last INSCOPE;
3056            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3057              !!!parse-error;              !!!cp ('t55');
3058                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3059              ## Ignore the token              ## Ignore the token
3060              !!!next-token;              !!!next-token;
3061              return;              return;
3062            }            }
3063          } elsif ({          } elsif ({
3064                    table => 1, caption => 1, td => 1, th => 1,                    applet => 1, table => 1, caption => 1, td => 1, th => 1,
3065                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3066                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3067              !!!cp ('t56');
3068            $in_scope = 0;            $in_scope = 0;
3069          }          }
3070        } # INSCOPE        } # INSCOPE
3071        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3072          !!!parse-error;          !!!cp ('t57');
3073            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3074          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3075          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3076          return;          return;
3077        }        }
3078        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3079          !!!parse-error;          !!!cp ('t58');
3080            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3081        }        }
3082                
3083        ## Step 2        ## Step 2
# Line 2005  sub _tree_construction_main ($) { Line 3088  sub _tree_construction_main ($) {
3088          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
3089              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3090              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3091               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe
3092              !!!cp ('t59');
3093            $furthest_block = $node;            $furthest_block = $node;
3094            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3095          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3096              !!!cp ('t60');
3097            last OE;            last OE;
3098          }          }
3099        } # OE        } # OE
3100                
3101        ## Step 3        ## Step 3
3102        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3103            !!!cp ('t61');
3104          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3105          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3106          !!!next-token;          !!!next-token;
# Line 2027  sub _tree_construction_main ($) { Line 3113  sub _tree_construction_main ($) {
3113        ## Step 5        ## Step 5
3114        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3115        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3116            !!!cp ('t62');
3117          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3118        }        }
3119                
# Line 2049  sub _tree_construction_main ($) { Line 3136  sub _tree_construction_main ($) {
3136          S7S2: {          S7S2: {
3137            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3138              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3139                  !!!cp ('t63');
3140                $node_i_in_active = $_;                $node_i_in_active = $_;
3141                last S7S2;                last S7S2;
3142              }              }
# Line 2062  sub _tree_construction_main ($) { Line 3150  sub _tree_construction_main ($) {
3150                    
3151          ## Step 4          ## Step 4
3152          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3153              !!!cp ('t64');
3154            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3155          }          }
3156                    
3157          ## Step 5          ## Step 5
3158          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3159              !!!cp ('t65');
3160            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3161            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3162            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2084  sub _tree_construction_main ($) { Line 3174  sub _tree_construction_main ($) {
3174        } # S7          } # S7  
3175                
3176        ## Step 8        ## Step 8
3177        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ({
3178               table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3179              }->{$common_ancestor_node->[1]}) {
3180            my $foster_parent_element;
3181            my $next_sibling;
3182                             OE: for (reverse 0..$#{$self->{open_elements}}) {
3183                               if ($self->{open_elements}->[$_]->[1] eq 'table') {
3184                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3185                                 if (defined $parent and $parent->node_type == 1) {
3186                                   !!!cp ('t65.1');
3187                                   $foster_parent_element = $parent;
3188                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3189                                 } else {
3190                                   !!!cp ('t65.2');
3191                                   $foster_parent_element
3192                                     = $self->{open_elements}->[$_ - 1]->[0];
3193                                 }
3194                                 last OE;
3195                               }
3196                             } # OE
3197                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3198                               unless defined $foster_parent_element;
3199            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3200            $open_tables->[-1]->[1] = 1; # tainted
3201          } else {
3202            !!!cp ('t65.3');
3203            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3204          }
3205                
3206        ## Step 9        ## Step 9
3207        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2101  sub _tree_construction_main ($) { Line 3218  sub _tree_construction_main ($) {
3218        my $i;        my $i;
3219        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3220          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3221              !!!cp ('t66');
3222            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3223            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3224          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3225              !!!cp ('t67');
3226            $i = $_;            $i = $_;
3227          }          }
3228        } # AFE        } # AFE
# Line 2113  sub _tree_construction_main ($) { Line 3232  sub _tree_construction_main ($) {
3232        undef $i;        undef $i;
3233        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3234          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3235              !!!cp ('t68');
3236            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3237            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3238          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3239              !!!cp ('t69');
3240            $i = $_;            $i = $_;
3241          }          }
3242        } # OE        } # OE
# Line 2126  sub _tree_construction_main ($) { Line 3247  sub _tree_construction_main ($) {
3247      } # FET      } # FET
3248    }; # $formatting_end_tag    }; # $formatting_end_tag
3249    
3250    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3251      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3252    }; # $insert_to_current    }; # $insert_to_current
3253    
3254    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3255                         my $child = shift;      my $child = shift;
3256                         if ({      if ({
3257                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3258                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3259                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3260                           # MUST        my $foster_parent_element;
3261                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3262                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3263                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3264                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3265                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3266                                   !!!cp ('t70');
3267                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3268                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3269                               } else {                               } else {
3270                                   !!!cp ('t71');
3271                                 $foster_parent_element                                 $foster_parent_element
3272                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3273                               }                               }
# Line 2156  sub _tree_construction_main ($) { Line 3278  sub _tree_construction_main ($) {
3278                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3279                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3280                             ($child, $next_sibling);                             ($child, $next_sibling);
3281                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3282                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3283                         }        !!!cp ('t72');
3284          $self->{open_elements}->[-1]->[0]->append_child ($child);
3285        }
3286    }; # $insert_to_foster    }; # $insert_to_foster
3287    
3288    my $in_body = sub {    B: {
3289      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3290      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3291        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle');
3292          $script_start_tag->();        ## Ignore the token
3293          return;        ## Stay in the phase
3294        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
3295          $style_start_tag->();        redo B;
3296          return;      } elsif ($token->{type} == START_TAG_TOKEN and
3297        } elsif ({               $token->{tag_name} eq 'html') {
3298                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3299                 }->{$token->{tag_name}}) {          !!!cp ('t79');
3300          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html:html');
3301          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
3302          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3303          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3304          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html:html');
3305            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3306          } else {        } else {
3307            $insert->($el);          !!!cp ('t81');
3308          }        }
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: There is an "as if in head" code clone  
         my $title_el;  
         !!!create-element ($title_el, 'title', $token->{attributes});  
         (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
           ->append_child ($title_el);  
         $self->{content_model_flag} = 'RCDATA';  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
         }  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
3309    
3310          !!!next-token;        !!!cp ('t82');
3311          return;        !!!parse-error (type => 'not first start tag');
3312        } elsif ($token->{tag_name} eq 'marquee' or        my $top_el = $self->{open_elements}->[0]->[0];
3313                 $token->{tag_name} eq 'object') {        for my $attr_name (keys %{$token->{attributes}}) {
3314          $reconstruct_active_formatting_elements->($insert_to_current);          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3315                      !!!cp ('t84');
3316          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            $top_el->set_attribute_ns
3317          push @$active_formatting_elements, ['#marker', ''];              (undef, [undef, $attr_name],
3318                         $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;  
3319          }          }
3320        } elsif ({        }
3321                  textarea => 1,        !!!next-token;
3322                  noembed => 1,        redo B;
3323                  noframes => 1,      } elsif ($token->{type} == COMMENT_TOKEN) {
3324                  noscript => 0, ## TODO: 1 if scripting is enabled        my $comment = $self->{document}->create_comment ($token->{data});
3325                 }->{$token->{tag_name}}) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3326          my $tag_name = $token->{tag_name};          !!!cp ('t85');
3327          my $el;          $self->{document}->append_child ($comment);
3328          !!!create-element ($el, $token->{tag_name}, $token->{attributes});        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3329                    !!!cp ('t86');
3330          if ($token->{tag_name} eq 'textarea') {          $self->{open_elements}->[0]->[0]->append_child ($comment);
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
3331        } else {        } else {
3332          $reconstruct_active_formatting_elements->($insert_to_current);          !!!cp ('t87');
3333                    $self->{open_elements}->[-1]->[0]->append_child ($comment);
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
3334        }        }
3335      } elsif ($token->{type} eq 'end tag') {        !!!next-token;
3336        if ($token->{tag_name} eq 'body') {        redo B;
3337          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3338            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
3339            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3340              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3341            }              !!!cp ('t88.2');
3342            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3343            !!!next-token;            } else {
3344            return;              !!!cp ('t88.1');
3345          } else {              ## Ignore the token.
3346            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
3347            ## 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;  
3348            }            }
3349          } # INSCOPE            unless (length $token->{data}) {
3350                        !!!cp ('t88');
3351          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
3352            !!!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;  
3353            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3354          }          }
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex=> 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
3355    
3356              !!!next-token;          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3357              last S2;            !!!cp ('t89');
3358            } else {            ## As if <head>
3359              ## Step 3            !!!create-element ($self->{head_element}, 'head');
3360              if (not $formatting_category->{$node->[1]} and            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3361                  #not $phrasing_category->{$node->[1]} and            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3362                  ($special_category->{$node->[1]} or  
3363                   $scoping_category->{$node->[1]})) {            ## Reprocess in the "in head" insertion mode...
3364                !!!parse-error (type => 'not closed:'.$node->[1]);            pop @{$self->{open_elements}};
3365                ## Ignore the token  
3366                !!!next-token;            ## Reprocess in the "after head" insertion mode...
3367                last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3368              }            !!!cp ('t90');
3369            }            ## As if </noscript>
3370                        pop @{$self->{open_elements}};
3371            ## Step 4            !!!parse-error (type => 'in noscript:#character');
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
3372                        
3373            ## Step 5;            ## Reprocess in the "in head" insertion mode...
3374            redo S2;            ## As if </head>
3375          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
3376    
3377    B: {            ## Reprocess in the "after head" insertion mode...
3378      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3379        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
3380          !!!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]);  
         }  
3381    
3382          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
3383          last B;          } else {
3384              !!!cp ('t92');
3385            }
3386    
3387          ## ISSUE: There is an issue in the spec.              ## "after head" insertion mode
3388        } else {              ## As if <body>
3389          if ($self->{insertion_mode} eq 'before head') {              !!!insert-element ('body');
3390            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';  
3391              ## reprocess              ## reprocess
3392              redo B;              redo B;
3393            } 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';  
3394              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3395                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3396              #} elsif ({                  !!!cp ('t93');
3397              #          base => 1, link => 1, meta => 1,                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3398              #          script => 1, style => 1, title => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3399              #         }->{$token->{tag_name}}) {                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3400              #  ## reprocess                  $self->{insertion_mode} = IN_HEAD_IM;
3401              } else {                  !!!next-token;
3402                ## reprocess                  redo B;
3403              }                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3404              redo B;                  !!!cp ('t94');
3405            } elsif ($token->{type} eq 'end tag') {                  #
3406              if ($token->{tag_name} eq 'html') {                } else {
3407                    !!!cp ('t95');
3408                    !!!parse-error (type => 'in head:head'); # or in head noscript
3409                    ## Ignore the token
3410                    !!!next-token;
3411                    redo B;
3412                  }
3413                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3414                  !!!cp ('t96');
3415                ## As if <head>                ## As if <head>
3416                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3417                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3418                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3419                $self->{insertion_mode} = 'in head';  
3420                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
3421                redo B;                ## Reprocess in the "in head" insertion mode...
3422              } else {              } else {
3423                !!!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;  
               }  
3424              }              }
               
             #  
           } 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';  
3425    
3426                my $text = '';              if ($token->{tag_name} eq 'base') {
3427                !!!next-token;                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3428                while ($token->{type} eq 'character') {                  !!!cp ('t98');
3429                  $text .= $token->{data};                  ## As if </noscript>
3430                  !!!next-token;                  pop @{$self->{open_elements}};
3431                }                  !!!parse-error (type => 'in noscript:base');
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
3432                                
3433                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
3434                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
3435                } else {                } else {
3436                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
3437                }                }
               !!!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);  
3438    
3439                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
3440                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3441              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
3442                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3443                ## 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}};  
3444                } else {                } else {
3445                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
3446                }                }
3447                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3448                !!!next-token;                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3449                redo B;                pop @{$self->{open_elements}} # <head>
3450              } elsif ($token->{tag_name} eq 'html') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
               #  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
3451                !!!next-token;                !!!next-token;
3452                redo B;                redo B;
3453              }              } elsif ($token->{tag_name} eq 'link') {
3454            } else {                ## NOTE: There is a "as if in head" code clone.
3455              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3456            }                  !!!cp ('t102');
3457                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3458            if ($self->{open_elements}->[-1]->[1] eq 'head') {                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3459              ## As if </head>                } else {
3460              pop @{$self->{open_elements}};                  !!!cp ('t103');
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
3461                }                }
3462              }                !!!insert-element ($token->{tag_name}, $token->{attributes});
3463                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3464              #                pop @{$self->{open_elements}} # <head>
3465            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
3466                !!!next-token;                !!!next-token;
3467                redo B;                redo B;
3468              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
3469                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
3470                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3471                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
3472                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3473                $self->{insertion_mode} = 'in head';                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3474                ## reprocess                } else {
3475                redo B;                  !!!cp ('t105');
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
3476                }                }
3477              }                !!!insert-element ($token->{tag_name}, $token->{attributes});
3478                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
3479    
3480              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
3481              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3482              ## into the current node" while characters might not be                    !!!cp ('t106');
3483              ## result in a new Text node.                    $self->{change_encoding}
3484              $reconstruct_active_formatting_elements->($insert_to_foster);                        ->($self, $token->{attributes}->{charset}->{value});
3485                                  
3486              if ({                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3487                   table => 1, tbody => 1, tfoot => 1,                        ->set_user_data (manakai_has_reference =>
3488                   thead => 1, tr => 1,                                             $token->{attributes}->{charset}
3489                  }->{$self->{open_elements}->[-1]->[1]}) {                                                 ->{has_reference});
3490                # MUST                  } elsif ($token->{attributes}->{content}) {
3491                my $foster_parent_element;                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3492                my $next_sibling;                    if ($token->{attributes}->{content}->{value}
3493                my $prev_sibling;                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3494                OE: for (reverse 0..$#{$self->{open_elements}}) {                            [\x09-\x0D\x20]*=
3495                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3496                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3497                    if (defined $parent and $parent->node_type == 1) {                      !!!cp ('t107');
3498                      $foster_parent_element = $parent;                      $self->{change_encoding}
3499                      $next_sibling = $self->{open_elements}->[$_]->[0];                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3500                      $prev_sibling = $next_sibling->previous_sibling;                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3501                            ->set_user_data (manakai_has_reference =>
3502                                                 $token->{attributes}->{content}
3503                                                       ->{has_reference});
3504                    } else {                    } else {
3505                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
3506                    }                    }
                   last OE;  
3507                  }                  }
               } # 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});  
3508                } else {                } else {
3509                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
3510                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
3511                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3512                }                        ->set_user_data (manakai_has_reference =>
3513              } else {                                             $token->{attributes}->{charset}
3514                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
3515              }                  }
3516                                if ($token->{attributes}->{content}) {
3517              !!!next-token;                    !!!cp ('t110');
3518              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3519            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
3520              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
3521              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
3522              !!!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}};  
3523                }                }
3524    
3525                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
3526                  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}};  
3527                !!!next-token;                !!!next-token;
3528                redo B;                redo B;
3529              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
3530                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3531                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
3532                       }->{$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]);  
3533                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3534                    !!!parse-error (type => 'in noscript:title');
3535                  
3536                    $self->{insertion_mode} = IN_HEAD_IM;
3537                    ## Reprocess in the "in head" insertion mode...
3538                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3539                    !!!cp ('t112');
3540                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3541                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3542                  } else {
3543                    !!!cp ('t113');
3544                }                }
3545    
3546                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
3547                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
3548                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
3549                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3550                  pop @{$self->{open_elements}} # <head>
3551                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3552                redo B;                redo B;
3553              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'style') {
3554                ## NOTE: There are code clones for this "table in table"                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3555                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## insertion mode IN_HEAD_IM)
3556                  ## NOTE: There is a "as if in head" code clone.
3557                ## As if </table>                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3558                ## have a table element in table scope                  !!!cp ('t114');
3559                my $i;                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3560                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3561                  my $node = $self->{open_elements}->[$_];                } else {
3562                  if ($node->[1] eq 'table') {                  !!!cp ('t115');
3563                    $i = $_;                }
3564                    last INSCOPE;                $parse_rcdata->(CDATA_CONTENT_MODEL);
3565                  } elsif ({                pop @{$self->{open_elements}} # <head>
3566                            table => 1, html => 1,                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3567                           }->{$node->[1]}) {                redo B;
3568                    last INSCOPE;              } elsif ($token->{tag_name} eq 'noscript') {
3569                  }                if ($self->{insertion_mode} == IN_HEAD_IM) {
3570                } # INSCOPE                  !!!cp ('t116');
3571                unless (defined $i) {                  ## NOTE: and scripting is disalbed
3572                  !!!parse-error (type => 'unmatched end tag:table');                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3573                  ## Ignore tokens </table><table>                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3574                  !!!next-token;                  !!!next-token;
3575                  redo B;                  redo B;
3576                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3577                    !!!cp ('t117');
3578                    !!!parse-error (type => 'in noscript:noscript');
3579                    ## Ignore the token
3580                    !!!next-token;
3581                    redo B;
3582                  } else {
3583                    !!!cp ('t118');
3584                    #
3585                }                }
3586                } elsif ($token->{tag_name} eq 'script') {
3587                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3588                    !!!cp ('t119');
3589                    ## As if </noscript>
3590                    pop @{$self->{open_elements}};
3591                    !!!parse-error (type => 'in noscript:script');
3592                                
3593                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
3594                if ({                  ## Reprocess in the "in head" insertion mode...
3595                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3596                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
3597                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3598                  !!!back-token; # <table>                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3599                  $token = {type => 'end tag', tag_name => 'table'};                } else {
3600                  !!!back-token;                  !!!cp ('t121');
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
3601                }                }
3602    
3603                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
3604                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
3605                  pop @{$self->{open_elements}} # <head>
3606                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3607                  redo B;
3608                } elsif ($token->{tag_name} eq 'body' or
3609                         $token->{tag_name} eq 'frameset') {
3610                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3611                    !!!cp ('t122');
3612                    ## As if </noscript>
3613                    pop @{$self->{open_elements}};
3614                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3615                    
3616                    ## Reprocess in the "in head" insertion mode...
3617                    ## As if </head>
3618                    pop @{$self->{open_elements}};
3619                    
3620                    ## Reprocess in the "after head" insertion mode...
3621                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3622                    !!!cp ('t124');
3623                    pop @{$self->{open_elements}};
3624                    
3625                    ## Reprocess in the "after head" insertion mode...
3626                  } else {
3627                    !!!cp ('t125');
3628                }                }
3629    
3630                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
3631                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3632                $self->_reset_insertion_mode;                if ($token->{tag_name} eq 'body') {
3633                    !!!cp ('t126');
3634                ## reprocess                  $self->{insertion_mode} = IN_BODY_IM;
3635                  } elsif ($token->{tag_name} eq 'frameset') {
3636                    !!!cp ('t127');
3637                    $self->{insertion_mode} = IN_FRAMESET_IM;
3638                  } else {
3639                    die "$0: tag name: $self->{tag_name}";
3640                  }
3641                  !!!next-token;
3642                redo B;                redo B;
3643              } else {              } else {
3644                  !!!cp ('t128');
3645                #                #
3646              }              }
3647            } elsif ($token->{type} eq 'end tag') {  
3648              if ($token->{tag_name} eq 'table') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3649                ## have a table element in table scope                !!!cp ('t129');
3650                my $i;                ## As if </noscript>
3651                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                pop @{$self->{open_elements}};
3652                  my $node = $self->{open_elements}->[$_];                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3653                  if ($node->[1] eq $token->{tag_name}) {                
3654                    $i = $_;                ## Reprocess in the "in head" insertion mode...
3655                    last INSCOPE;                ## As if </head>
3656                  } elsif ({                pop @{$self->{open_elements}};
3657                            table => 1, html => 1,  
3658                           }->{$node->[1]}) {                ## Reprocess in the "after head" insertion mode...
3659                    last INSCOPE;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3660                  }                !!!cp ('t130');
3661                } # INSCOPE                ## As if </head>
3662                unless (defined $i) {                pop @{$self->{open_elements}};
3663    
3664                  ## Reprocess in the "after head" insertion mode...
3665                } else {
3666                  !!!cp ('t131');
3667                }
3668    
3669                ## "after head" insertion mode
3670                ## As if <body>
3671                !!!insert-element ('body');
3672                $self->{insertion_mode} = IN_BODY_IM;
3673                ## reprocess
3674                redo B;
3675              } elsif ($token->{type} == END_TAG_TOKEN) {
3676                if ($token->{tag_name} eq 'head') {
3677                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3678                    !!!cp ('t132');
3679                    ## As if <head>
3680                    !!!create-element ($self->{head_element}, 'head');
3681                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3682                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3683    
3684                    ## Reprocess in the "in head" insertion mode...
3685                    pop @{$self->{open_elements}};
3686                    $self->{insertion_mode} = AFTER_HEAD_IM;
3687                    !!!next-token;
3688                    redo B;
3689                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3690                    !!!cp ('t133');
3691                    ## As if </noscript>
3692                    pop @{$self->{open_elements}};
3693                    !!!parse-error (type => 'in noscript:/head');
3694                    
3695                    ## Reprocess in the "in head" insertion mode...
3696                    pop @{$self->{open_elements}};
3697                    $self->{insertion_mode} = AFTER_HEAD_IM;
3698                    !!!next-token;
3699                    redo B;
3700                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3701                    !!!cp ('t134');
3702                    pop @{$self->{open_elements}};
3703                    $self->{insertion_mode} = AFTER_HEAD_IM;
3704                    !!!next-token;
3705                    redo B;
3706                  } else {
3707                    !!!cp ('t135');
3708                    #
3709                  }
3710                } elsif ($token->{tag_name} eq 'noscript') {
3711                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3712                    !!!cp ('t136');
3713                    pop @{$self->{open_elements}};
3714                    $self->{insertion_mode} = IN_HEAD_IM;
3715                    !!!next-token;
3716                    redo B;
3717                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3718                    !!!cp ('t137');
3719                    !!!parse-error (type => 'unmatched end tag:noscript');
3720                    ## Ignore the token ## ISSUE: An issue in the spec.
3721                    !!!next-token;
3722                    redo B;
3723                  } else {
3724                    !!!cp ('t138');
3725                    #
3726                  }
3727                } elsif ({
3728                          body => 1, html => 1,
3729                         }->{$token->{tag_name}}) {
3730                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3731                    !!!cp ('t139');
3732                    ## As if <head>
3733                    !!!create-element ($self->{head_element}, 'head');
3734                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3735                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3736    
3737                    $self->{insertion_mode} = IN_HEAD_IM;
3738                    ## Reprocess in the "in head" insertion mode...
3739                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3740                    !!!cp ('t140');
3741                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3742                  ## Ignore the token                  ## Ignore the token
3743                  !!!next-token;                  !!!next-token;
3744                  redo B;                  redo B;
3745                  } else {
3746                    !!!cp ('t141');
3747                }                }
3748                                
3749                ## generate implied end tags                #
3750                if ({              } elsif ({
3751                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3752                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3753                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3754                  !!!back-token;                  !!!cp ('t142');
3755                  $token = {type => 'end tag',                  ## As if <head>
3756                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!create-element ($self->{head_element}, 'head');
3757                  redo B;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3758                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3759    
3760                    $self->{insertion_mode} = IN_HEAD_IM;
3761                    ## Reprocess in the "in head" insertion mode...
3762                  } else {
3763                    !!!cp ('t143');
3764                }                }
3765    
3766                if ($self->{open_elements}->[-1]->[1] ne 'table') {                #
3767                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } else {
3768                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3769                    !!!cp ('t144');
3770                    #
3771                  } else {
3772                    !!!cp ('t145');
3773                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3774                    ## Ignore the token
3775                    !!!next-token;
3776                    redo B;
3777                }                }
3778                }
3779    
3780                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3781                  !!!cp ('t146');
3782                  ## As if </noscript>
3783                  pop @{$self->{open_elements}};
3784                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3785                  
3786                  ## Reprocess in the "in head" insertion mode...
3787                  ## As if </head>
3788                  pop @{$self->{open_elements}};
3789    
3790                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3791                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3792                  !!!cp ('t147');
3793                  ## As if </head>
3794                  pop @{$self->{open_elements}};
3795    
3796                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3797                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3798              } elsif ({  ## ISSUE: This case cannot be reached?
3799                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t148');
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
3800                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3801                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3802                !!!next-token;                !!!next-token;
3803                redo B;                redo B;
3804              } else {              } else {
3805                #                !!!cp ('t149');
3806              }              }
           } else {  
             #  
           }  
3807    
3808            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
3809            $in_body->($insert_to_foster);              ## As if <body>
3810            redo B;              !!!insert-element ('body');
3811          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
3812            if ($token->{type} eq 'character') {              ## reprocess
3813              ## NOTE: This is a code clone of "character in body".              redo B;
3814          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3815            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3816              !!!cp ('t149.1');
3817    
3818              ## NOTE: As if <head>
3819              !!!create-element ($self->{head_element}, 'head');
3820              $self->{open_elements}->[-1]->[0]->append_child
3821                  ($self->{head_element});
3822              #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3823              #$self->{insertion_mode} = IN_HEAD_IM;
3824              ## NOTE: Reprocess.
3825    
3826              ## NOTE: As if </head>
3827              #pop @{$self->{open_elements}};
3828              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3829              ## NOTE: Reprocess.
3830              
3831              #
3832            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3833              !!!cp ('t149.2');
3834    
3835              ## NOTE: As if </head>
3836              pop @{$self->{open_elements}};
3837              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3838              ## NOTE: Reprocess.
3839    
3840              #
3841            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3842              !!!cp ('t149.3');
3843    
3844              !!!parse-error (type => 'in noscript:#eof');
3845    
3846              ## As if </noscript>
3847              pop @{$self->{open_elements}};
3848              #$self->{insertion_mode} = IN_HEAD_IM;
3849              ## NOTE: Reprocess.
3850    
3851              ## NOTE: As if </head>
3852              pop @{$self->{open_elements}};
3853              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3854              ## NOTE: Reprocess.
3855    
3856              #
3857            } else {
3858              !!!cp ('t149.4');
3859              #
3860            }
3861    
3862            ## NOTE: As if <body>
3863            !!!insert-element ('body');
3864            $self->{insertion_mode} = IN_BODY_IM;
3865            ## NOTE: Reprocess.
3866            redo B;
3867          } else {
3868            die "$0: $token->{type}: Unknown token type";
3869          }
3870    
3871              ## ISSUE: An issue in the spec.
3872        } elsif ($self->{insertion_mode} & BODY_IMS) {
3873              if ($token->{type} == CHARACTER_TOKEN) {
3874                !!!cp ('t150');
3875                ## NOTE: There is a code clone of "character in body".
3876              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3877                            
3878              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3879    
3880              !!!next-token;              !!!next-token;
3881              redo B;              redo B;
3882            } 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') {  
3883              if ({              if ({
3884                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3885                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3886                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3887                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3888                    ## have an element in table scope
3889                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
3890                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
3891                my $i;                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3892                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
3893                  my $node = $self->{open_elements}->[$_];  
3894                  if ($node->[1] eq 'caption') {                      ## Close the cell
3895                    $i = $_;                      !!!back-token; # <?>
3896                    last INSCOPE;                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1]};
3897                  } elsif ({                      redo B;
3898                            table => 1, html => 1,                    } elsif ({
3899                           }->{$node->[1]}) {                              table => 1, html => 1,
3900                    last INSCOPE;                             }->{$node->[1]}) {
3901                        !!!cp ('t152');
3902                        ## ISSUE: This case can never be reached, maybe.
3903                        last;
3904                      }
3905                  }                  }
3906                } # INSCOPE  
3907                unless (defined $i) {                  !!!cp ('t153');
3908                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
3909                        value => $token->{tag_name});
3910                  ## Ignore the token                  ## Ignore the token
3911                  !!!next-token;                  !!!next-token;
3912                  redo B;                  redo B;
3913                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3914                                  !!!parse-error (type => 'not closed:caption');
3915                ## generate implied end tags                  
3916                if ({                  ## NOTE: As if </caption>.
3917                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
3918                     td => 1, th => 1, tr => 1,                  my $i;
3919                    }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: {
3920                  !!!back-token; # <?>                    for (reverse 0..$#{$self->{open_elements}}) {
3921                  $token = {type => 'end tag', tag_name => 'caption'};                      my $node = $self->{open_elements}->[$_];
3922                  !!!back-token;                      if ($node->[1] eq 'caption') {
3923                  $token = {type => 'end tag',                        !!!cp ('t155');
3924                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        $i = $_;
3925                  redo B;                        last INSCOPE;
3926                }                      } elsif ({
3927                                  table => 1, html => 1,
3928                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                               }->{$node->[1]}) {
3929                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        !!!cp ('t156');
3930                }                        last;
3931                        }
3932                splice @{$self->{open_elements}}, $i;                    }
   
               $clear_up_to_marker->();  
3933    
3934                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
3935                      !!!parse-error (type => 'start tag not allowed',
3936                                      value => $token->{tag_name});
3937                      ## Ignore the token
3938                      !!!next-token;
3939                      redo B;
3940                    } # INSCOPE
3941                    
3942                    ## generate implied end tags
3943                    while ({
3944                            dd => 1, dt => 1, li => 1, p => 1,
3945                           }->{$self->{open_elements}->[-1]->[1]}) {
3946                      !!!cp ('t158');
3947                      pop @{$self->{open_elements}};
3948                    }
3949    
3950                ## reprocess                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3951                redo B;                    !!!cp ('t159');
3952                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3953                    } else {
3954                      !!!cp ('t160');
3955                    }
3956                    
3957                    splice @{$self->{open_elements}}, $i;
3958                    
3959                    $clear_up_to_marker->();
3960                    
3961                    $self->{insertion_mode} = IN_TABLE_IM;
3962                    
3963                    ## reprocess
3964                    redo B;
3965                  } else {
3966                    !!!cp ('t161');
3967                    #
3968                  }
3969              } else {              } else {
3970                  !!!cp ('t162');
3971                #                #
3972              }              }
3973            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3974              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3975                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
3976                my $i;                  ## have an element in table scope
3977                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
3978                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3979                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
3980                    $i = $_;                    if ($node->[1] eq $token->{tag_name}) {
3981                    last INSCOPE;                      !!!cp ('t163');
3982                  } elsif ({                      $i = $_;
3983                            table => 1, html => 1,                      last INSCOPE;
3984                           }->{$node->[1]}) {                    } elsif ({
3985                    last INSCOPE;                              table => 1, html => 1,
3986                               }->{$node->[1]}) {
3987                        !!!cp ('t164');
3988                        last INSCOPE;
3989                      }
3990                    } # INSCOPE
3991                      unless (defined $i) {
3992                        !!!cp ('t165');
3993                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3994                        ## Ignore the token
3995                        !!!next-token;
3996                        redo B;
3997                      }
3998                    
3999                    ## generate implied end tags
4000                    while ({
4001                            dd => 1, dt => 1, li => 1, p => 1,
4002                           }->{$self->{open_elements}->[-1]->[1]}) {
4003                      !!!cp ('t166');
4004                      pop @{$self->{open_elements}};
4005                  }                  }
4006                } # INSCOPE  
4007                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4008                      !!!cp ('t167');
4009                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4010                    } else {
4011                      !!!cp ('t168');
4012                    }
4013                    
4014                    splice @{$self->{open_elements}}, $i;
4015                    
4016                    $clear_up_to_marker->();
4017                    
4018                    $self->{insertion_mode} = IN_ROW_IM;
4019                    
4020                    !!!next-token;
4021                    redo B;
4022                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4023                    !!!cp ('t169');
4024                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4025                  ## Ignore the token                  ## Ignore the token
4026                  !!!next-token;                  !!!next-token;
4027                  redo B;                  redo B;
4028                  } else {
4029                    !!!cp ('t170');
4030                    #
4031                }                }
4032                              } elsif ($token->{tag_name} eq 'caption') {
4033                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4034                if ({                  ## have a table element in table scope
4035                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
4036                     td => 1, th => 1, tr => 1,                  INSCOPE: {
4037                    }->{$self->{open_elements}->[-1]->[1]}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4038                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
4039                  $token = {type => 'end tag',                      if ($node->[1] eq $token->{tag_name}) {
4040                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t171');
4041                  redo B;                        $i = $_;
4042                }                        last INSCOPE;
4043                        } elsif ({
4044                                  table => 1, html => 1,
4045                                 }->{$node->[1]}) {
4046                          !!!cp ('t172');
4047                          last;
4048                        }
4049                      }
4050    
4051                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4052                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4053                                      value => $token->{tag_name});
4054                      ## Ignore the token
4055                      !!!next-token;
4056                      redo B;
4057                    } # INSCOPE
4058                    
4059                    ## generate implied end tags
4060                    while ({
4061                            dd => 1, dt => 1, li => 1, p => 1,
4062                           }->{$self->{open_elements}->[-1]->[1]}) {
4063                      !!!cp ('t174');
4064                      pop @{$self->{open_elements}};
4065                    }
4066                    
4067                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4068                      !!!cp ('t175');
4069                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4070                    } else {
4071                      !!!cp ('t176');
4072                    }
4073                    
4074                    splice @{$self->{open_elements}}, $i;
4075                    
4076                    $clear_up_to_marker->();
4077                    
4078                    $self->{insertion_mode} = IN_TABLE_IM;
4079                    
4080                    !!!next-token;
4081                    redo B;
4082                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4083                    !!!cp ('t177');
4084                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4085                    ## Ignore the token
4086                    !!!next-token;
4087                    redo B;
4088                  } else {
4089                    !!!cp ('t178');
4090                    #
4091                }                }
4092                } elsif ({
4093                          table => 1, tbody => 1, tfoot => 1,
4094                          thead => 1, tr => 1,
4095                         }->{$token->{tag_name}} and
4096                         $self->{insertion_mode} == IN_CELL_IM) {
4097                  ## have an element in table scope
4098                  my $i;
4099                  my $tn;
4100                  INSCOPE: {
4101                    for (reverse 0..$#{$self->{open_elements}}) {
4102                      my $node = $self->{open_elements}->[$_];
4103                      if ($node->[1] eq $token->{tag_name}) {
4104                        !!!cp ('t179');
4105                        $i = $_;
4106    
4107                        ## Close the cell
4108                        !!!back-token; # </?>
4109                        $token = {type => END_TAG_TOKEN, tag_name => $tn};
4110                        redo B;
4111                      } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4112                        !!!cp ('t180');
4113                        $tn = $node->[1];
4114                        ## NOTE: There is exactly one |td| or |th| element
4115                        ## in scope in the stack of open elements by definition.
4116                      } elsif ({
4117                                table => 1, html => 1,
4118                               }->{$node->[1]}) {
4119                        ## ISSUE: Can this be reached?
4120                        !!!cp ('t181');
4121                        last;
4122                      }
4123                    }
4124    
4125                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4126                    !!!parse-error (type => 'unmatched end tag',
4127                $clear_up_to_marker->();                      value => $token->{tag_name});
4128                    ## Ignore the token
4129                $self->{insertion_mode} = 'in table';                  !!!next-token;
4130                    redo B;
4131                !!!next-token;                } # INSCOPE
4132                redo B;              } elsif ($token->{tag_name} eq 'table' and
4133              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4134                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
4135    
4136                ## As if </caption>                ## As if </caption>
# Line 3508  sub _tree_construction_main ($) { Line 4139  sub _tree_construction_main ($) {
4139                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4140                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4141                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4142                      !!!cp ('t184');
4143                    $i = $_;                    $i = $_;
4144                    last INSCOPE;                    last INSCOPE;
4145                  } elsif ({                  } elsif ({
4146                            table => 1, html => 1,                            table => 1, html => 1,
4147                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4148                      !!!cp ('t185');
4149                    last INSCOPE;                    last INSCOPE;
4150                  }                  }
4151                } # INSCOPE                } # INSCOPE
4152                unless (defined $i) {                unless (defined $i) {
4153                    !!!cp ('t186');
4154                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption');
4155                  ## Ignore the token                  ## Ignore the token
4156                  !!!next-token;                  !!!next-token;
# Line 3524  sub _tree_construction_main ($) { Line 4158  sub _tree_construction_main ($) {
4158                }                }
4159                                
4160                ## generate implied end tags                ## generate implied end tags
4161                if ({                while ({
4162                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4163                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4164                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t187');
4165                  !!!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;  
4166                }                }
4167    
4168                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4169                    !!!cp ('t188');
4170                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4171                  } else {
4172                    !!!cp ('t189');
4173                }                }
4174    
4175                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4176    
4177                $clear_up_to_marker->();                $clear_up_to_marker->();
4178    
4179                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4180    
4181                ## reprocess                ## reprocess
4182                redo B;                redo B;
4183              } elsif ({              } elsif ({
4184                        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,  
4185                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4186                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4187                ## Ignore the token                  !!!cp ('t190');
4188                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
             } 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');  
4189                  ## Ignore the token                  ## Ignore the token
4190                  !!!next-token;                  !!!next-token;
4191                  redo B;                  redo B;
4192                } else {                } else {
4193                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4194                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4195                }                }
4196              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4197                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4198                          thead => 1, tr => 1,
4199                         }->{$token->{tag_name}} and
4200                         $self->{insertion_mode} == IN_CAPTION_IM) {
4201                  !!!cp ('t192');
4202                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4203                ## Ignore the token                ## Ignore the token
4204                !!!next-token;                !!!next-token;
4205                redo B;                redo B;
4206              } else {              } else {
4207                #                !!!cp ('t193');
4208                  #
4209              }              }
4210            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4211              #          for my $entry (@{$self->{open_elements}}) {
4212              if (not {
4213                dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,
4214                th => 1, thead => 1, tr => 1, body => 1, html => 1,
4215              }->{$entry->[1]}) {
4216                !!!cp ('t75');
4217                !!!parse-error (type => 'in body:#eof');
4218                last;
4219            }            }
4220            }
4221    
4222            ## As if </colgroup>          ## Stop parsing.
4223            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
4224              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
4225              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
4226          }
4227    
4228          $insert = $insert_to_current;
4229          #
4230        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4231          if ($token->{type} == CHARACTER_TOKEN) {
4232            if (not $open_tables->[-1]->[1] and # tainted
4233                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4234              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4235                  
4236              unless (length $token->{data}) {
4237                !!!cp ('t194');
4238              !!!next-token;              !!!next-token;
4239              redo B;              redo B;
4240            } else {            } else {
4241              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
4242            }            }
4243          } 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;  
               }  
             }  
4244    
4245              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character');
4246    
# Line 3646  sub _tree_construction_main ($) { Line 4249  sub _tree_construction_main ($) {
4249              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4250              ## result in a new Text node.              ## result in a new Text node.
4251              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4252                
4253              if ({              if ({
4254                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4255                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3659  sub _tree_construction_main ($) { Line 4262  sub _tree_construction_main ($) {
4262                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4263                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4264                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4265                        !!!cp ('t196');
4266                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4267                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4268                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4269                    } else {                    } else {
4270                        !!!cp ('t197');
4271                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4272                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4273                    }                    }
# Line 3674  sub _tree_construction_main ($) { Line 4279  sub _tree_construction_main ($) {
4279                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4280                if (defined $prev_sibling and                if (defined $prev_sibling and
4281                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4282                    !!!cp ('t198');
4283                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4284                } else {                } else {
4285                    !!!cp ('t199');
4286                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4287                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4288                     $next_sibling);                     $next_sibling);
4289                }                }
4290              } else {            $open_tables->[-1]->[1] = 1; # tainted
4291                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4292              }            !!!cp ('t200');
4293              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4294            }
4295                            
4296              !!!next-token;          !!!next-token;
4297              redo B;          redo B;
4298            } 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') {  
4299              if ({              if ({
4300                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4301                   th => 1, td => 1,                   th => 1, td => 1,
4302                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4303                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4304                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4305                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4306                           $self->{open_elements}->[-1]->[1] ne 'html') {
4307                      !!!cp ('t201');
4308                      pop @{$self->{open_elements}};
4309                    }
4310                    
4311                    !!!insert-element ('tbody');
4312                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4313                    ## reprocess in the "in table body" insertion mode...
4314                }                }
4315    
4316                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4317                    unless ($token->{tag_name} eq 'tr') {
4318                      !!!cp ('t202');
4319                      !!!parse-error (type => 'missing start tag:tr');
4320                    }
4321                    
4322                    ## Clear back to table body context
4323                    while (not {
4324                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4325                    }->{$self->{open_elements}->[-1]->[1]}) {
4326                      !!!cp ('t203');
4327                      ## ISSUE: Can this case be reached?
4328                      pop @{$self->{open_elements}};
4329                    }
4330                    
4331                    $self->{insertion_mode} = IN_ROW_IM;
4332                    if ($token->{tag_name} eq 'tr') {
4333                      !!!cp ('t204');
4334                      !!!insert-element ($token->{tag_name}, $token->{attributes});
4335                      !!!next-token;
4336                      redo B;
4337                    } else {
4338                      !!!cp ('t205');
4339                      !!!insert-element ('tr');
4340                      ## reprocess in the "in row" insertion mode
4341                    }
4342                  } else {
4343                    !!!cp ('t206');
4344                  }
4345    
4346                  ## Clear back to table row context
4347                while (not {                while (not {
4348                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4349                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4350                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4351                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4352                }                }
4353                                
4354                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
4355                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
4356                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
4357                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
4358                } else {                
4359                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
4360                redo B;                redo B;
4361              } elsif ({              } elsif ({
4362                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4363                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4364                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4365                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4366                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
4367                my $i;                  ## As if </tr>
4368                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4369                  my $node = $self->{open_elements}->[$_];                  my $i;
4370                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4371                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4372                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4373                    $i = $_;                      !!!cp ('t208');
4374                    last INSCOPE;                      $i = $_;
4375                  } elsif ({                      last INSCOPE;
4376                            table => 1, html => 1,                    } elsif ({
4377                           }->{$node->[1]}) {                              html => 1,
4378                    last INSCOPE;  
4379                                ## NOTE: This element does not appear here, maybe.
4380                                table => 1,
4381                               }->{$node->[1]}) {
4382                        !!!cp ('t209');
4383                        last INSCOPE;
4384                      }
4385                    } # INSCOPE
4386                    unless (defined $i) {
4387                     !!!cp ('t210');
4388    ## TODO: This type is wrong.
4389                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4390                      ## Ignore the token
4391                      !!!next-token;
4392                      redo B;
4393                    }
4394                    
4395                    ## Clear back to table row context
4396                    while (not {
4397                      tr => 1, html => 1,
4398                    }->{$self->{open_elements}->[-1]->[1]}) {
4399                      !!!cp ('t211');
4400                      ## ISSUE: Can this case be reached?
4401                      pop @{$self->{open_elements}};
4402                    }
4403                    
4404                    pop @{$self->{open_elements}}; # tr
4405                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4406                    if ($token->{tag_name} eq 'tr') {
4407                      !!!cp ('t212');
4408                      ## reprocess
4409                      redo B;
4410                    } else {
4411                      !!!cp ('t213');
4412                      ## reprocess in the "in table body" insertion mode...
4413                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4414                }                }
4415    
4416                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4417                while (not {                  ## have an element in table scope
4418                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4419                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4420                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4421                      if ({
4422                           tbody => 1, thead => 1, tfoot => 1,
4423                          }->{$node->[1]}) {
4424                        !!!cp ('t214');
4425                        $i = $_;
4426                        last INSCOPE;
4427                      } elsif ({
4428                                table => 1, html => 1,
4429                               }->{$node->[1]}) {
4430                        !!!cp ('t215');
4431                        last INSCOPE;
4432                      }
4433                    } # INSCOPE
4434                    unless (defined $i) {
4435                      !!!cp ('t216');
4436    ## TODO: This erorr type ios wrong.
4437                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4438                      ## Ignore the token
4439                      !!!next-token;
4440                      redo B;
4441                    }
4442    
4443                    ## Clear back to table body context
4444                    while (not {
4445                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4446                    }->{$self->{open_elements}->[-1]->[1]}) {
4447                      !!!cp ('t217');
4448                      ## ISSUE: Can this state be reached?
4449                      pop @{$self->{open_elements}};
4450                    }
4451                    
4452                    ## As if <{current node}>
4453                    ## have an element in table scope
4454                    ## true by definition
4455                    
4456                    ## Clear back to table body context
4457                    ## nop by definition
4458                    
4459                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4460                    $self->{insertion_mode} = IN_TABLE_IM;
4461                    ## reprocess in "in table" insertion mode...
4462                  } else {
4463                    !!!cp ('t218');
4464                }                }
4465    
4466                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4467                ## have an element in table scope                  ## Clear back to table context
4468                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4469                           $self->{open_elements}->[-1]->[1] ne 'html') {
4470                ## Clear back to table body context                    !!!cp ('t219');
4471                ## nop by definition                    ## ISSUE: Can this state be reached?
4472                      pop @{$self->{open_elements}};
4473                pop @{$self->{open_elements}};                  }
4474                $self->{insertion_mode} = 'in table';                  
4475                ## reprocess                  !!!insert-element ('colgroup');
4476                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4477                    ## reprocess
4478                    redo B;
4479                  } elsif ({
4480                            caption => 1,
4481                            colgroup => 1,
4482                            tbody => 1, tfoot => 1, thead => 1,
4483                           }->{$token->{tag_name}}) {
4484                    ## Clear back to table context
4485                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4486                           $self->{open_elements}->[-1]->[1] ne 'html') {
4487                      !!!cp ('t220');
4488                      ## ISSUE: Can this state be reached?
4489                      pop @{$self->{open_elements}};
4490                    }
4491                    
4492                    push @$active_formatting_elements, ['#marker', '']
4493                        if $token->{tag_name} eq 'caption';
4494                    
4495                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4496                    $self->{insertion_mode} = {
4497                                               caption => IN_CAPTION_IM,
4498                                               colgroup => IN_COLUMN_GROUP_IM,
4499                                               tbody => IN_TABLE_BODY_IM,
4500                                               tfoot => IN_TABLE_BODY_IM,
4501                                               thead => IN_TABLE_BODY_IM,
4502                                              }->{$token->{tag_name}};
4503                    !!!next-token;
4504                    redo B;
4505                  } else {
4506                    die "$0: in table: <>: $token->{tag_name}";
4507                  }
4508              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4509                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               !!!parse-error (type => 'not closed:table');  
4510    
4511                ## As if </table>                ## As if </table>
4512                ## have a table element in table scope                ## have a table element in table scope
# Line 3773  sub _tree_construction_main ($) { Line 4514  sub _tree_construction_main ($) {
4514                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4515                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4516                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4517                      !!!cp ('t221');
4518                    $i = $_;                    $i = $_;
4519                    last INSCOPE;                    last INSCOPE;
4520                  } elsif ({                  } elsif ({
4521                            table => 1, html => 1,                            #table => 1,
4522                              html => 1,
4523                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4524                      !!!cp ('t222');
4525                    last INSCOPE;                    last INSCOPE;
4526                  }                  }
4527                } # INSCOPE                } # INSCOPE
4528                unless (defined $i) {                unless (defined $i) {
4529                    !!!cp ('t223');
4530    ## TODO: The following is wrong, maybe.
4531                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table');
4532                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4533                  !!!next-token;                  !!!next-token;
4534                  redo B;                  redo B;
4535                }                }
4536                                
4537    ## TODO: Followings are removed from the latest spec.
4538                ## generate implied end tags                ## generate implied end tags
4539                if ({                while ({
4540                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4541                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4542                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t224');
4543                  !!!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;  
4544                }                }
4545    
4546                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4547                    !!!cp ('t225');
4548    ## ISSUE: Can this case be reached?
4549                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4550                  } else {
4551                    !!!cp ('t226');
4552                }                }
4553    
4554                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4555                  pop @{$open_tables};
4556    
4557                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4558    
4559                ## reprocess                ## reprocess
4560                redo B;                redo B;
4561              } else {          } elsif ($token->{tag_name} eq 'style') {
4562                #            if (not $open_tables->[-1]->[1]) { # tainted
4563              }              !!!cp ('t227.8');
4564            } elsif ($token->{type} eq 'end tag') {              ## NOTE: This is a "as if in head" code clone.
4565              if ({              $parse_rcdata->(CDATA_CONTENT_MODEL);
4566                   tbody => 1, tfoot => 1, thead => 1,              redo B;
4567                  }->{$token->{tag_name}}) {            } else {
4568                ## have an element in table scope              !!!cp ('t227.7');
4569                my $i;              #
4570                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
4571                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'script') {
4572                  if ($node->[1] eq $token->{tag_name}) {            if (not $open_tables->[-1]->[1]) { # tainted
4573                    $i = $_;              !!!cp ('t227.6');
4574                    last INSCOPE;              ## NOTE: This is a "as if in head" code clone.
4575                  } elsif ({              $script_start_tag->();
4576                            table => 1, html => 1,              redo B;
4577                           }->{$node->[1]}) {            } else {
4578                    last INSCOPE;              !!!cp ('t227.5');
4579                  }              #
4580                } # INSCOPE            }
4581                unless (defined $i) {          } elsif ($token->{tag_name} eq 'input') {
4582                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4583                  ## Ignore the token              if ($token->{attributes}->{type}) { ## TODO: case
4584                  !!!next-token;                my $type = lc $token->{attributes}->{type}->{value};
4585                  redo B;                if ($type eq 'hidden') {
4586                }                  !!!cp ('t227.3');
4587                    !!!parse-error (type => 'in table:'.$token->{tag_name});
4588    
4589                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4590    
4591                    ## TODO: form element pointer
4592    
               ## 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]);  
4593                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
4594    
               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  
4595                  !!!next-token;                  !!!next-token;
4596                  redo B;                  redo B;
4597                  } else {
4598                    !!!cp ('t227.2');
4599                    #
4600                }                }
   
               ## 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;  
4601              } else {              } else {
4602                  !!!cp ('t227.1');
4603                #                #
4604              }              }
4605            } else {            } else {
4606                !!!cp ('t227.4');
4607              #              #
4608            }            }
4609                      } else {
4610            ## As if in table            !!!cp ('t227');
4611            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4612            $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');  
4613    
4614              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table:'.$token->{tag_name});
             ## 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';  
4615    
4616                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
4617                          #
4618                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4619                redo B;              if ($token->{tag_name} eq 'tr' and
4620              } 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>  
4621                ## have an element in table scope                ## have an element in table scope
4622                my $i;                my $i;
4623                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4624                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4625                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4626                      !!!cp ('t228');
4627                    $i = $_;                    $i = $_;
4628                    last INSCOPE;                    last INSCOPE;
4629                  } elsif ({                  } elsif ({
4630                            table => 1, html => 1,                            table => 1, html => 1,
4631                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4632                      !!!cp ('t229');
4633                    last INSCOPE;                    last INSCOPE;
4634                  }                  }
4635                } # INSCOPE                } # INSCOPE
4636                unless (defined $i) {                unless (defined $i) {
4637                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
4638                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4639                  ## Ignore the token                  ## Ignore the token
4640                  !!!next-token;                  !!!next-token;
4641                  redo B;                  redo B;
4642                  } else {
4643                    !!!cp ('t232');
4644                }                }
4645    
4646                ## Clear back to table row context                ## Clear back to table row context
4647                while (not {                while (not {
4648                  tr => 1, html => 1,                  tr => 1, html => 1,
4649                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4650                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4651    ## ISSUE: Can this state be reached?
4652                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4653                }                }
4654    
4655                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
4656                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4657                ## reprocess                !!!next-token;
4658                redo B;                redo B;
4659              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4660                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
4661                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
4662                    ## have an element in table scope
4663                ## As if </table>                  my $i;
4664                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4665                my $i;                    my $node = $self->{open_elements}->[$_];
4666                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'tr') {
4667                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
4668                  if ($node->[1] eq 'table') {                      $i = $_;
4669                    $i = $_;                      last INSCOPE;
4670                    last INSCOPE;                    } elsif ({
4671                  } elsif ({                              table => 1, html => 1,
4672                            table => 1, html => 1,                             }->{$node->[1]}) {
4673                           }->{$node->[1]}) {                      !!!cp ('t234');
4674                    last INSCOPE;                      last INSCOPE;
4675                      }
4676                    } # INSCOPE
4677                    unless (defined $i) {
4678                      !!!cp ('t235');
4679    ## TODO: The following is wrong.
4680                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4681                      ## Ignore the token
4682                      !!!next-token;
4683                      redo B;
4684                  }                  }
4685                } # INSCOPE                  
4686                unless (defined $i) {                  ## Clear back to table row context
4687                  !!!parse-error (type => 'unmatched end tag:table');                  while (not {
4688                  ## Ignore tokens </table><table>                    tr => 1, html => 1,
4689                  !!!next-token;                  }->{$self->{open_elements}->[-1]->[1]}) {
4690                  redo B;                    !!!cp ('t236');
4691                }  ## ISSUE: Can this state be reached?
4692                                    pop @{$self->{open_elements}};
4693                ## generate implied end tags                  }
4694                if ({                  
4695                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}}; # tr
4696                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4697                    }->{$self->{open_elements}->[-1]->[1]}) {                  ## reprocess in the "in table body" insertion mode...
4698                  !!!back-token; # <table>                }
4699                  $token = {type => 'end tag', tag_name => 'table'};  
4700                  !!!back-token;                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4701                  $token = {type => 'end tag',                  ## have an element in table scope
4702                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  my $i;
4703                  redo B;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4704                }                    my $node = $self->{open_elements}->[$_];
4705                      if ({
4706                if ($self->{open_elements}->[-1]->[1] ne 'table') {                         tbody => 1, thead => 1, tfoot => 1,
4707                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        }->{$node->[1]}) {
4708                        !!!cp ('t237');
4709                        $i = $_;
4710                        last INSCOPE;
4711                      } elsif ({
4712                                table => 1, html => 1,
4713                               }->{$node->[1]}) {
4714                        !!!cp ('t238');
4715                        last INSCOPE;
4716                      }
4717                    } # INSCOPE
4718                    unless (defined $i) {
4719                      !!!cp ('t239');
4720                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4721                      ## Ignore the token
4722                      !!!next-token;
4723                      redo B;
4724                    }
4725                    
4726                    ## Clear back to table body context
4727                    while (not {
4728                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4729                    }->{$self->{open_elements}->[-1]->[1]}) {
4730                      !!!cp ('t240');
4731                      pop @{$self->{open_elements}};
4732                    }
4733                    
4734                    ## As if <{current node}>
4735                    ## have an element in table scope
4736                    ## true by definition
4737                    
4738                    ## Clear back to table body context
4739                    ## nop by definition
4740                    
4741                    pop @{$self->{open_elements}};
4742                    $self->{insertion_mode} = IN_TABLE_IM;
4743                    ## reprocess in the "in table" insertion mode...
4744                }                }
4745    
4746                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
4747                  ## When you edit the code fragment below, please ensure that
4748                  ## the code for <table> in the "in table" insertion mode
4749                  ## is synced with it.
4750    
4751                $self->_reset_insertion_mode;                ## have a table element in table scope
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
4752                my $i;                my $i;
4753                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4754                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4755                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4756                      !!!cp ('t241');
4757                    $i = $_;                    $i = $_;
4758                    last INSCOPE;                    last INSCOPE;
4759                  } elsif ({                  } elsif ({
4760                            table => 1, html => 1,                            table => 1, html => 1,
4761                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4762                      !!!cp ('t242');
4763                    last INSCOPE;                    last INSCOPE;
4764                  }                  }
4765                } # INSCOPE                } # INSCOPE
4766                unless (defined $i) {                unless (defined $i) {
4767                    !!!cp ('t243');
4768                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4769                  ## Ignore the token                  ## Ignore the token
4770                  !!!next-token;                  !!!next-token;
4771                  redo B;                  redo B;
4772                }                }
4773                    
4774                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
4775                while (not {                pop @{$open_tables};
4776                  tr => 1, html => 1,                
4777                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
4778                  !!!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';  
4779                !!!next-token;                !!!next-token;
4780                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;  
4781              } elsif ({              } elsif ({
4782                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4783                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4784                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
4785                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
4786                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4787                  my $node = $self->{open_elements}->[$_];                  my $i;
4788                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4789                    $i = $_;                    my $node = $self->{open_elements}->[$_];
4790                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
4791                  } elsif ({                      !!!cp ('t247');
4792                            table => 1, html => 1,                      $i = $_;
4793                           }->{$node->[1]}) {                      last INSCOPE;
4794                    last INSCOPE;                    } elsif ({
4795                                table => 1, html => 1,
4796                               }->{$node->[1]}) {
4797                        !!!cp ('t248');
4798                        last INSCOPE;
4799                      }
4800                    } # INSCOPE
4801                      unless (defined $i) {
4802                        !!!cp ('t249');
4803                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4804                        ## Ignore the token
4805                        !!!next-token;
4806                        redo B;
4807                      }
4808                    
4809                    ## As if </tr>
4810                    ## have an element in table scope
4811                    my $i;
4812                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4813                      my $node = $self->{open_elements}->[$_];
4814                      if ($node->[1] eq 'tr') {
4815                        !!!cp ('t250');
4816                        $i = $_;
4817                        last INSCOPE;
4818                      } elsif ({
4819                                table => 1, html => 1,
4820                               }->{$node->[1]}) {
4821                        !!!cp ('t251');
4822                        last INSCOPE;
4823                      }
4824                    } # INSCOPE
4825                      unless (defined $i) {
4826                        !!!cp ('t252');
4827                        !!!parse-error (type => 'unmatched end tag:tr');
4828                        ## Ignore the token
4829                        !!!next-token;
4830                        redo B;
4831                      }
4832                    
4833                    ## Clear back to table row context
4834                    while (not {
4835                      tr => 1, html => 1,
4836                    }->{$self->{open_elements}->[-1]->[1]}) {
4837                      !!!cp ('t253');
4838    ## ISSUE: Can this case be reached?
4839                      pop @{$self->{open_elements}};
4840                  }                  }
4841                } # INSCOPE                  
4842                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4843                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4844                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4845                }                }
4846    
               ## As if </tr>  
4847                ## have an element in table scope                ## 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 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4852                      !!!cp ('t254');
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 ('t255');
4859                    last INSCOPE;                    last INSCOPE;
4860                  }                  }
4861                } # INSCOPE                } # INSCOPE
4862                unless (defined $i) {                unless (defined $i) {
4863                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
4864                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
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                ## Clear back to table body context
4871                while (not {                while (not {
4872                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4873                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4874                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4875    ## ISSUE: Can this case be reached?
4876                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4877                }                }
4878    
4879                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4880                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4881                ## reprocess                !!!next-token;
4882                redo B;                redo B;
4883              } elsif ({              } elsif ({
4884                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4885                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4886                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4887                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4888                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4889                  !!!cp ('t258');
4890                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4891                ## Ignore the token                ## Ignore the token
4892                !!!next-token;                !!!next-token;
4893                redo B;                redo B;
4894              } else {          } else {
4895                #            !!!cp ('t259');
4896              }            !!!parse-error (type => 'in table:/'.$token->{tag_name});
           } else {  
             #  
           }  
4897    
4898            ## As if in table            $insert = $insert_to_foster;
4899            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4900            $in_body->($insert_to_foster);          }
4901            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4902          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4903            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
4904              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof');
4905              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
4906                          #
4907              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4908              !!!cp ('t259.2');
4909              #
4910            }
4911    
4912              !!!next-token;          ## Stop parsing
4913              redo B;          last B;
4914            } elsif ($token->{type} eq 'comment') {        } else {
4915              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
4916              my $comment = $self->{document}->create_comment ($token->{data});        }
4917              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4918              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
4919              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4920            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4921              if ({                unless (length $token->{data}) {
4922                   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  
4923                  !!!next-token;                  !!!next-token;
4924                  redo B;                  redo B;
4925                }                }
4926                }
4927                ## Close the cell              
4928                !!!back-token; # <?>              !!!cp ('t261');
4929                $token = {type => 'end tag', tag_name => $tn};              #
4930              } elsif ($token->{type} == START_TAG_TOKEN) {
4931                if ($token->{tag_name} eq 'col') {
4932                  !!!cp ('t262');
4933                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4934                  pop @{$self->{open_elements}};
4935                  !!!next-token;
4936                redo B;                redo B;
4937              } else {              } else {
4938                  !!!cp ('t263');
4939                #                #
4940              }              }
4941            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4942              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4943                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4944                my $i;                  !!!cp ('t264');
4945                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:colgroup');
                 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});  
4946                  ## Ignore the token                  ## Ignore the token
4947                  !!!next-token;                  !!!next-token;
4948                  redo B;                  redo B;
4949                  } else {
4950                    !!!cp ('t265');
4951                    pop @{$self->{open_elements}}; # colgroup
4952                    $self->{insertion_mode} = IN_TABLE_IM;
4953                    !!!next-token;
4954                    redo B;            
4955                }                }
4956                              } elsif ($token->{tag_name} eq 'col') {
4957                ## generate implied end tags                !!!cp ('t266');
4958                if ({                !!!parse-error (type => 'unmatched end tag:col');
                    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});  
4959                ## Ignore the token                ## Ignore the token
4960                !!!next-token;                !!!next-token;
4961                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;  
4962              } else {              } else {
4963                #                !!!cp ('t267');
4964                  #
4965              }              }
4966            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4967              #          if ($self->{open_elements}->[-1]->[1] eq 'html' or
4968            }              @{$self->{open_elements}} == 1) { # redundant, maybe
4969                        !!!cp ('t270.2');
4970            $in_body->($insert_to_current);            ## Stop parsing.
4971              last B;
4972            } else {
4973              ## NOTE: As if </colgroup>.
4974              !!!cp ('t270.1');
4975              pop @{$self->{open_elements}}; # colgroup
4976              $self->{insertion_mode} = IN_TABLE_IM;
4977              ## Reprocess.
4978            redo B;            redo B;
4979          } elsif ($self->{insertion_mode} eq 'in select') {          }
4980            if ($token->{type} eq 'character') {        } else {
4981              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          die "$0: $token->{type}: Unknown token type";
4982          }
4983    
4984              ## As if </colgroup>
4985              if ($self->{open_elements}->[-1]->[1] eq 'html') {
4986                !!!cp ('t269');
4987    ## TODO: Wrong error type?
4988                !!!parse-error (type => 'unmatched end tag:colgroup');
4989                ## Ignore the token
4990              !!!next-token;              !!!next-token;
4991              redo B;              redo B;
4992            } elsif ($token->{type} eq 'comment') {            } else {
4993              my $comment = $self->{document}->create_comment ($token->{data});              !!!cp ('t270');
4994              $self->{open_elements}->[-1]->[0]->append_child ($comment);              pop @{$self->{open_elements}}; # colgroup
4995              !!!next-token;              $self->{insertion_mode} = IN_TABLE_IM;
4996                ## reprocess
4997              redo B;              redo B;
4998            } elsif ($token->{type} eq 'start tag') {            }
4999        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5000          if ($token->{type} == CHARACTER_TOKEN) {
5001            !!!cp ('t271');
5002            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5003            !!!next-token;
5004            redo B;
5005          } elsif ($token->{type} == START_TAG_TOKEN) {
5006              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
5007                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5008                    !!!cp ('t272');
5009                  ## As if </option>                  ## As if </option>
5010                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5011                  } else {
5012                    !!!cp ('t273');
5013                }                }
5014    
5015                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 4392  sub _tree_construction_main ($) { Line 5017  sub _tree_construction_main ($) {
5017                redo B;                redo B;
5018              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
5019                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5020                    !!!cp ('t274');
5021                  ## As if </option>                  ## As if </option>
5022                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5023                  } else {
5024                    !!!cp ('t275');
5025                }                }
5026    
5027                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5028                    !!!cp ('t276');
5029                  ## As if </optgroup>                  ## As if </optgroup>
5030                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5031                  } else {
5032                    !!!cp ('t277');
5033                }                }
5034    
5035                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
5036                !!!next-token;                !!!next-token;
5037                redo B;                redo B;
5038              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5039                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5040                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5041                      {
5042                       caption => 1, table => 1,
5043                       tbody => 1, tfoot => 1, thead => 1,
5044                       tr => 1, td => 1, th => 1,
5045                      }->{$token->{tag_name}})) {
5046              ## TODO: The type below is not good - <select> is replaced by </select>
5047              !!!parse-error (type => 'not closed:select');
5048              ## NOTE: As if the token were </select> (<select> case) or
5049              ## as if there were </select> (otherwise).
5050                ## have an element in table scope                ## have an element in table scope
5051                my $i;                my $i;
5052                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5053                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5054                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5055                      !!!cp ('t278');
5056                    $i = $_;                    $i = $_;
5057                    last INSCOPE;                    last INSCOPE;
5058                  } elsif ({                  } elsif ({
5059                            table => 1, html => 1,                            table => 1, html => 1,
5060                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5061                      !!!cp ('t279');
5062                    last INSCOPE;                    last INSCOPE;
5063                  }                  }
5064                } # INSCOPE                } # INSCOPE
5065                unless (defined $i) {                unless (defined $i) {
5066                    !!!cp ('t280');
5067                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5068                  ## Ignore the token                  ## Ignore the token
5069                  !!!next-token;                  !!!next-token;
5070                  redo B;                  redo B;
5071                }                }
5072                                
5073                  !!!cp ('t281');
5074                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5075    
5076                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5077    
5078                !!!next-token;            if ($token->{tag_name} eq 'select') {
5079                redo B;              !!!cp ('t281.2');
5080              } else {              !!!next-token;
5081                #              redo B;
5082              }            } else {
5083            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t281.1');
5084                ## Reprocess the token.
5085                redo B;
5086              }
5087            } else {
5088              !!!cp ('t282');
5089              !!!parse-error (type => 'in select:'.$token->{tag_name});
5090              ## Ignore the token
5091              !!!next-token;
5092              redo B;
5093            }
5094          } elsif ($token->{type} == END_TAG_TOKEN) {
5095              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
5096                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
5097                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
5098                    !!!cp ('t283');
5099                  ## As if </option>                  ## As if </option>
5100                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
5101                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5102                    !!!cp ('t284');
5103                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5104                } else {                } else {
5105                    !!!cp ('t285');
5106                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5107                  ## Ignore the token                  ## Ignore the token
5108                }                }
# Line 4452  sub _tree_construction_main ($) { Line 5110  sub _tree_construction_main ($) {
5110                redo B;                redo B;
5111              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
5112                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5113                    !!!cp ('t286');
5114                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5115                } else {                } else {
5116                    !!!cp ('t287');
5117                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5118                  ## Ignore the token                  ## Ignore the token
5119                }                }
# Line 4465  sub _tree_construction_main ($) { Line 5125  sub _tree_construction_main ($) {
5125                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5126                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5127                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5128                      !!!cp ('t288');
5129                    $i = $_;                    $i = $_;
5130                    last INSCOPE;                    last INSCOPE;
5131                  } elsif ({                  } elsif ({
5132                            table => 1, html => 1,                            table => 1, html => 1,
5133                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5134                      !!!cp ('t289');
5135                    last INSCOPE;                    last INSCOPE;
5136                  }                  }
5137                } # INSCOPE                } # INSCOPE
5138                unless (defined $i) {                unless (defined $i) {
5139                    !!!cp ('t290');
5140                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5141                  ## Ignore the token                  ## Ignore the token
5142                  !!!next-token;                  !!!next-token;
5143                  redo B;                  redo B;
5144                }                }
5145                                
5146                  !!!cp ('t291');
5147                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5148    
5149                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5150    
5151                !!!next-token;                !!!next-token;
5152                redo B;                redo B;
5153              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5154                        caption => 1, table => 1, tbody => 1,                   {
5155                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5156                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5157                     }->{$token->{tag_name}}) {
5158    ## TODO: The following is wrong?
5159                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5160                                
5161                ## have an element in table scope                ## have an element in table scope
# Line 4497  sub _tree_construction_main ($) { Line 5163  sub _tree_construction_main ($) {
5163                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5164                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5165                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5166                      !!!cp ('t292');
5167                    $i = $_;                    $i = $_;
5168                    last INSCOPE;                    last INSCOPE;
5169                  } elsif ({                  } elsif ({
5170                            table => 1, html => 1,                            table => 1, html => 1,
5171                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5172                      !!!cp ('t293');
5173                    last INSCOPE;                    last INSCOPE;
5174                  }                  }
5175                } # INSCOPE                } # INSCOPE
5176                unless (defined $i) {                unless (defined $i) {
5177                    !!!cp ('t294');
5178                  ## Ignore the token                  ## Ignore the token
5179                  !!!next-token;                  !!!next-token;
5180                  redo B;                  redo B;
# Line 4517  sub _tree_construction_main ($) { Line 5186  sub _tree_construction_main ($) {
5186                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5187                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5188                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5189                      !!!cp ('t295');
5190                    $i = $_;                    $i = $_;
5191                    last INSCOPE;                    last INSCOPE;
5192                  } elsif ({                  } elsif ({
5193                            table => 1, html => 1,                            table => 1, html => 1,
5194                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5195    ## ISSUE: Can this state be reached?
5196                      !!!cp ('t296');
5197                    last INSCOPE;                    last INSCOPE;
5198                  }                  }
5199                } # INSCOPE                } # INSCOPE
5200                unless (defined $i) {                unless (defined $i) {
5201                    !!!cp ('t297');
5202    ## TODO: The following error type is correct?
5203                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5204                  ## Ignore the </select> token                  ## Ignore the </select> token
5205                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5206                  redo B;                  redo B;
5207                }                }
5208                                
5209                  !!!cp ('t298');
5210                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5211    
5212                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5213    
5214                ## reprocess                ## reprocess
5215                redo B;                redo B;
5216              } else {          } else {
5217                #            !!!cp ('t299');
5218              }            !!!parse-error (type => 'in select:/'.$token->{tag_name});
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
5219            ## Ignore the token            ## Ignore the token
5220            !!!next-token;            !!!next-token;
5221            redo B;            redo B;
5222          } elsif ($self->{insertion_mode} eq 'after body') {          }
5223            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5224              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5225                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
5226                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
5227              !!!parse-error (type => 'in body:#eof');
5228            } else {
5229              !!!cp ('t299.2');
5230            }
5231    
5232            ## Stop parsing.
5233            last B;
5234          } else {
5235            die "$0: $token->{type}: Unknown token type";
5236          }
5237        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5238          if ($token->{type} == CHARACTER_TOKEN) {
5239            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5240              my $data = $1;
5241              ## As if in body
5242              $reconstruct_active_formatting_elements->($insert_to_current);
5243                                
5244                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5245              
5246              unless (length $token->{data}) {
5247                !!!cp ('t300');
5248                !!!next-token;
5249                redo B;
5250              }
5251            }
5252            
5253            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5254              !!!cp ('t301');
5255              !!!parse-error (type => 'after html:#character');
5256    
5257                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
5258                  !!!next-token;          } else {
5259                  redo B;            !!!cp ('t302');
5260                }          }
5261              }          
5262                        ## "after body" insertion mode
5263              #          !!!parse-error (type => 'after body:#character');
5264              !!!parse-error (type => 'after body:#'.$token->{type});  
5265            } elsif ($token->{type} eq 'comment') {          $self->{insertion_mode} = IN_BODY_IM;
5266              my $comment = $self->{document}->create_comment ($token->{data});          ## reprocess
5267              $self->{open_elements}->[0]->[0]->append_child ($comment);          redo B;
5268          } elsif ($token->{type} == START_TAG_TOKEN) {
5269            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5270              !!!cp ('t303');
5271              !!!parse-error (type => 'after html:'.$token->{tag_name});
5272              
5273              ## Reprocess in the "after body" insertion mode.
5274            } else {
5275              !!!cp ('t304');
5276            }
5277    
5278            ## "after body" insertion mode
5279            !!!parse-error (type => 'after body:'.$token->{tag_name});
5280    
5281            $self->{insertion_mode} = IN_BODY_IM;
5282            ## reprocess
5283            redo B;
5284          } elsif ($token->{type} == END_TAG_TOKEN) {
5285            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5286              !!!cp ('t305');
5287              !!!parse-error (type => 'after html:/'.$token->{tag_name});
5288              
5289              $self->{insertion_mode} = AFTER_BODY_IM;
5290              ## Reprocess in the "after body" insertion mode.
5291            } else {
5292              !!!cp ('t306');
5293            }
5294    
5295            ## "after body" insertion mode
5296            if ($token->{tag_name} eq 'html') {
5297              if (defined $self->{inner_html_node}) {
5298                !!!cp ('t307');
5299                !!!parse-error (type => 'unmatched end tag:html');
5300                ## Ignore the token
5301              !!!next-token;              !!!next-token;
5302              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});  
             }  
5303            } else {            } else {
5304              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t308');
5305                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5306                !!!next-token;
5307                redo B;
5308            }            }
5309            } else {
5310              !!!cp ('t309');
5311              !!!parse-error (type => 'after body:/'.$token->{tag_name});
5312    
5313            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
5314            ## reprocess            ## reprocess
5315            redo B;            redo B;
5316          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
5317            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5318              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t309.2');
5319                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          ## Stop parsing
5320            last B;
5321                unless (length $token->{data}) {        } else {
5322                  !!!next-token;          die "$0: $token->{type}: Unknown token type";
5323                  redo B;        }
5324                }      } elsif ($self->{insertion_mode} & FRAME_IMS) {
5325              }        if ($token->{type} == CHARACTER_TOKEN) {
5326            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5327              #            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5328            } elsif ($token->{type} eq 'comment') {            
5329              my $comment = $self->{document}->create_comment ($token->{data});            unless (length $token->{data}) {
5330              $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!cp ('t310');
5331              !!!next-token;              !!!next-token;
5332              redo B;              redo B;
5333            } elsif ($token->{type} eq 'start tag') {            }
5334              if ($token->{tag_name} eq 'frameset') {          }
5335                !!!insert-element ($token->{tag_name}, $token->{attributes});          
5336                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5337                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5338              } elsif ($token->{tag_name} eq 'frame') {              !!!cp ('t311');
5339                !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!parse-error (type => 'in frameset:#character');
5340                pop @{$self->{open_elements}};            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5341                !!!next-token;              !!!cp ('t312');
5342                redo B;              !!!parse-error (type => 'after frameset:#character');
5343              } elsif ($token->{tag_name} eq 'noframes') {            } else { # "after html frameset"
5344                $in_body->($insert_to_current);              !!!cp ('t313');
5345                redo B;              !!!parse-error (type => 'after html:#character');
5346              } else {  
5347                #              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5348              }              ## Reprocess in the "after frameset" insertion mode.
5349            } elsif ($token->{type} eq 'end tag') {              !!!parse-error (type => 'after frameset:#character');
             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 {  
             #  
5350            }            }
5351                        
5352            if (defined $token->{tag_name}) {            ## Ignore the token.
5353              if (length $token->{data}) {
5354                !!!cp ('t314');
5355                ## reprocess the rest of characters
5356              } else {
5357                !!!cp ('t315');
5358                !!!next-token;
5359              }
5360              redo B;
5361            }
5362            
5363            die qq[$0: Character "$token->{data}"];
5364          } elsif ($token->{type} == START_TAG_TOKEN) {
5365            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5366              !!!cp ('t316');
5367              !!!parse-error (type => 'after html:'.$token->{tag_name});
5368    
5369              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5370              ## Process in the "after frameset" insertion mode.
5371            } else {
5372              !!!cp ('t317');
5373            }
5374    
5375            if ($token->{tag_name} eq 'frameset' and
5376                $self->{insertion_mode} == IN_FRAMESET_IM) {
5377              !!!cp ('t318');
5378              !!!insert-element ($token->{tag_name}, $token->{attributes});
5379              !!!next-token;
5380              redo B;
5381            } elsif ($token->{tag_name} eq 'frame' and
5382                     $self->{insertion_mode} == IN_FRAMESET_IM) {
5383              !!!cp ('t319');
5384              !!!insert-element ($token->{tag_name}, $token->{attributes});
5385              pop @{$self->{open_elements}};
5386              !!!next-token;
5387              redo B;
5388            } elsif ($token->{tag_name} eq 'noframes') {
5389              !!!cp ('t320');
5390              ## NOTE: As if in body.
5391              $parse_rcdata->(CDATA_CONTENT_MODEL);
5392              redo B;
5393            } else {
5394              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5395                !!!cp ('t321');
5396              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5397            } else {            } else {
5398              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t322');
5399                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5400            }            }
5401            ## Ignore the token            ## Ignore the token
5402            !!!next-token;            !!!next-token;
5403            redo B;            redo B;
5404          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
5405            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5406              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5407                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t323');
5408              !!!parse-error (type => 'after html:/'.$token->{tag_name});
5409    
5410              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5411              ## Process in the "after frameset" insertion mode.
5412            } else {
5413              !!!cp ('t324');
5414            }
5415    
5416            if ($token->{tag_name} eq 'frameset' and
5417                $self->{insertion_mode} == IN_FRAMESET_IM) {
5418              if ($self->{open_elements}->[-1]->[1] eq 'html' and
5419                  @{$self->{open_elements}} == 1) {
5420                !!!cp ('t325');
5421                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5422                ## Ignore the token
5423                !!!next-token;
5424              } else {
5425                !!!cp ('t326');
5426                pop @{$self->{open_elements}};
5427                !!!next-token;
5428              }
5429    
5430                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
5431                  !!!next-token;                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5432                  redo B;              !!!cp ('t327');
5433                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5434              } else {
5435                !!!cp ('t328');
5436              }
5437              redo B;
5438            } elsif ($token->{tag_name} eq 'html' and
5439                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5440              !!!cp ('t329');
5441              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5442              !!!next-token;
5443              redo B;
5444            } else {
5445              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5446                !!!cp ('t330');
5447                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5448              } else {
5449                !!!cp ('t331');
5450                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5451              }
5452              ## Ignore the token
5453              !!!next-token;
5454              redo B;
5455            }
5456          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5457            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5458                    @{$self->{open_elements}} == 1) { # redundant, maybe
5459              !!!cp ('t331.1');
5460              !!!parse-error (type => 'in body:#eof');
5461            } else {
5462              !!!cp ('t331.2');
5463            }
5464            
5465            ## Stop parsing
5466            last B;
5467          } else {
5468            die "$0: $token->{type}: Unknown token type";
5469          }
5470    
5471          ## ISSUE: An issue in spec here
5472        } else {
5473          die "$0: $self->{insertion_mode}: Unknown insertion mode";
5474        }
5475    
5476        ## "in body" insertion mode
5477        if ($token->{type} == START_TAG_TOKEN) {
5478          if ($token->{tag_name} eq 'script') {
5479            !!!cp ('t332');
5480            ## NOTE: This is an "as if in head" code clone
5481            $script_start_tag->();
5482            redo B;
5483          } elsif ($token->{tag_name} eq 'style') {
5484            !!!cp ('t333');
5485            ## NOTE: This is an "as if in head" code clone
5486            $parse_rcdata->(CDATA_CONTENT_MODEL);
5487            redo B;
5488          } elsif ({
5489                    base => 1, link => 1,
5490                   }->{$token->{tag_name}}) {
5491            !!!cp ('t334');
5492            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5493            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5494            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5495            !!!next-token;
5496            redo B;
5497          } elsif ($token->{tag_name} eq 'meta') {
5498            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5499            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5500            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5501    
5502            unless ($self->{confident}) {
5503              if ($token->{attributes}->{charset}) { ## TODO: And if supported
5504                !!!cp ('t335');
5505                $self->{change_encoding}
5506                    ->($self, $token->{attributes}->{charset}->{value});
5507                
5508                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5509                    ->set_user_data (manakai_has_reference =>
5510                                         $token->{attributes}->{charset}
5511                                             ->{has_reference});
5512              } elsif ($token->{attributes}->{content}) {
5513                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5514                if ($token->{attributes}->{content}->{value}
5515                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5516                        [\x09-\x0D\x20]*=
5517                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5518                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5519                  !!!cp ('t336');
5520                  $self->{change_encoding}
5521                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
5522                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5523                      ->set_user_data (manakai_has_reference =>
5524                                           $token->{attributes}->{content}
5525                                                 ->{has_reference});
5526              }              }
5527              }
5528            } else {
5529              if ($token->{attributes}->{charset}) {
5530                !!!cp ('t337');
5531                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5532                    ->set_user_data (manakai_has_reference =>
5533                                         $token->{attributes}->{charset}
5534                                             ->{has_reference});
5535              }
5536              if ($token->{attributes}->{content}) {
5537                !!!cp ('t338');
5538                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5539                    ->set_user_data (manakai_has_reference =>
5540                                         $token->{attributes}->{content}
5541                                             ->{has_reference});
5542              }
5543            }
5544    
5545              #          !!!next-token;
5546            } elsif ($token->{type} eq 'comment') {          redo B;
5547              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{tag_name} eq 'title') {
5548              $self->{open_elements}->[-1]->[0]->append_child ($comment);          !!!cp ('t341');
5549              !!!next-token;          ## NOTE: This is an "as if in head" code clone
5550            $parse_rcdata->(RCDATA_CONTENT_MODEL);
5551            redo B;
5552          } elsif ($token->{tag_name} eq 'body') {
5553            !!!parse-error (type => 'in body:body');
5554                  
5555            if (@{$self->{open_elements}} == 1 or
5556                $self->{open_elements}->[1]->[1] ne 'body') {
5557              !!!cp ('t342');
5558              ## Ignore the token
5559            } else {
5560              my $body_el = $self->{open_elements}->[1]->[0];
5561              for my $attr_name (keys %{$token->{attributes}}) {
5562                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5563                  !!!cp ('t343');
5564                  $body_el->set_attribute_ns
5565                    (undef, [undef, $attr_name],
5566                     $token->{attributes}->{$attr_name}->{value});
5567                }
5568              }
5569            }
5570            !!!next-token;
5571            redo B;
5572          } elsif ({
5573                    address => 1, blockquote => 1, center => 1, dir => 1,
5574                    div => 1, dl => 1, fieldset => 1,
5575                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5576                    menu => 1, ol => 1, p => 1, ul => 1,
5577                    pre => 1, listing => 1,
5578                   }->{$token->{tag_name}}) {
5579            ## has a p element in scope
5580            INSCOPE: for (reverse @{$self->{open_elements}}) {
5581              if ($_->[1] eq 'p') {
5582                !!!cp ('t344');
5583                !!!back-token;
5584                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5585              redo B;              redo B;
5586            } elsif ($token->{type} eq 'start tag') {            } elsif ({
5587              if ($token->{tag_name} eq 'noframes') {                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5588                $in_body->($insert_to_current);                      button => 1, marquee => 1, object => 1, html => 1,
5589                redo B;                     }->{$_->[1]}) {
5590                !!!cp ('t345');
5591                last INSCOPE;
5592              }
5593            } # INSCOPE
5594              
5595            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5596            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5597              !!!next-token;
5598              if ($token->{type} == CHARACTER_TOKEN) {
5599                $token->{data} =~ s/^\x0A//;
5600                unless (length $token->{data}) {
5601                  !!!cp ('t346');
5602                  !!!next-token;
5603              } else {              } else {
5604                #                !!!cp ('t349');
5605              }              }
5606            } elsif ($token->{type} eq 'end tag') {            } else {
5607              if ($token->{tag_name} eq 'html') {              !!!cp ('t348');
5608                $phase = 'trailing end';            }
5609                !!!next-token;          } else {
5610              !!!cp ('t347');
5611              !!!next-token;
5612            }
5613            redo B;
5614          } elsif ($token->{tag_name} eq 'form') {
5615            if (defined $self->{form_element}) {
5616              !!!cp ('t350');
5617              !!!parse-error (type => 'in form:form');
5618              ## Ignore the token
5619              !!!next-token;
5620              redo B;
5621            } else {
5622              ## has a p element in scope
5623              INSCOPE: for (reverse @{$self->{open_elements}}) {
5624                if ($_->[1] eq 'p') {
5625                  !!!cp ('t351');
5626                  !!!back-token;
5627                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5628                redo B;                redo B;
5629                } elsif ({
5630                          applet => 1, table => 1, caption => 1, td => 1, th => 1,
5631                          button => 1, marquee => 1, object => 1, html => 1,
5632                         }->{$_->[1]}) {
5633                  !!!cp ('t352');
5634                  last INSCOPE;
5635                }
5636              } # INSCOPE
5637                
5638              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5639              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5640              !!!next-token;
5641              redo B;
5642            }
5643          } elsif ($token->{tag_name} eq 'li') {
5644            ## has a p element in scope
5645            INSCOPE: for (reverse @{$self->{open_elements}}) {
5646              if ($_->[1] eq 'p') {
5647                !!!cp ('t353');
5648                !!!back-token;
5649                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5650                redo B;
5651              } elsif ({
5652                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5653                        button => 1, marquee => 1, object => 1, html => 1,
5654                       }->{$_->[1]}) {
5655                !!!cp ('t354');
5656                last INSCOPE;
5657              }
5658            } # INSCOPE
5659              
5660            ## Step 1
5661            my $i = -1;
5662            my $node = $self->{open_elements}->[$i];
5663            LI: {
5664              ## Step 2
5665              if ($node->[1] eq 'li') {
5666                if ($i != -1) {
5667                  !!!cp ('t355');
5668                  !!!parse-error (type => 'end tag missing:'.
5669                                  $self->{open_elements}->[-1]->[1]);
5670              } else {              } else {
5671                #                !!!cp ('t356');
5672              }              }
5673                splice @{$self->{open_elements}}, $i;
5674                last LI;
5675            } else {            } else {
5676              #              !!!cp ('t357');
5677            }            }
5678                        
5679            if (defined $token->{tag_name}) {            ## Step 3
5680              !!!parse-error (type => 'after frameset:'.$token->{tag_name});            if (not $formatting_category->{$node->[1]} and
5681                  #not $phrasing_category->{$node->[1]} and
5682                  ($special_category->{$node->[1]} or
5683                   $scoping_category->{$node->[1]}) and
5684                  $node->[1] ne 'address' and $node->[1] ne 'div') {
5685                !!!cp ('t358');
5686                last LI;
5687              }
5688              
5689              !!!cp ('t359');
5690              ## Step 4
5691              $i--;
5692              $node = $self->{open_elements}->[$i];
5693              redo LI;
5694            } # LI
5695              
5696            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5697            !!!next-token;
5698            redo B;
5699          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
5700            ## has a p element in scope
5701            INSCOPE: for (reverse @{$self->{open_elements}}) {
5702              if ($_->[1] eq 'p') {
5703                !!!cp ('t360');
5704                !!!back-token;
5705                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5706                redo B;
5707              } elsif ({
5708                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5709                        button => 1, marquee => 1, object => 1, html => 1,
5710                       }->{$_->[1]}) {
5711                !!!cp ('t361');
5712                last INSCOPE;
5713              }
5714            } # INSCOPE
5715              
5716            ## Step 1
5717            my $i = -1;
5718            my $node = $self->{open_elements}->[$i];
5719            LI: {
5720              ## Step 2
5721              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
5722                if ($i != -1) {
5723                  !!!cp ('t362');
5724                  !!!parse-error (type => 'end tag missing:'.
5725                                  $self->{open_elements}->[-1]->[1]);
5726                } else {
5727                  !!!cp ('t363');
5728                }
5729                splice @{$self->{open_elements}}, $i;
5730                last LI;
5731            } else {            } else {
5732              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t364');
5733            }            }
5734              
5735              ## Step 3
5736              if (not $formatting_category->{$node->[1]} and
5737                  #not $phrasing_category->{$node->[1]} and
5738                  ($special_category->{$node->[1]} or
5739                   $scoping_category->{$node->[1]}) and
5740                  $node->[1] ne 'address' and $node->[1] ne 'div') {
5741                !!!cp ('t365');
5742                last LI;
5743              }
5744              
5745              !!!cp ('t366');
5746              ## Step 4
5747              $i--;
5748              $node = $self->{open_elements}->[$i];
5749              redo LI;
5750            } # LI
5751              
5752            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5753            !!!next-token;
5754            redo B;
5755          } elsif ($token->{tag_name} eq 'plaintext') {
5756            ## has a p element in scope
5757            INSCOPE: for (reverse @{$self->{open_elements}}) {
5758              if ($_->[1] eq 'p') {
5759                !!!cp ('t367');
5760                !!!back-token;
5761                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5762                redo B;
5763              } elsif ({
5764                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5765                        button => 1, marquee => 1, object => 1, html => 1,
5766                       }->{$_->[1]}) {
5767                !!!cp ('t368');
5768                last INSCOPE;
5769              }
5770            } # INSCOPE
5771              
5772            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5773              
5774            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5775              
5776            !!!next-token;
5777            redo B;
5778          } elsif ($token->{tag_name} eq 'a') {
5779            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5780              my $node = $active_formatting_elements->[$i];
5781              if ($node->[1] eq 'a') {
5782                !!!cp ('t371');
5783                !!!parse-error (type => 'in a:a');
5784                
5785                !!!back-token;
5786                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
5787                $formatting_end_tag->($token->{tag_name});
5788                
5789                AFE2: for (reverse 0..$#$active_formatting_elements) {
5790                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5791                    !!!cp ('t372');
5792                    splice @$active_formatting_elements, $_, 1;
5793                    last AFE2;
5794                  }
5795                } # AFE2
5796                OE: for (reverse 0..$#{$self->{open_elements}}) {
5797                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5798                    !!!cp ('t373');
5799                    splice @{$self->{open_elements}}, $_, 1;
5800                    last OE;
5801                  }
5802                } # OE
5803                last AFE;
5804              } elsif ($node->[0] eq '#marker') {
5805                !!!cp ('t374');
5806                last AFE;
5807              }
5808            } # AFE
5809              
5810            $reconstruct_active_formatting_elements->($insert_to_current);
5811    
5812            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5813            push @$active_formatting_elements, $self->{open_elements}->[-1];
5814    
5815            !!!next-token;
5816            redo B;
5817          } elsif ({
5818                    b => 1, big => 1, em => 1, font => 1, i => 1,
5819                    s => 1, small => 1, strile => 1,
5820                    strong => 1, tt => 1, u => 1,
5821                   }->{$token->{tag_name}}) {
5822            !!!cp ('t375');
5823            $reconstruct_active_formatting_elements->($insert_to_current);
5824            
5825            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5826            push @$active_formatting_elements, $self->{open_elements}->[-1];
5827            
5828            !!!next-token;
5829            redo B;
5830          } elsif ($token->{tag_name} eq 'nobr') {
5831            $reconstruct_active_formatting_elements->($insert_to_current);
5832    
5833            ## has a |nobr| element in scope
5834            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5835              my $node = $self->{open_elements}->[$_];
5836              if ($node->[1] eq 'nobr') {
5837                !!!cp ('t376');
5838                !!!parse-error (type => 'in nobr:nobr');
5839                !!!back-token;
5840                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
5841                redo B;
5842              } elsif ({
5843                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5844                        button => 1, marquee => 1, object => 1, html => 1,
5845                       }->{$node->[1]}) {
5846                !!!cp ('t377');
5847                last INSCOPE;
5848              }
5849            } # INSCOPE
5850            
5851            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5852            push @$active_formatting_elements, $self->{open_elements}->[-1];
5853            
5854            !!!next-token;
5855            redo B;
5856          } elsif ($token->{tag_name} eq 'button') {
5857            ## has a button element in scope
5858            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5859              my $node = $self->{open_elements}->[$_];
5860              if ($node->[1] eq 'button') {
5861                !!!cp ('t378');
5862                !!!parse-error (type => 'in button:button');
5863                !!!back-token;
5864                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
5865                redo B;
5866              } elsif ({
5867                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5868                        button => 1, marquee => 1, object => 1, html => 1,
5869                       }->{$node->[1]}) {
5870                !!!cp ('t379');
5871                last INSCOPE;
5872              }
5873            } # INSCOPE
5874              
5875            $reconstruct_active_formatting_elements->($insert_to_current);
5876              
5877            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5878    
5879            ## TODO: associate with $self->{form_element} if defined
5880    
5881            push @$active_formatting_elements, ['#marker', ''];
5882    
5883            !!!next-token;
5884            redo B;
5885          } elsif ({
5886                    applet => 1, marquee => 1, object => 1,
5887                   }->{$token->{tag_name}}) {
5888            !!!cp ('t380');
5889            $reconstruct_active_formatting_elements->($insert_to_current);
5890            
5891            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5892            push @$active_formatting_elements, ['#marker', ''];
5893            
5894            !!!next-token;
5895            redo B;
5896          } elsif ($token->{tag_name} eq 'xmp') {
5897            !!!cp ('t381');
5898            $reconstruct_active_formatting_elements->($insert_to_current);
5899            $parse_rcdata->(CDATA_CONTENT_MODEL);
5900            redo B;
5901          } elsif ($token->{tag_name} eq 'table') {
5902            ## has a p element in scope
5903            INSCOPE: for (reverse @{$self->{open_elements}}) {
5904              if ($_->[1] eq 'p') {
5905                !!!cp ('t382');
5906                !!!back-token;
5907                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5908                redo B;
5909              } elsif ({
5910                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5911                        button => 1, marquee => 1, object => 1, html => 1,
5912                       }->{$_->[1]}) {
5913                !!!cp ('t383');
5914                last INSCOPE;
5915              }
5916            } # INSCOPE
5917              
5918            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5919            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5920    
5921            $self->{insertion_mode} = IN_TABLE_IM;
5922              
5923            !!!next-token;
5924            redo B;
5925          } elsif ({
5926                    area => 1, basefont => 1, bgsound => 1, br => 1,
5927                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
5928                    image => 1,
5929                   }->{$token->{tag_name}}) {
5930            if ($token->{tag_name} eq 'image') {
5931              !!!cp ('t384');
5932              !!!parse-error (type => 'image');
5933              $token->{tag_name} = 'img';
5934            } else {
5935              !!!cp ('t385');
5936            }
5937    
5938            ## NOTE: There is an "as if <br>" code clone.
5939            $reconstruct_active_formatting_elements->($insert_to_current);
5940            
5941            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5942            pop @{$self->{open_elements}};
5943            
5944            !!!next-token;
5945            redo B;
5946          } elsif ($token->{tag_name} eq 'hr') {
5947            ## has a p element in scope
5948            INSCOPE: for (reverse @{$self->{open_elements}}) {
5949              if ($_->[1] eq 'p') {
5950                !!!cp ('t386');
5951                !!!back-token;
5952                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5953                redo B;
5954              } elsif ({
5955                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5956                        button => 1, marquee => 1, object => 1, html => 1,
5957                       }->{$_->[1]}) {
5958                !!!cp ('t387');
5959                last INSCOPE;
5960              }
5961            } # INSCOPE
5962              
5963            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5964            pop @{$self->{open_elements}};
5965              
5966            !!!next-token;
5967            redo B;
5968          } elsif ($token->{tag_name} eq 'input') {
5969            !!!cp ('t388');
5970            $reconstruct_active_formatting_elements->($insert_to_current);
5971            
5972            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5973            ## TODO: associate with $self->{form_element} if defined
5974            pop @{$self->{open_elements}};
5975            
5976            !!!next-token;
5977            redo B;
5978          } elsif ($token->{tag_name} eq 'isindex') {
5979            !!!parse-error (type => 'isindex');
5980            
5981            if (defined $self->{form_element}) {
5982              !!!cp ('t389');
5983            ## Ignore the token            ## Ignore the token
5984            !!!next-token;            !!!next-token;
5985            redo B;            redo B;
5986            } else {
5987              my $at = $token->{attributes};
5988              my $form_attrs;
5989              $form_attrs->{action} = $at->{action} if $at->{action};
5990              my $prompt_attr = $at->{prompt};
5991              $at->{name} = {name => 'name', value => 'isindex'};
5992              delete $at->{action};
5993              delete $at->{prompt};
5994              my @tokens = (
5995                            {type => START_TAG_TOKEN, tag_name => 'form',
5996                             attributes => $form_attrs},
5997                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5998                            {type => START_TAG_TOKEN, tag_name => 'p'},
5999                            {type => START_TAG_TOKEN, tag_name => 'label'},
6000                           );
6001              if ($prompt_attr) {
6002                !!!cp ('t390');
6003                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
6004              } else {
6005                !!!cp ('t391');
6006                push @tokens, {type => CHARACTER_TOKEN,
6007                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
6008                ## TODO: make this configurable
6009              }
6010              push @tokens,
6011                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
6012                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6013                            {type => END_TAG_TOKEN, tag_name => 'label'},
6014                            {type => END_TAG_TOKEN, tag_name => 'p'},
6015                            {type => START_TAG_TOKEN, tag_name => 'hr'},
6016                            {type => END_TAG_TOKEN, tag_name => 'form'};
6017              $token = shift @tokens;
6018              !!!back-token (@tokens);
6019              redo B;
6020            }
6021          } elsif ($token->{tag_name} eq 'textarea') {
6022            my $tag_name = $token->{tag_name};
6023            my $el;
6024            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
6025            
6026            ## TODO: $self->{form_element} if defined
6027            $self->{content_model} = RCDATA_CONTENT_MODEL;
6028            delete $self->{escape}; # MUST
6029            
6030            $insert->($el);
6031            
6032            my $text = '';
6033            !!!next-token;
6034            if ($token->{type} == CHARACTER_TOKEN) {
6035              $token->{data} =~ s/^\x0A//;
6036              unless (length $token->{data}) {
6037                !!!cp ('t392');
6038                !!!next-token;
6039              } else {
6040                !!!cp ('t393');
6041              }
6042            } else {
6043              !!!cp ('t394');
6044            }
6045            while ($token->{type} == CHARACTER_TOKEN) {
6046              !!!cp ('t395');
6047              $text .= $token->{data};
6048              !!!next-token;
6049            }
6050            if (length $text) {
6051              !!!cp ('t396');
6052              $el->manakai_append_text ($text);
6053            }
6054            
6055            $self->{content_model} = PCDATA_CONTENT_MODEL;
6056            
6057            if ($token->{type} == END_TAG_TOKEN and
6058                $token->{tag_name} eq $tag_name) {
6059              !!!cp ('t397');
6060              ## Ignore the token
6061            } else {
6062              !!!cp ('t398');
6063              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
6064            }
6065            !!!next-token;
6066            redo B;
6067          } elsif ({
6068                    iframe => 1,
6069                    noembed => 1,
6070                    noframes => 1,
6071                    noscript => 0, ## TODO: 1 if scripting is enabled
6072                   }->{$token->{tag_name}}) {
6073            !!!cp ('t399');
6074            ## NOTE: There is an "as if in body" code clone.
6075            $parse_rcdata->(CDATA_CONTENT_MODEL);
6076            redo B;
6077          } elsif ($token->{tag_name} eq 'select') {
6078            !!!cp ('t400');
6079            $reconstruct_active_formatting_elements->($insert_to_current);
6080            
6081            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
6082    
6083            ## ISSUE: An issue in spec there          ## TODO: associate with $self->{form_element} if defined
6084            
6085            if ($self->{insertion_mode} & TABLE_IMS or
6086                $self->{insertion_mode} & BODY_TABLE_IMS or
6087                $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6088              !!!cp ('t400.1');
6089              $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6090          } else {          } else {
6091            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t400.2');
6092              $self->{insertion_mode} = IN_SELECT_IM;
6093          }          }
6094        }          !!!next-token;
6095      } elsif ($phase eq 'trailing end') {          redo B;
6096        ## states in the main stage is preserved yet # MUST        } elsif ({
6097                          caption => 1, col => 1, colgroup => 1, frame => 1,
6098        if ($token->{type} eq 'DOCTYPE') {                  frameset => 1, head => 1, option => 1, optgroup => 1,
6099          !!!parse-error (type => 'after html:#DOCTYPE');                  tbody => 1, td => 1, tfoot => 1, th => 1,
6100                    thead => 1, tr => 1,
6101                   }->{$token->{tag_name}}) {
6102            !!!cp ('t401');
6103            !!!parse-error (type => 'in body:'.$token->{tag_name});
6104          ## Ignore the token          ## Ignore the token
6105          !!!next-token;          !!!next-token;
6106          redo B;          redo B;
6107        } elsif ($token->{type} eq 'comment') {          
6108          my $comment = $self->{document}->create_comment ($token->{data});          ## ISSUE: An issue on HTML5 new elements in the spec.
6109          $self->{document}->append_child ($comment);        } else {
6110            !!!cp ('t402');
6111            $reconstruct_active_formatting_elements->($insert_to_current);
6112            
6113            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
6114            
6115          !!!next-token;          !!!next-token;
6116          redo B;          redo B;
6117        } elsif ($token->{type} eq 'character') {        }
6118          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6119            my $data = $1;        if ($token->{tag_name} eq 'body') {
6120            ## As if in the main phase.          ## has a |body| element in scope
6121            ## NOTE: The insertion mode in the main phase          my $i;
6122            ## just before the phase has been changed to the trailing          INSCOPE: for (reverse @{$self->{open_elements}}) {
6123            ## end phase is either "after body" or "after frameset".            if ($_->[1] eq 'body') {
6124            $reconstruct_active_formatting_elements->($insert_to_current)              !!!cp ('t405');
6125              if $phase eq 'main';              $i = $_;
6126                last INSCOPE;
6127              } elsif ({
6128                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6129                        button => 1, marquee => 1, object => 1, html => 1,
6130                       }->{$_->[1]}) {
6131                !!!cp ('t405.1');
6132                last INSCOPE;
6133              }
6134            } # INSCOPE
6135            unless (defined $i) {
6136              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6137              ## NOTE: Ignore the token.
6138              !!!next-token;
6139              redo B;
6140            }
6141    
6142            for (@{$self->{open_elements}}) {
6143              unless ({
6144                       dd => 1, dt => 1, li => 1, p => 1, td => 1,
6145                       th => 1, tr => 1, body => 1, html => 1,
6146                       tbody => 1, tfoot => 1, thead => 1,
6147                      }->{$_->[1]}) {
6148                !!!cp ('t403');
6149                !!!parse-error (type => 'not closed:'.$_->[1]);
6150                last;
6151              } else {
6152                !!!cp ('t404');
6153              }
6154            }
6155    
6156            $self->{insertion_mode} = AFTER_BODY_IM;
6157            !!!next-token;
6158            redo B;
6159          } elsif ($token->{tag_name} eq 'html') {
6160            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
6161              ## ISSUE: There is an issue in the spec.
6162              if ($self->{open_elements}->[-1]->[1] ne 'body') {
6163                !!!cp ('t406');
6164                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
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});
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});
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:'.$self->{open_elements}->[-1]->[1]);
6221              } else {
6222                !!!cp ('t414');
6223              }
6224    
6225              ## Step 3.
6226              splice @{$self->{open_elements}}, $i;
6227    
6228              ## Step 4.
6229              $clear_up_to_marker->()
6230                  if {
6231                    applet => 1, button => 1, marquee => 1, object => 1,
6232                  }->{$token->{tag_name}};
6233            }
6234            !!!next-token;
6235            redo B;
6236          } elsif ($token->{tag_name} eq 'form') {
6237            undef $self->{form_element};
6238    
6239            ## has an element in scope
6240            my $i;
6241            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6242              my $node = $self->{open_elements}->[$_];
6243              if ($node->[1] eq $token->{tag_name}) {
6244                !!!cp ('t418');
6245                $i = $_;
6246                last INSCOPE;
6247              } elsif ({
6248                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6249                        button => 1, marquee => 1, object => 1, html => 1,
6250                       }->{$node->[1]}) {
6251                !!!cp ('t419');
6252                last INSCOPE;
6253              }
6254            } # INSCOPE
6255    
6256            unless (defined $i) { # has an element in scope
6257              !!!cp ('t421');
6258              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6259            } else {
6260              ## Step 1. generate implied end tags
6261              while ({
6262                      dd => 1, dt => 1, li => 1, p => 1,
6263                     }->{$self->{open_elements}->[-1]->[1]}) {
6264                !!!cp ('t417');
6265                pop @{$self->{open_elements}};
6266              }
6267                        
6268            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
6269              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6270                !!!cp ('t417.1');
6271                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6272              } else {
6273                !!!cp ('t420');
6274              }  
6275                        
6276            unless (length $token->{data}) {            ## Step 3.
6277              !!!next-token;            splice @{$self->{open_elements}}, $i;
6278              redo B;          }
6279    
6280            !!!next-token;
6281            redo B;
6282          } elsif ({
6283                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6284                   }->{$token->{tag_name}}) {
6285            ## has an element in scope
6286            my $i;
6287            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6288              my $node = $self->{open_elements}->[$_];
6289              if ({
6290                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6291                  }->{$node->[1]}) {
6292                !!!cp ('t423');
6293                $i = $_;
6294                last INSCOPE;
6295              } elsif ({
6296                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6297                        button => 1, marquee => 1, object => 1, html => 1,
6298                       }->{$node->[1]}) {
6299                !!!cp ('t424');
6300                last INSCOPE;
6301              }
6302            } # INSCOPE
6303    
6304            unless (defined $i) { # has an element in scope
6305              !!!cp ('t425.1');
6306              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6307            } else {
6308              ## Step 1. generate implied end tags
6309              while ({
6310                      dd => 1, dt => 1, li => 1, p => 1,
6311                     }->{$self->{open_elements}->[-1]->[1]}) {
6312                !!!cp ('t422');
6313                pop @{$self->{open_elements}};
6314            }            }
6315              
6316              ## Step 2.
6317              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6318                !!!cp ('t425');
6319                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6320              } else {
6321                !!!cp ('t426');
6322              }
6323    
6324              ## Step 3.
6325              splice @{$self->{open_elements}}, $i;
6326          }          }
6327            
6328            !!!next-token;
6329            redo B;
6330          } elsif ($token->{tag_name} eq 'p') {
6331            ## has an element in scope
6332            my $i;
6333            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6334              my $node = $self->{open_elements}->[$_];
6335              if ($node->[1] eq $token->{tag_name}) {
6336                !!!cp ('t410.1');
6337                $i = $_;
6338                last INSCOPE;
6339              } elsif ({
6340                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6341                        button => 1, marquee => 1, object => 1, html => 1,
6342                       }->{$node->[1]}) {
6343                !!!cp ('t411.1');
6344                last INSCOPE;
6345              }
6346            } # INSCOPE
6347    
6348          !!!parse-error (type => 'after html:#character');          if (defined $i) {
6349          $phase = 'main';            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6350          ## reprocess              !!!cp ('t412.1');
6351                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6352              } else {
6353                !!!cp ('t414.1');
6354              }
6355    
6356              splice @{$self->{open_elements}}, $i;
6357            } else {
6358              !!!cp ('t413.1');
6359              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6360    
6361              !!!cp ('t415.1');
6362              ## As if <p>, then reprocess the current token
6363              my $el;
6364              !!!create-element ($el, 'p');
6365              $insert->($el);
6366              ## NOTE: Not inserted into |$self->{open_elements}|.
6367            }
6368    
6369            !!!next-token;
6370          redo B;          redo B;
6371        } elsif ($token->{type} eq 'start tag' or        } elsif ({
6372                 $token->{type} eq 'end tag') {                  a => 1,
6373          !!!parse-error (type => 'after html:'.$token->{tag_name});                  b => 1, big => 1, em => 1, font => 1, i => 1,
6374          $phase = 'main';                  nobr => 1, s => 1, small => 1, strile => 1,
6375          ## reprocess                  strong => 1, tt => 1, u => 1,
6376                   }->{$token->{tag_name}}) {
6377            !!!cp ('t427');
6378            $formatting_end_tag->($token->{tag_name});
6379          redo B;          redo B;
6380        } elsif ($token->{type} eq 'end-of-file') {        } elsif ($token->{tag_name} eq 'br') {
6381          ## Stop parsing          !!!cp ('t428');
6382          last B;          !!!parse-error (type => 'unmatched end tag:br');
6383    
6384            ## As if <br>
6385            $reconstruct_active_formatting_elements->($insert_to_current);
6386            
6387            my $el;
6388            !!!create-element ($el, 'br');
6389            $insert->($el);
6390            
6391            ## Ignore the token.
6392            !!!next-token;
6393            redo B;
6394          } elsif ({
6395                    caption => 1, col => 1, colgroup => 1, frame => 1,
6396                    frameset => 1, head => 1, option => 1, optgroup => 1,
6397                    tbody => 1, td => 1, tfoot => 1, th => 1,
6398                    thead => 1, tr => 1,
6399                    area => 1, basefont => 1, bgsound => 1,
6400                    embed => 1, hr => 1, iframe => 1, image => 1,
6401                    img => 1, input => 1, isindex => 1, noembed => 1,
6402                    noframes => 1, param => 1, select => 1, spacer => 1,
6403                    table => 1, textarea => 1, wbr => 1,
6404                    noscript => 0, ## TODO: if scripting is enabled
6405                   }->{$token->{tag_name}}) {
6406            !!!cp ('t429');
6407            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6408            ## Ignore the token
6409            !!!next-token;
6410            redo B;
6411            
6412            ## ISSUE: Issue on HTML5 new elements in spec
6413            
6414        } else {        } else {
6415          die "$0: $token->{type}: Unknown token";          ## Step 1
6416            my $node_i = -1;
6417            my $node = $self->{open_elements}->[$node_i];
6418    
6419            ## Step 2
6420            S2: {
6421              if ($node->[1] eq $token->{tag_name}) {
6422                ## Step 1
6423                ## generate implied end tags
6424                while ({
6425                        dd => 1, dt => 1, li => 1, p => 1,
6426                       }->{$self->{open_elements}->[-1]->[1]}) {
6427                  !!!cp ('t430');
6428                  ## ISSUE: Can this case be reached?
6429                  pop @{$self->{open_elements}};
6430                }
6431            
6432                ## Step 2
6433                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6434                  !!!cp ('t431');
6435                  ## NOTE: <x><y></x>
6436                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6437                } else {
6438                  !!!cp ('t432');
6439                }
6440                
6441                ## Step 3
6442                splice @{$self->{open_elements}}, $node_i;
6443    
6444                !!!next-token;
6445                last S2;
6446              } else {
6447                ## Step 3
6448                if (not $formatting_category->{$node->[1]} and
6449                    #not $phrasing_category->{$node->[1]} and
6450                    ($special_category->{$node->[1]} or
6451                     $scoping_category->{$node->[1]})) {
6452                  !!!cp ('t433');
6453                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6454                  ## Ignore the token
6455                  !!!next-token;
6456                  last S2;
6457                }
6458    
6459                !!!cp ('t434');
6460              }
6461              
6462              ## Step 4
6463              $node_i--;
6464              $node = $self->{open_elements}->[$node_i];
6465              
6466              ## Step 5;
6467              redo S2;
6468            } # S2
6469            redo B;
6470        }        }
6471      }      }
6472        redo B;
6473    } # B    } # B
6474    
6475    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4771  sub set_inner_html ($$$) { Line 6483  sub set_inner_html ($$$) {
6483    my $s = \$_[0];    my $s = \$_[0];
6484    my $onerror = $_[1];    my $onerror = $_[1];
6485    
6486      ## ISSUE: Should {confident} be true?
6487    
6488    my $nt = $node->node_type;    my $nt = $node->node_type;
6489    if ($nt == 9) {    if ($nt == 9) {
6490      # MUST      # MUST
# Line 4793  sub set_inner_html ($$$) { Line 6507  sub set_inner_html ($$$) {
6507      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
6508    
6509      ## Step 1 # MUST      ## Step 1 # MUST
6510      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
6511      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
6512        $doc->manakai_is_html (1);
6513      my $p = $class->new;      my $p = $class->new;
6514      $p->{document} = $doc;      $p->{document} = $doc;
6515    
6516      ## Step 9 # MUST      ## Step 8 # MUST
6517      my $i = 0;      my $i = 0;
6518      my $line = 1;      my $line = 1;
6519      my $column = 0;      my $column = 0;
6520      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6521        my $self = shift;        my $self = shift;
6522        $self->{next_input_character} = -1 and return if $i >= length $$s;  
6523        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
6524          unshift @{$self->{prev_char}}, $self->{next_char};
6525    
6526          $self->{next_char} = -1 and return if $i >= length $$s;
6527          $self->{next_char} = ord substr $$s, $i++, 1;
6528        $column++;        $column++;
6529          
6530        if ($self->{next_input_character} == 0x000D) { # CR        if ($self->{next_char} == 0x000A) { # LF
6531          if ($i >= length $$s) {          $line++;
6532            #          $column = 0;
6533          } else {          !!!cp ('i1');
6534            my $next_char = ord substr $$s, $i++, 1;        } elsif ($self->{next_char} == 0x000D) { # CR
6535            if ($next_char == 0x000A) { # LF          $i++ if substr ($$s, $i, 1) eq "\x0A";
6536              #          $self->{next_char} = 0x000A; # LF # MUST
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
         }  
         $self->{next_input_character} = 0x000A; # LF # MUST  
6537          $line++;          $line++;
6538          $column = -1;          $column = 0;
6539        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6540          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6541        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6542          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i3');
6543          } elsif ($self->{next_char} == 0x0000) { # NULL
6544            !!!cp ('i4');
6545            !!!parse-error (type => 'NULL');
6546            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6547        }        }
6548      };      };
6549        $p->{prev_char} = [-1, -1, -1];
6550        $p->{next_char} = -1;
6551            
6552      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6553        my (%opt) = @_;        my (%opt) = @_;
# Line 4841  sub set_inner_html ($$$) { Line 6561  sub set_inner_html ($$$) {
6561      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6562    
6563      ## Step 2      ## Step 2
6564      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6565      $p->{content_model_flag} = {      $p->{content_model} = {
6566        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
6567        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
6568        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
6569        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
6570        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
6571        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
6572        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
6573        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
6574        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
6575        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
6576      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
6577         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
6578            unless defined $p->{content_model};
6579            ## ISSUE: What is "the name of the element"? local name?
6580    
6581      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6582    
6583      ## Step 4      ## Step 3
6584      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6585        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6586    
6587      ## Step 5 # MUST      ## Step 4 # MUST
6588      $doc->append_child ($root);      $doc->append_child ($root);
6589    
6590      ## Step 6 # MUST      ## Step 5 # MUST
6591      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6592    
6593      undef $p->{head_element};      undef $p->{head_element};
6594    
6595      ## Step 7 # MUST      ## Step 6 # MUST
6596      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6597    
6598      ## Step 8 # MUST      ## Step 7 # MUST
6599      my $anode = $node;      my $anode = $node;
6600      AN: while (defined $anode) {      AN: while (defined $anode) {
6601        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6602          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6603          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6604            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6605                !!!cp ('i5');
6606              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6607              last AN;              last AN;
6608            }            }
# Line 4888  sub set_inner_html ($$$) { Line 6611  sub set_inner_html ($$$) {
6611        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6612      } # AN      } # AN
6613            
6614      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6615      {      {
6616        my $self = $p;        my $self = $p;
6617        !!!next-token;        !!!next-token;
6618      }      }
6619      $p->_tree_construction_main;      $p->_tree_construction_main;
6620    
6621      ## Step 11 # MUST      ## Step 10 # MUST
6622      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6623      for (@cn) {      for (@cn) {
6624        $node->remove_child ($_);        $node->remove_child ($_);
6625      }      }
6626      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6627    
6628      ## Step 12 # MUST      ## Step 11 # MUST
6629      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6630      for (@cn) {      for (@cn) {
6631          $this_doc->adopt_node ($_);
6632        $node->append_child ($_);        $node->append_child ($_);
6633      }      }
6634      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
6635    
6636      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6637    } else {    } else {
# Line 4918  sub set_inner_html ($$$) { Line 6641  sub set_inner_html ($$$) {
6641    
6642  } # tree construction stage  } # tree construction stage
6643    
6644  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6645    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  
6646    
6647  1;  1;
6648  # $Date$  # $Date$

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.108

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24