/[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.8 by wakaba, Sat Jun 23 02:26:51 2007 UTC revision 1.77 by wakaba, Mon Mar 3 10:20:19 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  ## ISSUE:
7    ## var doc = implementation.createDocument (null, null, null);
8    ## doc.write ('');
9    ## alert (doc.compatMode);
10    
11    ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12    ## TODO: 1252 parse error (revision 1264)
13    ## TODO: 8859-11 = 874 (revision 1271)
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
# Line 10  my $permitted_slash_tag_name = { Line 18  my $permitted_slash_tag_name = {
18    meta => 1,    meta => 1,
19    hr => 1,    hr => 1,
20    br => 1,    br => 1,
21    img=> 1,    img => 1,
22    embed => 1,    embed => 1,
23    param => 1,    param => 1,
24    area => 1,    area => 1,
# Line 18  my $permitted_slash_tag_name = { Line 26  my $permitted_slash_tag_name = {
26    input => 1,    input => 1,
27  };  };
28    
 my $entity_char = {  
   AElig => "\x{00C6}",  
   Aacute => "\x{00C1}",  
   Acirc => "\x{00C2}",  
   Agrave => "\x{00C0}",  
   Alpha => "\x{0391}",  
   Aring => "\x{00C5}",  
   Atilde => "\x{00C3}",  
   Auml => "\x{00C4}",  
   Beta => "\x{0392}",  
   Ccedil => "\x{00C7}",  
   Chi => "\x{03A7}",  
   Dagger => "\x{2021}",  
   Delta => "\x{0394}",  
   ETH => "\x{00D0}",  
   Eacute => "\x{00C9}",  
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 }; # $entity_char  
   
 ## TODO: Ensure that this table match to <http://html5.org/tools/web-apps-tracker?from=868&to=869>.  
 ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>  
