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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24