29  my $c1_entity_char = {  my $c1_entity_char = {
30       128, 8364,    0x80 => 0x20AC,
31       129, 65533,    0x81 => 0xFFFD,
32       130, 8218,    0x82 => 0x201A,
33       131, 402,    0x83 => 0x0192,
34       132, 8222,    0x84 => 0x201E,
35       133, 8230,    0x85 => 0x2026,
36       134, 8224,    0x86 => 0x2020,
37       135, 8225,    0x87 => 0x2021,
38       136, 710,    0x88 => 0x02C6,
39       137, 8240,    0x89 => 0x2030,
40       138, 352,    0x8A => 0x0160,
41       139, 8249,    0x8B => 0x2039,
42       140, 338,    0x8C => 0x0152,
43       141, 65533,    0x8D => 0xFFFD,
44       142, 381,    0x8E => 0x017D,
45       143, 65533,    0x8F => 0xFFFD,
46       144, 65533,    0x90 => 0xFFFD,
47       145, 8216,    0x91 => 0x2018,
48       146, 8217,    0x92 => 0x2019,
49       147, 8220,    0x93 => 0x201C,
50       148, 8221,    0x94 => 0x201D,
51       149, 8226,    0x95 => 0x2022,
52       150, 8211,    0x96 => 0x2013,
53       151, 8212,    0x97 => 0x2014,
54       152, 732,    0x98 => 0x02DC,
55       153, 8482,    0x99 => 0x2122,
56       154, 353,    0x9A => 0x0161,
57       155, 8250,    0x9B => 0x203A,
58       156, 339,    0x9C => 0x0153,
59       157, 65533,    0x9D => 0xFFFD,
60       158, 382,    0x9E => 0x017E,
61       159, 376,    0x9F => 0x0178,
62  }; # $c1_entity_char  }; # $c1_entity_char
63    
64  my $special_category = {  my $special_category = {
# Line 339  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} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
193        $line++;        $line++;
194        $column = 0;        $column = 0;
195      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
196        if ($i >= length $$s) {        $i++ if substr ($$s, $i, 1) eq "\x0A";
197          #        $self->{next_char} = 0x000A; # LF # MUST
       } 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  
198        $line++;        $line++;
199        $column = 0;        $column = 0;
200      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
201        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
202      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
203        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
204  ## TODO: test        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
       $self->{next_input_character} = 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 400  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 447  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  ## ISSUE: <http://html5.org/tools/web-apps-tracker?from=874&to=876>  ## 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;
# Line 456  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                !!!cp (36);
661              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
662              } else {
663                !!!cp (37);
664            }            }
665          } else {          } else {
666            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
667          }          }
668          $self->{state} = 'data';          $self->{state} = DATA_STATE;
669          !!!next-input-character;          !!!next-input-character;
670    
671          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
672    
673          redo A;          redo A;
674        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
675                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
676          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
677            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
678            # start tag or end tag            # start tag or end tag
679          ## Stay in this state          ## Stay in this state
680          !!!next-input-character;          !!!next-input-character;
681          redo A;          redo A;
682        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
683          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
684          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
685              !!!cp (39);
686              $self->{current_token}->{first_start_tag}
687                  = not defined $self->{last_emitted_start_tag_name};
688            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
689          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
690            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
691            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
692                !!!cp (40);
693              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
694              } else {
695                !!!cp (41);
696            }            }
697          } else {          } else {
698            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
699          }          }
700          $self->{state} = 'data';          $self->{state} = DATA_STATE;
701          # reconsume          # reconsume
702    
703          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
704    
705          redo A;          redo A;
706        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
707          !!!next-input-character;          !!!next-input-character;
708          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
709              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
710              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
711            # permitted slash            # permitted slash
712              !!!cp (42);
713            #            #
714          } else {          } else {
715              !!!cp (43);
716            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
717          }          }
718          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
719          # next-input-character is already done          # next-input-character is already done
720          redo A;          redo A;
721        } else {        } else {
722          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
723            $self->{current_token}->{tag_name} .= chr $self->{next_char};
724            # start tag or end tag            # start tag or end tag
725          ## Stay in the state          ## Stay in the state
726          !!!next-input-character;          !!!next-input-character;
727          redo A;          redo A;
728        }        }
729      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
730        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
731            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
732            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
733            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
734            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
735            !!!cp (45);
736          ## Stay in the state          ## Stay in the state
737          !!!next-input-character;          !!!next-input-character;
738          redo A;          redo A;
739        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
740          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
741              !!!cp (46);
742              $self->{current_token}->{first_start_tag}
743                  = not defined $self->{last_emitted_start_tag_name};
744            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
745          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
746            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
747            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
748                !!!cp (47);
749              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
750              } else {
751                !!!cp (48);
752            }            }
753          } else {          } else {
754            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
755          }          }
756          $self->{state} = 'data';          $self->{state} = DATA_STATE;
757          !!!next-input-character;          !!!next-input-character;
758    
759          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
760    
761          redo A;          redo A;
762        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
763                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
764          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
765            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
766                                value => ''};                                value => ''};
767          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
768          !!!next-input-character;          !!!next-input-character;
769          redo A;          redo A;
770        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
771          !!!next-input-character;          !!!next-input-character;
772          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
773              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
774              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
775            # permitted slash            # permitted slash
776              !!!cp (50);
777            #            #
778          } else {          } else {
779              !!!cp (51);
780            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
781          }          }
782          ## Stay in the state          ## Stay in the state
783          # next-input-character is already done          # next-input-character is already done
784          redo A;          redo A;
785        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
786          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
787          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
788              !!!cp (52);
789              $self->{current_token}->{first_start_tag}
790                  = not defined $self->{last_emitted_start_tag_name};
791            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
792          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
793            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
794            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
795                !!!cp (53);
796              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
797              } else {
798                !!!cp (54);
799            }            }
800          } else {          } else {
801            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
802          }          }
803          $self->{state} = 'data';          $self->{state} = DATA_STATE;
804          # reconsume          # reconsume
805    
806          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
807    
808          redo A;          redo A;
809        } else {        } else {
810          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
811                 0x0022 => 1, # "
812                 0x0027 => 1, # '
813                 0x003D => 1, # =
814                }->{$self->{next_char}}) {
815              !!!cp (55);
816              !!!parse-error (type => 'bad attribute name');
817            } else {
818              !!!cp (56);
819            }
820            $self->{current_attribute} = {name => chr ($self->{next_char}),
821                                value => ''};                                value => ''};
822          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
823          !!!next-input-character;          !!!next-input-character;
824          redo A;          redo A;
825        }        }
826      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
827        my $before_leave = sub {        my $before_leave = sub {
828          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
829              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
830            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
831              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
832            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
833          } else {          } else {
834              !!!cp (58);
835            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
836              = $self->{current_attribute};              = $self->{current_attribute};
837          }          }
838        }; # $before_leave        }; # $before_leave
839    
840        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
841            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
842            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
843            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
844            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
845            !!!cp (59);
846          $before_leave->();          $before_leave->();
847          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
848          !!!next-input-character;          !!!next-input-character;
849          redo A;          redo A;
850        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
851            !!!cp (60);
852          $before_leave->();          $before_leave->();
853          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
854          !!!next-input-character;          !!!next-input-character;
855          redo A;          redo A;
856        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
857          $before_leave->();          $before_leave->();
858          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
859              !!!cp (61);
860              $self->{current_token}->{first_start_tag}
861                  = not defined $self->{last_emitted_start_tag_name};
862            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
863          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
864            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
865              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
866            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
867              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
868            }            }
869          } else {          } else {
870            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
871          }          }
872          $self->{state} = 'data';          $self->{state} = DATA_STATE;
873          !!!next-input-character;          !!!next-input-character;
874    
875          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
876    
877          redo A;          redo A;
878        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
879                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
880          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
881            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
882          ## Stay in the state          ## Stay in the state
883          !!!next-input-character;          !!!next-input-character;
884          redo A;          redo A;
885        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
886          $before_leave->();          $before_leave->();
887          !!!next-input-character;          !!!next-input-character;
888          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
889              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
890              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
891            # permitted slash            # permitted slash
892              !!!cp (64);
893            #            #
894          } else {          } else {
895              !!!cp (65);
896            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
897          }          }
898          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
899          # next-input-character is already done          # next-input-character is already done
900          redo A;          redo A;
901        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
902          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
903          $before_leave->();          $before_leave->();
904          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
905              !!!cp (66);
906              $self->{current_token}->{first_start_tag}
907                  = not defined $self->{last_emitted_start_tag_name};
908            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
909          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
910            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
911            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
912                !!!cp (67);
913              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
914              } else {
915                !!!cp (68);
916            }            }
917          } else {          } else {
918            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
919          }          }
920          $self->{state} = 'data';          $self->{state} = DATA_STATE;
921          # reconsume          # reconsume
922    
923          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
924    
925          redo A;          redo A;
926        } else {        } else {
927          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
928                $self->{next_char} == 0x0027) { # '
929              !!!cp (69);
930              !!!parse-error (type => 'bad attribute name');
931            } else {
932              !!!cp (70);
933            }
934            $self->{current_attribute}->{name} .= chr ($self->{next_char});
935          ## Stay in the state          ## Stay in the state
936          !!!next-input-character;          !!!next-input-character;
937          redo A;          redo A;
938        }        }
939      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
940        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
941            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
942            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
943            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
944            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
945            !!!cp (71);
946          ## Stay in the state          ## Stay in the state
947          !!!next-input-character;          !!!next-input-character;
948          redo A;          redo A;
949        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
950          $self->{state} = 'before attribute value';          !!!cp (72);
951            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
952          !!!next-input-character;          !!!next-input-character;
953          redo A;          redo A;
954        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
955          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
956              !!!cp (73);
957              $self->{current_token}->{first_start_tag}
958                  = not defined $self->{last_emitted_start_tag_name};
959            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
960          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
961            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
962            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
963                !!!cp (74);
964              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
965              } else {
966                !!!cp (75);
967            }            }
968          } else {          } else {
969            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
970          }          }
971          $self->{state} = 'data';          $self->{state} = DATA_STATE;
972          !!!next-input-character;          !!!next-input-character;
973    
974          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
975    
976          redo A;          redo A;
977        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
978                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
979          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
980            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
981                                value => ''};                                value => ''};
982          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
983          !!!next-input-character;          !!!next-input-character;
984          redo A;          redo A;
985        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
986          !!!next-input-character;          !!!next-input-character;
987          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
988              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
989              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
990            # permitted slash            # permitted slash
991              !!!cp (77);
992            #            #
993          } else {          } else {
994              !!!cp (78);
995            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
996              ## TODO: Different error type for <aa / bb> than <aa/>
997          }          }
998          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
999          # next-input-character is already done          # next-input-character is already done
1000          redo A;          redo A;
1001        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1002          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1003          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1004              !!!cp (79);
1005              $self->{current_token}->{first_start_tag}
1006                  = not defined $self->{last_emitted_start_tag_name};
1007            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1008          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1009            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1010            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1011                !!!cp (80);
1012              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1013              } else {
1014                !!!cp (81);
1015            }            }
1016          } else {          } else {
1017            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1018          }          }
1019          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1020          # reconsume          # reconsume
1021    
1022          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1023    
1024          redo A;          redo A;
1025        } else {        } else {
1026          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1027            $self->{current_attribute} = {name => chr ($self->{next_char}),
1028                                value => ''};                                value => ''};
1029          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1030          !!!next-input-character;          !!!next-input-character;
1031          redo A;                  redo A;        
1032        }        }
1033      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1034        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1035            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1036            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1037            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1038            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1039            !!!cp (83);
1040          ## Stay in the state          ## Stay in the state
1041          !!!next-input-character;          !!!next-input-character;
1042          redo A;          redo A;
1043        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1044          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1045            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1046          !!!next-input-character;          !!!next-input-character;
1047          redo A;          redo A;
1048        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1049          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1050            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1051          ## reconsume          ## reconsume
1052          redo A;          redo A;
1053        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1054          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1055            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1056          !!!next-input-character;          !!!next-input-character;
1057          redo A;          redo A;
1058        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1059          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1060              !!!cp (87);
1061              $self->{current_token}->{first_start_tag}
1062                  = not defined $self->{last_emitted_start_tag_name};
1063            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1064          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1065            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1066            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1067                !!!cp (88);
1068              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1069              } else {
1070                !!!cp (89);
1071            }            }
1072          } else {          } else {
1073            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1074          }          }
1075          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1076          !!!next-input-character;          !!!next-input-character;
1077    
1078          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1079    
1080          redo A;          redo A;
1081        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1082          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1083          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1084              !!!cp (90);
1085              $self->{current_token}->{first_start_tag}
1086                  = not defined $self->{last_emitted_start_tag_name};
1087            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1088          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1089            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1090            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1091                !!!cp (91);
1092              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1093              } else {
1094                !!!cp (92);
1095            }            }
1096          } else {          } else {
1097            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1098          }          }
1099          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1100          ## reconsume          ## reconsume
1101    
1102          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1103    
1104          redo A;          redo A;
1105        } else {        } else {
1106          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1107          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1108              !!!parse-error (type => 'bad attribute value');
1109            } else {
1110              !!!cp (94);
1111            }
1112            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1113            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1114          !!!next-input-character;          !!!next-input-character;
1115          redo A;          redo A;
1116        }        }
1117      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1118        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1119          $self->{state} = 'before attribute name';          !!!cp (95);
1120            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1121          !!!next-input-character;          !!!next-input-character;
1122          redo A;          redo A;
1123        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1124          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1125          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1126            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1127          !!!next-input-character;          !!!next-input-character;
1128          redo A;          redo A;
1129        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1130          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1131          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1132              !!!cp (97);
1133              $self->{current_token}->{first_start_tag}
1134                  = not defined $self->{last_emitted_start_tag_name};
1135            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1136          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1137            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1138            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1139                !!!cp (98);
1140              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1141              } else {
1142                !!!cp (99);
1143            }            }
1144          } else {          } else {
1145            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1146          }          }
1147          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1148          ## reconsume          ## reconsume
1149    
1150          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1151    
1152          redo A;          redo A;
1153        } else {        } else {
1154          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1155            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1156          ## Stay in the state          ## Stay in the state
1157          !!!next-input-character;          !!!next-input-character;
1158          redo A;          redo A;
1159        }        }
1160      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1161        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1162          $self->{state} = 'before attribute name';          !!!cp (101);
1163            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1164          !!!next-input-character;          !!!next-input-character;
1165          redo A;          redo A;
1166        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1167          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1168          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1169            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1170          !!!next-input-character;          !!!next-input-character;
1171          redo A;          redo A;
1172        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1173          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1174          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1175              !!!cp (103);
1176              $self->{current_token}->{first_start_tag}
1177                  = not defined $self->{last_emitted_start_tag_name};
1178            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1179          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1180            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1181            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1182                !!!cp (104);
1183              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1184              } else {
1185                !!!cp (105);
1186            }            }
1187          } else {          } else {
1188            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1189          }          }
1190          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1191          ## reconsume          ## reconsume
1192    
1193          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1194    
1195          redo A;          redo A;
1196        } else {        } else {
1197          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1198            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1199          ## Stay in the state          ## Stay in the state
1200          !!!next-input-character;          !!!next-input-character;
1201          redo A;          redo A;
1202        }        }
1203      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1204        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1205            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1206            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1207            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1208            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1209          $self->{state} = 'before attribute name';          !!!cp (107);
1210          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1211          redo A;          !!!next-input-character;
1212        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1213          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1214          $self->{state} = 'entity in attribute value';          !!!cp (108);
1215          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1216          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1217        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1218          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1219          } elsif ($self->{next_char} == 0x003E) { # >
1220            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1221              !!!cp (109);
1222              $self->{current_token}->{first_start_tag}
1223                  = not defined $self->{last_emitted_start_tag_name};
1224            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1225          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1226            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1227            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1228                !!!cp (110);
1229              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1230              } else {
1231                !!!cp (111);
1232            }            }
1233          } else {          } else {
1234            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1235          }          }
1236          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1237          !!!next-input-character;          !!!next-input-character;
1238    
1239          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1240    
1241          redo A;          redo A;
1242        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1243          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1244          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1245              !!!cp (112);
1246              $self->{current_token}->{first_start_tag}
1247                  = not defined $self->{last_emitted_start_tag_name};
1248            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1249          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1250            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1251            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1252                !!!cp (113);
1253              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1254              } else {
1255                !!!cp (114);
1256            }            }
1257          } else {          } else {
1258            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1259          }          }
1260          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1261          ## reconsume          ## reconsume
1262    
1263          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1264    
1265          redo A;          redo A;
1266        } else {        } else {
1267          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1268                 0x0022 => 1, # "
1269                 0x0027 => 1, # '
1270                 0x003D => 1, # =
1271                }->{$self->{next_char}}) {
1272              !!!cp (115);
1273              !!!parse-error (type => 'bad attribute value');
1274            } else {
1275              !!!cp (116);
1276            }
1277            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1278          ## Stay in the state          ## Stay in the state
1279          !!!next-input-character;          !!!next-input-character;
1280          redo A;          redo A;
1281        }        }
1282      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1283        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1284              (1,
1285               $self->{last_attribute_value_state}
1286                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1287               $self->{last_attribute_value_state}
1288                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1289               -1);
1290    
1291        unless (defined $token) {        unless (defined $token) {
1292            !!!cp (117);
1293          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1294        } else {        } else {
1295            !!!cp (118);
1296          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1297            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1298          ## 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"
1299        }        }
1300    
1301        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1302        # next-input-character is already done        # next-input-character is already done
1303        redo A;        redo A;
1304      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1305          if ($self->{next_char} == 0x0009 or # HT
1306              $self->{next_char} == 0x000A or # LF
1307              $self->{next_char} == 0x000B or # VT
1308              $self->{next_char} == 0x000C or # FF
1309              $self->{next_char} == 0x0020) { # SP
1310            !!!cp (118);
1311            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1312            !!!next-input-character;
1313            redo A;
1314          } elsif ($self->{next_char} == 0x003E) { # >
1315            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1316              !!!cp (119);
1317              $self->{current_token}->{first_start_tag}
1318                  = not defined $self->{last_emitted_start_tag_name};
1319              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1320            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1321              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1322              if ($self->{current_token}->{attributes}) {
1323                !!!cp (120);
1324                !!!parse-error (type => 'end tag attribute');
1325              } else {
1326                !!!cp (121);
1327              }
1328            } else {
1329              die "$0: $self->{current_token}->{type}: Unknown token type";
1330            }
1331            $self->{state} = DATA_STATE;
1332            !!!next-input-character;
1333    
1334            !!!emit ($self->{current_token}); # start tag or end tag
1335    
1336            redo A;
1337          } elsif ($self->{next_char} == 0x002F) { # /
1338            !!!next-input-character;
1339            if ($self->{next_char} == 0x003E and # >
1340                $self->{current_token}->{type} == START_TAG_TOKEN and
1341                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1342              # permitted slash
1343              !!!cp (122);
1344              #
1345            } else {
1346              !!!cp (123);
1347              !!!parse-error (type => 'nestc');
1348            }
1349            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1350            # next-input-character is already done
1351            redo A;
1352          } else {
1353            !!!cp (124);
1354            !!!parse-error (type => 'no space between attributes');
1355            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1356            ## reconsume
1357            redo A;
1358          }
1359        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1360        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1361                
1362        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1363    
1364        BC: {        BC: {
1365          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1366            $self->{state} = 'data';            !!!cp (124);
1367              $self->{state} = DATA_STATE;
1368            !!!next-input-character;            !!!next-input-character;
1369    
1370            !!!emit ($token);            !!!emit ($token);
1371    
1372            redo A;            redo A;
1373          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1374            $self->{state} = 'data';            !!!cp (125);
1375              $self->{state} = DATA_STATE;
1376            ## reconsume            ## reconsume
1377    
1378            !!!emit ($token);            !!!emit ($token);
1379    
1380            redo A;            redo A;
1381          } else {          } else {
1382            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1383              $token->{data} .= chr ($self->{next_char});
1384            !!!next-input-character;            !!!next-input-character;
1385            redo BC;            redo BC;
1386          }          }
1387        } # BC        } # BC
1388      } elsif ($self->{state} eq 'markup declaration open') {  
1389          die "$0: _get_next_token: unexpected case [BC]";
1390        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1391        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1392    
1393        my @next_char;        my @next_char;
1394        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1395                
1396        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1397          !!!next-input-character;          !!!next-input-character;
1398          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1399          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1400            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1401            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1402              $self->{state} = COMMENT_START_STATE;
1403            !!!next-input-character;            !!!next-input-character;
1404            redo A;            redo A;
1405            } else {
1406              !!!cp (128);
1407          }          }
1408        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1409                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1410          !!!next-input-character;          !!!next-input-character;
1411          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1412          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1413              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1414            !!!next-input-character;            !!!next-input-character;
1415            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1416            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1417                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1418              !!!next-input-character;              !!!next-input-character;
1419              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1420              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1421                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
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} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1425                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
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} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1429                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
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} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1433                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1434                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1435                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1436                        $self->{state} = DOCTYPE_STATE;
1437                      !!!next-input-character;                      !!!next-input-character;
1438                      redo A;                      redo A;
1439                      } else {
1440                        !!!cp (130);
1441                    }                    }
1442                    } else {
1443                      !!!cp (131);
1444                  }                  }
1445                  } else {
1446                    !!!cp (132);
1447                }                }
1448                } else {
1449                  !!!cp (133);
1450              }              }
1451              } else {
1452                !!!cp (134);
1453            }            }
1454            } else {
1455              !!!cp (135);
1456          }          }
1457          } else {
1458            !!!cp (136);
1459        }        }
1460    
1461        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1462        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1463        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1464        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1465        redo A;        redo A;
1466                
1467        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1468        ## 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?
1469      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1470        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1471          $self->{state} = 'comment dash';          !!!cp (137);
1472            $self->{state} = COMMENT_START_DASH_STATE;
1473            !!!next-input-character;
1474            redo A;
1475          } elsif ($self->{next_char} == 0x003E) { # >
1476            !!!cp (138);
1477            !!!parse-error (type => 'bogus comment');
1478            $self->{state} = DATA_STATE;
1479            !!!next-input-character;
1480    
1481            !!!emit ($self->{current_token}); # comment
1482    
1483            redo A;
1484          } elsif ($self->{next_char} == -1) {
1485            !!!cp (139);
1486            !!!parse-error (type => 'unclosed comment');
1487            $self->{state} = DATA_STATE;
1488            ## reconsume
1489    
1490            !!!emit ($self->{current_token}); # comment
1491    
1492            redo A;
1493          } else {
1494            !!!cp (140);
1495            $self->{current_token}->{data} # comment
1496                .= chr ($self->{next_char});
1497            $self->{state} = COMMENT_STATE;
1498            !!!next-input-character;
1499            redo A;
1500          }
1501        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1502          if ($self->{next_char} == 0x002D) { # -
1503            !!!cp (141);
1504            $self->{state} = COMMENT_END_STATE;
1505            !!!next-input-character;
1506            redo A;
1507          } elsif ($self->{next_char} == 0x003E) { # >
1508            !!!cp (142);
1509            !!!parse-error (type => 'bogus comment');
1510            $self->{state} = DATA_STATE;
1511            !!!next-input-character;
1512    
1513            !!!emit ($self->{current_token}); # comment
1514    
1515            redo A;
1516          } elsif ($self->{next_char} == -1) {
1517            !!!cp (143);
1518            !!!parse-error (type => 'unclosed comment');
1519            $self->{state} = DATA_STATE;
1520            ## reconsume
1521    
1522            !!!emit ($self->{current_token}); # comment
1523    
1524            redo A;
1525          } else {
1526            !!!cp (144);
1527            $self->{current_token}->{data} # comment
1528                .= '-' . chr ($self->{next_char});
1529            $self->{state} = COMMENT_STATE;
1530            !!!next-input-character;
1531            redo A;
1532          }
1533        } elsif ($self->{state} == COMMENT_STATE) {
1534          if ($self->{next_char} == 0x002D) { # -
1535            !!!cp (145);
1536            $self->{state} = COMMENT_END_DASH_STATE;
1537          !!!next-input-character;          !!!next-input-character;
1538          redo A;          redo A;
1539        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1540            !!!cp (146);
1541          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1542          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1543          ## reconsume          ## reconsume
1544    
1545          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1546    
1547          redo A;          redo A;
1548        } else {        } else {
1549          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1550            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1551          ## Stay in the state          ## Stay in the state
1552          !!!next-input-character;          !!!next-input-character;
1553          redo A;          redo A;
1554        }        }
1555      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1556        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1557          $self->{state} = 'comment end';          !!!cp (148);
1558            $self->{state} = COMMENT_END_STATE;
1559          !!!next-input-character;          !!!next-input-character;
1560          redo A;          redo A;
1561        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1562            !!!cp (149);
1563          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1564          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1565          ## reconsume          ## reconsume
1566    
1567          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1568    
1569          redo A;          redo A;
1570        } else {        } else {
1571          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1572          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1573            $self->{state} = COMMENT_STATE;
1574          !!!next-input-character;          !!!next-input-character;
1575          redo A;          redo A;
1576        }        }
1577      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1578        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1579          $self->{state} = 'data';          !!!cp (151);
1580            $self->{state} = DATA_STATE;
1581          !!!next-input-character;          !!!next-input-character;
1582    
1583          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1584    
1585          redo A;          redo A;
1586        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1587            !!!cp (152);
1588          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1589          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1590          ## Stay in the state          ## Stay in the state
1591          !!!next-input-character;          !!!next-input-character;
1592          redo A;          redo A;
1593        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1594            !!!cp (153);
1595          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1596          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1597          ## reconsume          ## reconsume
1598    
1599          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1600    
1601          redo A;          redo A;
1602        } else {        } else {
1603            !!!cp (154);
1604          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1605          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1606          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1607          !!!next-input-character;          !!!next-input-character;
1608          redo A;          redo A;
1609        }        }
1610      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1611        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1612            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1613            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1614            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1615            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1616          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1617            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1618          !!!next-input-character;          !!!next-input-character;
1619          redo A;          redo A;
1620        } else {        } else {
1621            !!!cp (156);
1622          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1623          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1624          ## reconsume          ## reconsume
1625          redo A;          redo A;
1626        }        }
1627      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1628        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1629            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1630            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1631            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1632            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1633            !!!cp (157);
1634          ## Stay in the state          ## Stay in the state
1635          !!!next-input-character;          !!!next-input-character;
1636          redo A;          redo A;
1637        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
1638                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (158);
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
1639          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1640          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1641          !!!next-input-character;          !!!next-input-character;
1642    
1643          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1644    
1645          redo A;          redo A;
1646        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1647            !!!cp (159);
1648          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1649          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1650          ## reconsume          ## reconsume
1651    
1652          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1653    
1654          redo A;          redo A;
1655        } else {        } else {
1656          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
1657                            name => chr ($self->{next_input_character}),          $self->{current_token}
1658                            error => 1};              = {type => DOCTYPE_TOKEN,
1659                   name => chr ($self->{next_char}),
1660                   #quirks => 0,
1661                  };
1662  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1663          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1664          !!!next-input-character;          !!!next-input-character;
1665          redo A;          redo A;
1666        }        }
1667      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1668        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
1669            $self->{next_input_character} == 0x000A or # LF        if ($self->{next_char} == 0x0009 or # HT
1670            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000A or # LF
1671            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000B or # VT
1672            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000C or # FF
1673          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{next_char} == 0x0020) { # SP
1674          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
1675            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1679          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
1680          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1681          !!!next-input-character;          !!!next-input-character;
1682    
1683          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1684    
1685          redo A;          redo A;
1686        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
1687                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
1688          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
1689          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
1690            ## reconsume
1691    
1692            $self->{current_token}->{quirks} = 1;
1693            !!!emit ($self->{current_token}); # DOCTYPE
1694    
1695            redo A;
1696          } else {
1697            !!!cp (164);
1698            $self->{current_token}->{name}
1699              .= chr ($self->{next_char}); # DOCTYPE
1700            ## Stay in the state
1701            !!!next-input-character;
1702            redo A;
1703          }
1704        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1705          if ($self->{next_char} == 0x0009 or # HT
1706              $self->{next_char} == 0x000A or # LF
1707              $self->{next_char} == 0x000B or # VT
1708              $self->{next_char} == 0x000C or # FF
1709              $self->{next_char} == 0x0020) { # SP
1710            !!!cp (165);
1711          ## Stay in the state          ## Stay in the state
1712          !!!next-input-character;          !!!next-input-character;
1713          redo A;          redo A;
1714        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1715            !!!cp (166);
1716            $self->{state} = DATA_STATE;
1717            !!!next-input-character;
1718    
1719            !!!emit ($self->{current_token}); # DOCTYPE
1720    
1721            redo A;
1722          } elsif ($self->{next_char} == -1) {
1723            !!!cp (167);
1724          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1725          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
1726          ## reconsume          ## reconsume
1727    
1728          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
1729          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1730    
1731          redo A;          redo A;
1732          } elsif ($self->{next_char} == 0x0050 or # P
1733                   $self->{next_char} == 0x0070) { # p
1734            !!!next-input-character;
1735            if ($self->{next_char} == 0x0055 or # U
1736                $self->{next_char} == 0x0075) { # u
1737              !!!next-input-character;
1738              if ($self->{next_char} == 0x0042 or # B
1739                  $self->{next_char} == 0x0062) { # b
1740                !!!next-input-character;
1741                if ($self->{next_char} == 0x004C or # L
1742                    $self->{next_char} == 0x006C) { # l
1743                  !!!next-input-character;
1744                  if ($self->{next_char} == 0x0049 or # I
1745                      $self->{next_char} == 0x0069) { # i
1746                    !!!next-input-character;
1747                    if ($self->{next_char} == 0x0043 or # C
1748                        $self->{next_char} == 0x0063) { # c
1749                      !!!cp (168);
1750                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1751                      !!!next-input-character;
1752                      redo A;
1753                    } else {
1754                      !!!cp (169);
1755                    }
1756                  } else {
1757                    !!!cp (170);
1758                  }
1759                } else {
1760                  !!!cp (171);
1761                }
1762              } else {
1763                !!!cp (172);
1764              }
1765            } else {
1766              !!!cp (173);
1767            }
1768    
1769            #
1770          } elsif ($self->{next_char} == 0x0053 or # S
1771                   $self->{next_char} == 0x0073) { # s
1772            !!!next-input-character;
1773            if ($self->{next_char} == 0x0059 or # Y
1774                $self->{next_char} == 0x0079) { # y
1775              !!!next-input-character;
1776              if ($self->{next_char} == 0x0053 or # S
1777                  $self->{next_char} == 0x0073) { # s
1778                !!!next-input-character;
1779                if ($self->{next_char} == 0x0054 or # T
1780                    $self->{next_char} == 0x0074) { # t
1781                  !!!next-input-character;
1782                  if ($self->{next_char} == 0x0045 or # E
1783                      $self->{next_char} == 0x0065) { # e
1784                    !!!next-input-character;
1785                    if ($self->{next_char} == 0x004D or # M
1786                        $self->{next_char} == 0x006D) { # m
1787                      !!!cp (174);
1788                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1789                      !!!next-input-character;
1790                      redo A;
1791                    } else {
1792                      !!!cp (175);
1793                    }
1794                  } else {
1795                    !!!cp (176);
1796                  }
1797                } else {
1798                  !!!cp (177);
1799                }
1800              } else {
1801                !!!cp (178);
1802              }
1803            } else {
1804              !!!cp (179);
1805            }
1806    
1807            #
1808        } else {        } else {
1809          $self->{current_token}->{name}          !!!cp (180);
1810            .= chr ($self->{next_input_character}); # DOCTYPE          !!!next-input-character;
1811          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          #
1812          }
1813    
1814          !!!parse-error (type => 'string after DOCTYPE name');
1815          $self->{current_token}->{quirks} = 1;
1816    
1817          $self->{state} = BOGUS_DOCTYPE_STATE;
1818          # next-input-character is already done
1819          redo A;
1820        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1821          if ({
1822                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1823                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1824              }->{$self->{next_char}}) {
1825            !!!cp (181);
1826          ## Stay in the state          ## Stay in the state
1827          !!!next-input-character;          !!!next-input-character;
1828          redo A;          redo A;
1829          } elsif ($self->{next_char} eq 0x0022) { # "
1830            !!!cp (182);
1831            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1832            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1833            !!!next-input-character;
1834            redo A;
1835          } elsif ($self->{next_char} eq 0x0027) { # '
1836            !!!cp (183);
1837            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1838            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1839            !!!next-input-character;
1840            redo A;
1841          } elsif ($self->{next_char} eq 0x003E) { # >
1842            !!!cp (184);
1843            !!!parse-error (type => 'no PUBLIC literal');
1844    
1845            $self->{state} = DATA_STATE;
1846            !!!next-input-character;
1847    
1848            $self->{current_token}->{quirks} = 1;
1849            !!!emit ($self->{current_token}); # DOCTYPE
1850    
1851            redo A;
1852          } elsif ($self->{next_char} == -1) {
1853            !!!cp (185);
1854            !!!parse-error (type => 'unclosed DOCTYPE');
1855    
1856            $self->{state} = DATA_STATE;
1857            ## reconsume
1858    
1859            $self->{current_token}->{quirks} = 1;
1860            !!!emit ($self->{current_token}); # DOCTYPE
1861    
1862            redo A;
1863          } else {
1864            !!!cp (186);
1865            !!!parse-error (type => 'string after PUBLIC');
1866            $self->{current_token}->{quirks} = 1;
1867    
1868            $self->{state} = BOGUS_DOCTYPE_STATE;
1869            !!!next-input-character;
1870            redo A;
1871        }        }
1872      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1873        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0022) { # "
1874            $self->{next_input_character} == 0x000A or # LF          !!!cp (187);
1875            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1876            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
1877            $self->{next_input_character} == 0x0020) { # SP          redo A;
1878          } elsif ($self->{next_char} == 0x003E) { # >
1879            !!!cp (188);
1880            !!!parse-error (type => 'unclosed PUBLIC literal');
1881    
1882            $self->{state} = DATA_STATE;
1883            !!!next-input-character;
1884    
1885            $self->{current_token}->{quirks} = 1;
1886            !!!emit ($self->{current_token}); # DOCTYPE
1887    
1888            redo A;
1889          } elsif ($self->{next_char} == -1) {
1890            !!!cp (189);
1891            !!!parse-error (type => 'unclosed PUBLIC literal');
1892    
1893            $self->{state} = DATA_STATE;
1894            ## reconsume
1895    
1896            $self->{current_token}->{quirks} = 1;
1897            !!!emit ($self->{current_token}); # DOCTYPE
1898    
1899            redo A;
1900          } else {
1901            !!!cp (190);
1902            $self->{current_token}->{public_identifier} # DOCTYPE
1903                .= chr $self->{next_char};
1904          ## Stay in the state          ## Stay in the state
1905          !!!next-input-character;          !!!next-input-character;
1906          redo A;          redo A;
1907        } elsif ($self->{next_input_character} == 0x003E) { # >        }
1908          $self->{state} = 'data';      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1909          if ($self->{next_char} == 0x0027) { # '
1910            !!!cp (191);
1911            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1912            !!!next-input-character;
1913            redo A;
1914          } elsif ($self->{next_char} == 0x003E) { # >
1915            !!!cp (192);
1916            !!!parse-error (type => 'unclosed PUBLIC literal');
1917    
1918            $self->{state} = DATA_STATE;
1919          !!!next-input-character;          !!!next-input-character;
1920    
1921            $self->{current_token}->{quirks} = 1;
1922            !!!emit ($self->{current_token}); # DOCTYPE
1923    
1924            redo A;
1925          } elsif ($self->{next_char} == -1) {
1926            !!!cp (193);
1927            !!!parse-error (type => 'unclosed PUBLIC literal');
1928    
1929            $self->{state} = DATA_STATE;
1930            ## reconsume
1931    
1932            $self->{current_token}->{quirks} = 1;
1933          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1934    
1935          redo A;          redo A;
1936        } elsif ($self->{next_input_character} == -1) {        } else {
1937            !!!cp (194);
1938            $self->{current_token}->{public_identifier} # DOCTYPE
1939                .= chr $self->{next_char};
1940            ## Stay in the state
1941            !!!next-input-character;
1942            redo A;
1943          }
1944        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1945          if ({
1946                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1947                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1948              }->{$self->{next_char}}) {
1949            !!!cp (195);
1950            ## Stay in the state
1951            !!!next-input-character;
1952            redo A;
1953          } elsif ($self->{next_char} == 0x0022) { # "
1954            !!!cp (196);
1955            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1956            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1957            !!!next-input-character;
1958            redo A;
1959          } elsif ($self->{next_char} == 0x0027) { # '
1960            !!!cp (197);
1961            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1962            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1963            !!!next-input-character;
1964            redo A;
1965          } elsif ($self->{next_char} == 0x003E) { # >
1966            !!!cp (198);
1967            $self->{state} = DATA_STATE;
1968            !!!next-input-character;
1969    
1970            !!!emit ($self->{current_token}); # DOCTYPE
1971    
1972            redo A;
1973          } elsif ($self->{next_char} == -1) {
1974            !!!cp (199);
1975          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1976          $self->{state} = 'data';  
1977            $self->{state} = DATA_STATE;
1978          ## reconsume          ## reconsume
1979    
1980            $self->{current_token}->{quirks} = 1;
1981          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1982    
1983          redo A;          redo A;
1984        } else {        } else {
1985          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (200);
1986          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
1987          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
1988    
1989            $self->{state} = BOGUS_DOCTYPE_STATE;
1990          !!!next-input-character;          !!!next-input-character;
1991          redo A;          redo A;
1992        }        }
1993      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1994        if ($self->{next_input_character} == 0x003E) { # >        if ({
1995          $self->{state} = 'data';              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1996                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1997              }->{$self->{next_char}}) {
1998            !!!cp (201);
1999            ## Stay in the state
2000            !!!next-input-character;
2001            redo A;
2002          } elsif ($self->{next_char} == 0x0022) { # "
2003            !!!cp (202);
2004            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2005            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2006            !!!next-input-character;
2007            redo A;
2008          } elsif ($self->{next_char} == 0x0027) { # '
2009            !!!cp (203);
2010            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2011            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2012            !!!next-input-character;
2013            redo A;
2014          } elsif ($self->{next_char} == 0x003E) { # >
2015            !!!cp (204);
2016            !!!parse-error (type => 'no SYSTEM literal');
2017            $self->{state} = DATA_STATE;
2018          !!!next-input-character;          !!!next-input-character;
2019    
2020            $self->{current_token}->{quirks} = 1;
2021          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2022    
2023          redo A;          redo A;
2024        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2025            !!!cp (205);
2026          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2027          $self->{state} = 'data';  
2028            $self->{state} = DATA_STATE;
2029            ## reconsume
2030    
2031            $self->{current_token}->{quirks} = 1;
2032            !!!emit ($self->{current_token}); # DOCTYPE
2033    
2034            redo A;
2035          } else {
2036            !!!cp (206);
2037            !!!parse-error (type => 'string after SYSTEM');
2038            $self->{current_token}->{quirks} = 1;
2039    
2040            $self->{state} = BOGUS_DOCTYPE_STATE;
2041            !!!next-input-character;
2042            redo A;
2043          }
2044        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2045          if ($self->{next_char} == 0x0022) { # "
2046            !!!cp (207);
2047            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2048            !!!next-input-character;
2049            redo A;
2050          } elsif ($self->{next_char} == 0x003E) { # >
2051            !!!cp (208);
2052            !!!parse-error (type => 'unclosed PUBLIC literal');
2053    
2054            $self->{state} = DATA_STATE;
2055            !!!next-input-character;
2056    
2057            $self->{current_token}->{quirks} = 1;
2058            !!!emit ($self->{current_token}); # DOCTYPE
2059    
2060            redo A;
2061          } elsif ($self->{next_char} == -1) {
2062            !!!cp (209);
2063            !!!parse-error (type => 'unclosed SYSTEM literal');
2064    
2065            $self->{state} = DATA_STATE;
2066          ## reconsume          ## reconsume
2067    
2068            $self->{current_token}->{quirks} = 1;
2069          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2070    
2071          redo A;          redo A;
2072        } else {        } else {
2073            !!!cp (210);
2074            $self->{current_token}->{system_identifier} # DOCTYPE
2075                .= chr $self->{next_char};
2076            ## Stay in the state
2077            !!!next-input-character;
2078            redo A;
2079          }
2080        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2081          if ($self->{next_char} == 0x0027) { # '
2082            !!!cp (211);
2083            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2084            !!!next-input-character;
2085            redo A;
2086          } elsif ($self->{next_char} == 0x003E) { # >
2087            !!!cp (212);
2088            !!!parse-error (type => 'unclosed PUBLIC literal');
2089    
2090            $self->{state} = DATA_STATE;
2091            !!!next-input-character;
2092    
2093            $self->{current_token}->{quirks} = 1;
2094            !!!emit ($self->{current_token}); # DOCTYPE
2095    
2096            redo A;
2097          } elsif ($self->{next_char} == -1) {
2098            !!!cp (213);
2099            !!!parse-error (type => 'unclosed SYSTEM literal');
2100    
2101            $self->{state} = DATA_STATE;
2102            ## reconsume
2103    
2104            $self->{current_token}->{quirks} = 1;
2105            !!!emit ($self->{current_token}); # DOCTYPE
2106    
2107            redo A;
2108          } else {
2109            !!!cp (214);
2110            $self->{current_token}->{system_identifier} # DOCTYPE
2111                .= chr $self->{next_char};
2112            ## Stay in the state
2113            !!!next-input-character;
2114            redo A;
2115          }
2116        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2117          if ({
2118                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2119                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2120              }->{$self->{next_char}}) {
2121            !!!cp (215);
2122            ## Stay in the state
2123            !!!next-input-character;
2124            redo A;
2125          } elsif ($self->{next_char} == 0x003E) { # >
2126            !!!cp (216);
2127            $self->{state} = DATA_STATE;
2128            !!!next-input-character;
2129    
2130            !!!emit ($self->{current_token}); # DOCTYPE
2131    
2132            redo A;
2133          } elsif ($self->{next_char} == -1) {
2134            !!!cp (217);
2135            !!!parse-error (type => 'unclosed DOCTYPE');
2136    
2137            $self->{state} = DATA_STATE;
2138            ## reconsume
2139    
2140            $self->{current_token}->{quirks} = 1;
2141            !!!emit ($self->{current_token}); # DOCTYPE
2142    
2143            redo A;
2144          } else {
2145            !!!cp (218);
2146            !!!parse-error (type => 'string after SYSTEM literal');
2147            #$self->{current_token}->{quirks} = 1;
2148    
2149            $self->{state} = BOGUS_DOCTYPE_STATE;
2150            !!!next-input-character;
2151            redo A;
2152          }
2153        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2154          if ($self->{next_char} == 0x003E) { # >
2155            !!!cp (219);
2156            $self->{state} = DATA_STATE;
2157            !!!next-input-character;
2158    
2159            !!!emit ($self->{current_token}); # DOCTYPE
2160    
2161            redo A;
2162          } elsif ($self->{next_char} == -1) {
2163            !!!cp (220);
2164            !!!parse-error (type => 'unclosed DOCTYPE');
2165            $self->{state} = DATA_STATE;
2166            ## reconsume
2167    
2168            !!!emit ($self->{current_token}); # DOCTYPE
2169    
2170            redo A;
2171          } else {
2172            !!!cp (221);
2173          ## Stay in the state          ## Stay in the state
2174          !!!next-input-character;          !!!next-input-character;
2175          redo A;          redo A;
# Line 1495  sub _get_next_token ($) { Line 2182  sub _get_next_token ($) {
2182    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2183  } # _get_next_token  } # _get_next_token
2184    
2185  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2186    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2187      
2188    if ($self->{next_input_character} == 0x0023) { # #    if ({
2189           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2190           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2191           $additional => 1,
2192          }->{$self->{next_char}}) {
2193        ## Don't consume
2194        ## No error
2195        return undef;
2196      } elsif ($self->{next_char} == 0x0023) { # #
2197      !!!next-input-character;      !!!next-input-character;
2198      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2199          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2200        my $num;        my $code;
2201        X: {        X: {
2202          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2203          !!!next-input-character;          !!!next-input-character;
2204          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2205              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2206            $num ||= 0;            $code ||= 0;
2207            $num *= 0x10;            $code *= 0x10;
2208            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2209            redo X;            redo X;
2210          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2211                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2212            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
2213            $num ||= 0;            $code *= 0x10;
2214            $num *= 0x10;            $code += $self->{next_char} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
2215            redo X;            redo X;
2216          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2217                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2218            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
2219            $num ||= 0;            $code *= 0x10;
2220            $num *= 0x10;            $code += $self->{next_char} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
2221            redo X;            redo X;
2222          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2223            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
2224            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2225            !!!back-next-input-character ($x_char);            $self->{next_char} = 0x0023; # #
2226            return undef;            return undef;
2227          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2228            !!!next-input-character;            !!!next-input-character;
2229          } else {          } else {
2230            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
2231          }          }
2232    
2233          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2234          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2235          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
2236            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
2237            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2238          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
2239            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
2240            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
2241              $code = 0x000A;
2242            } elsif (0x80 <= $code and $code <= 0x9F) {
2243              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2244              $code = $c1_entity_char->{$code};
2245          }          }
2246    
2247          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code,
2248                    has_reference => 1};
2249        } # X        } # X
2250      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2251               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2252        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2253        !!!next-input-character;        !!!next-input-character;
2254                
2255        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2256                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2257          $code *= 10;          $code *= 10;
2258          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2259                    
2260          !!!next-input-character;          !!!next-input-character;
2261        }        }
2262    
2263        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2264          !!!next-input-character;          !!!next-input-character;
2265        } else {        } else {
2266          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
2267        }        }
2268    
2269        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2270        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2271          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
2272          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
2273            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2274            $code = 0xFFFD;
2275          } elsif ($code == 0x000D) {
2276            !!!parse-error (type => 'CR character reference');
2277            $code = 0x000A;
2278        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2279          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2280          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2281        }        }
2282                
2283        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2284      } else {      } else {
2285        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2286        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_char});
2287        $self->{next_input_character} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2288        return undef;        return undef;
2289      }      }
2290    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2291              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2292             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2293              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2294      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2295      !!!next-input-character;      !!!next-input-character;
2296    
2297      my $value = $entity_name;      my $value = $entity_name;
2298      my $match;      my $match = 0;
2299        require Whatpm::_NamedEntityList;
2300        our $EntityChar;
2301    
2302      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2303             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2304             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
2305               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
2306              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
2307               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
2308              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
2309               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
2310        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
2311        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
2312          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
2313          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
2314              $value = $EntityChar->{$entity_name};
2315              $match = 1;
2316              !!!next-input-character;
2317              last;
2318            } else {
2319              $value = $EntityChar->{$entity_name};
2320              $match = -1;
2321              !!!next-input-character;
2322            }
2323        } else {        } else {
2324          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_char};
2325            $match *= 2;
2326            !!!next-input-character;
2327        }        }
       !!!next-input-character;  
2328      }      }
2329            
2330      if ($match) {      if ($match > 0) {
2331        if ($self->{next_input_character} == 0x003B) { # ;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2332          !!!next-input-character;      } elsif ($match < 0) {
2333          !!!parse-error (type => 'no refc');
2334          if ($in_attr and $match < -1) {
2335            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2336        } else {        } else {
2337          !!!parse-error (type => 'refc');          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2338        }        }
   
       return {type => 'character', data => $value};  
2339      } else {      } else {
2340        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2341        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
2342        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
2343      }      }
2344    } else {    } else {
2345      ## no characters are consumed      ## no characters are consumed
# Line 1639  sub _initialize_tree_constructor ($) { Line 2354  sub _initialize_tree_constructor ($) {
2354    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
2355    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
2356    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
2357    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
2358  } # _initialize_tree_constructor  } # _initialize_tree_constructor
2359    
2360  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1666  sub _construct_tree ($) { Line 2381  sub _construct_tree ($) {
2381        
2382    !!!next-token;    !!!next-token;
2383    
2384    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2385    undef $self->{form_element};    undef $self->{form_element};
2386    undef $self->{head_element};    undef $self->{head_element};
2387    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1679  sub _construct_tree ($) { Line 2394  sub _construct_tree ($) {
2394    
2395  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2396    my $self = shift;    my $self = shift;
2397    B: {    INITIAL: {
2398        if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2399          if ($token->{error}) {        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2400            ## ISSUE: Spec currently left this case undefined.        ## error, switch to a conformance checking mode for another
2401            !!!parse-error (type => 'bogus DOCTYPE');        ## language.
2402          }        my $doctype_name = $token->{name};
2403          my $doctype = $self->{document}->create_document_type_definition        $doctype_name = '' unless defined $doctype_name;
2404            ($token->{name});        $doctype_name =~ tr/a-z/A-Z/;
2405          $self->{document}->append_child ($doctype);        if (not defined $token->{name} or # <!DOCTYPE>
2406          #$phase = 'root element';            defined $token->{public_identifier} or
2407          !!!next-token;            defined $token->{system_identifier}) {
2408          #redo B;          !!!parse-error (type => 'not HTML5');
2409          return;        } elsif ($doctype_name ne 'HTML') {
2410        } elsif ({          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2411                  comment => 1,          !!!parse-error (type => 'not HTML5');
2412                  'start tag' => 1,        }
2413                  'end tag' => 1,        
2414                  'end-of-file' => 1,        my $doctype = $self->{document}->create_document_type_definition
2415                 }->{$token->{type}}) {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2416          ## ISSUE: Spec currently left this case undefined.        $doctype->public_id ($token->{public_identifier})
2417          !!!parse-error (type => 'missing DOCTYPE');            if defined $token->{public_identifier};
2418          #$phase = 'root element';        $doctype->system_id ($token->{system_identifier})
2419          ## reprocess            if defined $token->{system_identifier};
2420          #redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
2421          return;        ## ISSUE: internalSubset = null??
2422        } elsif ($token->{type} eq 'character') {        $self->{document}->append_child ($doctype);
2423          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        
2424            $self->{document}->manakai_append_text ($1);        if ($token->{quirks} or $doctype_name ne 'HTML') {
2425            ## ISSUE: DOM3 Core does not allow Document > Text          $self->{document}->manakai_compat_mode ('quirks');
2426            unless (length $token->{data}) {        } elsif (defined $token->{public_identifier}) {
2427              ## Stay in the phase          my $pubid = $token->{public_identifier};
2428              !!!next-token;          $pubid =~ tr/a-z/A-z/;
2429              redo B;          if ({
2430              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
2431              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2432              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2433              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
2434              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
2435              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
2436              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
2437              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
2438              "-//IETF//DTD HTML 2.0//EN" => 1,
2439              "-//IETF//DTD HTML 2.1E//EN" => 1,
2440              "-//IETF//DTD HTML 3.0//EN" => 1,
2441              "-//IETF//DTD HTML 3.0//EN//" => 1,
2442              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
2443              "-//IETF//DTD HTML 3.2//EN" => 1,
2444              "-//IETF//DTD HTML 3//EN" => 1,
2445              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
2446              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
2447              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
2448              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
2449              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
2450              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
2451              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
2452              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
2453              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
2454              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
2455              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
2456              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
2457              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
2458              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
2459              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
2460              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
2461              "-//IETF//DTD HTML STRICT//EN" => 1,
2462              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
2463              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
2464              "-//IETF//DTD HTML//EN" => 1,
2465              "-//IETF//DTD HTML//EN//2.0" => 1,
2466              "-//IETF//DTD HTML//EN//3.0" => 1,
2467              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
2468              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
2469              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
2470              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
2471              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
2472              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
2473              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
2474              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
2475              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2476              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2477              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2478              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2479              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2480              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2481              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2482              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2483              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
2484              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
2485              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
2486              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
2487              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
2488              "-//W3C//DTD HTML 3.2//EN" => 1,
2489              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
2490              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
2491              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
2492              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
2493              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
2494              "-//W3C//DTD W3 HTML//EN" => 1,
2495              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
2496              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
2497              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
2498              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
2499              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
2500              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2501              "HTML" => 1,
2502            }->{$pubid}) {
2503              $self->{document}->manakai_compat_mode ('quirks');
2504            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2505                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2506              if (defined $token->{system_identifier}) {
2507                $self->{document}->manakai_compat_mode ('quirks');
2508              } else {
2509                $self->{document}->manakai_compat_mode ('limited quirks');
2510            }            }
2511            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
2512                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
2513              $self->{document}->manakai_compat_mode ('limited quirks');
2514            }
2515          }
2516          if (defined $token->{system_identifier}) {
2517            my $sysid = $token->{system_identifier};
2518            $sysid =~ tr/A-Z/a-z/;
2519            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2520              $self->{document}->manakai_compat_mode ('quirks');
2521            }
2522          }
2523          
2524          ## Go to the root element phase.
2525          !!!next-token;
2526          return;
2527        } elsif ({
2528                  START_TAG_TOKEN, 1,
2529                  END_TAG_TOKEN, 1,
2530                  END_OF_FILE_TOKEN, 1,
2531                 }->{$token->{type}}) {
2532          !!!parse-error (type => 'no DOCTYPE');
2533          $self->{document}->manakai_compat_mode ('quirks');
2534          ## Go to the root element phase
2535          ## reprocess
2536          return;
2537        } elsif ($token->{type} == CHARACTER_TOKEN) {
2538          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2539            ## Ignore the token
2540    
2541            unless (length $token->{data}) {
2542              ## Stay in the phase
2543              !!!next-token;
2544              redo INITIAL;
2545          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
2546        }        }
2547      } # B  
2548          !!!parse-error (type => 'no DOCTYPE');
2549          $self->{document}->manakai_compat_mode ('quirks');
2550          ## Go to the root element phase
2551          ## reprocess
2552          return;
2553        } elsif ($token->{type} == COMMENT_TOKEN) {
2554          my $comment = $self->{document}->create_comment ($token->{data});
2555          $self->{document}->append_child ($comment);
2556          
2557          ## Stay in the phase.
2558          !!!next-token;
2559          redo INITIAL;
2560        } else {
2561          die "$0: $token->{type}: Unknown token type";
2562        }
2563      } # INITIAL
2564  } # _tree_construction_initial  } # _tree_construction_initial
2565    
2566  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2567    my $self = shift;    my $self = shift;
2568        
2569    B: {    B: {
2570        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2571          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2572          ## Ignore the token          ## Ignore the token
2573          ## Stay in the phase          ## Stay in the phase
2574          !!!next-token;          !!!next-token;
2575          redo B;          redo B;
2576        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2577          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2578          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2579          ## Stay in the phase          ## Stay in the phase
2580          !!!next-token;          !!!next-token;
2581          redo B;          redo B;
2582        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2583          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2584            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2585            ## ISSUE: DOM3 Core does not allow Document > Text  
2586            unless (length $token->{data}) {            unless (length $token->{data}) {
2587              ## Stay in the phase              ## Stay in the phase
2588              !!!next-token;              !!!next-token;
2589              redo B;              redo B;
2590            }            }
2591          }          }
2592    
2593            $self->{application_cache_selection}->(undef);
2594    
2595            #
2596          } elsif ($token->{type} == START_TAG_TOKEN) {
2597            if ($token->{tag_name} eq 'html' and
2598                $token->{attributes}->{manifest}) {
2599              $self->{application_cache_selection}
2600                   ->($token->{attributes}->{manifest}->{value});
2601              ## ISSUE: No relative reference resolution?
2602            } else {
2603              $self->{application_cache_selection}->(undef);
2604            }
2605    
2606            ## ISSUE: There is an issue in the spec
2607          #          #
2608        } elsif ({        } elsif ({
2609                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2610                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2611                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2612            $self->{application_cache_selection}->(undef);
2613    
2614          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2615          #          #
2616        } else {        } else {
2617          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2618        }        }
2619    
2620        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2621        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2622        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2623        ## reprocess        ## reprocess
2624        #redo B;        #redo B;
2625        return;        return; ## Go to the main phase.
2626    } # B    } # B
2627  } # _tree_construction_root_element  } # _tree_construction_root_element
2628    
# Line 1785  sub _reset_insertion_mode ($) { Line 2638  sub _reset_insertion_mode ($) {
2638            
2639      ## Step 3      ## Step 3
2640      S3: {      S3: {
2641        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2642        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2643          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2644              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2645            #        ## The second "if" is in the scope of the first "if"!?
2646          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2647            $node = $self->{inner_html_node};          $last = 1;
2648            if (defined $self->{inner_html_node}) {
2649              if ($self->{inner_html_node}->[1] eq 'td' or
2650                  $self->{inner_html_node}->[1] eq 'th') {
2651                #
2652              } else {
2653                $node = $self->{inner_html_node};
2654              }
2655          }          }
2656        }        }
2657            
2658        ## Step 4..13        ## Step 4..13
2659        my $new_mode = {        my $new_mode = {
2660                        select => 'in select',                        select => IN_SELECT_IM,
2661                        td => 'in cell',                        td => IN_CELL_IM,
2662                        th => 'in cell',                        th => IN_CELL_IM,
2663                        tr => 'in row',                        tr => IN_ROW_IM,
2664                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2665                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2666                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2667                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2668                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2669                        table => 'in table',                        table => IN_TABLE_IM,
2670                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2671                        body => 'in body',                        body => IN_BODY_IM,
2672                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2673                       }->{$node->[1]};                       }->{$node->[1]};
2674        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2675                
2676        ## Step 14        ## Step 14
2677        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2678          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2679            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2680          } else {          } else {
2681            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2682          }          }
2683          return;          return;
2684        }        }
2685                
2686        ## Step 15        ## Step 15
2687        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2688                
2689        ## Step 16        ## Step 16
2690        $i--;        $i--;
# Line 1838  sub _reset_insertion_mode ($) { Line 2698  sub _reset_insertion_mode ($) {
2698  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2699    my $self = shift;    my $self = shift;
2700    
   my $phase = 'main';  
   
2701    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2702    
2703    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1922  sub _tree_construction_main ($) { Line 2780  sub _tree_construction_main ($) {
2780      }      }
2781    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2782    
2783    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2784      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2785      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2786      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2787       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2788        ->append_child ($style_el);      my $el;
2789      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2790                  
2791        ## Step 2
2792        $insert->($el); # /context node/->append_child ($el)
2793    
2794        ## Step 3
2795        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2796        delete $self->{escape}; # MUST
2797    
2798        ## Step 4
2799      my $text = '';      my $text = '';
2800      !!!next-token;      !!!next-token;
2801      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2802        $text .= $token->{data};        $text .= $token->{data};
2803        !!!next-token;        !!!next-token;
2804      } # stop if non-character token or tokenizer stops tokenising      }
2805    
2806        ## Step 5
2807      if (length $text) {      if (length $text) {
2808        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2809          $el->append_child ($text);
2810      }      }
2811        
2812      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2813                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2814      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2815        ## Step 7
2816        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2817        ## Ignore the token        ## Ignore the token
2818      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2819        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2820        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2821          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2822        } else {
2823          die "$0: $content_model_flag in parse_rcdata";
2824      }      }
2825      !!!next-token;      !!!next-token;
2826    }; # $style_start_tag    }; # $parse_rcdata
2827    
2828    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2829        my $insert = $_[0];
2830      my $script_el;      my $script_el;
2831      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2832      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2833    
2834      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2835        delete $self->{escape}; # MUST
2836            
2837      my $text = '';      my $text = '';
2838      !!!next-token;      !!!next-token;
2839      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2840        $text .= $token->{data};        $text .= $token->{data};
2841        !!!next-token;        !!!next-token;
2842      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 1968  sub _tree_construction_main ($) { Line 2844  sub _tree_construction_main ($) {
2844        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2845      }      }
2846                                
2847      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2848    
2849      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2850          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2851        ## Ignore the token        ## Ignore the token
2852      } else {      } else {
# Line 1984  sub _tree_construction_main ($) { Line 2860  sub _tree_construction_main ($) {
2860      } else {      } else {
2861        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2862        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2863          
2864        (($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);  
2865                
2866        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2867                
# Line 2180  sub _tree_construction_main ($) { Line 3055  sub _tree_construction_main ($) {
3055    }; # $formatting_end_tag    }; # $formatting_end_tag
3056    
3057    my $insert_to_current = sub {    my $insert_to_current = sub {
3058      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3059    }; # $insert_to_current    }; # $insert_to_current
3060    
3061    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2214  sub _tree_construction_main ($) { Line 3089  sub _tree_construction_main ($) {
3089                         }                         }
3090    }; # $insert_to_foster    }; # $insert_to_foster
3091    
3092    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  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
         }  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!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,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           ## TODO: ignore first LF <http://html5.org/tools/web-apps-tracker?from=866&to=867>  
           $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') { ## TODO: This is incorrect maybe  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=866&to=867>  
             !!!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});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## 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  
3093    
3094    B: {    B: {
3095      if ($phase eq 'main') {      if ($token->{type} == DOCTYPE_TOKEN) {
3096        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
3097          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
3098          ## Ignore the token        ## Stay in the phase
3099          ## Stay in the phase        !!!next-token;
3100          !!!next-token;        redo B;
3101          redo B;      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3102        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3103                 $token->{tag_name} eq 'html') {          #
3104          ## TODO: unless it is the first start tag token, parse-error        } else {
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
3105          ## Generate implied end tags          ## Generate implied end tags
3106          if ({          if ({
3107               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,
3108                 tbody => 1, tfoot=> 1, thead => 1,
3109              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
3110            !!!back-token;            !!!back-token;
3111            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
3112            redo B;            redo B;
3113          }          }
3114                    
# Line 3006  sub _tree_construction_main ($) { Line 3121  sub _tree_construction_main ($) {
3121            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3122          }          }
3123    
         ## Stop parsing  
         last B;  
   
3124          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
3125          }
3126    
3127          ## Stop parsing
3128          last B;
3129        } elsif ($token->{type} == START_TAG_TOKEN and
3130                 $token->{tag_name} eq 'html') {
3131          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3132            ## Turn into the main phase
3133            !!!parse-error (type => 'after html:html');
3134            $self->{insertion_mode} = AFTER_BODY_IM;
3135          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3136            ## Turn into the main phase
3137            !!!parse-error (type => 'after html:html');
3138            $self->{insertion_mode} = AFTER_FRAMESET_IM;
3139          }
3140    
3141    ## ISSUE: "aa<html>" is not a parse error.
3142    ## ISSUE: "<html>" in fragment is not a parse error.
3143          unless ($token->{first_start_tag}) {
3144            !!!parse-error (type => 'not first start tag');
3145          }
3146          my $top_el = $self->{open_elements}->[0]->[0];
3147          for my $attr_name (keys %{$token->{attributes}}) {
3148            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3149              $top_el->set_attribute_ns
3150                (undef, [undef, $attr_name],
3151                 $token->{attributes}->{$attr_name}->{value});
3152            }
3153          }
3154          !!!next-token;
3155          redo B;
3156        } elsif ($token->{type} == COMMENT_TOKEN) {
3157          my $comment = $self->{document}->create_comment ($token->{data});
3158          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3159            $self->{document}->append_child ($comment);
3160          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3161            $self->{open_elements}->[0]->[0]->append_child ($comment);
3162        } else {        } else {
3163          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3164            if ($token->{type} eq 'character') {        }
3165              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        !!!next-token;
3166                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);        redo B;
3167                unless (length $token->{data}) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3168                  !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
3169                  redo B;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3170                }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3171              }            unless (length $token->{data}) {
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
3172              !!!next-token;              !!!next-token;
3173              redo B;              redo B;
3174            } elsif ($token->{type} eq 'start tag') {            }
3175              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};          }
3176              !!!create-element ($self->{head_element}, 'head', $attr);  
3177              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3178              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            ## As if <head>
3179              $self->{insertion_mode} = 'in head';            !!!create-element ($self->{head_element}, 'head');
3180              if ($token->{tag_name} eq 'head') {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3181                !!!next-token;            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3182              #} elsif ({  
3183              #          base => 1, link => 1, meta => 1,            ## Reprocess in the "in head" insertion mode...
3184              #          script => 1, style => 1, title => 1,            pop @{$self->{open_elements}};
3185              #         }->{$token->{tag_name}}) {  
3186              #  ## reprocess            ## Reprocess in the "after head" insertion mode...
3187              } else {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3188                ## reprocess            ## As if </noscript>
3189              }            pop @{$self->{open_elements}};
3190              !!!parse-error (type => 'in noscript:#character');
3191              
3192              ## Reprocess in the "in head" insertion mode...
3193              ## As if </head>
3194              pop @{$self->{open_elements}};
3195    
3196              ## Reprocess in the "after head" insertion mode...
3197            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3198              pop @{$self->{open_elements}};
3199    
3200              ## Reprocess in the "after head" insertion mode...
3201            }
3202    
3203                ## "after head" insertion mode
3204                ## As if <body>
3205                !!!insert-element ('body');
3206                $self->{insertion_mode} = IN_BODY_IM;
3207                ## reprocess
3208              redo B;              redo B;
3209            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3210              if ($token->{tag_name} eq 'html') {              if ($token->{tag_name} eq 'head') {
3211                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3212                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3213                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3214                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3215                    $self->{insertion_mode} = IN_HEAD_IM;
3216                    !!!next-token;
3217                    redo B;
3218                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3219                    #
3220                  } else {
3221                    !!!parse-error (type => 'in head:head'); # or in head noscript
3222                    ## Ignore the token
3223                    !!!next-token;
3224                    redo B;
3225                  }
3226                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3227                ## As if <head>                ## As if <head>
3228                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3229                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3230                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3231                $self->{insertion_mode} = 'in head';  
3232                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
3233                redo B;                ## Reprocess in the "in head" insertion mode...
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
3234              }              }
3235            } else {  
3236              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
3237            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3238          } elsif ($self->{insertion_mode} eq 'in head') {                  ## As if </noscript>
3239            if ($token->{type} eq 'character') {                  pop @{$self->{open_elements}};
3240              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!parse-error (type => 'in noscript:base');
3241                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                
3242                unless (length $token->{data}) {                  $self->{insertion_mode} = IN_HEAD_IM;
3243                  !!!next-token;                  ## Reprocess in the "in head" insertion mode...
                 redo B;  
3244                }                }
             }  
               
             #  
           } 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';  
3245    
3246                my $text = '';                ## NOTE: There is a "as if in head" code clone.
3247                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3248                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3249                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3250                  }
3251                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3252                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3253                  pop @{$self->{open_elements}}
3254                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3255                !!!next-token;                !!!next-token;
3256                while ($token->{type} eq 'character') {                redo B;
3257                  $text .= $token->{data};              } elsif ($token->{tag_name} eq 'link') {
3258                  !!!next-token;                ## NOTE: There is a "as if in head" code clone.
3259                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3260                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3261                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3262                }                }
3263                if (length $text) {                !!!insert-element ($token->{tag_name}, $token->{attributes});
3264                  $title_el->manakai_append_text ($text);                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3265                  pop @{$self->{open_elements}}
3266                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3267                  !!!next-token;
3268                  redo B;
3269                } elsif ($token->{tag_name} eq 'meta') {
3270                  ## NOTE: There is a "as if in head" code clone.
3271                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3272                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3273                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3274                }                }
3275                                !!!insert-element ($token->{tag_name}, $token->{attributes});
3276                $self->{content_model_flag} = 'PCDATA';                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3277                  
3278                if ($token->{type} eq 'end tag' and                unless ($self->{confident}) {
3279                    $token->{tag_name} eq 'title') {                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3280                  ## Ignore the token                    $self->{change_encoding}
3281                          ->($self, $token->{attributes}->{charset}->{value});
3282                      
3283                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3284                          ->set_user_data (manakai_has_reference =>
3285                                               $token->{attributes}->{charset}
3286                                                   ->{has_reference});
3287                    } elsif ($token->{attributes}->{content}) {
3288                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3289                      if ($token->{attributes}->{content}->{value}
3290                          =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3291                              [\x09-\x0D\x20]*=
3292                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3293                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3294                        $self->{change_encoding}
3295                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3296                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3297                            ->set_user_data (manakai_has_reference =>
3298                                                 $token->{attributes}->{content}
3299                                                       ->{has_reference});
3300                      }
3301                    }
3302                } else {                } else {
3303                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  if ($token->{attributes}->{charset}) {
3304                  ## ISSUE: And ignore?                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3305                          ->set_user_data (manakai_has_reference =>
3306                                               $token->{attributes}->{charset}
3307                                                   ->{has_reference});
3308                    }
3309                    if ($token->{attributes}->{content}) {
3310                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3311                          ->set_user_data (manakai_has_reference =>
3312                                               $token->{attributes}->{content}
3313                                                   ->{has_reference});
3314                    }
3315                }                }
               !!!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);  
3316    
3317                  pop @{$self->{open_elements}}
3318                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3319                !!!next-token;                !!!next-token;
3320                redo B;                redo B;
3321              } elsif ($token->{tag_name} eq 'head') {              } elsif ($token->{tag_name} eq 'title') {
3322                !!!parse-error (type => 'in head:head');                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3323                ## Ignore the token                  ## As if </noscript>
3324                !!!next-token;                  pop @{$self->{open_elements}};
3325                    !!!parse-error (type => 'in noscript:title');
3326                  
3327                    $self->{insertion_mode} = IN_HEAD_IM;
3328                    ## Reprocess in the "in head" insertion mode...
3329                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3330                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3331                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3332                  }
3333    
3334                  ## NOTE: There is a "as if in head" code clone.
3335                  my $parent = defined $self->{head_element} ? $self->{head_element}
3336                      : $self->{open_elements}->[-1]->[0];
3337                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
3338                                  sub { $parent->append_child ($_[0]) });
3339                  pop @{$self->{open_elements}}
3340                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3341                redo B;                redo B;
3342              } else {              } elsif ($token->{tag_name} eq 'style') {
3343                #                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3344              }                ## insertion mode IN_HEAD_IM)
3345            } elsif ($token->{type} eq 'end tag') {                ## NOTE: There is a "as if in head" code clone.
3346              if ($token->{tag_name} eq 'head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3347                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3348                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3349                  }
3350                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3351                  pop @{$self->{open_elements}}
3352                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3353                  redo B;
3354                } elsif ($token->{tag_name} eq 'noscript') {
3355                  if ($self->{insertion_mode} == IN_HEAD_IM) {
3356                    ## NOTE: and scripting is disalbed
3357                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3358                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3359                    !!!next-token;
3360                    redo B;
3361                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3362                    !!!parse-error (type => 'in noscript:noscript');
3363                    ## Ignore the token
3364                    !!!next-token;
3365                    redo B;
3366                  } else {
3367                    #
3368                  }
3369                } elsif ($token->{tag_name} eq 'script') {
3370                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3371                    ## As if </noscript>
3372                    pop @{$self->{open_elements}};
3373                    !!!parse-error (type => 'in noscript:script');
3374                  
3375                    $self->{insertion_mode} = IN_HEAD_IM;
3376                    ## Reprocess in the "in head" insertion mode...
3377                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3378                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3379                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3380                  }
3381    
3382                  ## NOTE: There is a "as if in head" code clone.
3383                  $script_start_tag->($insert_to_current);
3384                  pop @{$self->{open_elements}}
3385                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3386                  redo B;
3387                } elsif ($token->{tag_name} eq 'body' or
3388                         $token->{tag_name} eq 'frameset') {
3389                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3390                    ## As if </noscript>
3391                    pop @{$self->{open_elements}};
3392                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3393                    
3394                    ## Reprocess in the "in head" insertion mode...
3395                    ## As if </head>
3396                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3397                    
3398                    ## Reprocess in the "after head" insertion mode...
3399                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3400                    pop @{$self->{open_elements}};
3401                    
3402                    ## Reprocess in the "after head" insertion mode...
3403                  }
3404    
3405                  ## "after head" insertion mode
3406                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3407                  if ($token->{tag_name} eq 'body') {
3408                    $self->{insertion_mode} = IN_BODY_IM;
3409                  } elsif ($token->{tag_name} eq 'frameset') {
3410                    $self->{insertion_mode} = IN_FRAMESET_IM;
3411                } else {                } else {
3412                  !!!parse-error (type => 'unmatched end tag:head');                  die "$0: tag name: $self->{tag_name}";
3413                }                }
               $self->{insertion_mode} = 'after head';  
3414                !!!next-token;                !!!next-token;
3415                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'html') {  
               #  
3416              } else {              } else {
3417                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                #
               ## Ignore the token  
               !!!next-token;  
               redo B;  
3418              }              }
           } else {  
             #  
           }  
3419    
3420            if ($self->{open_elements}->[-1]->[1] eq 'head') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3421              ## As if </head>                ## As if </noscript>
3422              pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3423            }                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3424            $self->{insertion_mode} = 'after head';                
3425            ## reprocess                ## Reprocess in the "in head" insertion mode...
3426            redo B;                ## As if </head>
3427                  pop @{$self->{open_elements}};
3428    
3429            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
3430          } elsif ($self->{insertion_mode} eq 'after head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3431            if ($token->{type} eq 'character') {                ## As if </head>
3432              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                pop @{$self->{open_elements}};
3433                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
3434                unless (length $token->{data}) {                ## Reprocess in the "after head" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
3435              }              }
               
             #  
           } 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 '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';  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       base => 1, link => 1, meta => 1,  
                       script => 1, style => 1, title => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'after head:'.$token->{tag_name});  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
3436    
3437              !!!next-token;              ## "after head" insertion mode
3438              redo B;              ## As if <body>
3439            } elsif ($token->{type} eq 'comment') {              !!!insert-element ('body');
3440              ## NOTE: There is a code clone of "comment in body".              $self->{insertion_mode} = IN_BODY_IM;
3441              my $comment = $self->{document}->create_comment ($token->{data});              ## reprocess
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
3442              redo B;              redo B;
3443            }            } elsif ($token->{type} == END_TAG_TOKEN) {
3444          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3445            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3446              ## NOTE: There are "character in table" code clones.                  ## As if <head>
3447              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
3448                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3449                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3450                unless (length $token->{data}) {  
3451                    ## Reprocess in the "in head" insertion mode...
3452                    pop @{$self->{open_elements}};
3453                    $self->{insertion_mode} = AFTER_HEAD_IM;
3454                    !!!next-token;
3455                    redo B;
3456                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3457                    ## As if </noscript>
3458                    pop @{$self->{open_elements}};
3459                    !!!parse-error (type => 'in noscript:script');
3460                    
3461                    ## Reprocess in the "in head" insertion mode...
3462                    pop @{$self->{open_elements}};
3463                    $self->{insertion_mode} = AFTER_HEAD_IM;
3464                    !!!next-token;
3465                    redo B;
3466                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3467                    pop @{$self->{open_elements}};
3468                    $self->{insertion_mode} = AFTER_HEAD_IM;
3469                  !!!next-token;                  !!!next-token;
3470                  redo B;                  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,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
3471                } else {                } else {
3472                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
3473                }                }
3474              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3475                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
             }  
               
             !!!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 ({  
                  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]);  
3476                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3477                    $self->{insertion_mode} = IN_HEAD_IM;
3478                    !!!next-token;
3479                    redo B;
3480                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3481                    !!!parse-error (type => 'unmatched end tag:noscript');
3482                    ## Ignore the token ## ISSUE: An issue in the spec.
3483                    !!!next-token;
3484                    redo B;
3485                  } else {
3486                    #
3487                }                }
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!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}};  
               !!!next-token;  
               redo B;  
3488              } elsif ({              } elsif ({
3489                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3490                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3491                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3492                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
3493                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
3494                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3495                  pop @{$self->{open_elements}};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3496                }  
3497                    $self->{insertion_mode} = IN_HEAD_IM;
3498                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
3499                $self->{insertion_mode} = $token->{tag_name} eq 'col'                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3500                  ? 'in column group' : 'in table body';                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3501                ## reprocess                  ## Ignore the token
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
3502                  !!!next-token;                  !!!next-token;
3503                  redo B;                  redo B;
3504                }                }
3505                                
3506                ## generate implied end tags                #
3507                if ({              } elsif ({
3508                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3509                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3510                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3511                  !!!back-token; # <table>                  ## As if <head>
3512                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3513                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3514                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3515    
3516                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3517                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3518                }                }
3519    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3520                #                #
3521              }              } else {
3522            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3523              if ($token->{tag_name} eq 'table') {                  #
3524                ## have a table element in table scope                } else {
               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) {  
3525                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3526                  ## Ignore the token                  ## Ignore the token
3527                  !!!next-token;                  !!!next-token;
3528                  redo B;                  redo B;
3529                }                }
3530                              }
               ## 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 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
3531    
3532                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3533                  ## As if </noscript>
3534                  pop @{$self->{open_elements}};
3535                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3536                  
3537                  ## Reprocess in the "in head" insertion mode...
3538                  ## As if </head>
3539                  pop @{$self->{open_elements}};
3540    
3541                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3542                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3543                  ## As if </head>
3544                  pop @{$self->{open_elements}};
3545    
3546                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3547                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
             } elsif ({  
                       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}}) {  
3548                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3549                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3550                !!!next-token;                !!!next-token;
3551                redo B;                redo B;
             } else {  
               #  
3552              }              }
3553    
3554                ## "after head" insertion mode
3555                ## As if <body>
3556                !!!insert-element ('body');
3557                $self->{insertion_mode} = IN_BODY_IM;
3558                ## reprocess
3559                redo B;
3560            } else {            } else {
3561              #              die "$0: $token->{type}: Unknown token type";
3562            }            }
3563    
3564            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3565            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
3566            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3567          } elsif ($self->{insertion_mode} eq 'in caption') {              ## NOTE: There is a code clone of "character in body".
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
3568              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3569                            
3570              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3571    
3572              !!!next-token;              !!!next-token;
3573              redo B;              redo B;
3574            } 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') {  
3575              if ({              if ({
3576                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3577                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3578                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3579                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3580                    ## have an element in table scope
3581                    my $tn;
3582                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3583                      my $node = $self->{open_elements}->[$_];
3584                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3585                        $tn = $node->[1];
3586                        last INSCOPE;
3587                      } elsif ({
3588                                table => 1, html => 1,
3589                               }->{$node->[1]}) {
3590                        last INSCOPE;
3591                      }
3592                    } # INSCOPE
3593                      unless (defined $tn) {
3594                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3595                        ## Ignore the token
3596                        !!!next-token;
3597                        redo B;
3598                      }
3599                    
3600                    ## Close the cell
3601                    !!!back-token; # <?>
3602                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3603                    redo B;
3604                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3605                    !!!parse-error (type => 'not closed:caption');
3606                    
3607                    ## As if </caption>
3608                    ## have a table element in table scope
3609                    my $i;
3610                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3611                      my $node = $self->{open_elements}->[$_];
3612                      if ($node->[1] eq 'caption') {
3613                        $i = $_;
3614                        last INSCOPE;
3615                      } elsif ({
3616                                table => 1, html => 1,
3617                               }->{$node->[1]}) {
3618                        last INSCOPE;
3619                      }
3620                    } # INSCOPE
3621                      unless (defined $i) {
3622                        !!!parse-error (type => 'unmatched end tag:caption');
3623                        ## Ignore the token
3624                        !!!next-token;
3625                        redo B;
3626                      }
3627                    
3628                    ## generate implied end tags
3629                    if ({
3630                         dd => 1, dt => 1, li => 1, p => 1,
3631                         td => 1, th => 1, tr => 1,
3632                         tbody => 1, tfoot=> 1, thead => 1,
3633                        }->{$self->{open_elements}->[-1]->[1]}) {
3634                      !!!back-token; # <?>
3635                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3636                      !!!back-token;
3637                      $token = {type => END_TAG_TOKEN,
3638                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3639                      redo B;
3640                    }
3641    
3642                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3643                ## have a table element in table scope                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3644                  }                  }
3645                } # INSCOPE                  
3646                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3647                  !!!parse-error (type => 'unmatched end tag:caption');                  
3648                    $clear_up_to_marker->();
3649                    
3650                    $self->{insertion_mode} = IN_TABLE_IM;
3651                    
3652                    ## reprocess
3653                    redo B;
3654                  } else {
3655                    #
3656                  }
3657                } else {
3658                  #
3659                }
3660              } elsif ($token->{type} == END_TAG_TOKEN) {
3661                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3662                  if ($self->{insertion_mode} == IN_CELL_IM) {
3663                    ## have an element in table scope
3664                    my $i;
3665                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3666                      my $node = $self->{open_elements}->[$_];
3667                      if ($node->[1] eq $token->{tag_name}) {
3668                        $i = $_;
3669                        last INSCOPE;
3670                      } elsif ({
3671                                table => 1, html => 1,
3672                               }->{$node->[1]}) {
3673                        last INSCOPE;
3674                      }
3675                    } # INSCOPE
3676                      unless (defined $i) {
3677                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3678                        ## Ignore the token
3679                        !!!next-token;
3680                        redo B;
3681                      }
3682                    
3683                    ## generate implied end tags
3684                    if ({
3685                         dd => 1, dt => 1, li => 1, p => 1,
3686                         td => ($token->{tag_name} eq 'th'),
3687                         th => ($token->{tag_name} eq 'td'),
3688                         tr => 1,
3689                         tbody => 1, tfoot=> 1, thead => 1,
3690                        }->{$self->{open_elements}->[-1]->[1]}) {
3691                      !!!back-token;
3692                      $token = {type => END_TAG_TOKEN,
3693                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3694                      redo B;
3695                    }
3696                    
3697                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3698                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3699                    }
3700                    
3701                    splice @{$self->{open_elements}}, $i;
3702                    
3703                    $clear_up_to_marker->();
3704                    
3705                    $self->{insertion_mode} = IN_ROW_IM;
3706                    
3707                    !!!next-token;
3708                    redo B;
3709                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3710                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3711                  ## Ignore the token                  ## Ignore the token
3712                  !!!next-token;                  !!!next-token;
3713                  redo B;                  redo B;
3714                  } else {
3715                    #
3716                }                }
3717                              } elsif ($token->{tag_name} eq 'caption') {
3718                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3719                if ({                  ## have a table element in table scope
3720                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3721                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3722                    }->{$self->{open_elements}->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3723                  !!!back-token; # <?>                    if ($node->[1] eq $token->{tag_name}) {
3724                  $token = {type => 'end tag', tag_name => 'caption'};                      $i = $_;
3725                  !!!back-token;                      last INSCOPE;
3726                  $token = {type => 'end tag',                    } elsif ({
3727                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              table => 1, html => 1,
3728                               }->{$node->[1]}) {
3729                        last INSCOPE;
3730                      }
3731                    } # INSCOPE
3732                      unless (defined $i) {
3733                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3734                        ## Ignore the token
3735                        !!!next-token;
3736                        redo B;
3737                      }
3738                    
3739                    ## generate implied end tags
3740                    if ({
3741                         dd => 1, dt => 1, li => 1, p => 1,
3742                         td => 1, th => 1, tr => 1,
3743                         tbody => 1, tfoot=> 1, thead => 1,
3744                        }->{$self->{open_elements}->[-1]->[1]}) {
3745                      !!!back-token;
3746                      $token = {type => END_TAG_TOKEN,
3747                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3748                      redo B;
3749                    }
3750                    
3751                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3752                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3753                    }
3754                    
3755                    splice @{$self->{open_elements}}, $i;
3756                    
3757                    $clear_up_to_marker->();
3758                    
3759                    $self->{insertion_mode} = IN_TABLE_IM;
3760                    
3761                    !!!next-token;
3762                  redo B;                  redo B;
3763                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3764                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3765                    ## Ignore the token
3766                    !!!next-token;
3767                    redo B;
3768                  } else {
3769                    #
3770                }                }
3771                } elsif ({
3772                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3773                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3774                }                       }->{$token->{tag_name}} and
3775                         $self->{insertion_mode} == IN_CELL_IM) {
3776                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  
3777                my $i;                my $i;
3778                  my $tn;
3779                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3780                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3781                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3782                    $i = $_;                    $i = $_;
3783                    last INSCOPE;                    last INSCOPE;
3784                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3785                      $tn = $node->[1];
3786                      ## NOTE: There is exactly one |td| or |th| element
3787                      ## in scope in the stack of open elements by definition.
3788                  } elsif ({                  } elsif ({
3789                            table => 1, html => 1,                            table => 1, html => 1,
3790                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3544  sub _tree_construction_main ($) { Line 3797  sub _tree_construction_main ($) {
3797                  !!!next-token;                  !!!next-token;
3798                  redo B;                  redo B;
3799                }                }
                 
               ## 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->();  
3800    
3801                $self->{insertion_mode} = 'in table';                ## Close the cell
3802                  !!!back-token; # </?>
3803                !!!next-token;                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3804                redo B;                redo B;
3805              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3806                         $self->{insertion_mode} == IN_CAPTION_IM) {
3807                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3808    
3809                ## As if </caption>                ## As if </caption>
# Line 3596  sub _tree_construction_main ($) { Line 3831  sub _tree_construction_main ($) {
3831                if ({                if ({
3832                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3833                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3834                       tbody => 1, tfoot=> 1, thead => 1,
3835                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3836                  !!!back-token; # </table>                  !!!back-token; # </table>
3837                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3838                  !!!back-token;                  !!!back-token;
3839                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3840                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3841                  redo B;                  redo B;
3842                }                }
# Line 3613  sub _tree_construction_main ($) { Line 3849  sub _tree_construction_main ($) {
3849    
3850                $clear_up_to_marker->();                $clear_up_to_marker->();
3851    
3852                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3853    
3854                ## reprocess                ## reprocess
3855                redo B;                redo B;
3856              } elsif ({              } elsif ({
3857                        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,  
3858                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3859                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3860                ## Ignore the token                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
               redo B;  
             } 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');  
3861                  ## Ignore the token                  ## Ignore the token
3862                  !!!next-token;                  !!!next-token;
3863                  redo B;                  redo B;
3864                } else {                } else {
3865                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3866                }                }
3867              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3868                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3869                          thead => 1, tr => 1,
3870                         }->{$token->{tag_name}} and
3871                         $self->{insertion_mode} == IN_CAPTION_IM) {
3872                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3873                ## Ignore the token                ## Ignore the token
3874                !!!next-token;                !!!next-token;
3875                redo B;                redo B;
3876              } else {              } else {
3877                #                #
3878              }              }
3879            } else {        } else {
3880              #          die "$0: $token->{type}: Unknown token type";
3881            }        }
3882    
3883            ## As if </colgroup>        $insert = $insert_to_current;
3884            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3885              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3886              ## 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.  
3887              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3888                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3889                                
# Line 3715  sub _tree_construction_main ($) { Line 3900  sub _tree_construction_main ($) {
3900              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3901              ## result in a new Text node.              ## result in a new Text node.
3902              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3903                
3904              if ({              if ({
3905                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3906                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3755  sub _tree_construction_main ($) { Line 3940  sub _tree_construction_main ($) {
3940                            
3941              !!!next-token;              !!!next-token;
3942              redo B;              redo B;
3943            } 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') {  
3944              if ({              if ({
3945                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3946                   th => 1, td => 1,                   th => 1, td => 1,
3947                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3948                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3949                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3950                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3951                           $self->{open_elements}->[-1]->[1] ne 'html') {
3952                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3953                      pop @{$self->{open_elements}};
3954                    }
3955                    
3956                    !!!insert-element ('tbody');
3957                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3958                    ## reprocess in the "in table body" insertion mode...
3959                }                }
3960    
3961                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3962                    unless ($token->{tag_name} eq 'tr') {
3963                      !!!parse-error (type => 'missing start tag:tr');
3964                    }
3965                    
3966                    ## Clear back to table body context
3967                    while (not {
3968                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3969                    }->{$self->{open_elements}->[-1]->[1]}) {
3970                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3971                      pop @{$self->{open_elements}};
3972                    }
3973                    
3974                    $self->{insertion_mode} = IN_ROW_IM;
3975                    if ($token->{tag_name} eq 'tr') {
3976                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3977                      !!!next-token;
3978                      redo B;
3979                    } else {
3980                      !!!insert-element ('tr');
3981                      ## reprocess in the "in row" insertion mode
3982                    }
3983                  }
3984    
3985                  ## Clear back to table row context
3986                while (not {                while (not {
3987                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3988                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3989                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3990                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3991                }                }
3992                                
3993                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3994                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
3995                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3996                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3997                } else {                
3998                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3999                redo B;                redo B;
4000              } elsif ({              } elsif ({
4001                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4002                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4003                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4004                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4005                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
4006                my $i;                  ## As if </tr>
4007                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4008                  my $node = $self->{open_elements}->[$_];                  my $i;
4009                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4010                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4011                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4012                    $i = $_;                      $i = $_;
4013                    last INSCOPE;                      last INSCOPE;
4014                  } elsif ({                    } elsif ({
4015                            table => 1, html => 1,                              table => 1, html => 1,
4016                           }->{$node->[1]}) {                             }->{$node->[1]}) {
4017                    last INSCOPE;                      last INSCOPE;
4018                      }
4019                    } # INSCOPE
4020                    unless (defined $i) {
4021                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4022                      ## Ignore the token
4023                      !!!next-token;
4024                      redo B;
4025                    }
4026                    
4027                    ## Clear back to table row context
4028                    while (not {
4029                      tr => 1, html => 1,
4030                    }->{$self->{open_elements}->[-1]->[1]}) {
4031                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4032                      pop @{$self->{open_elements}};
4033                    }
4034                    
4035                    pop @{$self->{open_elements}}; # tr
4036                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4037                    if ($token->{tag_name} eq 'tr') {
4038                      ## reprocess
4039                      redo B;
4040                    } else {
4041                      ## reprocess in the "in table body" insertion mode...
4042                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4043                }                }
4044    
4045                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4046                while (not {                  ## have an element in table scope
4047                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4048                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4049                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4050                      if ({
4051                           tbody => 1, thead => 1, tfoot => 1,
4052                          }->{$node->[1]}) {
4053                        $i = $_;
4054                        last INSCOPE;
4055                      } elsif ({
4056                                table => 1, html => 1,
4057                               }->{$node->[1]}) {
4058                        last INSCOPE;
4059                      }
4060                    } # INSCOPE
4061                    unless (defined $i) {
4062                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4063                      ## Ignore the token
4064                      !!!next-token;
4065                      redo B;
4066                    }
4067    
4068                    ## Clear back to table body context
4069                    while (not {
4070                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4071                    }->{$self->{open_elements}->[-1]->[1]}) {
4072                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4073                      pop @{$self->{open_elements}};
4074                    }
4075                    
4076                    ## As if <{current node}>
4077                    ## have an element in table scope
4078                    ## true by definition
4079                    
4080                    ## Clear back to table body context
4081                    ## nop by definition
4082                    
4083                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4084                    $self->{insertion_mode} = IN_TABLE_IM;
4085                    ## reprocess in "in table" insertion mode...
4086                }                }
4087    
4088                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4089                ## have an element in table scope                  ## Clear back to table context
4090                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4091                           $self->{open_elements}->[-1]->[1] ne 'html') {
4092                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4093                ## nop by definition                    pop @{$self->{open_elements}};
4094                    }
4095                pop @{$self->{open_elements}};                  
4096                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
4097                ## reprocess                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4098                redo B;                  ## reprocess
4099                    redo B;
4100                  } elsif ({
4101                            caption => 1,
4102                            colgroup => 1,
4103                            tbody => 1, tfoot => 1, thead => 1,
4104                           }->{$token->{tag_name}}) {
4105                    ## Clear back to table context
4106                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4107                           $self->{open_elements}->[-1]->[1] ne 'html') {
4108                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4109                      pop @{$self->{open_elements}};
4110                    }
4111                    
4112                    push @$active_formatting_elements, ['#marker', '']
4113                        if $token->{tag_name} eq 'caption';
4114                    
4115                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4116                    $self->{insertion_mode} = {
4117                                               caption => IN_CAPTION_IM,
4118                                               colgroup => IN_COLUMN_GROUP_IM,
4119                                               tbody => IN_TABLE_BODY_IM,
4120                                               tfoot => IN_TABLE_BODY_IM,
4121                                               thead => IN_TABLE_BODY_IM,
4122                                              }->{$token->{tag_name}};
4123                    !!!next-token;
4124                    redo B;
4125                  } else {
4126                    die "$0: in table: <>: $token->{tag_name}";
4127                  }
4128              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4129                ## 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');  
4130    
4131                ## As if </table>                ## As if </table>
4132                ## have a table element in table scope                ## have a table element in table scope
# Line 3861  sub _tree_construction_main ($) { Line 4153  sub _tree_construction_main ($) {
4153                if ({                if ({
4154                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4155                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4156                       tbody => 1, tfoot=> 1, thead => 1,
4157                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4158                  !!!back-token; # <table>                  !!!back-token; # <table>
4159                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
4160                  !!!back-token;                  !!!back-token;
4161                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
4162                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4163                  redo B;                  redo B;
4164                }                }
# Line 3876  sub _tree_construction_main ($) { Line 4169  sub _tree_construction_main ($) {
4169    
4170                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4171    
4172                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4173    
4174                ## reprocess                ## reprocess
4175                redo B;                redo B;
4176              } else {          } else {
4177                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
4178              }  
4179            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
4180              if ({            #
4181                   tbody => 1, tfoot => 1, thead => 1,          }
4182                  }->{$token->{tag_name}}) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4183                if ($token->{tag_name} eq 'tr' and
4184                    $self->{insertion_mode} == IN_ROW_IM) {
4185                ## have an element in table scope                ## have an element in table scope
4186                my $i;                my $i;
4187                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3907  sub _tree_construction_main ($) { Line 4202  sub _tree_construction_main ($) {
4202                  redo B;                  redo B;
4203                }                }
4204    
4205                ## Clear back to table body context                ## Clear back to table row context
4206                while (not {                while (not {
4207                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4208                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4209                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4210                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4211                }                }
4212    
4213                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
4214                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4215                !!!next-token;                !!!next-token;
4216                redo B;                redo B;
4217              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4218                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
4219                my $i;                  ## As if </tr>
4220                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4221                  my $node = $self->{open_elements}->[$_];                  my $i;
4222                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4223                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4224                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4225                    $i = $_;                      $i = $_;
4226                    last INSCOPE;                      last INSCOPE;
4227                  } elsif ({                    } elsif ({
4228                            table => 1, html => 1,                              table => 1, html => 1,
4229                           }->{$node->[1]}) {                             }->{$node->[1]}) {
4230                    last INSCOPE;                      last INSCOPE;
4231                      }
4232                    } # INSCOPE
4233                    unless (defined $i) {
4234                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4235                      ## Ignore the token
4236                      !!!next-token;
4237                      redo B;
4238                  }                  }
4239                } # INSCOPE                  
4240                unless (defined $i) {                  ## Clear back to table row context
4241                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
4242                  ## 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,  
4243                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4244                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4245                my $foster_parent_element;                    pop @{$self->{open_elements}};
4246                my $next_sibling;                  }
4247                my $prev_sibling;                  
4248                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
4249                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4250                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
4251                    if (defined $parent and $parent->node_type == 1) {                }
4252                      $foster_parent_element = $parent;  
4253                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4254                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
4255                    } else {                  my $i;
4256                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4257                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
4258                      if ({
4259                           tbody => 1, thead => 1, tfoot => 1,
4260                          }->{$node->[1]}) {
4261                        $i = $_;
4262                        last INSCOPE;
4263                      } elsif ({
4264                                table => 1, html => 1,
4265                               }->{$node->[1]}) {
4266                        last INSCOPE;
4267                    }                    }
4268                    last OE;                  } # INSCOPE
4269                    unless (defined $i) {
4270                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4271                      ## Ignore the token
4272                      !!!next-token;
4273                      redo B;
4274                  }                  }
4275                } # OE                  
4276                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
4277                $prev_sibling = $foster_parent_element->last_child                  while (not {
4278                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4279                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
4280                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4281                  $prev_sibling->manakai_append_text ($token->{data});                    pop @{$self->{open_elements}};
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## 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;  
4282                  }                  }
4283                } # INSCOPE                  
4284                unless (defined $i) {                  ## As if <{current node}>
4285                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
4286                  ## Ignore the token                  ## true by definition
4287                  !!!next-token;                  
4288                  redo B;                  ## Clear back to table body context
4289                }                  ## nop by definition
4290                    
               ## 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]);  
4291                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4292                    $self->{insertion_mode} = IN_TABLE_IM;
4293                    ## reprocess in the "in table" insertion mode...
4294                }                }
4295    
               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>  
4296                ## have a table element in table scope                ## have a table element in table scope
4297                my $i;                my $i;
4298                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4299                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4300                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
4301                    $i = $_;                    $i = $_;
4302                    last INSCOPE;                    last INSCOPE;
4303                  } elsif ({                  } elsif ({
# Line 4119  sub _tree_construction_main ($) { Line 4307  sub _tree_construction_main ($) {
4307                  }                  }
4308                } # INSCOPE                } # INSCOPE
4309                unless (defined $i) {                unless (defined $i) {
4310                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4311                  ## Ignore tokens </table><table>                  ## Ignore the token
4312                  !!!next-token;                  !!!next-token;
4313                  redo B;                  redo B;
4314                }                }
4315                  
4316                ## generate implied end tags                ## generate implied end tags
4317                if ({                if ({
4318                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4319                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4320                       tbody => 1, tfoot=> 1, thead => 1,
4321                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
4322                  !!!back-token;                  !!!back-token;
4323                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
4324                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4325                  redo B;                  redo B;
4326                }                }
4327                  
4328                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4329                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4330                }                }
4331                    
4332                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4333                  
4334                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4335                  
               ## 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';  
4336                !!!next-token;                !!!next-token;
4337                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;  
4338              } elsif ({              } elsif ({
4339                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4340                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4341                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
4342                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
4343                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4344                  my $node = $self->{open_elements}->[$_];                  my $i;
4345                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4346                    $i = $_;                    my $node = $self->{open_elements}->[$_];
4347                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
4348                  } elsif ({                      $i = $_;
4349                            table => 1, html => 1,                      last INSCOPE;
4350                           }->{$node->[1]}) {                    } elsif ({
4351                    last INSCOPE;                              table => 1, html => 1,
4352                               }->{$node->[1]}) {
4353                        last INSCOPE;
4354                      }
4355                    } # INSCOPE
4356                      unless (defined $i) {
4357                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4358                        ## Ignore the token
4359                        !!!next-token;
4360                        redo B;
4361                      }
4362                    
4363                    ## As if </tr>
4364                    ## have an element in table scope
4365                    my $i;
4366                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4367                      my $node = $self->{open_elements}->[$_];
4368                      if ($node->[1] eq 'tr') {
4369                        $i = $_;
4370                        last INSCOPE;
4371                      } elsif ({
4372                                table => 1, html => 1,
4373                               }->{$node->[1]}) {
4374                        last INSCOPE;
4375                      }
4376                    } # INSCOPE
4377                      unless (defined $i) {
4378                        !!!parse-error (type => 'unmatched end tag:tr');
4379                        ## Ignore the token
4380                        !!!next-token;
4381                        redo B;
4382                      }
4383                    
4384                    ## Clear back to table row context
4385                    while (not {
4386                      tr => 1, html => 1,
4387                    }->{$self->{open_elements}->[-1]->[1]}) {
4388                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4389                      pop @{$self->{open_elements}};
4390                  }                  }
4391                } # INSCOPE                  
4392                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4393                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4394                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4395                }                }
4396    
               ## As if </tr>  
4397                ## have an element in table scope                ## have an element in table scope
4398                my $i;                my $i;
4399                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4400                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4401                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4402                    $i = $_;                    $i = $_;
4403                    last INSCOPE;                    last INSCOPE;
4404                  } elsif ({                  } elsif ({
# Line 4257  sub _tree_construction_main ($) { Line 4408  sub _tree_construction_main ($) {
4408                  }                  }
4409                } # INSCOPE                } # INSCOPE
4410                unless (defined $i) {                unless (defined $i) {
4411                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4412                  ## Ignore the token                  ## Ignore the token
4413                  !!!next-token;                  !!!next-token;
4414                  redo B;                  redo B;
4415                }                }
4416    
4417                ## Clear back to table row context                ## Clear back to table body context
4418                while (not {                while (not {
4419                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4420                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4421                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4423                }                }
4424    
4425                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4426                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4427                ## reprocess                !!!next-token;
4428                redo B;                redo B;
4429              } elsif ({              } elsif ({
4430                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4431                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4432                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4433                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4434                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4435                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4436                ## Ignore the token                ## Ignore the token
4437                !!!next-token;                !!!next-token;
4438                redo B;                redo B;
4439              } else {          } else {
4440                #            !!!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});  
4441    
4442              !!!next-token;            $insert = $insert_to_foster;
4443              redo B;            #
4444            } elsif ($token->{type} eq 'comment') {          }
4445              ## NOTE: This is a code clone of "comment in body".        } else {
4446              my $comment = $self->{document}->create_comment ($token->{data});          die "$0: $token->{type}: Unknown token type";
4447              $self->{open_elements}->[-1]->[0]->append_child ($comment);        }
4448              !!!next-token;      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4449              redo B;            if ($token->{type} == CHARACTER_TOKEN) {
4450            } elsif ($token->{type} eq 'start tag') {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4451              if ({                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4452                   caption => 1, col => 1, colgroup => 1,                unless (length $token->{data}) {
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
4453                  !!!next-token;                  !!!next-token;
4454                  redo B;                  redo B;
4455                }                }
4456                }
4457                ## Close the cell              
4458                !!!back-token; # <?>              #
4459                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
4460                if ($token->{tag_name} eq 'col') {
4461                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4462                  pop @{$self->{open_elements}};
4463                  !!!next-token;
4464                redo B;                redo B;
4465              } else {              } else {
4466                #                #
4467              }              }
4468            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4469              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4470                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4471                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               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});  
4472                  ## Ignore the token                  ## Ignore the token
4473                  !!!next-token;                  !!!next-token;
4474                  redo B;                  redo B;
4475                  } else {
4476                    pop @{$self->{open_elements}}; # colgroup
4477                    $self->{insertion_mode} = IN_TABLE_IM;
4478                    !!!next-token;
4479                    redo B;            
4480                }                }
4481                              } elsif ($token->{tag_name} eq 'col') {
4482                ## generate implied end tags                !!!parse-error (type => 'unmatched end tag:col');
               if ({  
                    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});  
4483                ## Ignore the token                ## Ignore the token
4484                !!!next-token;                !!!next-token;
4485                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;  
4486              } else {              } else {
4487                #                #
4488              }              }
4489            } else {            } else {
4490              #              #
4491            }            }
4492              
4493            $in_body->($insert_to_current);            ## As if </colgroup>
4494            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4495          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
4496            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4497              !!!next-token;              !!!next-token;
4498              redo B;              redo B;
4499            } elsif ($token->{type} eq 'comment') {            } else {
4500              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
4501              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = IN_TABLE_IM;
4502              !!!next-token;              ## reprocess
4503              redo B;              redo B;
4504            } elsif ($token->{type} eq 'start tag') {            }
4505        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4506          if ($token->{type} == CHARACTER_TOKEN) {
4507            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4508            !!!next-token;
4509            redo B;
4510          } elsif ($token->{type} == START_TAG_TOKEN) {
4511              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4512                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4513                  ## As if </option>                  ## As if </option>
# Line 4502  sub _tree_construction_main ($) { Line 4560  sub _tree_construction_main ($) {
4560    
4561                !!!next-token;                !!!next-token;
4562                redo B;                redo B;
4563              } else {          } else {
4564                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4565              }            ## Ignore the token
4566            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4567              redo B;
4568            }
4569          } elsif ($token->{type} == END_TAG_TOKEN) {
4570              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4571                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4572                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4607  sub _tree_construction_main ($) { Line 4668  sub _tree_construction_main ($) {
4668    
4669                ## reprocess                ## reprocess
4670                redo B;                redo B;
4671              } else {          } else {
4672                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4673            ## Ignore the token            ## Ignore the token
4674            !!!next-token;            !!!next-token;
4675            redo B;            redo B;
4676          } elsif ($self->{insertion_mode} eq 'after body') {          }
4677            if ($token->{type} eq 'character') {        } else {
4678              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4679                ## As if in body        }
4680                $reconstruct_active_formatting_elements->($insert_to_current);      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4681          if ($token->{type} == CHARACTER_TOKEN) {
4682            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4683              my $data = $1;
4684              ## As if in body
4685              $reconstruct_active_formatting_elements->($insert_to_current);
4686                                
4687                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4688              
4689              unless (length $token->{data}) {
4690                !!!next-token;
4691                redo B;
4692              }
4693            }
4694            
4695            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4696              !!!parse-error (type => 'after html:#character');
4697    
4698                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4699                  !!!next-token;          }
4700                  redo B;          
4701                }          ## "after body" insertion mode
4702              }          !!!parse-error (type => 'after body:#character');
4703                
4704              #          $self->{insertion_mode} = IN_BODY_IM;
4705              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4706            } elsif ($token->{type} eq 'comment') {          redo B;
4707              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} == START_TAG_TOKEN) {
4708              $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4709              !!!parse-error (type => 'after html:'.$token->{tag_name});
4710              
4711              ## Reprocess in the "main" phase, "after body" insertion mode...
4712            }
4713    
4714            ## "after body" insertion mode
4715            !!!parse-error (type => 'after body:'.$token->{tag_name});
4716    
4717            $self->{insertion_mode} = IN_BODY_IM;
4718            ## reprocess
4719            redo B;
4720          } elsif ($token->{type} == END_TAG_TOKEN) {
4721            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4722              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4723              
4724              $self->{insertion_mode} = AFTER_BODY_IM;
4725              ## Reprocess in the "main" phase, "after body" insertion mode...
4726            }
4727    
4728            ## "after body" insertion mode
4729            if ($token->{tag_name} eq 'html') {
4730              if (defined $self->{inner_html_node}) {
4731                !!!parse-error (type => 'unmatched end tag:html');
4732                ## Ignore the token
4733              !!!next-token;              !!!next-token;
4734              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});  
             }  
4735            } else {            } else {
4736              !!!parse-error (type => 'after body:#'.$token->{type});              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4737                !!!next-token;
4738                redo B;
4739            }            }
4740            } else {
4741              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4742    
4743            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4744            ## reprocess            ## reprocess
4745            redo B;            redo B;
4746          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4747            if ($token->{type} eq 'character') {        } else {
4748              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4749                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4750        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4751                unless (length $token->{data}) {        if ($token->{type} == CHARACTER_TOKEN) {
4752                  !!!next-token;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4753                  redo B;            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4754                }            
4755              }            unless (length $token->{data}) {
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
4756              !!!next-token;              !!!next-token;
4757              redo B;              redo B;
4758            } elsif ($token->{type} eq 'start tag') {            }
4759              if ($token->{tag_name} eq 'frameset') {          }
4760                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4761                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4762                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4763              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4764                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4765                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
4766                !!!next-token;            } else { # "after html frameset"
4767                redo B;              !!!parse-error (type => 'after html:#character');
4768              } elsif ($token->{tag_name} eq 'noframes') {  
4769                $in_body->($insert_to_current);              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4770                redo B;              ## Reprocess in the "main" phase, "after frameset"...
4771              } else {              !!!parse-error (type => 'after frameset:#character');
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             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 {  
             #  
4772            }            }
4773                        
4774            if (defined $token->{tag_name}) {            ## Ignore the token.
4775              if (length $token->{data}) {
4776                ## reprocess the rest of characters
4777              } else {
4778                !!!next-token;
4779              }
4780              redo B;
4781            }
4782            
4783            die qq[$0: Character "$token->{data}"];
4784          } elsif ($token->{type} == START_TAG_TOKEN) {
4785            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4786              !!!parse-error (type => 'after html:'.$token->{tag_name});
4787    
4788              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4789              ## Process in the "main" phase, "after frameset" insertion mode...
4790            }
4791    
4792            if ($token->{tag_name} eq 'frameset' and
4793                $self->{insertion_mode} == IN_FRAMESET_IM) {
4794              !!!insert-element ($token->{tag_name}, $token->{attributes});
4795              !!!next-token;
4796              redo B;
4797            } elsif ($token->{tag_name} eq 'frame' and
4798                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4799              !!!insert-element ($token->{tag_name}, $token->{attributes});
4800              pop @{$self->{open_elements}};
4801              !!!next-token;
4802              redo B;
4803            } elsif ($token->{tag_name} eq 'noframes') {
4804              ## NOTE: As if in body.
4805              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4806              redo B;
4807            } else {
4808              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4809              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4810            } else {            } else {
4811              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4812            }            }
4813            ## Ignore the token            ## Ignore the token
4814            !!!next-token;            !!!next-token;
4815            redo B;            redo B;
4816          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4817            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4818              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4819                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4820    
4821                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4822                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
4823                  redo B;          }
               }  
             }  
4824    
4825              #          if ($token->{tag_name} eq 'frameset' and
4826            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4827              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4828              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4829                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4830                ## Ignore the token
4831              !!!next-token;              !!!next-token;
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
4832            } else {            } else {
4833              #              pop @{$self->{open_elements}};
4834                !!!next-token;
4835            }            }
4836              
4837            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
4838              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4839                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4840              }
4841              redo B;
4842            } elsif ($token->{tag_name} eq 'html' and
4843                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4844              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4845              !!!next-token;
4846              redo B;
4847            } else {
4848              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4849                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4850            } else {            } else {
4851              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4852            }            }
4853            ## Ignore the token            ## Ignore the token
4854            !!!next-token;            !!!next-token;
4855            redo B;            redo B;
4856            }
4857          } else {
4858            die "$0: $token->{type}: Unknown token type";
4859          }
4860    
4861          ## ISSUE: An issue in spec here
4862        } else {
4863          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4864        }
4865    
4866        ## "in body" insertion mode
4867        if ($token->{type} == START_TAG_TOKEN) {
4868          if ($token->{tag_name} eq 'script') {
4869            ## NOTE: This is an "as if in head" code clone
4870            $script_start_tag->($insert);
4871            redo B;
4872          } elsif ($token->{tag_name} eq 'style') {
4873            ## NOTE: This is an "as if in head" code clone
4874            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4875            redo B;
4876          } elsif ({
4877                    base => 1, link => 1,
4878                   }->{$token->{tag_name}}) {
4879            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4880            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4881            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4882            !!!next-token;
4883            redo B;
4884          } elsif ($token->{tag_name} eq 'meta') {
4885            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4886            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4887            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4888    
4889            ## ISSUE: An issue in spec there          unless ($self->{confident}) {
4890              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4891                $self->{change_encoding}
4892                    ->($self, $token->{attributes}->{charset}->{value});
4893                
4894                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4895                    ->set_user_data (manakai_has_reference =>
4896                                         $token->{attributes}->{charset}
4897                                             ->{has_reference});
4898              } elsif ($token->{attributes}->{content}) {
4899                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4900                if ($token->{attributes}->{content}->{value}
4901                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4902                        [\x09-\x0D\x20]*=
4903                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4904                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4905                  $self->{change_encoding}
4906                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4907                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4908                      ->set_user_data (manakai_has_reference =>
4909                                           $token->{attributes}->{content}
4910                                                 ->{has_reference});
4911                }
4912              }
4913          } else {          } else {
4914            die "$0: $self->{insertion_mode}: Unknown insertion mode";            if ($token->{attributes}->{charset}) {
4915                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4916                    ->set_user_data (manakai_has_reference =>
4917                                         $token->{attributes}->{charset}
4918                                             ->{has_reference});
4919              }
4920              if ($token->{attributes}->{content}) {
4921                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4922                    ->set_user_data (manakai_has_reference =>
4923                                         $token->{attributes}->{content}
4924                                             ->{has_reference});
4925              }
4926          }          }
4927        }  
     } elsif ($phase eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
4928          !!!next-token;          !!!next-token;
4929          redo B;          redo B;
4930        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'title') {
4931          my $comment = $self->{document}->create_comment ($token->{data});          !!!parse-error (type => 'in body:title');
4932          $self->{document}->append_child ($comment);          ## NOTE: This is an "as if in head" code clone
4933            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4934              if (defined $self->{head_element}) {
4935                $self->{head_element}->append_child ($_[0]);
4936              } else {
4937                $insert->($_[0]);
4938              }
4939            });
4940            redo B;
4941          } elsif ($token->{tag_name} eq 'body') {
4942            !!!parse-error (type => 'in body:body');
4943                  
4944            if (@{$self->{open_elements}} == 1 or
4945                $self->{open_elements}->[1]->[1] ne 'body') {
4946              ## Ignore the token
4947            } else {
4948              my $body_el = $self->{open_elements}->[1]->[0];
4949              for my $attr_name (keys %{$token->{attributes}}) {
4950                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4951                  $body_el->set_attribute_ns
4952                    (undef, [undef, $attr_name],
4953                     $token->{attributes}->{$attr_name}->{value});
4954                }
4955              }
4956            }
4957          !!!next-token;          !!!next-token;
4958          redo B;          redo B;
4959        } elsif ($token->{type} eq 'character') {        } elsif ({
4960          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  address => 1, blockquote => 1, center => 1, dir => 1,
4961            my $data = $1;                  div => 1, dl => 1, fieldset => 1, listing => 1,
4962            ## As if in the main phase.                  menu => 1, ol => 1, p => 1, ul => 1,
4963            ## NOTE: The insertion mode in the main phase                  pre => 1,
4964            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4965            ## end phase is either "after body" or "after frameset".          ## has a p element in scope
4966            $reconstruct_active_formatting_elements->($insert_to_current)          INSCOPE: for (reverse @{$self->{open_elements}}) {
4967              if $phase eq 'main';            if ($_->[1] eq 'p') {
4968                !!!back-token;
4969                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4970                redo B;
4971              } elsif ({
4972                        table => 1, caption => 1, td => 1, th => 1,
4973                        button => 1, marquee => 1, object => 1, html => 1,
4974                       }->{$_->[1]}) {
4975                last INSCOPE;
4976              }
4977            } # INSCOPE
4978              
4979            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4980            if ($token->{tag_name} eq 'pre') {
4981              !!!next-token;
4982              if ($token->{type} == CHARACTER_TOKEN) {
4983                $token->{data} =~ s/^\x0A//;
4984                unless (length $token->{data}) {
4985                  !!!next-token;
4986                }
4987              }
4988            } else {
4989              !!!next-token;
4990            }
4991            redo B;
4992          } elsif ($token->{tag_name} eq 'form') {
4993            if (defined $self->{form_element}) {
4994              !!!parse-error (type => 'in form:form');
4995              ## Ignore the token
4996              !!!next-token;
4997              redo B;
4998            } else {
4999              ## has a p element in scope
5000              INSCOPE: for (reverse @{$self->{open_elements}}) {
5001                if ($_->[1] eq 'p') {
5002                  !!!back-token;
5003                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5004                  redo B;
5005                } elsif ({
5006                          table => 1, caption => 1, td => 1, th => 1,
5007                          button => 1, marquee => 1, object => 1, html => 1,
5008                         }->{$_->[1]}) {
5009                  last INSCOPE;
5010                }
5011              } # INSCOPE
5012                
5013              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5014              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5015              !!!next-token;
5016              redo B;
5017            }
5018          } elsif ($token->{tag_name} eq 'li') {
5019            ## has a p element in scope
5020            INSCOPE: for (reverse @{$self->{open_elements}}) {
5021              if ($_->[1] eq 'p') {
5022                !!!back-token;
5023                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5024                redo B;
5025              } elsif ({
5026                        table => 1, caption => 1, td => 1, th => 1,
5027                        button => 1, marquee => 1, object => 1, html => 1,
5028                       }->{$_->[1]}) {
5029                last INSCOPE;
5030              }
5031            } # INSCOPE
5032              
5033            ## Step 1
5034            my $i = -1;
5035            my $node = $self->{open_elements}->[$i];
5036            LI: {
5037              ## Step 2
5038              if ($node->[1] eq 'li') {
5039                if ($i != -1) {
5040                  !!!parse-error (type => 'end tag missing:'.
5041                                  $self->{open_elements}->[-1]->[1]);
5042                }
5043                splice @{$self->{open_elements}}, $i;
5044                last LI;
5045              }
5046              
5047              ## Step 3
5048              if (not $formatting_category->{$node->[1]} and
5049                  #not $phrasing_category->{$node->[1]} and
5050                  ($special_category->{$node->[1]} or
5051                   $scoping_category->{$node->[1]}) and
5052                  $node->[1] ne 'address' and $node->[1] ne 'div') {
5053                last LI;
5054              }
5055              
5056              ## Step 4
5057              $i--;
5058              $node = $self->{open_elements}->[$i];
5059              redo LI;
5060            } # LI
5061              
5062            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5063            !!!next-token;
5064            redo B;
5065          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
5066            ## has a p element in scope
5067            INSCOPE: for (reverse @{$self->{open_elements}}) {
5068              if ($_->[1] eq 'p') {
5069                !!!back-token;
5070                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5071                redo B;
5072              } elsif ({
5073                        table => 1, caption => 1, td => 1, th => 1,
5074                        button => 1, marquee => 1, object => 1, html => 1,
5075                       }->{$_->[1]}) {
5076                last INSCOPE;
5077              }
5078            } # INSCOPE
5079              
5080            ## Step 1
5081            my $i = -1;
5082            my $node = $self->{open_elements}->[$i];
5083            LI: {
5084              ## Step 2
5085              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
5086                if ($i != -1) {
5087                  !!!parse-error (type => 'end tag missing:'.
5088                                  $self->{open_elements}->[-1]->[1]);
5089                }
5090                splice @{$self->{open_elements}}, $i;
5091                last LI;
5092              }
5093              
5094              ## Step 3
5095              if (not $formatting_category->{$node->[1]} and
5096                  #not $phrasing_category->{$node->[1]} and
5097                  ($special_category->{$node->[1]} or
5098                   $scoping_category->{$node->[1]}) and
5099                  $node->[1] ne 'address' and $node->[1] ne 'div') {
5100                last LI;
5101              }
5102              
5103              ## Step 4
5104              $i--;
5105              $node = $self->{open_elements}->[$i];
5106              redo LI;
5107            } # LI
5108              
5109            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5110            !!!next-token;
5111            redo B;
5112          } elsif ($token->{tag_name} eq 'plaintext') {
5113            ## has a p element in scope
5114            INSCOPE: for (reverse @{$self->{open_elements}}) {
5115              if ($_->[1] eq 'p') {
5116                !!!back-token;
5117                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5118                redo B;
5119              } elsif ({
5120                        table => 1, caption => 1, td => 1, th => 1,
5121                        button => 1, marquee => 1, object => 1, html => 1,
5122                       }->{$_->[1]}) {
5123                last INSCOPE;
5124              }
5125            } # INSCOPE
5126              
5127            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5128              
5129            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5130              
5131            !!!next-token;
5132            redo B;
5133          } elsif ({
5134                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5135                   }->{$token->{tag_name}}) {
5136            ## has a p element in scope
5137            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5138              my $node = $self->{open_elements}->[$_];
5139              if ($node->[1] eq 'p') {
5140                !!!back-token;
5141                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5142                redo B;
5143              } elsif ({
5144                        table => 1, caption => 1, td => 1, th => 1,
5145                        button => 1, marquee => 1, object => 1, html => 1,
5146                       }->{$node->[1]}) {
5147                last INSCOPE;
5148              }
5149            } # INSCOPE
5150              
5151            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
5152            ## has an element in scope
5153            #my $i;
5154            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5155            #  my $node = $self->{open_elements}->[$_];
5156            #  if ({
5157            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5158            #      }->{$node->[1]}) {
5159            #    $i = $_;
5160            #    last INSCOPE;
5161            #  } elsif ({
5162            #            table => 1, caption => 1, td => 1, th => 1,
5163            #            button => 1, marquee => 1, object => 1, html => 1,
5164            #           }->{$node->[1]}) {
5165            #    last INSCOPE;
5166            #  }
5167            #} # INSCOPE
5168            #  
5169            #if (defined $i) {
5170            #  !!! parse-error (type => 'in hn:hn');
5171            #  splice @{$self->{open_elements}}, $i;
5172            #}
5173              
5174            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5175              
5176            !!!next-token;
5177            redo B;
5178          } elsif ($token->{tag_name} eq 'a') {
5179            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5180              my $node = $active_formatting_elements->[$i];
5181              if ($node->[1] eq 'a') {
5182                !!!parse-error (type => 'in a:a');
5183                
5184                !!!back-token;
5185                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
5186                $formatting_end_tag->($token->{tag_name});
5187                
5188                AFE2: for (reverse 0..$#$active_formatting_elements) {
5189                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5190                    splice @$active_formatting_elements, $_, 1;
5191                    last AFE2;
5192                  }
5193                } # AFE2
5194                OE: for (reverse 0..$#{$self->{open_elements}}) {
5195                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5196                    splice @{$self->{open_elements}}, $_, 1;
5197                    last OE;
5198                  }
5199                } # OE
5200                last AFE;
5201              } elsif ($node->[0] eq '#marker') {
5202                last AFE;
5203              }
5204            } # AFE
5205              
5206            $reconstruct_active_formatting_elements->($insert_to_current);
5207    
5208            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5209            push @$active_formatting_elements, $self->{open_elements}->[-1];
5210    
5211            !!!next-token;
5212            redo B;
5213          } elsif ({
5214                    b => 1, big => 1, em => 1, font => 1, i => 1,
5215                    s => 1, small => 1, strile => 1,
5216                    strong => 1, tt => 1, u => 1,
5217                   }->{$token->{tag_name}}) {
5218            $reconstruct_active_formatting_elements->($insert_to_current);
5219            
5220            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5221            push @$active_formatting_elements, $self->{open_elements}->[-1];
5222            
5223            !!!next-token;
5224            redo B;
5225          } elsif ($token->{tag_name} eq 'nobr') {
5226            $reconstruct_active_formatting_elements->($insert_to_current);
5227    
5228            ## has a |nobr| element in scope
5229            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5230              my $node = $self->{open_elements}->[$_];
5231              if ($node->[1] eq 'nobr') {
5232                !!!parse-error (type => 'in nobr:nobr');
5233                !!!back-token;
5234                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
5235                redo B;
5236              } elsif ({
5237                        table => 1, caption => 1, td => 1, th => 1,
5238                        button => 1, marquee => 1, object => 1, html => 1,
5239                       }->{$node->[1]}) {
5240                last INSCOPE;
5241              }
5242            } # INSCOPE
5243            
5244            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5245            push @$active_formatting_elements, $self->{open_elements}->[-1];
5246            
5247            !!!next-token;
5248            redo B;
5249          } elsif ($token->{tag_name} eq 'button') {
5250            ## has a button element in scope
5251            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5252              my $node = $self->{open_elements}->[$_];
5253              if ($node->[1] eq 'button') {
5254                !!!parse-error (type => 'in button:button');
5255                !!!back-token;
5256                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
5257                redo B;
5258              } elsif ({
5259                        table => 1, caption => 1, td => 1, th => 1,
5260                        button => 1, marquee => 1, object => 1, html => 1,
5261                       }->{$node->[1]}) {
5262                last INSCOPE;
5263              }
5264            } # INSCOPE
5265              
5266            $reconstruct_active_formatting_elements->($insert_to_current);
5267              
5268            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5269            push @$active_formatting_elements, ['#marker', ''];
5270    
5271            !!!next-token;
5272            redo B;
5273          } elsif ($token->{tag_name} eq 'marquee' or
5274                   $token->{tag_name} eq 'object') {
5275            $reconstruct_active_formatting_elements->($insert_to_current);
5276            
5277            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5278            push @$active_formatting_elements, ['#marker', ''];
5279            
5280            !!!next-token;
5281            redo B;
5282          } elsif ($token->{tag_name} eq 'xmp') {
5283            $reconstruct_active_formatting_elements->($insert_to_current);
5284            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5285            redo B;
5286          } elsif ($token->{tag_name} eq 'table') {
5287            ## has a p element in scope
5288            INSCOPE: for (reverse @{$self->{open_elements}}) {
5289              if ($_->[1] eq 'p') {
5290                !!!back-token;
5291                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5292                redo B;
5293              } elsif ({
5294                        table => 1, caption => 1, td => 1, th => 1,
5295                        button => 1, marquee => 1, object => 1, html => 1,
5296                       }->{$_->[1]}) {
5297                last INSCOPE;
5298              }
5299            } # INSCOPE
5300              
5301            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5302              
5303            $self->{insertion_mode} = IN_TABLE_IM;
5304              
5305            !!!next-token;
5306            redo B;
5307          } elsif ({
5308                    area => 1, basefont => 1, bgsound => 1, br => 1,
5309                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
5310                    image => 1,
5311                   }->{$token->{tag_name}}) {
5312            if ($token->{tag_name} eq 'image') {
5313              !!!parse-error (type => 'image');
5314              $token->{tag_name} = 'img';
5315            }
5316    
5317            ## NOTE: There is an "as if <br>" code clone.
5318            $reconstruct_active_formatting_elements->($insert_to_current);
5319            
5320            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5321            pop @{$self->{open_elements}};
5322            
5323            !!!next-token;
5324            redo B;
5325          } elsif ($token->{tag_name} eq 'hr') {
5326            ## has a p element in scope
5327            INSCOPE: for (reverse @{$self->{open_elements}}) {
5328              if ($_->[1] eq 'p') {
5329                !!!back-token;
5330                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5331                redo B;
5332              } elsif ({
5333                        table => 1, caption => 1, td => 1, th => 1,
5334                        button => 1, marquee => 1, object => 1, html => 1,
5335                       }->{$_->[1]}) {
5336                last INSCOPE;
5337              }
5338            } # INSCOPE
5339                        
5340            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5341            pop @{$self->{open_elements}};
5342                        
5343            !!!next-token;
5344            redo B;
5345          } elsif ($token->{tag_name} eq 'input') {
5346            $reconstruct_active_formatting_elements->($insert_to_current);
5347            
5348            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5349            ## TODO: associate with $self->{form_element} if defined
5350            pop @{$self->{open_elements}};
5351            
5352            !!!next-token;
5353            redo B;
5354          } elsif ($token->{tag_name} eq 'isindex') {
5355            !!!parse-error (type => 'isindex');
5356            
5357            if (defined $self->{form_element}) {
5358              ## Ignore the token
5359              !!!next-token;
5360              redo B;
5361            } else {
5362              my $at = $token->{attributes};
5363              my $form_attrs;
5364              $form_attrs->{action} = $at->{action} if $at->{action};
5365              my $prompt_attr = $at->{prompt};
5366              $at->{name} = {name => 'name', value => 'isindex'};
5367              delete $at->{action};
5368              delete $at->{prompt};
5369              my @tokens = (
5370                            {type => START_TAG_TOKEN, tag_name => 'form',
5371                             attributes => $form_attrs},
5372                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5373                            {type => START_TAG_TOKEN, tag_name => 'p'},
5374                            {type => START_TAG_TOKEN, tag_name => 'label'},
5375                           );
5376              if ($prompt_attr) {
5377                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5378              } else {
5379                push @tokens, {type => CHARACTER_TOKEN,
5380                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5381                ## TODO: make this configurable
5382              }
5383              push @tokens,
5384                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5385                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5386                            {type => END_TAG_TOKEN, tag_name => 'label'},
5387                            {type => END_TAG_TOKEN, tag_name => 'p'},
5388                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5389                            {type => END_TAG_TOKEN, tag_name => 'form'};
5390              $token = shift @tokens;
5391              !!!back-token (@tokens);
5392              redo B;
5393            }
5394          } elsif ($token->{tag_name} eq 'textarea') {
5395            my $tag_name = $token->{tag_name};
5396            my $el;
5397            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5398            
5399            ## TODO: $self->{form_element} if defined
5400            $self->{content_model} = RCDATA_CONTENT_MODEL;
5401            delete $self->{escape}; # MUST
5402            
5403            $insert->($el);
5404            
5405            my $text = '';
5406            !!!next-token;
5407            if ($token->{type} == CHARACTER_TOKEN) {
5408              $token->{data} =~ s/^\x0A//;
5409            unless (length $token->{data}) {            unless (length $token->{data}) {
5410              !!!next-token;              !!!next-token;
             redo B;  
5411            }            }
5412          }          }
5413            while ($token->{type} == CHARACTER_TOKEN) {
5414              $text .= $token->{data};
5415              !!!next-token;
5416            }
5417            if (length $text) {
5418              $el->manakai_append_text ($text);
5419            }
5420            
5421            $self->{content_model} = PCDATA_CONTENT_MODEL;
5422            
5423            if ($token->{type} == END_TAG_TOKEN and
5424                $token->{tag_name} eq $tag_name) {
5425              ## Ignore the token
5426            } else {
5427              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5428            }
5429            !!!next-token;
5430            redo B;
5431          } elsif ({
5432                    iframe => 1,
5433                    noembed => 1,
5434                    noframes => 1,
5435                    noscript => 0, ## TODO: 1 if scripting is enabled
5436                   }->{$token->{tag_name}}) {
5437            ## NOTE: There is an "as if in body" code clone.
5438            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5439            redo B;
5440          } elsif ($token->{tag_name} eq 'select') {
5441            $reconstruct_active_formatting_elements->($insert_to_current);
5442            
5443            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5444            
5445            $self->{insertion_mode} = IN_SELECT_IM;
5446            !!!next-token;
5447            redo B;
5448          } elsif ({
5449                    caption => 1, col => 1, colgroup => 1, frame => 1,
5450                    frameset => 1, head => 1, option => 1, optgroup => 1,
5451                    tbody => 1, td => 1, tfoot => 1, th => 1,
5452                    thead => 1, tr => 1,
5453                   }->{$token->{tag_name}}) {
5454            !!!parse-error (type => 'in body:'.$token->{tag_name});
5455            ## Ignore the token
5456            !!!next-token;
5457            redo B;
5458            
5459            ## ISSUE: An issue on HTML5 new elements in the spec.
5460          } else {
5461            $reconstruct_active_formatting_elements->($insert_to_current);
5462            
5463            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5464            
5465            !!!next-token;
5466            redo B;
5467          }
5468        } elsif ($token->{type} == END_TAG_TOKEN) {
5469          if ($token->{tag_name} eq 'body') {
5470            if (@{$self->{open_elements}} > 1 and
5471                $self->{open_elements}->[1]->[1] eq 'body') {
5472              for (@{$self->{open_elements}}) {
5473                unless ({
5474                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
5475                           th => 1, tr => 1, body => 1, html => 1,
5476                         tbody => 1, tfoot => 1, thead => 1,
5477                        }->{$_->[1]}) {
5478                  !!!parse-error (type => 'not closed:'.$_->[1]);
5479                }
5480              }
5481    
5482          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
5483          $phase = 'main';            !!!next-token;
5484          ## reprocess            redo B;
5485            } else {
5486              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5487              ## Ignore the token
5488              !!!next-token;
5489              redo B;
5490            }
5491          } elsif ($token->{tag_name} eq 'html') {
5492            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5493              ## ISSUE: There is an issue in the spec.
5494              if ($self->{open_elements}->[-1]->[1] ne 'body') {
5495                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5496              }
5497              $self->{insertion_mode} = AFTER_BODY_IM;
5498              ## reprocess
5499              redo B;
5500            } else {
5501              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5502              ## Ignore the token
5503              !!!next-token;
5504              redo B;
5505            }
5506          } elsif ({
5507                    address => 1, blockquote => 1, center => 1, dir => 1,
5508                    div => 1, dl => 1, fieldset => 1, listing => 1,
5509                    menu => 1, ol => 1, pre => 1, ul => 1,
5510                    p => 1,
5511                    dd => 1, dt => 1, li => 1,
5512                    button => 1, marquee => 1, object => 1,
5513                   }->{$token->{tag_name}}) {
5514            ## has an element in scope
5515            my $i;
5516            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5517              my $node = $self->{open_elements}->[$_];
5518              if ($node->[1] eq $token->{tag_name}) {
5519                ## generate implied end tags
5520                if ({
5521                     dd => ($token->{tag_name} ne 'dd'),
5522                     dt => ($token->{tag_name} ne 'dt'),
5523                     li => ($token->{tag_name} ne 'li'),
5524                     p => ($token->{tag_name} ne 'p'),
5525                     td => 1, th => 1, tr => 1,
5526                     tbody => 1, tfoot=> 1, thead => 1,
5527                    }->{$self->{open_elements}->[-1]->[1]}) {
5528                  !!!back-token;
5529                  $token = {type => END_TAG_TOKEN,
5530                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5531                  redo B;
5532                }
5533                $i = $_;
5534                last INSCOPE unless $token->{tag_name} eq 'p';
5535              } elsif ({
5536                        table => 1, caption => 1, td => 1, th => 1,
5537                        button => 1, marquee => 1, object => 1, html => 1,
5538                       }->{$node->[1]}) {
5539                last INSCOPE;
5540              }
5541            } # INSCOPE
5542            
5543            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5544              if (defined $i) {
5545                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5546              } else {
5547                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5548              }
5549            }
5550            
5551            if (defined $i) {
5552              splice @{$self->{open_elements}}, $i;
5553            } elsif ($token->{tag_name} eq 'p') {
5554              ## As if <p>, then reprocess the current token
5555              my $el;
5556              !!!create-element ($el, 'p');
5557              $insert->($el);
5558            }
5559            $clear_up_to_marker->()
5560              if {
5561                button => 1, marquee => 1, object => 1,
5562              }->{$token->{tag_name}};
5563            !!!next-token;
5564          redo B;          redo B;
5565        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
5566                 $token->{type} eq 'end tag') {          ## has an element in scope
5567          !!!parse-error (type => 'after html:'.$token->{tag_name});          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5568          $phase = 'main';            my $node = $self->{open_elements}->[$_];
5569          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
5570                ## generate implied end tags
5571                if ({
5572                     dd => 1, dt => 1, li => 1, p => 1,
5573                     td => 1, th => 1, tr => 1,
5574                     tbody => 1, tfoot=> 1, thead => 1,
5575                    }->{$self->{open_elements}->[-1]->[1]}) {
5576                  !!!back-token;
5577                  $token = {type => END_TAG_TOKEN,
5578                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5579                  redo B;
5580                }
5581                last INSCOPE;
5582              } elsif ({
5583                        table => 1, caption => 1, td => 1, th => 1,
5584                        button => 1, marquee => 1, object => 1, html => 1,
5585                       }->{$node->[1]}) {
5586                last INSCOPE;
5587              }
5588            } # INSCOPE
5589            
5590            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5591              pop @{$self->{open_elements}};
5592            } else {
5593              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5594            }
5595    
5596            undef $self->{form_element};
5597            !!!next-token;
5598          redo B;          redo B;
5599        } elsif ($token->{type} eq 'end-of-file') {        } elsif ({
5600          ## Stop parsing                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5601          last B;                 }->{$token->{tag_name}}) {
5602            ## has an element in scope
5603            my $i;
5604            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5605              my $node = $self->{open_elements}->[$_];
5606              if ({
5607                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5608                  }->{$node->[1]}) {
5609                ## generate implied end tags
5610                if ({
5611                     dd => 1, dt => 1, li => 1, p => 1,
5612                     td => 1, th => 1, tr => 1,
5613                     tbody => 1, tfoot=> 1, thead => 1,
5614                    }->{$self->{open_elements}->[-1]->[1]}) {
5615                  !!!back-token;
5616                  $token = {type => END_TAG_TOKEN,
5617                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5618                  redo B;
5619                }
5620                $i = $_;
5621                last INSCOPE;
5622              } elsif ({
5623                        table => 1, caption => 1, td => 1, th => 1,
5624                        button => 1, marquee => 1, object => 1, html => 1,
5625                       }->{$node->[1]}) {
5626                last INSCOPE;
5627              }
5628            } # INSCOPE
5629            
5630            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5631              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5632            }
5633            
5634            splice @{$self->{open_elements}}, $i if defined $i;
5635            !!!next-token;
5636            redo B;
5637          } elsif ({
5638                    a => 1,
5639                    b => 1, big => 1, em => 1, font => 1, i => 1,
5640                    nobr => 1, s => 1, small => 1, strile => 1,
5641                    strong => 1, tt => 1, u => 1,
5642                   }->{$token->{tag_name}}) {
5643            $formatting_end_tag->($token->{tag_name});
5644            redo B;
5645          } elsif ($token->{tag_name} eq 'br') {
5646            !!!parse-error (type => 'unmatched end tag:br');
5647    
5648            ## As if <br>
5649            $reconstruct_active_formatting_elements->($insert_to_current);
5650            
5651            my $el;
5652            !!!create-element ($el, 'br');
5653            $insert->($el);
5654            
5655            ## Ignore the token.
5656            !!!next-token;
5657            redo B;
5658          } elsif ({
5659                    caption => 1, col => 1, colgroup => 1, frame => 1,
5660                    frameset => 1, head => 1, option => 1, optgroup => 1,
5661                    tbody => 1, td => 1, tfoot => 1, th => 1,
5662                    thead => 1, tr => 1,
5663                    area => 1, basefont => 1, bgsound => 1,
5664                    embed => 1, hr => 1, iframe => 1, image => 1,
5665                    img => 1, input => 1, isindex => 1, noembed => 1,
5666                    noframes => 1, param => 1, select => 1, spacer => 1,
5667                    table => 1, textarea => 1, wbr => 1,
5668                    noscript => 0, ## TODO: if scripting is enabled
5669                   }->{$token->{tag_name}}) {
5670            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5671            ## Ignore the token
5672            !!!next-token;
5673            redo B;
5674            
5675            ## ISSUE: Issue on HTML5 new elements in spec
5676            
5677        } else {        } else {
5678          die "$0: $token->{type}: Unknown token";          ## Step 1
5679            my $node_i = -1;
5680            my $node = $self->{open_elements}->[$node_i];
5681    
5682            ## Step 2
5683            S2: {
5684              if ($node->[1] eq $token->{tag_name}) {
5685                ## Step 1
5686                ## generate implied end tags
5687                if ({
5688                     dd => 1, dt => 1, li => 1, p => 1,
5689                     td => 1, th => 1, tr => 1,
5690                     tbody => 1, tfoot => 1, thead => 1,
5691                    }->{$self->{open_elements}->[-1]->[1]}) {
5692                  !!!back-token;
5693                  $token = {type => END_TAG_TOKEN,
5694                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5695                  redo B;
5696                }
5697            
5698                ## Step 2
5699                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5700                  ## NOTE: <x><y></x>
5701                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5702                }
5703                
5704                ## Step 3
5705                splice @{$self->{open_elements}}, $node_i;
5706    
5707                !!!next-token;
5708                last S2;
5709              } else {
5710                ## Step 3
5711                if (not $formatting_category->{$node->[1]} and
5712                    #not $phrasing_category->{$node->[1]} and
5713                    ($special_category->{$node->[1]} or
5714                     $scoping_category->{$node->[1]})) {
5715                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5716                  ## Ignore the token
5717                  !!!next-token;
5718                  last S2;
5719                }
5720              }
5721              
5722              ## Step 4
5723              $node_i--;
5724              $node = $self->{open_elements}->[$node_i];
5725              
5726              ## Step 5;
5727              redo S2;
5728            } # S2
5729            redo B;
5730        }        }
5731      }      }
5732        redo B;
5733    } # B    } # B
5734    
5735      ## NOTE: The "trailing end" phase in HTML5 is split into
5736      ## two insertion modes: "after html body" and "after html frameset".
5737      ## NOTE: States in the main stage is preserved while
5738      ## the parser stays in the trailing end phase. # MUST
5739    
5740    ## Stop parsing # MUST    ## Stop parsing # MUST
5741        
5742    ## TODO: script stuffs    ## TODO: script stuffs
# Line 4840  sub set_inner_html ($$$) { Line 5748  sub set_inner_html ($$$) {
5748    my $s = \$_[0];    my $s = \$_[0];
5749    my $onerror = $_[1];    my $onerror = $_[1];
5750    
5751      ## ISSUE: Should {confident} be true?
5752    
5753    my $nt = $node->node_type;    my $nt = $node->node_type;
5754    if ($nt == 9) {    if ($nt == 9) {
5755      # MUST      # MUST
# Line 4862  sub set_inner_html ($$$) { Line 5772  sub set_inner_html ($$$) {
5772      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5773    
5774      ## Step 1 # MUST      ## Step 1 # MUST
5775      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5776      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5777        $doc->manakai_is_html (1);
5778      my $p = $class->new;      my $p = $class->new;
5779      $p->{document} = $doc;      $p->{document} = $doc;
5780    
# Line 4871  sub set_inner_html ($$$) { Line 5782  sub set_inner_html ($$$) {
5782      my $i = 0;      my $i = 0;
5783      my $line = 1;      my $line = 1;
5784      my $column = 0;      my $column = 0;
5785      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
5786        my $self = shift;        my $self = shift;
5787        $self->{next_input_character} = -1 and return if $i >= length $$s;  
5788        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
5789          unshift @{$self->{prev_char}}, $self->{next_char};
5790    
5791          $self->{next_char} = -1 and return if $i >= length $$s;
5792          $self->{next_char} = ord substr $$s, $i++, 1;
5793        $column++;        $column++;
5794    
5795        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
5796          $line++;          $line++;
5797          $column = 0;          $column = 0;
5798        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
5799          if ($i >= length $$s) {          $i++ if substr ($$s, $i, 1) eq "\x0A";
5800            #          $self->{next_char} = 0x000A; # LF # MUST
         } 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  
5801          $line++;          $line++;
5802          $column = 0;          $column = 0;
5803        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
5804          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5805        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
5806          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!parse-error (type => 'NULL');
5807            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5808        }        }
5809      };      };
5810        $p->{prev_char} = [-1, -1, -1];
5811        $p->{next_char} = -1;
5812            
5813      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5814        my (%opt) = @_;        my (%opt) = @_;
# Line 4913  sub set_inner_html ($$$) { Line 5822  sub set_inner_html ($$$) {
5822      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5823    
5824      ## Step 2      ## Step 2
5825      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5826      $p->{content_model_flag} = {      $p->{content_model} = {
5827        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5828        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5829        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5830        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5831        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5832        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5833        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5834        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5835        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5836        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5837      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5838         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5839            unless defined $p->{content_model};
5840            ## ISSUE: What is "the name of the element"? local name?
5841    
5842      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5843    
# Line 4951  sub set_inner_html ($$$) { Line 5862  sub set_inner_html ($$$) {
5862        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5863          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5864          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5865            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5866              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5867              last AN;              last AN;
5868            }            }
# Line 4978  sub set_inner_html ($$$) { Line 5889  sub set_inner_html ($$$) {
5889      ## Step 12 # MUST      ## Step 12 # MUST
5890      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5891      for (@cn) {      for (@cn) {
5892          $this_doc->adopt_node ($_);
5893        $node->append_child ($_);        $node->append_child ($_);
5894      }      }
5895      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5896    
5897      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5898    } else {    } else {
# Line 4990  sub set_inner_html ($$$) { Line 5902  sub set_inner_html ($$$) {
5902    
5903  } # tree construction stage  } # tree construction stage
5904    
5905  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5906    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  
5907    
5908  1;  1;
5909  # $Date$  # $Date$

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.77

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24