/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm.src
Suika

Diff of /markup/html/whatpm/Whatpm/HTML.pm.src

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7 by wakaba, Wed May 30 12:24:50 2007 UTC revision 1.71 by wakaba, Sun Mar 2 03:39:41 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  ## ISSUE:
7    ## var doc = implementation.createDocument (null, null, null);
8    ## doc.write ('');
9    ## alert (doc.compatMode);
10    
11    ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12    ## TODO: 1252 parse error (revision 1264)
13    ## TODO: 8859-11 = 874 (revision 1271)
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
# Line 10  my $permitted_slash_tag_name = { Line 18  my $permitted_slash_tag_name = {
18    meta => 1,    meta => 1,
19    hr => 1,    hr => 1,
20    br => 1,    br => 1,
21    img=> 1,    img => 1,
22    embed => 1,    embed => 1,
23    param => 1,    param => 1,
24    area => 1,    area => 1,
# Line 18  my $permitted_slash_tag_name = { Line 26  my $permitted_slash_tag_name = {
26    input => 1,    input => 1,
27  };  };
28    
 my $entity_char = {  
   AElig => "\x{00C6}",  
   Aacute => "\x{00C1}",  
   Acirc => "\x{00C2}",  
   Agrave => "\x{00C0}",  
   Alpha => "\x{0391}",  
   Aring => "\x{00C5}",  
   Atilde => "\x{00C3}",  
   Auml => "\x{00C4}",  
   Beta => "\x{0392}",  
   Ccedil => "\x{00C7}",  
   Chi => "\x{03A7}",  
   Dagger => "\x{2021}",  
   Delta => "\x{0394}",  
   ETH => "\x{00D0}",  
   Eacute => "\x{00C9}",  
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 }; # $entity_char  
   
 ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>  
29  my $c1_entity_char = {  my $c1_entity_char = {
30       128, 8364,    0x80 => 0x20AC,
31       129, 65533,    0x81 => 0xFFFD,
32       130, 8218,    0x82 => 0x201A,
33       131, 402,    0x83 => 0x0192,
34       132, 8222,    0x84 => 0x201E,
35       133, 8230,    0x85 => 0x2026,
36       134, 8224,    0x86 => 0x2020,
37       135, 8225,    0x87 => 0x2021,
38       136, 710,    0x88 => 0x02C6,
39       137, 8240,    0x89 => 0x2030,
40       138, 352,    0x8A => 0x0160,
41       139, 8249,    0x8B => 0x2039,
42       140, 338,    0x8C => 0x0152,
43       141, 65533,    0x8D => 0xFFFD,
44       142, 381,    0x8E => 0x017D,
45       143, 65533,    0x8F => 0xFFFD,
46       144, 65533,    0x90 => 0xFFFD,
47       145, 8216,    0x91 => 0x2018,
48       146, 8217,    0x92 => 0x2019,
49       147, 8220,    0x93 => 0x201C,
50       148, 8221,    0x94 => 0x201D,
51       149, 8226,    0x95 => 0x2022,
52       150, 8211,    0x96 => 0x2013,
53       151, 8212,    0x97 => 0x2014,
54       152, 732,    0x98 => 0x02DC,
55       153, 8482,    0x99 => 0x2122,
56       154, 353,    0x9A => 0x0161,
57       155, 8250,    0x9B => 0x203A,
58       156, 339,    0x9C => 0x0153,
59       157, 65533,    0x9D => 0xFFFD,
60       158, 382,    0x9E => 0x017E,
61       159, 376,    0x9F => 0x0178,
62  }; # $c1_entity_char  }; # $c1_entity_char
63    
64  my $special_category = {  my $special_category = {
# Line 338  my $formatting_category = { Line 83  my $formatting_category = {
83  };  };
84  # $phrasing_category: all other elements  # $phrasing_category: all other elements
85    
86    sub parse_byte_string ($$$$;$) {
87      my $self = ref $_[0] ? shift : shift->new;
88      my $charset = shift;
89      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
90      my $s;
91      
92      if (defined $charset) {
93        require Encode; ## TODO: decode(utf8) don't delete BOM
94        $s = \ (Encode::decode ($charset, $$bytes_s));
95        $self->{input_encoding} = lc $charset; ## TODO: normalize name
96        $self->{confident} = 1;
97      } else {
98        ## TODO: Implement HTML5 detection algorithm
99        require Whatpm::Charset::UniversalCharDet;
100        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
101            (substr ($$bytes_s, 0, 1024));
102        $charset ||= 'windows-1252';
103        $s = \ (Encode::decode ($charset, $$bytes_s));
104        $self->{input_encoding} = $charset;
105        $self->{confident} = 0;
106      }
107    
108      $self->{change_encoding} = sub {
109        my $self = shift;
110        my $charset = lc shift;
111        ## 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_input_character} = sub {
183      my $self = shift;      my $self = shift;
184    
185        pop @{$self->{prev_input_character}};
186        unshift @{$self->{prev_input_character}}, $self->{next_input_character};
187    
188      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_input_character} = -1 and return if $i >= length $$s;
189      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_input_character} = ord substr $$s, $i++, 1;
190      $column++;      $column++;
# Line 358  sub parse_string ($$$;$) { Line 193  sub parse_string ($$$;$) {
193        $line++;        $line++;
194        $column = 0;        $column = 0;
195      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_input_character} == 0x000D) { # CR
196        if ($i >= length $$s) {        $i++ if substr ($$s, $i, 1) eq "\x0A";
         #  
       } else {  
         my $next_char = ord substr $$s, $i++, 1;  
         if ($next_char == 0x000A) { # LF  
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
       }  
197        $self->{next_input_character} = 0x000A; # LF # MUST        $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_input_character} > 0x10FFFF) {
201        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
202      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_input_character} == 0x0000) { # NULL
203          !!!parse-error (type => 'NULL');
204        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
205      }      }
206    };    };
207      $self->{prev_input_character} = [-1, -1, -1];
208      $self->{next_input_character} = -1;
209    
210    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
211      my (%opt) = @_;      my (%opt) = @_;
# Line 403  sub new ($) { Line 232  sub new ($) {
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    
290    sub DOCTYPE_TOKEN () { 1 }
291    sub COMMENT_TOKEN () { 2 }
292    sub START_TAG_TOKEN () { 3 }
293    sub END_TAG_TOKEN () { 4 }
294    sub END_OF_FILE_TOKEN () { 5 }
295    sub CHARACTER_TOKEN () { 6 }
296    
297    sub AFTER_HTML_IMS () { 0b100 }
298    sub HEAD_IMS ()       { 0b1000 }
299    sub BODY_IMS ()       { 0b10000 }
300    sub BODY_TABLE_IMS () { 0b100000 }
301    sub TABLE_IMS ()      { 0b1000000 }
302    sub ROW_IMS ()        { 0b10000000 }
303    sub BODY_AFTER_IMS () { 0b100000000 }
304    sub FRAME_IMS ()      { 0b1000000000 }
305    
306    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
307    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
308    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
309    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
310    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
311    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
312    sub IN_BODY_IM () { BODY_IMS }
313    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
314    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
315    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
316    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
317    sub IN_TABLE_IM () { TABLE_IMS }
318    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
319    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
320    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
321    sub IN_SELECT_IM () { 0b01 }
322    sub IN_COLUMN_GROUP_IM () { 0b10 }
323    
324  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
325    
326  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
327    my $self = shift;    my $self = shift;
328    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
329    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
330    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
331    undef $self->{current_attribute};    undef $self->{current_attribute};
332    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 420  sub _initialize_tokenizer ($) { Line 335  sub _initialize_tokenizer ($) {
335    # $self->{next_input_character}    # $self->{next_input_character}
336    !!!next-input-character;    !!!next-input-character;
337    $self->{token} = [];    $self->{token} = [];
338      # $self->{escape}
339  } # _initialize_tokenizer  } # _initialize_tokenizer
340    
341  ## A token has:  ## A token has:
342  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
343  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
344  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
345      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
346  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
347  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
348  ##   ->{data} (comment, character)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
349    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
350  ## Macros  ##        ->{name}
351  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
352  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
353  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
354    
355  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
356    
# Line 444  sub _initialize_tokenizer ($) { Line 360  sub _initialize_tokenizer ($) {
360  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
361  ## and removed from the list.  ## and removed from the list.
362    
363    ## NOTE: HTML5 "Writing HTML documents" section, applied to
364    ## documents and not to user agents and conformance checkers,
365    ## contains some requirements that are not detected by the
366    ## parsing algorithm:
367    ## - Some requirements on character encoding declarations. ## TODO
368    ## - "Elements MUST NOT contain content that their content model disallows."
369    ##   ... Some are parse error, some are not (will be reported by c.c.).
370    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
371    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
372    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
373    
374    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
375    ## be detected by the HTML5 parsing algorithm:
376    ## - Text,
377    
378  sub _get_next_token ($) {  sub _get_next_token ($) {
379    my $self = shift;    my $self = shift;
380    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 451  sub _get_next_token ($) { Line 382  sub _get_next_token ($) {
382    }    }
383    
384    A: {    A: {
385      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
386        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
387          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
388              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
389            !!!next-input-character;            !!!next-input-character;
390            redo A;            redo A;
391          } else {          } else {
392            #            #
393          }          }
394          } elsif ($self->{next_input_character} == 0x002D) { # -
395            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
396              unless ($self->{escape}) {
397                if ($self->{prev_input_character}->[0] == 0x002D and # -
398                    $self->{prev_input_character}->[1] == 0x0021 and # !
399                    $self->{prev_input_character}->[2] == 0x003C) { # <
400                  $self->{escape} = 1;
401                }
402              }
403            }
404            
405            #
406        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
407          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
408            $self->{state} = 'tag open';              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
409                 not $self->{escape})) {
410              $self->{state} = TAG_OPEN_STATE;
411            !!!next-input-character;            !!!next-input-character;
412            redo A;            redo A;
413          } else {          } else {
414            #            #
415          }          }
416          } elsif ($self->{next_input_character} == 0x003E) { # >
417            if ($self->{escape} and
418                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
419              if ($self->{prev_input_character}->[0] == 0x002D and # -
420                  $self->{prev_input_character}->[1] == 0x002D) { # -
421                delete $self->{escape};
422              }
423            }
424            
425            #
426        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
427          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
428          last A; ## TODO: ok?          last A; ## TODO: ok?
429        }        }
430        # Anything else        # Anything else
431        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
432                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
433        ## Stay in the data state        ## Stay in the data state
434        !!!next-input-character;        !!!next-input-character;
# Line 482  sub _get_next_token ($) { Line 436  sub _get_next_token ($) {
436        !!!emit ($token);        !!!emit ($token);
437    
438        redo A;        redo A;
439      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
440        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
441                
442        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
443    
444        $self->{state} = 'data';        $self->{state} = DATA_STATE;
445        # next-input-character is already done        # next-input-character is already done
446    
447        unless (defined $token) {        unless (defined $token) {
448          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
449        } else {        } else {
450          !!!emit ($token);          !!!emit ($token);
451        }        }
452    
453        redo A;        redo A;
454      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
455        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
456          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
457            !!!next-input-character;            !!!next-input-character;
458            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
459            redo A;            redo A;
460          } else {          } else {
461            ## reconsume            ## reconsume
462            $self->{state} = 'data';            $self->{state} = DATA_STATE;
463    
464            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
465    
466            redo A;            redo A;
467          }          }
468        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
469          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
470            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
471            !!!next-input-character;            !!!next-input-character;
472            redo A;            redo A;
473          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
474            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
475            !!!next-input-character;            !!!next-input-character;
476            redo A;            redo A;
477          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
478                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
479            $self->{current_token}            $self->{current_token}
480              = {type => 'start tag',              = {type => START_TAG_TOKEN,
481                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
482            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
483            !!!next-input-character;            !!!next-input-character;
484            redo A;            redo A;
485          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
486                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
487            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
488                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
489            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
490            !!!next-input-character;            !!!next-input-character;
491            redo A;            redo A;
492          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
493            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
494            $self->{state} = 'data';            $self->{state} = DATA_STATE;
495            !!!next-input-character;            !!!next-input-character;
496    
497            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
498    
499            redo A;            redo A;
500          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
501            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
502            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
503            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
504            redo A;            redo A;
505          } else {          } else {
506            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
507            $self->{state} = 'data';            $self->{state} = DATA_STATE;
508            ## reconsume            ## reconsume
509    
510            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
511    
512            redo A;            redo A;
513          }          }
514        } else {        } else {
515          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
516        }        }
517      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
518        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
519            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
520          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
521          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
522              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
523                push @next_char, $self->{next_input_character};
524                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
525                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
526                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
527                  !!!next-input-character;
528                  next TAGNAME;
529                } else {
530                  $self->{next_input_character} = shift @next_char; # reconsume
531                  !!!back-next-input-character (@next_char);
532                  $self->{state} = DATA_STATE;
533    
534                  !!!emit ({type => CHARACTER_TOKEN, data => '</'});
535      
536                  redo A;
537                }
538              }
539            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
540            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
541            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
542            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
543              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
544              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
545            } else {                    $self->{next_input_character} == 0x0020 or # SP
546              !!!parse-error (type => 'unmatched end tag');                    $self->{next_input_character} == 0x003E or # >
547                      $self->{next_input_character} == 0x002F or # /
548                      $self->{next_input_character} == -1) {
549              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
550              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
551              $self->{state} = 'data';              $self->{state} = DATA_STATE;
552                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
             !!!emit ({type => 'character', data => '</'});  
   
553              redo A;              redo A;
554              } else {
555                $self->{next_input_character} = shift @next_char;
556                !!!back-next-input-character (@next_char);
557                # and consume...
558            }            }
         }  
         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;  
559          } else {          } else {
560            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
561            !!!back-next-input-character (@next_char);            # next-input-character is already done
562            # and consume...            $self->{state} = DATA_STATE;
563              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
564              redo A;
565          }          }
566        }        }
567                
568        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
569            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
570          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
571                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
572          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
573          !!!next-input-character;          !!!next-input-character;
574          redo A;          redo A;
575        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
576                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
577          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
578                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
579          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
580          !!!next-input-character;          !!!next-input-character;
581          redo A;          redo A;
582        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
583          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
584          $self->{state} = 'data';          $self->{state} = DATA_STATE;
585          !!!next-input-character;          !!!next-input-character;
586          redo A;          redo A;
587        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
588          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
589          $self->{state} = 'data';          $self->{state} = DATA_STATE;
590          # reconsume          # reconsume
591    
592          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
593    
594          redo A;          redo A;
595        } else {        } else {
596          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
597          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
598          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
599          redo A;          redo A;
600        }        }
601      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
602        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
603            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
604            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
605            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
606            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
607          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
608          !!!next-input-character;          !!!next-input-character;
609          redo A;          redo A;
610        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
611          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
612              $self->{current_token}->{first_start_tag}
613                  = not defined $self->{last_emitted_start_tag_name};
614            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
615          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
616            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
617            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
618              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
619            }            }
620          } else {          } else {
621            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
622          }          }
623          $self->{state} = 'data';          $self->{state} = DATA_STATE;
624          !!!next-input-character;          !!!next-input-character;
625    
626          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
627    
628          redo A;          redo A;
629        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 676  sub _get_next_token ($) { Line 633  sub _get_next_token ($) {
633          ## Stay in this state          ## Stay in this state
634          !!!next-input-character;          !!!next-input-character;
635          redo A;          redo A;
636        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
637          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
638          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
639              $self->{current_token}->{first_start_tag}
640                  = not defined $self->{last_emitted_start_tag_name};
641            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
642          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
643            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
644            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
645              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
646            }            }
647          } else {          } else {
648            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
649          }          }
650          $self->{state} = 'data';          $self->{state} = DATA_STATE;
651          # reconsume          # reconsume
652    
653          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
654    
655          redo A;          redo A;
656        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
657          !!!next-input-character;          !!!next-input-character;
658          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
659              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
660              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
661            # permitted slash            # permitted slash
662            #            #
663          } else {          } else {
664            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
665          }          }
666          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
667          # next-input-character is already done          # next-input-character is already done
668          redo A;          redo A;
669        } else {        } else {
# Line 716  sub _get_next_token ($) { Line 673  sub _get_next_token ($) {
673          !!!next-input-character;          !!!next-input-character;
674          redo A;          redo A;
675        }        }
676      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
677        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
678            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
679            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 726  sub _get_next_token ($) { Line 683  sub _get_next_token ($) {
683          !!!next-input-character;          !!!next-input-character;
684          redo A;          redo A;
685        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
686          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
687              $self->{current_token}->{first_start_tag}
688                  = not defined $self->{last_emitted_start_tag_name};
689            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
690          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
691            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
692            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
693              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
694            }            }
695          } else {          } else {
696            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
697          }          }
698          $self->{state} = 'data';          $self->{state} = DATA_STATE;
699          !!!next-input-character;          !!!next-input-character;
700    
701          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
702    
703          redo A;          redo A;
704        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
705                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
706          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
707                                value => ''};                                value => ''};
708          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
709          !!!next-input-character;          !!!next-input-character;
710          redo A;          redo A;
711        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
712          !!!next-input-character;          !!!next-input-character;
713          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
714              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
715              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
716            # permitted slash            # permitted slash
717            #            #
# Line 763  sub _get_next_token ($) { Line 721  sub _get_next_token ($) {
721          ## Stay in the state          ## Stay in the state
722          # next-input-character is already done          # next-input-character is already done
723          redo A;          redo A;
724        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
725          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
726          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
727              $self->{current_token}->{first_start_tag}
728                  = not defined $self->{last_emitted_start_tag_name};
729            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
730          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
731            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
732            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
733              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
734            }            }
735          } else {          } else {
736            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
737          }          }
738          $self->{state} = 'data';          $self->{state} = DATA_STATE;
739          # reconsume          # reconsume
740    
741          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
742    
743          redo A;          redo A;
744        } else {        } else {
745          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
746                                value => ''};                                value => ''};
747          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
748          !!!next-input-character;          !!!next-input-character;
749          redo A;          redo A;
750        }        }
751      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
752        my $before_leave = sub {        my $before_leave = sub {
753          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
754              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
755            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
756            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
757          } else {          } else {
758            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 808  sub _get_next_token ($) { Line 766  sub _get_next_token ($) {
766            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
767            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
768          $before_leave->();          $before_leave->();
769          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
770          !!!next-input-character;          !!!next-input-character;
771          redo A;          redo A;
772        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
773          $before_leave->();          $before_leave->();
774          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
775          !!!next-input-character;          !!!next-input-character;
776          redo A;          redo A;
777        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
778          $before_leave->();          $before_leave->();
779          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
780              $self->{current_token}->{first_start_tag}
781                  = not defined $self->{last_emitted_start_tag_name};
782            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
783          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
784            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
785            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
786              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
787            }            }
788          } else {          } else {
789            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
790          }          }
791          $self->{state} = 'data';          $self->{state} = DATA_STATE;
792          !!!next-input-character;          !!!next-input-character;
793    
794          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
795    
796          redo A;          redo A;
797        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 845  sub _get_next_token ($) { Line 804  sub _get_next_token ($) {
804          $before_leave->();          $before_leave->();
805          !!!next-input-character;          !!!next-input-character;
806          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
807              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
808              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
809            # permitted slash            # permitted slash
810            #            #
811          } else {          } else {
812            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
813          }          }
814          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
815          # next-input-character is already done          # next-input-character is already done
816          redo A;          redo A;
817        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
818          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
819          $before_leave->();          $before_leave->();
820          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
821              $self->{current_token}->{first_start_tag}
822                  = not defined $self->{last_emitted_start_tag_name};
823            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
824          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
825            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
826            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
827              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
828            }            }
829          } else {          } else {
830            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
831          }          }
832          $self->{state} = 'data';          $self->{state} = DATA_STATE;
833          # reconsume          # reconsume
834    
835          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
836    
837          redo A;          redo A;
838        } else {        } else {
# Line 882  sub _get_next_token ($) { Line 841  sub _get_next_token ($) {
841          !!!next-input-character;          !!!next-input-character;
842          redo A;          redo A;
843        }        }
844      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
845        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
846            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
847            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 892  sub _get_next_token ($) { Line 851  sub _get_next_token ($) {
851          !!!next-input-character;          !!!next-input-character;
852          redo A;          redo A;
853        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
854          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
855          !!!next-input-character;          !!!next-input-character;
856          redo A;          redo A;
857        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
858          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
859              $self->{current_token}->{first_start_tag}
860                  = not defined $self->{last_emitted_start_tag_name};
861            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
862          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
863            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
864            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
865              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
866            }            }
867          } else {          } else {
868            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
869          }          }
870          $self->{state} = 'data';          $self->{state} = DATA_STATE;
871          !!!next-input-character;          !!!next-input-character;
872    
873          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
874    
875          redo A;          redo A;
876        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
877                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
878          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
879                                value => ''};                                value => ''};
880          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
881          !!!next-input-character;          !!!next-input-character;
882          redo A;          redo A;
883        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
884          !!!next-input-character;          !!!next-input-character;
885          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
886              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
887              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
888            # permitted slash            # permitted slash
889            #            #
890          } else {          } else {
891            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
892              ## TODO: Different error type for <aa / bb> than <aa/>
893          }          }
894          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
895          # next-input-character is already done          # next-input-character is already done
896          redo A;          redo A;
897        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
898          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
899          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
900              $self->{current_token}->{first_start_tag}
901                  = not defined $self->{last_emitted_start_tag_name};
902            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
903          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
904            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
905            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
906              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
907            }            }
908          } else {          } else {
909            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
910          }          }
911          $self->{state} = 'data';          $self->{state} = DATA_STATE;
912          # reconsume          # reconsume
913    
914          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
915    
916          redo A;          redo A;
917        } else {        } else {
918          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
919                                value => ''};                                value => ''};
920          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
921          !!!next-input-character;          !!!next-input-character;
922          redo A;                  redo A;        
923        }        }
924      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
925        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
926            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
927            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 970  sub _get_next_token ($) { Line 931  sub _get_next_token ($) {
931          !!!next-input-character;          !!!next-input-character;
932          redo A;          redo A;
933        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
934          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
935          !!!next-input-character;          !!!next-input-character;
936          redo A;          redo A;
937        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
938          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
939          ## reconsume          ## reconsume
940          redo A;          redo A;
941        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
942          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
943          !!!next-input-character;          !!!next-input-character;
944          redo A;          redo A;
945        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
946          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
947              $self->{current_token}->{first_start_tag}
948                  = not defined $self->{last_emitted_start_tag_name};
949            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
950          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
951            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
952            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
953              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
954            }            }
955          } else {          } else {
956            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
957          }          }
958          $self->{state} = 'data';          $self->{state} = DATA_STATE;
959          !!!next-input-character;          !!!next-input-character;
960    
961          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
962    
963          redo A;          redo A;
964        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
965          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
966          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
967              $self->{current_token}->{first_start_tag}
968                  = not defined $self->{last_emitted_start_tag_name};
969            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
970          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
971            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
972            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
973              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
974            }            }
975          } else {          } else {
976            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
977          }          }
978          $self->{state} = 'data';          $self->{state} = DATA_STATE;
979          ## reconsume          ## reconsume
980    
981          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
982    
983          redo A;          redo A;
984        } else {        } else {
985          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
986          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
987          !!!next-input-character;          !!!next-input-character;
988          redo A;          redo A;
989        }        }
990      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
991        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
992          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
993          !!!next-input-character;          !!!next-input-character;
994          redo A;          redo A;
995        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
996          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
997          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
998          !!!next-input-character;          !!!next-input-character;
999          redo A;          redo A;
1000        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1001          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1002          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1003              $self->{current_token}->{first_start_tag}
1004                  = not defined $self->{last_emitted_start_tag_name};
1005            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1006          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1007            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1008            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1009              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1010            }            }
1011          } else {          } else {
1012            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1013          }          }
1014          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1015          ## reconsume          ## reconsume
1016    
1017          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1018    
1019          redo A;          redo A;
1020        } else {        } else {
# Line 1060  sub _get_next_token ($) { Line 1023  sub _get_next_token ($) {
1023          !!!next-input-character;          !!!next-input-character;
1024          redo A;          redo A;
1025        }        }
1026      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1027        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1028          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1029          !!!next-input-character;          !!!next-input-character;
1030          redo A;          redo A;
1031        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1032          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1033          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1034          !!!next-input-character;          !!!next-input-character;
1035          redo A;          redo A;
1036        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1037          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1038          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1039              $self->{current_token}->{first_start_tag}
1040                  = not defined $self->{last_emitted_start_tag_name};
1041            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1042          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1043            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1044            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1045              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1046            }            }
1047          } else {          } else {
1048            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1049          }          }
1050          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1051          ## reconsume          ## reconsume
1052    
1053          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1054    
1055          redo A;          redo A;
1056        } else {        } else {
# Line 1095  sub _get_next_token ($) { Line 1059  sub _get_next_token ($) {
1059          !!!next-input-character;          !!!next-input-character;
1060          redo A;          redo A;
1061        }        }
1062      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1063        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1064            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1065            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1066            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1067            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1068          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1069          !!!next-input-character;          !!!next-input-character;
1070          redo A;          redo A;
1071        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1072          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1073          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1074          !!!next-input-character;          !!!next-input-character;
1075          redo A;          redo A;
1076        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1077          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1078              $self->{current_token}->{first_start_tag}
1079                  = not defined $self->{last_emitted_start_tag_name};
1080            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1081          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1082            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1083            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1084              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1085            }            }
1086          } else {          } else {
1087            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1088          }          }
1089          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1090          !!!next-input-character;          !!!next-input-character;
1091    
1092          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1093    
1094          redo A;          redo A;
1095        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
1096          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1097          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1098              $self->{current_token}->{first_start_tag}
1099                  = not defined $self->{last_emitted_start_tag_name};
1100            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1101          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1102            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1103            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1104              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1105            }            }
1106          } else {          } else {
1107            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1108          }          }
1109          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1110          ## reconsume          ## reconsume
1111    
1112          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1113    
1114          redo A;          redo A;
1115        } else {        } else {
# Line 1153  sub _get_next_token ($) { Line 1118  sub _get_next_token ($) {
1118          !!!next-input-character;          !!!next-input-character;
1119          redo A;          redo A;
1120        }        }
1121      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1122        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1123    
1124        unless (defined $token) {        unless (defined $token) {
1125          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1126        } else {        } else {
1127          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1128            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1129          ## 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"
1130        }        }
1131    
1132        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1133        # next-input-character is already done        # next-input-character is already done
1134        redo A;        redo A;
1135      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1136        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1137                
1138        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1139    
1140        BC: {        BC: {
1141          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1142            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1143            !!!next-input-character;            !!!next-input-character;
1144    
1145            !!!emit ($token);            !!!emit ($token);
1146    
1147            redo A;            redo A;
1148          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1149            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1150            ## reconsume            ## reconsume
1151    
1152            !!!emit ($token);            !!!emit ($token);
# Line 1192  sub _get_next_token ($) { Line 1158  sub _get_next_token ($) {
1158            redo BC;            redo BC;
1159          }          }
1160        } # BC        } # BC
1161      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1162        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1163    
1164        my @next_char;        my @next_char;
# Line 1202  sub _get_next_token ($) { Line 1168  sub _get_next_token ($) {
1168          !!!next-input-character;          !!!next-input-character;
1169          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1170          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1171            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1172            $self->{state} = 'comment';            $self->{state} = COMMENT_START_STATE;
1173            !!!next-input-character;            !!!next-input-character;
1174            redo A;            redo A;
1175          }          }
# Line 1234  sub _get_next_token ($) { Line 1200  sub _get_next_token ($) {
1200                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1201                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1202                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1203                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1204                      !!!next-input-character;                      !!!next-input-character;
1205                      redo A;                      redo A;
1206                    }                    }
# Line 1245  sub _get_next_token ($) { Line 1211  sub _get_next_token ($) {
1211          }          }
1212        }        }
1213    
1214        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1215        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1216        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1217        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1218        redo A;        redo A;
1219                
1220        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1221        ## 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?
1222      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1223        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1224          $self->{state} = 'comment dash';          $self->{state} = COMMENT_START_DASH_STATE;
1225            !!!next-input-character;
1226            redo A;
1227          } elsif ($self->{next_input_character} == 0x003E) { # >
1228            !!!parse-error (type => 'bogus comment');
1229            $self->{state} = DATA_STATE;
1230          !!!next-input-character;          !!!next-input-character;
1231    
1232            !!!emit ($self->{current_token}); # comment
1233    
1234          redo A;          redo A;
1235        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1236          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238            ## reconsume
1239    
1240            !!!emit ($self->{current_token}); # comment
1241    
1242            redo A;
1243          } else {
1244            $self->{current_token}->{data} # comment
1245                .= chr ($self->{next_input_character});
1246            $self->{state} = COMMENT_STATE;
1247            !!!next-input-character;
1248            redo A;
1249          }
1250        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1251          if ($self->{next_input_character} == 0x002D) { # -
1252            $self->{state} = COMMENT_END_STATE;
1253            !!!next-input-character;
1254            redo A;
1255          } elsif ($self->{next_input_character} == 0x003E) { # >
1256            !!!parse-error (type => 'bogus comment');
1257            $self->{state} = DATA_STATE;
1258            !!!next-input-character;
1259    
1260            !!!emit ($self->{current_token}); # comment
1261    
1262            redo A;
1263          } elsif ($self->{next_input_character} == -1) {
1264            !!!parse-error (type => 'unclosed comment');
1265            $self->{state} = DATA_STATE;
1266            ## reconsume
1267    
1268            !!!emit ($self->{current_token}); # comment
1269    
1270            redo A;
1271          } else {
1272            $self->{current_token}->{data} # comment
1273                .= '-' . chr ($self->{next_input_character});
1274            $self->{state} = COMMENT_STATE;
1275            !!!next-input-character;
1276            redo A;
1277          }
1278        } elsif ($self->{state} == COMMENT_STATE) {
1279          if ($self->{next_input_character} == 0x002D) { # -
1280            $self->{state} = COMMENT_END_DASH_STATE;
1281            !!!next-input-character;
1282            redo A;
1283          } elsif ($self->{next_input_character} == -1) {
1284            !!!parse-error (type => 'unclosed comment');
1285            $self->{state} = DATA_STATE;
1286          ## reconsume          ## reconsume
1287    
1288          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1289    
1290          redo A;          redo A;
1291        } else {        } else {
# Line 1273  sub _get_next_token ($) { Line 1294  sub _get_next_token ($) {
1294          !!!next-input-character;          !!!next-input-character;
1295          redo A;          redo A;
1296        }        }
1297      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1298        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1299          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1300          !!!next-input-character;          !!!next-input-character;
1301          redo A;          redo A;
1302        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1303          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1304          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1305          ## reconsume          ## reconsume
1306    
1307          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1308    
1309          redo A;          redo A;
1310        } else {        } else {
1311          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1312          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1313          !!!next-input-character;          !!!next-input-character;
1314          redo A;          redo A;
1315        }        }
1316      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1317        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1319          !!!next-input-character;          !!!next-input-character;
1320    
1321          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1322    
1323          redo A;          redo A;
1324        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1310  sub _get_next_token ($) { Line 1329  sub _get_next_token ($) {
1329          redo A;          redo A;
1330        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1331          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1332          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1333          ## reconsume          ## reconsume
1334    
1335          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1336    
1337          redo A;          redo A;
1338        } else {        } else {
1339          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1340          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1341          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1342          !!!next-input-character;          !!!next-input-character;
1343          redo A;          redo A;
1344        }        }
1345      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1346        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1347            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1348            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1349            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1350            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1351          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1352          !!!next-input-character;          !!!next-input-character;
1353          redo A;          redo A;
1354        } else {        } else {
1355          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1356          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1357          ## reconsume          ## reconsume
1358          redo A;          redo A;
1359        }        }
1360      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1361        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1362            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1363            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1348  sub _get_next_token ($) { Line 1366  sub _get_next_token ($) {
1366          ## Stay in the state          ## Stay in the state
1367          !!!next-input-character;          !!!next-input-character;
1368          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
 ## 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;  
1369        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1370          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1371          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1372          !!!next-input-character;          !!!next-input-character;
1373    
1374          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1375    
1376          redo A;          redo A;
1377        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1378          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1379          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1380          ## reconsume          ## reconsume
1381    
1382          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1383    
1384          redo A;          redo A;
1385        } else {        } else {
1386          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1387                            name => chr ($self->{next_input_character}),              = {type => DOCTYPE_TOKEN,
1388                            error => 1};                 name => chr ($self->{next_input_character}),
1389                   correct => 1};
1390  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1391          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1392          !!!next-input-character;          !!!next-input-character;
1393          redo A;          redo A;
1394        }        }
1395      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1396    ## ISSUE: Redundant "First," in the spec.
1397        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1398            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1399            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1400            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1401            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1402          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
         $self->{state} = 'after DOCTYPE name';  
1403          !!!next-input-character;          !!!next-input-character;
1404          redo A;          redo A;
1405        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1406          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
1407          !!!next-input-character;          !!!next-input-character;
1408    
1409          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1410    
1411          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
         $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE  
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
1412        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1413          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1414          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
1415          ## reconsume          ## reconsume
1416    
1417          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1418          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1419    
1420          redo A;          redo A;
1421        } else {        } else {
1422          $self->{current_token}->{name}          $self->{current_token}->{name}
1423            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1424          ## Stay in the state          ## Stay in the state
1425          !!!next-input-character;          !!!next-input-character;
1426          redo A;          redo A;
1427        }        }
1428      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1429        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1430            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1431            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1436  sub _get_next_token ($) { Line 1435  sub _get_next_token ($) {
1435          !!!next-input-character;          !!!next-input-character;
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1438          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1439            !!!next-input-character;
1440    
1441            !!!emit ($self->{current_token}); # DOCTYPE
1442    
1443            redo A;
1444          } elsif ($self->{next_input_character} == -1) {
1445            !!!parse-error (type => 'unclosed DOCTYPE');
1446            $self->{state} = DATA_STATE;
1447            ## reconsume
1448    
1449            delete $self->{current_token}->{correct};
1450            !!!emit ($self->{current_token}); # DOCTYPE
1451    
1452            redo A;
1453          } elsif ($self->{next_input_character} == 0x0050 or # P
1454                   $self->{next_input_character} == 0x0070) { # p
1455            !!!next-input-character;
1456            if ($self->{next_input_character} == 0x0055 or # U
1457                $self->{next_input_character} == 0x0075) { # u
1458              !!!next-input-character;
1459              if ($self->{next_input_character} == 0x0042 or # B
1460                  $self->{next_input_character} == 0x0062) { # b
1461                !!!next-input-character;
1462                if ($self->{next_input_character} == 0x004C or # L
1463                    $self->{next_input_character} == 0x006C) { # l
1464                  !!!next-input-character;
1465                  if ($self->{next_input_character} == 0x0049 or # I
1466                      $self->{next_input_character} == 0x0069) { # i
1467                    !!!next-input-character;
1468                    if ($self->{next_input_character} == 0x0043 or # C
1469                        $self->{next_input_character} == 0x0063) { # c
1470                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1471                      !!!next-input-character;
1472                      redo A;
1473                    }
1474                  }
1475                }
1476              }
1477            }
1478    
1479            #
1480          } elsif ($self->{next_input_character} == 0x0053 or # S
1481                   $self->{next_input_character} == 0x0073) { # s
1482            !!!next-input-character;
1483            if ($self->{next_input_character} == 0x0059 or # Y
1484                $self->{next_input_character} == 0x0079) { # y
1485              !!!next-input-character;
1486              if ($self->{next_input_character} == 0x0053 or # S
1487                  $self->{next_input_character} == 0x0073) { # s
1488                !!!next-input-character;
1489                if ($self->{next_input_character} == 0x0054 or # T
1490                    $self->{next_input_character} == 0x0074) { # t
1491                  !!!next-input-character;
1492                  if ($self->{next_input_character} == 0x0045 or # E
1493                      $self->{next_input_character} == 0x0065) { # e
1494                    !!!next-input-character;
1495                    if ($self->{next_input_character} == 0x004D or # M
1496                        $self->{next_input_character} == 0x006D) { # m
1497                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1498                      !!!next-input-character;
1499                      redo A;
1500                    }
1501                  }
1502                }
1503              }
1504            }
1505    
1506            #
1507          } else {
1508            !!!next-input-character;
1509            #
1510          }
1511    
1512          !!!parse-error (type => 'string after DOCTYPE name');
1513          $self->{state} = BOGUS_DOCTYPE_STATE;
1514          # next-input-character is already done
1515          redo A;
1516        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1517          if ({
1518                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1519                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1520              }->{$self->{next_input_character}}) {
1521            ## Stay in the state
1522            !!!next-input-character;
1523            redo A;
1524          } elsif ($self->{next_input_character} eq 0x0022) { # "
1525            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1526            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1527            !!!next-input-character;
1528            redo A;
1529          } elsif ($self->{next_input_character} eq 0x0027) { # '
1530            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1531            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1532            !!!next-input-character;
1533            redo A;
1534          } elsif ($self->{next_input_character} eq 0x003E) { # >
1535            !!!parse-error (type => 'no PUBLIC literal');
1536    
1537            $self->{state} = DATA_STATE;
1538            !!!next-input-character;
1539    
1540            delete $self->{current_token}->{correct};
1541            !!!emit ($self->{current_token}); # DOCTYPE
1542    
1543            redo A;
1544          } elsif ($self->{next_input_character} == -1) {
1545            !!!parse-error (type => 'unclosed DOCTYPE');
1546    
1547            $self->{state} = DATA_STATE;
1548            ## reconsume
1549    
1550            delete $self->{current_token}->{correct};
1551            !!!emit ($self->{current_token}); # DOCTYPE
1552    
1553            redo A;
1554          } else {
1555            !!!parse-error (type => 'string after PUBLIC');
1556            $self->{state} = BOGUS_DOCTYPE_STATE;
1557            !!!next-input-character;
1558            redo A;
1559          }
1560        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1561          if ($self->{next_input_character} == 0x0022) { # "
1562            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1563            !!!next-input-character;
1564            redo A;
1565          } elsif ($self->{next_input_character} == 0x003E) { # >
1566            !!!parse-error (type => 'unclosed PUBLIC literal');
1567    
1568            $self->{state} = DATA_STATE;
1569            !!!next-input-character;
1570    
1571            delete $self->{current_token}->{correct};
1572            !!!emit ($self->{current_token}); # DOCTYPE
1573    
1574            redo A;
1575          } elsif ($self->{next_input_character} == -1) {
1576            !!!parse-error (type => 'unclosed PUBLIC literal');
1577    
1578            $self->{state} = DATA_STATE;
1579            ## reconsume
1580    
1581            delete $self->{current_token}->{correct};
1582            !!!emit ($self->{current_token}); # DOCTYPE
1583    
1584            redo A;
1585          } else {
1586            $self->{current_token}->{public_identifier} # DOCTYPE
1587                .= chr $self->{next_input_character};
1588            ## Stay in the state
1589            !!!next-input-character;
1590            redo A;
1591          }
1592        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1593          if ($self->{next_input_character} == 0x0027) { # '
1594            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1595            !!!next-input-character;
1596            redo A;
1597          } elsif ($self->{next_input_character} == 0x003E) { # >
1598            !!!parse-error (type => 'unclosed PUBLIC literal');
1599    
1600            $self->{state} = DATA_STATE;
1601            !!!next-input-character;
1602    
1603            delete $self->{current_token}->{correct};
1604            !!!emit ($self->{current_token}); # DOCTYPE
1605    
1606            redo A;
1607          } elsif ($self->{next_input_character} == -1) {
1608            !!!parse-error (type => 'unclosed PUBLIC literal');
1609    
1610            $self->{state} = DATA_STATE;
1611            ## reconsume
1612    
1613            delete $self->{current_token}->{correct};
1614            !!!emit ($self->{current_token}); # DOCTYPE
1615    
1616            redo A;
1617          } else {
1618            $self->{current_token}->{public_identifier} # DOCTYPE
1619                .= chr $self->{next_input_character};
1620            ## Stay in the state
1621            !!!next-input-character;
1622            redo A;
1623          }
1624        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1625          if ({
1626                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1627                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1628              }->{$self->{next_input_character}}) {
1629            ## Stay in the state
1630            !!!next-input-character;
1631            redo A;
1632          } elsif ($self->{next_input_character} == 0x0022) { # "
1633            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1634            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1635            !!!next-input-character;
1636            redo A;
1637          } elsif ($self->{next_input_character} == 0x0027) { # '
1638            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1639            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1640            !!!next-input-character;
1641            redo A;
1642          } elsif ($self->{next_input_character} == 0x003E) { # >
1643            $self->{state} = DATA_STATE;
1644          !!!next-input-character;          !!!next-input-character;
1645    
1646          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1647    
1648          redo A;          redo A;
1649        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1650          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1651          $self->{state} = 'data';  
1652            $self->{state} = DATA_STATE;
1653          ## reconsume          ## reconsume
1654    
1655            delete $self->{current_token}->{correct};
1656          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1657    
1658          redo A;          redo A;
1659        } else {        } else {
1660          !!!parse-error (type => 'string after DOCTYPE name');          !!!parse-error (type => 'string after PUBLIC literal');
1661          $self->{current_token}->{error} = 1; # DOCTYPE          $self->{state} = BOGUS_DOCTYPE_STATE;
         $self->{state} = 'bogus DOCTYPE';  
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        }        }
1665      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1666          if ({
1667                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1668                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1669              }->{$self->{next_input_character}}) {
1670            ## Stay in the state
1671            !!!next-input-character;
1672            redo A;
1673          } elsif ($self->{next_input_character} == 0x0022) { # "
1674            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1675            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1676            !!!next-input-character;
1677            redo A;
1678          } elsif ($self->{next_input_character} == 0x0027) { # '
1679            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1680            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1681            !!!next-input-character;
1682            redo A;
1683          } elsif ($self->{next_input_character} == 0x003E) { # >
1684            !!!parse-error (type => 'no SYSTEM literal');
1685            $self->{state} = DATA_STATE;
1686            !!!next-input-character;
1687    
1688            delete $self->{current_token}->{correct};
1689            !!!emit ($self->{current_token}); # DOCTYPE
1690    
1691            redo A;
1692          } elsif ($self->{next_input_character} == -1) {
1693            !!!parse-error (type => 'unclosed DOCTYPE');
1694    
1695            $self->{state} = DATA_STATE;
1696            ## reconsume
1697    
1698            delete $self->{current_token}->{correct};
1699            !!!emit ($self->{current_token}); # DOCTYPE
1700    
1701            redo A;
1702          } else {
1703            !!!parse-error (type => 'string after SYSTEM');
1704            $self->{state} = BOGUS_DOCTYPE_STATE;
1705            !!!next-input-character;
1706            redo A;
1707          }
1708        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1709          if ($self->{next_input_character} == 0x0022) { # "
1710            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1711            !!!next-input-character;
1712            redo A;
1713          } elsif ($self->{next_input_character} == 0x003E) { # >
1714            !!!parse-error (type => 'unclosed PUBLIC literal');
1715    
1716            $self->{state} = DATA_STATE;
1717            !!!next-input-character;
1718    
1719            delete $self->{current_token}->{correct};
1720            !!!emit ($self->{current_token}); # DOCTYPE
1721    
1722            redo A;
1723          } elsif ($self->{next_input_character} == -1) {
1724            !!!parse-error (type => 'unclosed SYSTEM literal');
1725    
1726            $self->{state} = DATA_STATE;
1727            ## reconsume
1728    
1729            delete $self->{current_token}->{correct};
1730            !!!emit ($self->{current_token}); # DOCTYPE
1731    
1732            redo A;
1733          } else {
1734            $self->{current_token}->{system_identifier} # DOCTYPE
1735                .= chr $self->{next_input_character};
1736            ## Stay in the state
1737            !!!next-input-character;
1738            redo A;
1739          }
1740        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1741          if ($self->{next_input_character} == 0x0027) { # '
1742            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1743            !!!next-input-character;
1744            redo A;
1745          } elsif ($self->{next_input_character} == 0x003E) { # >
1746            !!!parse-error (type => 'unclosed PUBLIC literal');
1747    
1748            $self->{state} = DATA_STATE;
1749            !!!next-input-character;
1750    
1751            delete $self->{current_token}->{correct};
1752            !!!emit ($self->{current_token}); # DOCTYPE
1753    
1754            redo A;
1755          } elsif ($self->{next_input_character} == -1) {
1756            !!!parse-error (type => 'unclosed SYSTEM literal');
1757    
1758            $self->{state} = DATA_STATE;
1759            ## reconsume
1760    
1761            delete $self->{current_token}->{correct};
1762            !!!emit ($self->{current_token}); # DOCTYPE
1763    
1764            redo A;
1765          } else {
1766            $self->{current_token}->{system_identifier} # DOCTYPE
1767                .= chr $self->{next_input_character};
1768            ## Stay in the state
1769            !!!next-input-character;
1770            redo A;
1771          }
1772        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1773          if ({
1774                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1775                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1776              }->{$self->{next_input_character}}) {
1777            ## Stay in the state
1778            !!!next-input-character;
1779            redo A;
1780          } elsif ($self->{next_input_character} == 0x003E) { # >
1781            $self->{state} = DATA_STATE;
1782            !!!next-input-character;
1783    
1784            !!!emit ($self->{current_token}); # DOCTYPE
1785    
1786            redo A;
1787          } elsif ($self->{next_input_character} == -1) {
1788            !!!parse-error (type => 'unclosed DOCTYPE');
1789    
1790            $self->{state} = DATA_STATE;
1791            ## reconsume
1792    
1793            delete $self->{current_token}->{correct};
1794            !!!emit ($self->{current_token}); # DOCTYPE
1795    
1796            redo A;
1797          } else {
1798            !!!parse-error (type => 'string after SYSTEM literal');
1799            $self->{state} = BOGUS_DOCTYPE_STATE;
1800            !!!next-input-character;
1801            redo A;
1802          }
1803        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1804        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1805          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1806          !!!next-input-character;          !!!next-input-character;
1807    
1808            delete $self->{current_token}->{correct};
1809          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1810    
1811          redo A;          redo A;
1812        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1813          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1814          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1815          ## reconsume          ## reconsume
1816    
1817            delete $self->{current_token}->{correct};
1818          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1819    
1820          redo A;          redo A;
1821        } else {        } else {
# Line 1490  sub _get_next_token ($) { Line 1831  sub _get_next_token ($) {
1831    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1832  } # _get_next_token  } # _get_next_token
1833    
1834  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1835    my $self = shift;    my ($self, $in_attr) = @_;
1836      
1837    if ($self->{next_input_character} == 0x0023) { # #    if ({
1838           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1839           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1840          }->{$self->{next_input_character}}) {
1841        ## Don't consume
1842        ## No error
1843        return undef;
1844      } elsif ($self->{next_input_character} == 0x0023) { # #
1845      !!!next-input-character;      !!!next-input-character;
1846      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1847          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1848        my $num;        my $code;
1849        X: {        X: {
1850          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1851          !!!next-input-character;          !!!next-input-character;
1852          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1853              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1854            $num ||= 0;            $code ||= 0;
1855            $num *= 0x10;            $code *= 0x10;
1856            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1857            redo X;            redo X;
1858          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1859                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1860            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1861            $num ||= 0;            $code *= 0x10;
1862            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1863            redo X;            redo X;
1864          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1865                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1866            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1867            $num ||= 0;            $code *= 0x10;
1868            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1869            redo X;            redo X;
1870          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1871            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1872              !!!back-next-input-character ($x_char, $self->{next_input_character});
1873            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1874            return undef;            return undef;
1875          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1876            !!!next-input-character;            !!!next-input-character;
# Line 1532  sub _tokenize_attempt_to_consume_an_enti Line 1878  sub _tokenize_attempt_to_consume_an_enti
1878            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1879          }          }
1880    
1881          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1882          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1883          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1884            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1885            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1886          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1887            ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>          } elsif ($code == 0x000D) {
1888            ## ISSUE: Not in the spec yet; parse error?            !!!parse-error (type => 'CR character reference');
1889            $num = $c1_entity_char->{$num};            $code = 0x000A;
1890            } elsif (0x80 <= $code and $code <= 0x9F) {
1891              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1892              $code = $c1_entity_char->{$code};
1893          }          }
1894    
1895          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code,
1896                    has_reference => 1};
1897        } # X        } # X
1898      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1899               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1564  sub _tokenize_attempt_to_consume_an_enti Line 1914  sub _tokenize_attempt_to_consume_an_enti
1914          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1915        }        }
1916    
1917        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1918        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1919          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1920          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1921            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1922            $code = 0xFFFD;
1923          } elsif ($code == 0x000D) {
1924            !!!parse-error (type => 'CR character reference');
1925            $code = 0x000A;
1926        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1927          ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
         ## ISSUE: Not in the spec yet; parse error?  
1928          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1929        }        }
1930                
1931        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1932      } else {      } else {
1933        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1934        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1589  sub _tokenize_attempt_to_consume_an_enti Line 1943  sub _tokenize_attempt_to_consume_an_enti
1943      !!!next-input-character;      !!!next-input-character;
1944    
1945      my $value = $entity_name;      my $value = $entity_name;
1946      my $match;      my $match = 0;
1947        require Whatpm::_NamedEntityList;
1948        our $EntityChar;
1949    
1950      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1951             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1952             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1953               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1954              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1955               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1956              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1957               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1958                $self->{next_input_character} == 0x003B)) { # ;
1959        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1960        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1961          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1962          $match = 1;            $value = $EntityChar->{$entity_name};
1963              $match = 1;
1964              !!!next-input-character;
1965              last;
1966            } else {
1967              $value = $EntityChar->{$entity_name};
1968              $match = -1;
1969              !!!next-input-character;
1970            }
1971        } else {        } else {
1972          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1973            $match *= 2;
1974            !!!next-input-character;
1975        }        }
       !!!next-input-character;  
1976      }      }
1977            
1978      if ($match) {      if ($match > 0) {
1979        if ($self->{next_input_character} == 0x003B) { # ;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1980          !!!next-input-character;      } elsif ($match < 0) {
1981          !!!parse-error (type => 'no refc');
1982          if ($in_attr and $match < -1) {
1983            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1984        } else {        } else {
1985          !!!parse-error (type => 'refc');          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1986        }        }
   
       return {type => 'character', data => $value};  
1987      } else {      } else {
1988        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1989        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1990        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1991      }      }
1992    } else {    } else {
1993      ## no characters are consumed      ## no characters are consumed
# Line 1636  sub _initialize_tree_constructor ($) { Line 2002  sub _initialize_tree_constructor ($) {
2002    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
2003    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
2004    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
2005    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
2006  } # _initialize_tree_constructor  } # _initialize_tree_constructor
2007    
2008  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1663  sub _construct_tree ($) { Line 2029  sub _construct_tree ($) {
2029        
2030    !!!next-token;    !!!next-token;
2031    
2032    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2033    undef $self->{form_element};    undef $self->{form_element};
2034    undef $self->{head_element};    undef $self->{head_element};
2035    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1676  sub _construct_tree ($) { Line 2042  sub _construct_tree ($) {
2042    
2043  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2044    my $self = shift;    my $self = shift;
2045    B: {    INITIAL: {
2046        if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2047          if ($token->{error}) {        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2048            ## ISSUE: Spec currently left this case undefined.        ## error, switch to a conformance checking mode for another
2049            !!!parse-error (type => 'bogus DOCTYPE');        ## language.
2050          }        my $doctype_name = $token->{name};
2051          my $doctype = $self->{document}->create_document_type_definition        $doctype_name = '' unless defined $doctype_name;
2052            ($token->{name});        $doctype_name =~ tr/a-z/A-Z/;
2053          $self->{document}->append_child ($doctype);        if (not defined $token->{name} or # <!DOCTYPE>
2054          #$phase = 'root element';            defined $token->{public_identifier} or
2055          !!!next-token;            defined $token->{system_identifier}) {
2056          #redo B;          !!!parse-error (type => 'not HTML5');
2057          return;        } elsif ($doctype_name ne 'HTML') {
2058        } elsif ({          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2059                  comment => 1,          !!!parse-error (type => 'not HTML5');
2060                  'start tag' => 1,        }
2061                  'end tag' => 1,        
2062                  'end-of-file' => 1,        my $doctype = $self->{document}->create_document_type_definition
2063                 }->{$token->{type}}) {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2064          ## ISSUE: Spec currently left this case undefined.        $doctype->public_id ($token->{public_identifier})
2065          !!!parse-error (type => 'missing DOCTYPE');            if defined $token->{public_identifier};
2066          #$phase = 'root element';        $doctype->system_id ($token->{system_identifier})
2067          ## reprocess            if defined $token->{system_identifier};
2068          #redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
2069          return;        ## ISSUE: internalSubset = null??
2070        } elsif ($token->{type} eq 'character') {        $self->{document}->append_child ($doctype);
2071          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        
2072            $self->{document}->manakai_append_text ($1);        if (not $token->{correct} or $doctype_name ne 'HTML') {
2073            ## ISSUE: DOM3 Core does not allow Document > Text          $self->{document}->manakai_compat_mode ('quirks');
2074            unless (length $token->{data}) {        } elsif (defined $token->{public_identifier}) {
2075              ## Stay in the phase          my $pubid = $token->{public_identifier};
2076              !!!next-token;          $pubid =~ tr/a-z/A-z/;
2077              redo B;          if ({
2078              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
2079              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2080              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2081              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
2082              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
2083              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
2084              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
2085              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
2086              "-//IETF//DTD HTML 2.0//EN" => 1,
2087              "-//IETF//DTD HTML 2.1E//EN" => 1,
2088              "-//IETF//DTD HTML 3.0//EN" => 1,
2089              "-//IETF//DTD HTML 3.0//EN//" => 1,
2090              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
2091              "-//IETF//DTD HTML 3.2//EN" => 1,
2092              "-//IETF//DTD HTML 3//EN" => 1,
2093              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
2094              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
2095              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
2096              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
2097              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
2098              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
2099              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
2100              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
2101              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
2102              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
2103              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
2104              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
2105              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
2106              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
2107              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
2108              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
2109              "-//IETF//DTD HTML STRICT//EN" => 1,
2110              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
2111              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
2112              "-//IETF//DTD HTML//EN" => 1,
2113              "-//IETF//DTD HTML//EN//2.0" => 1,
2114              "-//IETF//DTD HTML//EN//3.0" => 1,
2115              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
2116              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
2117              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
2118              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
2119              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
2120              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
2121              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
2122              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
2123              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2124              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2125              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2126              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2127              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2128              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
2129              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
2130              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
2131              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
2132              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
2133              "-//W3C//DTD HTML 3.2//EN" => 1,
2134              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
2135              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
2136              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
2137              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
2138              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
2139              "-//W3C//DTD W3 HTML//EN" => 1,
2140              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
2141              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
2142              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
2143              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
2144              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
2145              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2146              "HTML" => 1,
2147            }->{$pubid}) {
2148              $self->{document}->manakai_compat_mode ('quirks');
2149            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2150                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2151              if (defined $token->{system_identifier}) {
2152                $self->{document}->manakai_compat_mode ('quirks');
2153              } else {
2154                $self->{document}->manakai_compat_mode ('limited quirks');
2155            }            }
2156            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
2157                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
2158              $self->{document}->manakai_compat_mode ('limited quirks');
2159          }          }
         ## 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";  
2160        }        }
2161      } # B        if (defined $token->{system_identifier}) {
2162            my $sysid = $token->{system_identifier};
2163            $sysid =~ tr/A-Z/a-z/;
2164            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2165              $self->{document}->manakai_compat_mode ('quirks');
2166            }
2167          }
2168          
2169          ## Go to the root element phase.
2170          !!!next-token;
2171          return;
2172        } elsif ({
2173                  START_TAG_TOKEN, 1,
2174                  END_TAG_TOKEN, 1,
2175                  END_OF_FILE_TOKEN, 1,
2176                 }->{$token->{type}}) {
2177          !!!parse-error (type => 'no DOCTYPE');
2178          $self->{document}->manakai_compat_mode ('quirks');
2179          ## Go to the root element phase
2180          ## reprocess
2181          return;
2182        } elsif ($token->{type} == CHARACTER_TOKEN) {
2183          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2184            ## Ignore the token
2185    
2186            unless (length $token->{data}) {
2187              ## Stay in the phase
2188              !!!next-token;
2189              redo INITIAL;
2190            }
2191          }
2192    
2193          !!!parse-error (type => 'no DOCTYPE');
2194          $self->{document}->manakai_compat_mode ('quirks');
2195          ## Go to the root element phase
2196          ## reprocess
2197          return;
2198        } elsif ($token->{type} == COMMENT_TOKEN) {
2199          my $comment = $self->{document}->create_comment ($token->{data});
2200          $self->{document}->append_child ($comment);
2201          
2202          ## Stay in the phase.
2203          !!!next-token;
2204          redo INITIAL;
2205        } else {
2206          die "$0: $token->{type}: Unknown token type";
2207        }
2208      } # INITIAL
2209  } # _tree_construction_initial  } # _tree_construction_initial
2210    
2211  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2212    my $self = shift;    my $self = shift;
2213        
2214    B: {    B: {
2215        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2216          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2217          ## Ignore the token          ## Ignore the token
2218          ## Stay in the phase          ## Stay in the phase
2219          !!!next-token;          !!!next-token;
2220          redo B;          redo B;
2221        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2222          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2223          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2224          ## Stay in the phase          ## Stay in the phase
2225          !!!next-token;          !!!next-token;
2226          redo B;          redo B;
2227        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2228          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2229            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2230            ## ISSUE: DOM3 Core does not allow Document > Text  
2231            unless (length $token->{data}) {            unless (length $token->{data}) {
2232              ## Stay in the phase              ## Stay in the phase
2233              !!!next-token;              !!!next-token;
2234              redo B;              redo B;
2235            }            }
2236          }          }
2237    
2238            $self->{application_cache_selection}->(undef);
2239    
2240            #
2241          } elsif ($token->{type} == START_TAG_TOKEN) {
2242            if ($token->{tag_name} eq 'html' and
2243                $token->{attributes}->{manifest}) {
2244              $self->{application_cache_selection}
2245                   ->($token->{attributes}->{manifest}->{value});
2246              ## ISSUE: No relative reference resolution?
2247            } else {
2248              $self->{application_cache_selection}->(undef);
2249            }
2250    
2251            ## ISSUE: There is an issue in the spec
2252          #          #
2253        } elsif ({        } elsif ({
2254                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2255                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2256                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2257            $self->{application_cache_selection}->(undef);
2258    
2259          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2260          #          #
2261        } else {        } else {
2262          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2263        }        }
2264    
2265        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2266        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2267        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2268        ## reprocess        ## reprocess
2269        #redo B;        #redo B;
2270        return;        return; ## Go to the main phase.
2271    } # B    } # B
2272  } # _tree_construction_root_element  } # _tree_construction_root_element
2273    
# Line 1782  sub _reset_insertion_mode ($) { Line 2283  sub _reset_insertion_mode ($) {
2283            
2284      ## Step 3      ## Step 3
2285      S3: {      S3: {
2286        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2287        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2288          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2289              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2290            #        ## The second "if" is in the scope of the first "if"!?
2291          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2292            $node = $self->{inner_html_node};          $last = 1;
2293            if (defined $self->{inner_html_node}) {
2294              if ($self->{inner_html_node}->[1] eq 'td' or
2295                  $self->{inner_html_node}->[1] eq 'th') {
2296                #
2297              } else {
2298                $node = $self->{inner_html_node};
2299              }
2300          }          }
2301        }        }
2302            
2303        ## Step 4..13        ## Step 4..13
2304        my $new_mode = {        my $new_mode = {
2305                        select => 'in select',                        select => IN_SELECT_IM,
2306                        td => 'in cell',                        td => IN_CELL_IM,
2307                        th => 'in cell',                        th => IN_CELL_IM,
2308                        tr => 'in row',                        tr => IN_ROW_IM,
2309                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2310                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2311                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2312                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2313                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2314                        table => 'in table',                        table => IN_TABLE_IM,
2315                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2316                        body => 'in body',                        body => IN_BODY_IM,
2317                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2318                       }->{$node->[1]};                       }->{$node->[1]};
2319        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2320                
2321        ## Step 14        ## Step 14
2322        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2323          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2324            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2325          } else {          } else {
2326            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2327          }          }
2328          return;          return;
2329        }        }
2330                
2331        ## Step 15        ## Step 15
2332        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2333                
2334        ## Step 16        ## Step 16
2335        $i--;        $i--;
# Line 1835  sub _reset_insertion_mode ($) { Line 2343  sub _reset_insertion_mode ($) {
2343  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2344    my $self = shift;    my $self = shift;
2345    
   my $phase = 'main';  
   
2346    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2347    
2348    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1919  sub _tree_construction_main ($) { Line 2425  sub _tree_construction_main ($) {
2425      }      }
2426    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2427    
2428    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2429      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2430      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2431      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2432       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2433        ->append_child ($style_el);      my $el;
2434      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2435                  
2436        ## Step 2
2437        $insert->($el); # /context node/->append_child ($el)
2438    
2439        ## Step 3
2440        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2441        delete $self->{escape}; # MUST
2442    
2443        ## Step 4
2444      my $text = '';      my $text = '';
2445      !!!next-token;      !!!next-token;
2446      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2447        $text .= $token->{data};        $text .= $token->{data};
2448        !!!next-token;        !!!next-token;
2449      } # stop if non-character token or tokenizer stops tokenising      }
2450    
2451        ## Step 5
2452      if (length $text) {      if (length $text) {
2453        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2454          $el->append_child ($text);
2455      }      }
2456        
2457      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2458                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2459      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2460        ## Step 7
2461        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2462        ## Ignore the token        ## Ignore the token
2463      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2464        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2465        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2466          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2467        } else {
2468          die "$0: $content_model_flag in parse_rcdata";
2469      }      }
2470      !!!next-token;      !!!next-token;
2471    }; # $style_start_tag    }; # $parse_rcdata
2472    
2473    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2474        my $insert = $_[0];
2475      my $script_el;      my $script_el;
2476      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2477      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2478    
2479      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2480        delete $self->{escape}; # MUST
2481            
2482      my $text = '';      my $text = '';
2483      !!!next-token;      !!!next-token;
2484      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2485        $text .= $token->{data};        $text .= $token->{data};
2486        !!!next-token;        !!!next-token;
2487      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 1965  sub _tree_construction_main ($) { Line 2489  sub _tree_construction_main ($) {
2489        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2490      }      }
2491                                
2492      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2493    
2494      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2495          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2496        ## Ignore the token        ## Ignore the token
2497      } else {      } else {
# Line 1981  sub _tree_construction_main ($) { Line 2505  sub _tree_construction_main ($) {
2505      } else {      } else {
2506        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2507        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2508          
2509        (($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);  
2510                
2511        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2512                
# Line 2177  sub _tree_construction_main ($) { Line 2700  sub _tree_construction_main ($) {
2700    }; # $formatting_end_tag    }; # $formatting_end_tag
2701    
2702    my $insert_to_current = sub {    my $insert_to_current = sub {
2703      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2704    }; # $insert_to_current    }; # $insert_to_current
2705    
2706    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2211  sub _tree_construction_main ($) { Line 2734  sub _tree_construction_main ($) {
2734                         }                         }
2735    }; # $insert_to_foster    }; # $insert_to_foster
2736    
2737    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') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
         }  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 form => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         undef $self->{form_element} if $token->{tag_name} eq 'form';  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
2738    
2739    B: {    B: {
2740      if ($phase eq 'main') {      if ($token->{type} == DOCTYPE_TOKEN) {
2741        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
2742          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
2743          ## Ignore the token        ## Stay in the phase
2744          ## Stay in the phase        !!!next-token;
2745          !!!next-token;        redo B;
2746          redo B;      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2747        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2748                 $token->{tag_name} eq 'html') {          #
2749          ## 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') {  
2750          ## Generate implied end tags          ## Generate implied end tags
2751          if ({          if ({
2752               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,
2753                 tbody => 1, tfoot=> 1, thead => 1,
2754              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2755            !!!back-token;            !!!back-token;
2756            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2757            redo B;            redo B;
2758          }          }
2759                    
# Line 2990  sub _tree_construction_main ($) { Line 2766  sub _tree_construction_main ($) {
2766            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2767          }          }
2768    
         ## Stop parsing  
         last B;  
   
2769          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
2770          }
2771    
2772          ## Stop parsing
2773          last B;
2774        } elsif ($token->{type} == START_TAG_TOKEN and
2775                 $token->{tag_name} eq 'html') {
2776          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2777            ## Turn into the main phase
2778            !!!parse-error (type => 'after html:html');
2779            $self->{insertion_mode} = AFTER_BODY_IM;
2780          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2781            ## Turn into the main phase
2782            !!!parse-error (type => 'after html:html');
2783            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2784          }
2785    
2786    ## ISSUE: "aa<html>" is not a parse error.
2787    ## ISSUE: "<html>" in fragment is not a parse error.
2788          unless ($token->{first_start_tag}) {
2789            !!!parse-error (type => 'not first start tag');
2790          }
2791          my $top_el = $self->{open_elements}->[0]->[0];
2792          for my $attr_name (keys %{$token->{attributes}}) {
2793            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2794              $top_el->set_attribute_ns
2795                (undef, [undef, $attr_name],
2796                 $token->{attributes}->{$attr_name}->{value});
2797            }
2798          }
2799          !!!next-token;
2800          redo B;
2801        } elsif ($token->{type} == COMMENT_TOKEN) {
2802          my $comment = $self->{document}->create_comment ($token->{data});
2803          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2804            $self->{document}->append_child ($comment);
2805          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2806            $self->{open_elements}->[0]->[0]->append_child ($comment);
2807        } else {        } else {
2808          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2809            if ($token->{type} eq 'character') {        }
2810              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        !!!next-token;
2811                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);        redo B;
2812                unless (length $token->{data}) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2813                  !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
2814                  redo B;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2815                }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2816              }            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);  
2817              !!!next-token;              !!!next-token;
2818              redo B;              redo B;
2819            } elsif ($token->{type} eq 'start tag') {            }
2820              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};          }
2821              !!!create-element ($self->{head_element}, 'head', $attr);  
2822              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2823              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            ## As if <head>
2824              $self->{insertion_mode} = 'in head';            !!!create-element ($self->{head_element}, 'head');
2825              if ($token->{tag_name} eq 'head') {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2826                !!!next-token;            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2827              #} elsif ({  
2828              #          base => 1, link => 1, meta => 1,            ## Reprocess in the "in head" insertion mode...
2829              #          script => 1, style => 1, title => 1,            pop @{$self->{open_elements}};
2830              #         }->{$token->{tag_name}}) {  
2831              #  ## reprocess            ## Reprocess in the "after head" insertion mode...
2832              } else {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2833                ## reprocess            ## As if </noscript>
2834              }            pop @{$self->{open_elements}};
2835              !!!parse-error (type => 'in noscript:#character');
2836              
2837              ## Reprocess in the "in head" insertion mode...
2838              ## As if </head>
2839              pop @{$self->{open_elements}};
2840    
2841              ## Reprocess in the "after head" insertion mode...
2842            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2843              pop @{$self->{open_elements}};
2844    
2845              ## Reprocess in the "after head" insertion mode...
2846            }
2847    
2848                ## "after head" insertion mode
2849                ## As if <body>
2850                !!!insert-element ('body');
2851                $self->{insertion_mode} = IN_BODY_IM;
2852                ## reprocess
2853              redo B;              redo B;
2854            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2855              if ($token->{tag_name} eq 'html') {              if ($token->{tag_name} eq 'head') {
2856                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2857                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2858                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2859                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2860                    $self->{insertion_mode} = IN_HEAD_IM;
2861                    !!!next-token;
2862                    redo B;
2863                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2864                    #
2865                  } else {
2866                    !!!parse-error (type => 'in head:head'); # or in head noscript
2867                    ## Ignore the token
2868                    !!!next-token;
2869                    redo B;
2870                  }
2871                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2872                ## As if <head>                ## As if <head>
2873                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2874                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2875                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2876                $self->{insertion_mode} = 'in head';  
2877                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2878                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;  
2879              }              }
2880            } else {  
2881              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
2882            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2883          } elsif ($self->{insertion_mode} eq 'in head') {                  ## As if </noscript>
2884            if ($token->{type} eq 'character') {                  pop @{$self->{open_elements}};
2885              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!parse-error (type => 'in noscript:base');
2886                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                
2887                unless (length $token->{data}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2888                  !!!next-token;                  ## Reprocess in the "in head" insertion mode...
                 redo B;  
2889                }                }
             }  
               
             #  
           } 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';  
2890    
2891                my $text = '';                ## NOTE: There is a "as if in head" code clone.
2892                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2893                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2894                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2895                  }
2896                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2897                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2898                  pop @{$self->{open_elements}}
2899                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2900                !!!next-token;                !!!next-token;
2901                while ($token->{type} eq 'character') {                redo B;
2902                  $text .= $token->{data};              } elsif ($token->{tag_name} eq 'link') {
2903                  !!!next-token;                ## NOTE: There is a "as if in head" code clone.
2904                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2905                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2906                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2907                }                }
2908                if (length $text) {                !!!insert-element ($token->{tag_name}, $token->{attributes});
2909                  $title_el->manakai_append_text ($text);                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2910                  pop @{$self->{open_elements}}
2911                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2912                  !!!next-token;
2913                  redo B;
2914                } elsif ($token->{tag_name} eq 'meta') {
2915                  ## NOTE: There is a "as if in head" code clone.
2916                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2917                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2918                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2919                }                }
2920                                !!!insert-element ($token->{tag_name}, $token->{attributes});
2921                $self->{content_model_flag} = 'PCDATA';                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2922                  
2923                if ($token->{type} eq 'end tag' and                unless ($self->{confident}) {
2924                    $token->{tag_name} eq 'title') {                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2925                  ## Ignore the token                    $self->{change_encoding}
2926                          ->($self, $token->{attributes}->{charset}->{value});
2927                      
2928                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2929                          ->set_user_data (manakai_has_reference =>
2930                                               $token->{attributes}->{charset}
2931                                                   ->{has_reference});
2932                    } elsif ($token->{attributes}->{content}) {
2933                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2934                      if ($token->{attributes}->{content}->{value}
2935                          =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2936                              [\x09-\x0D\x20]*=
2937                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2938                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2939                        $self->{change_encoding}
2940                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2941                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2942                            ->set_user_data (manakai_has_reference =>
2943                                                 $token->{attributes}->{content}
2944                                                       ->{has_reference});
2945                      }
2946                    }
2947                } else {                } else {
2948                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  if ($token->{attributes}->{charset}) {
2949                  ## ISSUE: And ignore?                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2950                          ->set_user_data (manakai_has_reference =>
2951                                               $token->{attributes}->{charset}
2952                                                   ->{has_reference});
2953                    }
2954                    if ($token->{attributes}->{content}) {
2955                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2956                          ->set_user_data (manakai_has_reference =>
2957                                               $token->{attributes}->{content}
2958                                                   ->{has_reference});
2959                    }
2960                }                }
               !!!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);  
2961    
2962                  pop @{$self->{open_elements}}
2963                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2964                !!!next-token;                !!!next-token;
2965                redo B;                redo B;
2966              } elsif ($token->{tag_name} eq 'head') {              } elsif ($token->{tag_name} eq 'title') {
2967                !!!parse-error (type => 'in head:head');                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2968                ## Ignore the token                  ## As if </noscript>
2969                !!!next-token;                  pop @{$self->{open_elements}};
2970                    !!!parse-error (type => 'in noscript:title');
2971                  
2972                    $self->{insertion_mode} = IN_HEAD_IM;
2973                    ## Reprocess in the "in head" insertion mode...
2974                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2975                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2976                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2977                  }
2978    
2979                  ## NOTE: There is a "as if in head" code clone.
2980                  my $parent = defined $self->{head_element} ? $self->{head_element}
2981                      : $self->{open_elements}->[-1]->[0];
2982                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2983                                  sub { $parent->append_child ($_[0]) });
2984                  pop @{$self->{open_elements}}
2985                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2986                redo B;                redo B;
2987              } else {              } elsif ($token->{tag_name} eq 'style') {
2988                #                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2989              }                ## insertion mode IN_HEAD_IM)
2990            } elsif ($token->{type} eq 'end tag') {                ## NOTE: There is a "as if in head" code clone.
2991              if ($token->{tag_name} eq 'head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2992                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2993                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2994                  }
2995                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2996                  pop @{$self->{open_elements}}
2997                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2998                  redo B;
2999                } elsif ($token->{tag_name} eq 'noscript') {
3000                  if ($self->{insertion_mode} == IN_HEAD_IM) {
3001                    ## NOTE: and scripting is disalbed
3002                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3003                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3004                    !!!next-token;
3005                    redo B;
3006                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3007                    !!!parse-error (type => 'in noscript:noscript');
3008                    ## Ignore the token
3009                    !!!next-token;
3010                    redo B;
3011                  } else {
3012                    #
3013                  }
3014                } elsif ($token->{tag_name} eq 'script') {
3015                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3016                    ## As if </noscript>
3017                    pop @{$self->{open_elements}};
3018                    !!!parse-error (type => 'in noscript:script');
3019                  
3020                    $self->{insertion_mode} = IN_HEAD_IM;
3021                    ## Reprocess in the "in head" insertion mode...
3022                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3023                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3024                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3025                  }
3026    
3027                  ## NOTE: There is a "as if in head" code clone.
3028                  $script_start_tag->($insert_to_current);
3029                  pop @{$self->{open_elements}}
3030                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3031                  redo B;
3032                } elsif ($token->{tag_name} eq 'body' or
3033                         $token->{tag_name} eq 'frameset') {
3034                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3035                    ## As if </noscript>
3036                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3037                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3038                    
3039                    ## Reprocess in the "in head" insertion mode...
3040                    ## As if </head>
3041                    pop @{$self->{open_elements}};
3042                    
3043                    ## Reprocess in the "after head" insertion mode...
3044                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3045                    pop @{$self->{open_elements}};
3046                    
3047                    ## Reprocess in the "after head" insertion mode...
3048                  }
3049    
3050                  ## "after head" insertion mode
3051                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3052                  if ($token->{tag_name} eq 'body') {
3053                    $self->{insertion_mode} = IN_BODY_IM;
3054                  } elsif ($token->{tag_name} eq 'frameset') {
3055                    $self->{insertion_mode} = IN_FRAMESET_IM;
3056                } else {                } else {
3057                  !!!parse-error (type => 'unmatched end tag:head');                  die "$0: tag name: $self->{tag_name}";
3058                }                }
               $self->{insertion_mode} = 'after head';  
3059                !!!next-token;                !!!next-token;
3060                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'html') {  
               #  
3061              } else {              } else {
3062                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                #
               ## Ignore the token  
               !!!next-token;  
               redo B;  
3063              }              }
           } else {  
             #  
           }  
3064    
3065            if ($self->{open_elements}->[-1]->[1] eq 'head') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3066              ## As if </head>                ## As if </noscript>
3067              pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3068            }                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3069            $self->{insertion_mode} = 'after head';                
3070            ## reprocess                ## Reprocess in the "in head" insertion mode...
3071            redo B;                ## As if </head>
3072                  pop @{$self->{open_elements}};
3073    
3074            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
3075          } elsif ($self->{insertion_mode} eq 'after head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3076            if ($token->{type} eq 'character') {                ## As if </head>
3077              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                pop @{$self->{open_elements}};
3078                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
3079                unless (length $token->{data}) {                ## Reprocess in the "after head" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
3080              }              }
               
             #  
           } 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});  
3081    
3082              !!!next-token;              ## "after head" insertion mode
3083              redo B;              ## As if <body>
3084            } elsif ($token->{type} eq 'comment') {              !!!insert-element ('body');
3085              ## NOTE: There is a code clone of "comment in body".              $self->{insertion_mode} = IN_BODY_IM;
3086              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);  
3087              redo B;              redo B;
3088            }            } elsif ($token->{type} == END_TAG_TOKEN) {
3089          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3090            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3091              ## NOTE: There are "character in table" code clones.                  ## As if <head>
3092              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
3093                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3094                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3095                unless (length $token->{data}) {  
3096                    ## Reprocess in the "in head" insertion mode...
3097                    pop @{$self->{open_elements}};
3098                    $self->{insertion_mode} = AFTER_HEAD_IM;
3099                    !!!next-token;
3100                    redo B;
3101                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3102                    ## As if </noscript>
3103                    pop @{$self->{open_elements}};
3104                    !!!parse-error (type => 'in noscript:script');
3105                    
3106                    ## Reprocess in the "in head" insertion mode...
3107                    pop @{$self->{open_elements}};
3108                    $self->{insertion_mode} = AFTER_HEAD_IM;
3109                    !!!next-token;
3110                    redo B;
3111                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3112                    pop @{$self->{open_elements}};
3113                    $self->{insertion_mode} = AFTER_HEAD_IM;
3114                  !!!next-token;                  !!!next-token;
3115                  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});  
3116                } else {                } else {
3117                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
3118                }                }
3119              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3120                $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]);  
3121                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3122                    $self->{insertion_mode} = IN_HEAD_IM;
3123                    !!!next-token;
3124                    redo B;
3125                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3126                    !!!parse-error (type => 'unmatched end tag:noscript');
3127                    ## Ignore the token ## ISSUE: An issue in the spec.
3128                    !!!next-token;
3129                    redo B;
3130                  } else {
3131                    #
3132                }                }
   
               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;  
3133              } elsif ({              } elsif ({
3134                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3135                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3136                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3137                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
3138                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
3139                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3140                  pop @{$self->{open_elements}};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3141                }  
3142                    $self->{insertion_mode} = IN_HEAD_IM;
3143                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
3144                $self->{insertion_mode} = $token->{tag_name} eq 'col'                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3145                  ? 'in column group' : 'in table body';                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3146                ## 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>  
3147                  !!!next-token;                  !!!next-token;
3148                  redo B;                  redo B;
3149                }                }
3150                                
3151                ## generate implied end tags                #
3152                if ({              } elsif ({
3153                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3154                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3155                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3156                  !!!back-token; # <table>                  ## As if <head>
3157                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3158                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3159                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3160    
3161                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3162                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3163                }                }
3164    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3165                #                #
3166              }              } else {
3167            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3168              if ($token->{tag_name} eq 'table') {                  #
3169                ## 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) {  
3170                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3171                  ## Ignore the token                  ## Ignore the token
3172                  !!!next-token;                  !!!next-token;
3173                  redo B;                  redo B;
3174                }                }
3175                              }
               ## 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]);  
               }  
3176    
3177                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3178                  ## As if </noscript>
3179                  pop @{$self->{open_elements}};
3180                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3181                  
3182                  ## Reprocess in the "in head" insertion mode...
3183                  ## As if </head>
3184                  pop @{$self->{open_elements}};
3185    
3186                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3187                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3188                  ## As if </head>
3189                  pop @{$self->{open_elements}};
3190    
3191                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3192                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}}) {  
3193                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3194                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3195                !!!next-token;                !!!next-token;
3196                redo B;                redo B;
             } else {  
               #  
3197              }              }
3198    
3199                ## "after head" insertion mode
3200                ## As if <body>
3201                !!!insert-element ('body');
3202                $self->{insertion_mode} = IN_BODY_IM;
3203                ## reprocess
3204                redo B;
3205            } else {            } else {
3206              #              die "$0: $token->{type}: Unknown token type";
3207            }            }
3208    
3209            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3210            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
3211            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3212          } 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".  
3213              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3214                            
3215              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3216    
3217              !!!next-token;              !!!next-token;
3218              redo B;              redo B;
3219            } 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') {  
3220              if ({              if ({
3221                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3222                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3223                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3224                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3225                    ## have an element in table scope
3226                    my $tn;
3227                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3228                      my $node = $self->{open_elements}->[$_];
3229                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3230                        $tn = $node->[1];
3231                        last INSCOPE;
3232                      } elsif ({
3233                                table => 1, html => 1,
3234                               }->{$node->[1]}) {
3235                        last INSCOPE;
3236                      }
3237                    } # INSCOPE
3238                      unless (defined $tn) {
3239                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3240                        ## Ignore the token
3241                        !!!next-token;
3242                        redo B;
3243                      }
3244                    
3245                    ## Close the cell
3246                    !!!back-token; # <?>
3247                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3248                    redo B;
3249                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3250                    !!!parse-error (type => 'not closed:caption');
3251                    
3252                    ## As if </caption>
3253                    ## have a table element in table scope
3254                    my $i;
3255                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3256                      my $node = $self->{open_elements}->[$_];
3257                      if ($node->[1] eq 'caption') {
3258                        $i = $_;
3259                        last INSCOPE;
3260                      } elsif ({
3261                                table => 1, html => 1,
3262                               }->{$node->[1]}) {
3263                        last INSCOPE;
3264                      }
3265                    } # INSCOPE
3266                      unless (defined $i) {
3267                        !!!parse-error (type => 'unmatched end tag:caption');
3268                        ## Ignore the token
3269                        !!!next-token;
3270                        redo B;
3271                      }
3272                    
3273                    ## generate implied end tags
3274                    if ({
3275                         dd => 1, dt => 1, li => 1, p => 1,
3276                         td => 1, th => 1, tr => 1,
3277                         tbody => 1, tfoot=> 1, thead => 1,
3278                        }->{$self->{open_elements}->[-1]->[1]}) {
3279                      !!!back-token; # <?>
3280                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3281                      !!!back-token;
3282                      $token = {type => END_TAG_TOKEN,
3283                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3284                      redo B;
3285                    }
3286    
3287                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3288                ## 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;  
3289                  }                  }
3290                } # INSCOPE                  
3291                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3292                  !!!parse-error (type => 'unmatched end tag:caption');                  
3293                    $clear_up_to_marker->();
3294                    
3295                    $self->{insertion_mode} = IN_TABLE_IM;
3296                    
3297                    ## reprocess
3298                    redo B;
3299                  } else {
3300                    #
3301                  }
3302                } else {
3303                  #
3304                }
3305              } elsif ($token->{type} == END_TAG_TOKEN) {
3306                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3307                  if ($self->{insertion_mode} == IN_CELL_IM) {
3308                    ## have an element in table scope
3309                    my $i;
3310                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3311                      my $node = $self->{open_elements}->[$_];
3312                      if ($node->[1] eq $token->{tag_name}) {
3313                        $i = $_;
3314                        last INSCOPE;
3315                      } elsif ({
3316                                table => 1, html => 1,
3317                               }->{$node->[1]}) {
3318                        last INSCOPE;
3319                      }
3320                    } # INSCOPE
3321                      unless (defined $i) {
3322                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3323                        ## Ignore the token
3324                        !!!next-token;
3325                        redo B;
3326                      }
3327                    
3328                    ## generate implied end tags
3329                    if ({
3330                         dd => 1, dt => 1, li => 1, p => 1,
3331                         td => ($token->{tag_name} eq 'th'),
3332                         th => ($token->{tag_name} eq 'td'),
3333                         tr => 1,
3334                         tbody => 1, tfoot=> 1, thead => 1,
3335                        }->{$self->{open_elements}->[-1]->[1]}) {
3336                      !!!back-token;
3337                      $token = {type => END_TAG_TOKEN,
3338                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3339                      redo B;
3340                    }
3341                    
3342                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3343                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3344                    }
3345                    
3346                    splice @{$self->{open_elements}}, $i;
3347                    
3348                    $clear_up_to_marker->();
3349                    
3350                    $self->{insertion_mode} = IN_ROW_IM;
3351                    
3352                    !!!next-token;
3353                    redo B;
3354                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3355                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3356                  ## Ignore the token                  ## Ignore the token
3357                  !!!next-token;                  !!!next-token;
3358                  redo B;                  redo B;
3359                  } else {
3360                    #
3361                }                }
3362                              } elsif ($token->{tag_name} eq 'caption') {
3363                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3364                if ({                  ## have a table element in table scope
3365                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3366                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3367                    }->{$self->{open_elements}->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3368                  !!!back-token; # <?>                    if ($node->[1] eq $token->{tag_name}) {
3369                  $token = {type => 'end tag', tag_name => 'caption'};                      $i = $_;
3370                  !!!back-token;                      last INSCOPE;
3371                  $token = {type => 'end tag',                    } elsif ({
3372                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              table => 1, html => 1,
3373                               }->{$node->[1]}) {
3374                        last INSCOPE;
3375                      }
3376                    } # INSCOPE
3377                      unless (defined $i) {
3378                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3379                        ## Ignore the token
3380                        !!!next-token;
3381                        redo B;
3382                      }
3383                    
3384                    ## generate implied end tags
3385                    if ({
3386                         dd => 1, dt => 1, li => 1, p => 1,
3387                         td => 1, th => 1, tr => 1,
3388                         tbody => 1, tfoot=> 1, thead => 1,
3389                        }->{$self->{open_elements}->[-1]->[1]}) {
3390                      !!!back-token;
3391                      $token = {type => END_TAG_TOKEN,
3392                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3393                      redo B;
3394                    }
3395                    
3396                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3397                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3398                    }
3399                    
3400                    splice @{$self->{open_elements}}, $i;
3401                    
3402                    $clear_up_to_marker->();
3403                    
3404                    $self->{insertion_mode} = IN_TABLE_IM;
3405                    
3406                    !!!next-token;
3407                  redo B;                  redo B;
3408                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3409                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3410                    ## Ignore the token
3411                    !!!next-token;
3412                    redo B;
3413                  } else {
3414                    #
3415                }                }
3416                } elsif ({
3417                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3418                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3419                }                       }->{$token->{tag_name}} and
3420                         $self->{insertion_mode} == IN_CELL_IM) {
3421                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  
3422                my $i;                my $i;
3423                  my $tn;
3424                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3425                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3426                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3427                    $i = $_;                    $i = $_;
3428                    last INSCOPE;                    last INSCOPE;
3429                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3430                      $tn = $node->[1];
3431                      ## NOTE: There is exactly one |td| or |th| element
3432                      ## in scope in the stack of open elements by definition.
3433                  } elsif ({                  } elsif ({
3434                            table => 1, html => 1,                            table => 1, html => 1,
3435                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3528  sub _tree_construction_main ($) { Line 3442  sub _tree_construction_main ($) {
3442                  !!!next-token;                  !!!next-token;
3443                  redo B;                  redo B;
3444                }                }
                 
               ## 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]);  
               }  
3445    
3446                splice @{$self->{open_elements}}, $i;                ## Close the cell
3447                  !!!back-token; # </?>
3448                $clear_up_to_marker->();                $token = {type => END_TAG_TOKEN, tag_name => $tn};
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
3449                redo B;                redo B;
3450              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3451                         $self->{insertion_mode} == IN_CAPTION_IM) {
3452                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3453    
3454                ## As if </caption>                ## As if </caption>
# Line 3580  sub _tree_construction_main ($) { Line 3476  sub _tree_construction_main ($) {
3476                if ({                if ({
3477                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3478                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3479                       tbody => 1, tfoot=> 1, thead => 1,
3480                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3481                  !!!back-token; # </table>                  !!!back-token; # </table>
3482                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3483                  !!!back-token;                  !!!back-token;
3484                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3485                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3486                  redo B;                  redo B;
3487                }                }
# Line 3597  sub _tree_construction_main ($) { Line 3494  sub _tree_construction_main ($) {
3494    
3495                $clear_up_to_marker->();                $clear_up_to_marker->();
3496    
3497                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3498    
3499                ## reprocess                ## reprocess
3500                redo B;                redo B;
3501              } elsif ({              } elsif ({
3502                        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,  
3503                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3504                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3505                ## 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');  
3506                  ## Ignore the token                  ## Ignore the token
3507                  !!!next-token;                  !!!next-token;
3508                  redo B;                  redo B;
3509                } else {                } else {
3510                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3511                }                }
3512              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3513                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3514                          thead => 1, tr => 1,
3515                         }->{$token->{tag_name}} and
3516                         $self->{insertion_mode} == IN_CAPTION_IM) {
3517                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3518                ## Ignore the token                ## Ignore the token
3519                !!!next-token;                !!!next-token;
3520                redo B;                redo B;
3521              } else {              } else {
3522                #                #
3523              }              }
3524            } else {        } else {
3525              #          die "$0: $token->{type}: Unknown token type";
3526            }        }
3527    
3528            ## As if </colgroup>        $insert = $insert_to_current;
3529            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3530              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3531              ## 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.  
3532              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3533                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3534                                
# Line 3699  sub _tree_construction_main ($) { Line 3545  sub _tree_construction_main ($) {
3545              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3546              ## result in a new Text node.              ## result in a new Text node.
3547              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3548                
3549              if ({              if ({
3550                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3551                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3739  sub _tree_construction_main ($) { Line 3585  sub _tree_construction_main ($) {
3585                            
3586              !!!next-token;              !!!next-token;
3587              redo B;              redo B;
3588            } 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') {  
3589              if ({              if ({
3590                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3591                   th => 1, td => 1,                   th => 1, td => 1,
3592                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3593                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3594                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3595                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3596                           $self->{open_elements}->[-1]->[1] ne 'html') {
3597                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3598                      pop @{$self->{open_elements}};
3599                    }
3600                    
3601                    !!!insert-element ('tbody');
3602                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3603                    ## reprocess in the "in table body" insertion mode...
3604                }                }
3605    
3606                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3607                    unless ($token->{tag_name} eq 'tr') {
3608                      !!!parse-error (type => 'missing start tag:tr');
3609                    }
3610                    
3611                    ## Clear back to table body context
3612                    while (not {
3613                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3614                    }->{$self->{open_elements}->[-1]->[1]}) {
3615                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3616                      pop @{$self->{open_elements}};
3617                    }
3618                    
3619                    $self->{insertion_mode} = IN_ROW_IM;
3620                    if ($token->{tag_name} eq 'tr') {
3621                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3622                      !!!next-token;
3623                      redo B;
3624                    } else {
3625                      !!!insert-element ('tr');
3626                      ## reprocess in the "in row" insertion mode
3627                    }
3628                  }
3629    
3630                  ## Clear back to table row context
3631                while (not {                while (not {
3632                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3633                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3634                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3635                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3636                }                }
3637                                
3638                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3639                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
3640                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3641                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3642                } else {                
3643                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3644                redo B;                redo B;
3645              } elsif ({              } elsif ({
3646                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3647                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3648                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3649                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3650                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3651                my $i;                  ## As if </tr>
3652                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3653                  my $node = $self->{open_elements}->[$_];                  my $i;
3654                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3655                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3656                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3657                    $i = $_;                      $i = $_;
3658                    last INSCOPE;                      last INSCOPE;
3659                  } elsif ({                    } elsif ({
3660                            table => 1, html => 1,                              table => 1, html => 1,
3661                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3662                    last INSCOPE;                      last INSCOPE;
3663                      }
3664                    } # INSCOPE
3665                    unless (defined $i) {
3666                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3667                      ## Ignore the token
3668                      !!!next-token;
3669                      redo B;
3670                    }
3671                    
3672                    ## Clear back to table row context
3673                    while (not {
3674                      tr => 1, html => 1,
3675                    }->{$self->{open_elements}->[-1]->[1]}) {
3676                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3677                      pop @{$self->{open_elements}};
3678                    }
3679                    
3680                    pop @{$self->{open_elements}}; # tr
3681                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3682                    if ($token->{tag_name} eq 'tr') {
3683                      ## reprocess
3684                      redo B;
3685                    } else {
3686                      ## reprocess in the "in table body" insertion mode...
3687                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3688                }                }
3689    
3690                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3691                while (not {                  ## have an element in table scope
3692                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3693                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3694                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3695                      if ({
3696                           tbody => 1, thead => 1, tfoot => 1,
3697                          }->{$node->[1]}) {
3698                        $i = $_;
3699                        last INSCOPE;
3700                      } elsif ({
3701                                table => 1, html => 1,
3702                               }->{$node->[1]}) {
3703                        last INSCOPE;
3704                      }
3705                    } # INSCOPE
3706                    unless (defined $i) {
3707                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3708                      ## Ignore the token
3709                      !!!next-token;
3710                      redo B;
3711                    }
3712    
3713                    ## Clear back to table body context
3714                    while (not {
3715                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3716                    }->{$self->{open_elements}->[-1]->[1]}) {
3717                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3718                      pop @{$self->{open_elements}};
3719                    }
3720                    
3721                    ## As if <{current node}>
3722                    ## have an element in table scope
3723                    ## true by definition
3724                    
3725                    ## Clear back to table body context
3726                    ## nop by definition
3727                    
3728                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3729                    $self->{insertion_mode} = IN_TABLE_IM;
3730                    ## reprocess in "in table" insertion mode...
3731                }                }
3732    
3733                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3734                ## have an element in table scope                  ## Clear back to table context
3735                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3736                           $self->{open_elements}->[-1]->[1] ne 'html') {
3737                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3738                ## nop by definition                    pop @{$self->{open_elements}};
3739                    }
3740                pop @{$self->{open_elements}};                  
3741                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3742                ## reprocess                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3743                redo B;                  ## reprocess
3744                    redo B;
3745                  } elsif ({
3746                            caption => 1,
3747                            colgroup => 1,
3748                            tbody => 1, tfoot => 1, thead => 1,
3749                           }->{$token->{tag_name}}) {
3750                    ## Clear back to table context
3751                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3752                           $self->{open_elements}->[-1]->[1] ne 'html') {
3753                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3754                      pop @{$self->{open_elements}};
3755                    }
3756                    
3757                    push @$active_formatting_elements, ['#marker', '']
3758                        if $token->{tag_name} eq 'caption';
3759                    
3760                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3761                    $self->{insertion_mode} = {
3762                                               caption => IN_CAPTION_IM,
3763                                               colgroup => IN_COLUMN_GROUP_IM,
3764                                               tbody => IN_TABLE_BODY_IM,
3765                                               tfoot => IN_TABLE_BODY_IM,
3766                                               thead => IN_TABLE_BODY_IM,
3767                                              }->{$token->{tag_name}};
3768                    !!!next-token;
3769                    redo B;
3770                  } else {
3771                    die "$0: in table: <>: $token->{tag_name}";
3772                  }
3773              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3774                ## 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');  
3775    
3776                ## As if </table>                ## As if </table>
3777                ## have a table element in table scope                ## have a table element in table scope
# Line 3845  sub _tree_construction_main ($) { Line 3798  sub _tree_construction_main ($) {
3798                if ({                if ({
3799                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3800                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3801                       tbody => 1, tfoot=> 1, thead => 1,
3802                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3803                  !!!back-token; # <table>                  !!!back-token; # <table>
3804                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3805                  !!!back-token;                  !!!back-token;
3806                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3807                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3808                  redo B;                  redo B;
3809                }                }
# Line 3860  sub _tree_construction_main ($) { Line 3814  sub _tree_construction_main ($) {
3814    
3815                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3816    
3817                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3818    
3819                ## reprocess                ## reprocess
3820                redo B;                redo B;
3821              } else {          } else {
3822                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3823              }  
3824            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3825              if ({            #
3826                   tbody => 1, tfoot => 1, thead => 1,          }
3827                  }->{$token->{tag_name}}) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3828                if ($token->{tag_name} eq 'tr' and
3829                    $self->{insertion_mode} == IN_ROW_IM) {
3830                ## have an element in table scope                ## have an element in table scope
3831                my $i;                my $i;
3832                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3891  sub _tree_construction_main ($) { Line 3847  sub _tree_construction_main ($) {
3847                  redo B;                  redo B;
3848                }                }
3849    
3850                ## Clear back to table body context                ## Clear back to table row context
3851                while (not {                while (not {
3852                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3853                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3854                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3855                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3856                }                }
3857    
3858                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3859                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3860                !!!next-token;                !!!next-token;
3861                redo B;                redo B;
3862              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3863                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3864                my $i;                  ## As if </tr>
3865                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3866                  my $node = $self->{open_elements}->[$_];                  my $i;
3867                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3868                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3869                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3870                    $i = $_;                      $i = $_;
3871                    last INSCOPE;                      last INSCOPE;
3872                  } elsif ({                    } elsif ({
3873                            table => 1, html => 1,                              table => 1, html => 1,
3874                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3875                    last INSCOPE;                      last INSCOPE;
3876                      }
3877                    } # INSCOPE
3878                    unless (defined $i) {
3879                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3880                      ## Ignore the token
3881                      !!!next-token;
3882                      redo B;
3883                  }                  }
3884                } # INSCOPE                  
3885                unless (defined $i) {                  ## Clear back to table row context
3886                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3887                  ## 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,  
3888                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3889                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3890                my $foster_parent_element;                    pop @{$self->{open_elements}};
3891                my $next_sibling;                  }
3892                my $prev_sibling;                  
3893                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3894                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3895                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3896                    if (defined $parent and $parent->node_type == 1) {                }
3897                      $foster_parent_element = $parent;  
3898                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3899                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3900                    } else {                  my $i;
3901                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3902                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
3903                      if ({
3904                           tbody => 1, thead => 1, tfoot => 1,
3905                          }->{$node->[1]}) {
3906                        $i = $_;
3907                        last INSCOPE;
3908                      } elsif ({
3909                                table => 1, html => 1,
3910                               }->{$node->[1]}) {
3911                        last INSCOPE;
3912                    }                    }
3913                    last OE;                  } # INSCOPE
3914                    unless (defined $i) {
3915                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3916                      ## Ignore the token
3917                      !!!next-token;
3918                      redo B;
3919                  }                  }
3920                } # OE                  
3921                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3922                $prev_sibling = $foster_parent_element->last_child                  while (not {
3923                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3924                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3925                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3926                  $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;  
3927                  }                  }
3928                } # INSCOPE                  
3929                unless (defined $i) {                  ## As if <{current node}>
3930                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3931                  ## Ignore the token                  ## true by definition
3932                  !!!next-token;                  
3933                  redo B;                  ## Clear back to table body context
3934                }                  ## nop by definition
3935                    
               ## 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]);  
3936                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3937                    $self->{insertion_mode} = IN_TABLE_IM;
3938                    ## reprocess in the "in table" insertion mode...
3939                }                }
3940    
               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>  
3941                ## have a table element in table scope                ## have a table element in table scope
3942                my $i;                my $i;
3943                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3944                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3945                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3946                    $i = $_;                    $i = $_;
3947                    last INSCOPE;                    last INSCOPE;
3948                  } elsif ({                  } elsif ({
# Line 4103  sub _tree_construction_main ($) { Line 3952  sub _tree_construction_main ($) {
3952                  }                  }
3953                } # INSCOPE                } # INSCOPE
3954                unless (defined $i) {                unless (defined $i) {
3955                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3956                  ## Ignore tokens </table><table>                  ## Ignore the token
3957                  !!!next-token;                  !!!next-token;
3958                  redo B;                  redo B;
3959                }                }
3960                  
3961                ## generate implied end tags                ## generate implied end tags
3962                if ({                if ({
3963                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3964                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3965                       tbody => 1, tfoot=> 1, thead => 1,
3966                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3967                  !!!back-token;                  !!!back-token;
3968                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3969                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3970                  redo B;                  redo B;
3971                }                }
3972                  
3973                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3974                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3975                }                }
3976                    
3977                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3978                  
3979                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3980                  
               ## 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';  
3981                !!!next-token;                !!!next-token;
3982                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;  
3983              } elsif ({              } elsif ({
3984                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3985                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3986                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
3987                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
3988                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3989                  my $node = $self->{open_elements}->[$_];                  my $i;
3990                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3991                    $i = $_;                    my $node = $self->{open_elements}->[$_];
3992                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
3993                  } elsif ({                      $i = $_;
3994                            table => 1, html => 1,                      last INSCOPE;
3995                           }->{$node->[1]}) {                    } elsif ({
3996                    last INSCOPE;                              table => 1, html => 1,
3997                               }->{$node->[1]}) {
3998                        last INSCOPE;
3999                      }
4000                    } # INSCOPE
4001                      unless (defined $i) {
4002                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4003                        ## Ignore the token
4004                        !!!next-token;
4005                        redo B;
4006                      }
4007                    
4008                    ## As if </tr>
4009                    ## have an element in table scope
4010                    my $i;
4011                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4012                      my $node = $self->{open_elements}->[$_];
4013                      if ($node->[1] eq 'tr') {
4014                        $i = $_;
4015                        last INSCOPE;
4016                      } elsif ({
4017                                table => 1, html => 1,
4018                               }->{$node->[1]}) {
4019                        last INSCOPE;
4020                      }
4021                    } # INSCOPE
4022                      unless (defined $i) {
4023                        !!!parse-error (type => 'unmatched end tag:tr');
4024                        ## Ignore the token
4025                        !!!next-token;
4026                        redo B;
4027                      }
4028                    
4029                    ## Clear back to table row context
4030                    while (not {
4031                      tr => 1, html => 1,
4032                    }->{$self->{open_elements}->[-1]->[1]}) {
4033                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4034                      pop @{$self->{open_elements}};
4035                  }                  }
4036                } # INSCOPE                  
4037                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4038                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4039                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4040                }                }
4041    
               ## As if </tr>  
4042                ## have an element in table scope                ## have an element in table scope
4043                my $i;                my $i;
4044                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4045                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4046                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4047                    $i = $_;                    $i = $_;
4048                    last INSCOPE;                    last INSCOPE;
4049                  } elsif ({                  } elsif ({
# Line 4241  sub _tree_construction_main ($) { Line 4053  sub _tree_construction_main ($) {
4053                  }                  }
4054                } # INSCOPE                } # INSCOPE
4055                unless (defined $i) {                unless (defined $i) {
4056                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4057                  ## Ignore the token                  ## Ignore the token
4058                  !!!next-token;                  !!!next-token;
4059                  redo B;                  redo B;
4060                }                }
4061    
4062                ## Clear back to table row context                ## Clear back to table body context
4063                while (not {                while (not {
4064                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4065                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4066                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4067                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4068                }                }
4069    
4070                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4071                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4072                ## reprocess                !!!next-token;
4073                redo B;                redo B;
4074              } elsif ({              } elsif ({
4075                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4076                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4077                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4078                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4079                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4080                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4081                ## Ignore the token                ## Ignore the token
4082                !!!next-token;                !!!next-token;
4083                redo B;                redo B;
4084              } else {          } else {
4085                #            !!!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});  
4086    
4087              !!!next-token;            $insert = $insert_to_foster;
4088              redo B;            #
4089            } elsif ($token->{type} eq 'comment') {          }
4090              ## NOTE: This is a code clone of "comment in body".        } else {
4091              my $comment = $self->{document}->create_comment ($token->{data});          die "$0: $token->{type}: Unknown token type";
4092              $self->{open_elements}->[-1]->[0]->append_child ($comment);        }
4093              !!!next-token;      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4094              redo B;            if ($token->{type} == CHARACTER_TOKEN) {
4095            } elsif ($token->{type} eq 'start tag') {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4096              if ({                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4097                   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  
4098                  !!!next-token;                  !!!next-token;
4099                  redo B;                  redo B;
4100                }                }
4101                }
4102                ## Close the cell              
4103                !!!back-token; # <?>              #
4104                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
4105                if ($token->{tag_name} eq 'col') {
4106                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4107                  pop @{$self->{open_elements}};
4108                  !!!next-token;
4109                redo B;                redo B;
4110              } else {              } else {
4111                #                #
4112              }              }
4113            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4114              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4115                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4116                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});  
4117                  ## Ignore the token                  ## Ignore the token
4118                  !!!next-token;                  !!!next-token;
4119                  redo B;                  redo B;
4120                  } else {
4121                    pop @{$self->{open_elements}}; # colgroup
4122                    $self->{insertion_mode} = IN_TABLE_IM;
4123                    !!!next-token;
4124                    redo B;            
4125                }                }
4126                              } elsif ($token->{tag_name} eq 'col') {
4127                ## 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});  
4128                ## Ignore the token                ## Ignore the token
4129                !!!next-token;                !!!next-token;
4130                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;  
4131              } else {              } else {
4132                #                #
4133              }              }
4134            } else {            } else {
4135              #              #
4136            }            }
4137              
4138            $in_body->($insert_to_current);            ## As if </colgroup>
4139            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4140          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
4141            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4142              !!!next-token;              !!!next-token;
4143              redo B;              redo B;
4144            } elsif ($token->{type} eq 'comment') {            } else {
4145              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
4146              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = IN_TABLE_IM;
4147              !!!next-token;              ## reprocess
4148              redo B;              redo B;
4149            } elsif ($token->{type} eq 'start tag') {            }
4150        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4151          if ($token->{type} == CHARACTER_TOKEN) {
4152            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4153            !!!next-token;
4154            redo B;
4155          } elsif ($token->{type} == START_TAG_TOKEN) {
4156              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4157                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4158                  ## As if </option>                  ## As if </option>
# Line 4486  sub _tree_construction_main ($) { Line 4205  sub _tree_construction_main ($) {
4205    
4206                !!!next-token;                !!!next-token;
4207                redo B;                redo B;
4208              } else {          } else {
4209                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4210              }            ## Ignore the token
4211            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4212              redo B;
4213            }
4214          } elsif ($token->{type} == END_TAG_TOKEN) {
4215              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4216                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4217                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4591  sub _tree_construction_main ($) { Line 4313  sub _tree_construction_main ($) {
4313    
4314                ## reprocess                ## reprocess
4315                redo B;                redo B;
4316              } else {          } else {
4317                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4318            ## Ignore the token            ## Ignore the token
4319            !!!next-token;            !!!next-token;
4320            redo B;            redo B;
4321          } elsif ($self->{insertion_mode} eq 'after body') {          }
4322            if ($token->{type} eq 'character') {        } else {
4323              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4324                ## As if in body        }
4325                $reconstruct_active_formatting_elements->($insert_to_current);      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4326          if ($token->{type} == CHARACTER_TOKEN) {
4327            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4328              my $data = $1;
4329              ## As if in body
4330              $reconstruct_active_formatting_elements->($insert_to_current);
4331                                
4332                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4333              
4334              unless (length $token->{data}) {
4335                !!!next-token;
4336                redo B;
4337              }
4338            }
4339            
4340            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4341              !!!parse-error (type => 'after html:#character');
4342    
4343                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4344                  !!!next-token;          }
4345                  redo B;          
4346                }          ## "after body" insertion mode
4347              }          !!!parse-error (type => 'after body:#character');
4348                
4349              #          $self->{insertion_mode} = IN_BODY_IM;
4350              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4351            } elsif ($token->{type} eq 'comment') {          redo B;
4352              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} == START_TAG_TOKEN) {
4353              $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4354              !!!parse-error (type => 'after html:'.$token->{tag_name});
4355              
4356              ## Reprocess in the "main" phase, "after body" insertion mode...
4357            }
4358    
4359            ## "after body" insertion mode
4360            !!!parse-error (type => 'after body:'.$token->{tag_name});
4361    
4362            $self->{insertion_mode} = IN_BODY_IM;
4363            ## reprocess
4364            redo B;
4365          } elsif ($token->{type} == END_TAG_TOKEN) {
4366            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4367              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4368              
4369              $self->{insertion_mode} = AFTER_BODY_IM;
4370              ## Reprocess in the "main" phase, "after body" insertion mode...
4371            }
4372    
4373            ## "after body" insertion mode
4374            if ($token->{tag_name} eq 'html') {
4375              if (defined $self->{inner_html_node}) {
4376                !!!parse-error (type => 'unmatched end tag:html');
4377                ## Ignore the token
4378              !!!next-token;              !!!next-token;
4379              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});  
             }  
4380            } else {            } else {
4381              !!!parse-error (type => 'after body:#'.$token->{type});              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4382                !!!next-token;
4383                redo B;
4384            }            }
4385            } else {
4386              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4387    
4388            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4389            ## reprocess            ## reprocess
4390            redo B;            redo B;
4391          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4392            if ($token->{type} eq 'character') {        } else {
4393              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4394                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4395        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4396                unless (length $token->{data}) {        if ($token->{type} == CHARACTER_TOKEN) {
4397                  !!!next-token;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4398                  redo B;            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4399                }            
4400              }            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);  
4401              !!!next-token;              !!!next-token;
4402              redo B;              redo B;
4403            } elsif ($token->{type} eq 'start tag') {            }
4404              if ($token->{tag_name} eq 'frameset') {          }
4405                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4406                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4407                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4408              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4409                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4410                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
4411                !!!next-token;            } else { # "after html frameset"
4412                redo B;              !!!parse-error (type => 'after html:#character');
4413              } elsif ($token->{tag_name} eq 'noframes') {  
4414                $in_body->($insert_to_current);              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4415                redo B;              ## Reprocess in the "main" phase, "after frameset"...
4416              } 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 {  
             #  
4417            }            }
4418                        
4419            if (defined $token->{tag_name}) {            ## Ignore the token.
4420              if (length $token->{data}) {
4421                ## reprocess the rest of characters
4422              } else {
4423                !!!next-token;
4424              }
4425              redo B;
4426            }
4427            
4428            die qq[$0: Character "$token->{data}"];
4429          } elsif ($token->{type} == START_TAG_TOKEN) {
4430            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4431              !!!parse-error (type => 'after html:'.$token->{tag_name});
4432    
4433              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4434              ## Process in the "main" phase, "after frameset" insertion mode...
4435            }
4436    
4437            if ($token->{tag_name} eq 'frameset' and
4438                $self->{insertion_mode} == IN_FRAMESET_IM) {
4439              !!!insert-element ($token->{tag_name}, $token->{attributes});
4440              !!!next-token;
4441              redo B;
4442            } elsif ($token->{tag_name} eq 'frame' and
4443                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4444              !!!insert-element ($token->{tag_name}, $token->{attributes});
4445              pop @{$self->{open_elements}};
4446              !!!next-token;
4447              redo B;
4448            } elsif ($token->{tag_name} eq 'noframes') {
4449              ## NOTE: As if in body.
4450              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4451              redo B;
4452            } else {
4453              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4454              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4455            } else {            } else {
4456              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4457            }            }
4458            ## Ignore the token            ## Ignore the token
4459            !!!next-token;            !!!next-token;
4460            redo B;            redo B;
4461          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4462            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4463              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4464                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4465    
4466                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4467                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
4468                  redo B;          }
               }  
             }  
4469    
4470              #          if ($token->{tag_name} eq 'frameset' and
4471            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4472              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4473              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4474                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4475                ## Ignore the token
4476              !!!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 {  
               #  
             }  
4477            } else {            } else {
4478              #              pop @{$self->{open_elements}};
4479                !!!next-token;
4480            }            }
4481              
4482            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
4483              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4484                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4485              }
4486              redo B;
4487            } elsif ($token->{tag_name} eq 'html' and
4488                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4489              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4490              !!!next-token;
4491              redo B;
4492            } else {
4493              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4494                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4495            } else {            } else {
4496              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4497            }            }
4498            ## Ignore the token            ## Ignore the token
4499            !!!next-token;            !!!next-token;
4500            redo B;            redo B;
4501            }
4502          } else {
4503            die "$0: $token->{type}: Unknown token type";
4504          }
4505    
4506          ## ISSUE: An issue in spec here
4507        } else {
4508          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4509        }
4510    
4511            ## ISSUE: An issue in spec there      ## "in body" insertion mode
4512        if ($token->{type} == START_TAG_TOKEN) {
4513          if ($token->{tag_name} eq 'script') {
4514            ## NOTE: This is an "as if in head" code clone
4515            $script_start_tag->($insert);
4516            redo B;
4517          } elsif ($token->{tag_name} eq 'style') {
4518            ## NOTE: This is an "as if in head" code clone
4519            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4520            redo B;
4521          } elsif ({
4522                    base => 1, link => 1,
4523                   }->{$token->{tag_name}}) {
4524            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4525            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4526            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4527            !!!next-token;
4528            redo B;
4529          } elsif ($token->{tag_name} eq 'meta') {
4530            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4531            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4532            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4533    
4534            unless ($self->{confident}) {
4535              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4536                $self->{change_encoding}
4537                    ->($self, $token->{attributes}->{charset}->{value});
4538                
4539                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4540                    ->set_user_data (manakai_has_reference =>
4541                                         $token->{attributes}->{charset}
4542                                             ->{has_reference});
4543              } elsif ($token->{attributes}->{content}) {
4544                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4545                if ($token->{attributes}->{content}->{value}
4546                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4547                        [\x09-\x0D\x20]*=
4548                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4549                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4550                  $self->{change_encoding}
4551                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4552                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4553                      ->set_user_data (manakai_has_reference =>
4554                                           $token->{attributes}->{content}
4555                                                 ->{has_reference});
4556                }
4557              }
4558          } else {          } else {
4559            die "$0: $self->{insertion_mode}: Unknown insertion mode";            if ($token->{attributes}->{charset}) {
4560                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4561                    ->set_user_data (manakai_has_reference =>
4562                                         $token->{attributes}->{charset}
4563                                             ->{has_reference});
4564              }
4565              if ($token->{attributes}->{content}) {
4566                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4567                    ->set_user_data (manakai_has_reference =>
4568                                         $token->{attributes}->{content}
4569                                             ->{has_reference});
4570              }
4571          }          }
4572        }  
     } 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  
4573          !!!next-token;          !!!next-token;
4574          redo B;          redo B;
4575        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'title') {
4576          my $comment = $self->{document}->create_comment ($token->{data});          !!!parse-error (type => 'in body:title');
4577          $self->{document}->append_child ($comment);          ## NOTE: This is an "as if in head" code clone
4578            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4579              if (defined $self->{head_element}) {
4580                $self->{head_element}->append_child ($_[0]);
4581              } else {
4582                $insert->($_[0]);
4583              }
4584            });
4585            redo B;
4586          } elsif ($token->{tag_name} eq 'body') {
4587            !!!parse-error (type => 'in body:body');
4588                  
4589            if (@{$self->{open_elements}} == 1 or
4590                $self->{open_elements}->[1]->[1] ne 'body') {
4591              ## Ignore the token
4592            } else {
4593              my $body_el = $self->{open_elements}->[1]->[0];
4594              for my $attr_name (keys %{$token->{attributes}}) {
4595                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4596                  $body_el->set_attribute_ns
4597                    (undef, [undef, $attr_name],
4598                     $token->{attributes}->{$attr_name}->{value});
4599                }
4600              }
4601            }
4602          !!!next-token;          !!!next-token;
4603          redo B;          redo B;
4604        } elsif ($token->{type} eq 'character') {        } elsif ({
4605          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  address => 1, blockquote => 1, center => 1, dir => 1,
4606            my $data = $1;                  div => 1, dl => 1, fieldset => 1, listing => 1,
4607            ## As if in the main phase.                  menu => 1, ol => 1, p => 1, ul => 1,
4608            ## NOTE: The insertion mode in the main phase                  pre => 1,
4609            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4610            ## end phase is either "after body" or "after frameset".          ## has a p element in scope
4611            $reconstruct_active_formatting_elements->($insert_to_current)          INSCOPE: for (reverse @{$self->{open_elements}}) {
4612              if $phase eq 'main';            if ($_->[1] eq 'p') {
4613                !!!back-token;
4614                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4615                redo B;
4616              } elsif ({
4617                        table => 1, caption => 1, td => 1, th => 1,
4618                        button => 1, marquee => 1, object => 1, html => 1,
4619                       }->{$_->[1]}) {
4620                last INSCOPE;
4621              }
4622            } # INSCOPE
4623              
4624            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4625            if ($token->{tag_name} eq 'pre') {
4626              !!!next-token;
4627              if ($token->{type} == CHARACTER_TOKEN) {
4628                $token->{data} =~ s/^\x0A//;
4629                unless (length $token->{data}) {
4630                  !!!next-token;
4631                }
4632              }
4633            } else {
4634              !!!next-token;
4635            }
4636            redo B;
4637          } elsif ($token->{tag_name} eq 'form') {
4638            if (defined $self->{form_element}) {
4639              !!!parse-error (type => 'in form:form');
4640              ## Ignore the token
4641              !!!next-token;
4642              redo B;
4643            } else {
4644              ## has a p element in scope
4645              INSCOPE: for (reverse @{$self->{open_elements}}) {
4646                if ($_->[1] eq 'p') {
4647                  !!!back-token;
4648                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4649                  redo B;
4650                } elsif ({
4651                          table => 1, caption => 1, td => 1, th => 1,
4652                          button => 1, marquee => 1, object => 1, html => 1,
4653                         }->{$_->[1]}) {
4654                  last INSCOPE;
4655                }
4656              } # INSCOPE
4657                
4658              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4659              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4660              !!!next-token;
4661              redo B;
4662            }
4663          } elsif ($token->{tag_name} eq 'li') {
4664            ## has a p element in scope
4665            INSCOPE: for (reverse @{$self->{open_elements}}) {
4666              if ($_->[1] eq 'p') {
4667                !!!back-token;
4668                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4669                redo B;
4670              } elsif ({
4671                        table => 1, caption => 1, td => 1, th => 1,
4672                        button => 1, marquee => 1, object => 1, html => 1,
4673                       }->{$_->[1]}) {
4674                last INSCOPE;
4675              }
4676            } # INSCOPE
4677              
4678            ## Step 1
4679            my $i = -1;
4680            my $node = $self->{open_elements}->[$i];
4681            LI: {
4682              ## Step 2
4683              if ($node->[1] eq 'li') {
4684                if ($i != -1) {
4685                  !!!parse-error (type => 'end tag missing:'.
4686                                  $self->{open_elements}->[-1]->[1]);
4687                }
4688                splice @{$self->{open_elements}}, $i;
4689                last LI;
4690              }
4691              
4692              ## Step 3
4693              if (not $formatting_category->{$node->[1]} and
4694                  #not $phrasing_category->{$node->[1]} and
4695                  ($special_category->{$node->[1]} or
4696                   $scoping_category->{$node->[1]}) and
4697                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4698                last LI;
4699              }
4700              
4701              ## Step 4
4702              $i--;
4703              $node = $self->{open_elements}->[$i];
4704              redo LI;
4705            } # LI
4706              
4707            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4708            !!!next-token;
4709            redo B;
4710          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4711            ## has a p element in scope
4712            INSCOPE: for (reverse @{$self->{open_elements}}) {
4713              if ($_->[1] eq 'p') {
4714                !!!back-token;
4715                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4716                redo B;
4717              } elsif ({
4718                        table => 1, caption => 1, td => 1, th => 1,
4719                        button => 1, marquee => 1, object => 1, html => 1,
4720                       }->{$_->[1]}) {
4721                last INSCOPE;
4722              }
4723            } # INSCOPE
4724              
4725            ## Step 1
4726            my $i = -1;
4727            my $node = $self->{open_elements}->[$i];
4728            LI: {
4729              ## Step 2
4730              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4731                if ($i != -1) {
4732                  !!!parse-error (type => 'end tag missing:'.
4733                                  $self->{open_elements}->[-1]->[1]);
4734                }
4735                splice @{$self->{open_elements}}, $i;
4736                last LI;
4737              }
4738              
4739              ## Step 3
4740              if (not $formatting_category->{$node->[1]} and
4741                  #not $phrasing_category->{$node->[1]} and
4742                  ($special_category->{$node->[1]} or
4743                   $scoping_category->{$node->[1]}) and
4744                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4745                last LI;
4746              }
4747              
4748              ## Step 4
4749              $i--;
4750              $node = $self->{open_elements}->[$i];
4751              redo LI;
4752            } # LI
4753              
4754            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4755            !!!next-token;
4756            redo B;
4757          } elsif ($token->{tag_name} eq 'plaintext') {
4758            ## has a p element in scope
4759            INSCOPE: for (reverse @{$self->{open_elements}}) {
4760              if ($_->[1] eq 'p') {
4761                !!!back-token;
4762                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4763                redo B;
4764              } elsif ({
4765                        table => 1, caption => 1, td => 1, th => 1,
4766                        button => 1, marquee => 1, object => 1, html => 1,
4767                       }->{$_->[1]}) {
4768                last INSCOPE;
4769              }
4770            } # INSCOPE
4771              
4772            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4773              
4774            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4775              
4776            !!!next-token;
4777            redo B;
4778          } elsif ({
4779                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4780                   }->{$token->{tag_name}}) {
4781            ## has a p element in scope
4782            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4783              my $node = $self->{open_elements}->[$_];
4784              if ($node->[1] eq 'p') {
4785                !!!back-token;
4786                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4787                redo B;
4788              } elsif ({
4789                        table => 1, caption => 1, td => 1, th => 1,
4790                        button => 1, marquee => 1, object => 1, html => 1,
4791                       }->{$node->[1]}) {
4792                last INSCOPE;
4793              }
4794            } # INSCOPE
4795              
4796            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4797            ## has an element in scope
4798            #my $i;
4799            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4800            #  my $node = $self->{open_elements}->[$_];
4801            #  if ({
4802            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4803            #      }->{$node->[1]}) {
4804            #    $i = $_;
4805            #    last INSCOPE;
4806            #  } elsif ({
4807            #            table => 1, caption => 1, td => 1, th => 1,
4808            #            button => 1, marquee => 1, object => 1, html => 1,
4809            #           }->{$node->[1]}) {
4810            #    last INSCOPE;
4811            #  }
4812            #} # INSCOPE
4813            #  
4814            #if (defined $i) {
4815            #  !!! parse-error (type => 'in hn:hn');
4816            #  splice @{$self->{open_elements}}, $i;
4817            #}
4818              
4819            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4820              
4821            !!!next-token;
4822            redo B;
4823          } elsif ($token->{tag_name} eq 'a') {
4824            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4825              my $node = $active_formatting_elements->[$i];
4826              if ($node->[1] eq 'a') {
4827                !!!parse-error (type => 'in a:a');
4828                
4829                !!!back-token;
4830                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4831                $formatting_end_tag->($token->{tag_name});
4832                
4833                AFE2: for (reverse 0..$#$active_formatting_elements) {
4834                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4835                    splice @$active_formatting_elements, $_, 1;
4836                    last AFE2;
4837                  }
4838                } # AFE2
4839                OE: for (reverse 0..$#{$self->{open_elements}}) {
4840                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4841                    splice @{$self->{open_elements}}, $_, 1;
4842                    last OE;
4843                  }
4844                } # OE
4845                last AFE;
4846              } elsif ($node->[0] eq '#marker') {
4847                last AFE;
4848              }
4849            } # AFE
4850              
4851            $reconstruct_active_formatting_elements->($insert_to_current);
4852    
4853            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4854            push @$active_formatting_elements, $self->{open_elements}->[-1];
4855    
4856            !!!next-token;
4857            redo B;
4858          } elsif ({
4859                    b => 1, big => 1, em => 1, font => 1, i => 1,
4860                    s => 1, small => 1, strile => 1,
4861                    strong => 1, tt => 1, u => 1,
4862                   }->{$token->{tag_name}}) {
4863            $reconstruct_active_formatting_elements->($insert_to_current);
4864            
4865            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4866            push @$active_formatting_elements, $self->{open_elements}->[-1];
4867            
4868            !!!next-token;
4869            redo B;
4870          } elsif ($token->{tag_name} eq 'nobr') {
4871            $reconstruct_active_formatting_elements->($insert_to_current);
4872    
4873            ## has a |nobr| element in scope
4874            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4875              my $node = $self->{open_elements}->[$_];
4876              if ($node->[1] eq 'nobr') {
4877                !!!parse-error (type => 'in nobr:nobr');
4878                !!!back-token;
4879                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4880                redo B;
4881              } elsif ({
4882                        table => 1, caption => 1, td => 1, th => 1,
4883                        button => 1, marquee => 1, object => 1, html => 1,
4884                       }->{$node->[1]}) {
4885                last INSCOPE;
4886              }
4887            } # INSCOPE
4888            
4889            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4890            push @$active_formatting_elements, $self->{open_elements}->[-1];
4891            
4892            !!!next-token;
4893            redo B;
4894          } elsif ($token->{tag_name} eq 'button') {
4895            ## has a button element in scope
4896            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4897              my $node = $self->{open_elements}->[$_];
4898              if ($node->[1] eq 'button') {
4899                !!!parse-error (type => 'in button:button');
4900                !!!back-token;
4901                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4902                redo B;
4903              } elsif ({
4904                        table => 1, caption => 1, td => 1, th => 1,
4905                        button => 1, marquee => 1, object => 1, html => 1,
4906                       }->{$node->[1]}) {
4907                last INSCOPE;
4908              }
4909            } # INSCOPE
4910              
4911            $reconstruct_active_formatting_elements->($insert_to_current);
4912              
4913            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4914            push @$active_formatting_elements, ['#marker', ''];
4915    
4916            !!!next-token;
4917            redo B;
4918          } elsif ($token->{tag_name} eq 'marquee' or
4919                   $token->{tag_name} eq 'object') {
4920            $reconstruct_active_formatting_elements->($insert_to_current);
4921            
4922            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4923            push @$active_formatting_elements, ['#marker', ''];
4924            
4925            !!!next-token;
4926            redo B;
4927          } elsif ($token->{tag_name} eq 'xmp') {
4928            $reconstruct_active_formatting_elements->($insert_to_current);
4929            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4930            redo B;
4931          } elsif ($token->{tag_name} eq 'table') {
4932            ## has a p element in scope
4933            INSCOPE: for (reverse @{$self->{open_elements}}) {
4934              if ($_->[1] eq 'p') {
4935                !!!back-token;
4936                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4937                redo B;
4938              } elsif ({
4939                        table => 1, caption => 1, td => 1, th => 1,
4940                        button => 1, marquee => 1, object => 1, html => 1,
4941                       }->{$_->[1]}) {
4942                last INSCOPE;
4943              }
4944            } # INSCOPE
4945              
4946            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4947              
4948            $self->{insertion_mode} = IN_TABLE_IM;
4949              
4950            !!!next-token;
4951            redo B;
4952          } elsif ({
4953                    area => 1, basefont => 1, bgsound => 1, br => 1,
4954                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4955                    image => 1,
4956                   }->{$token->{tag_name}}) {
4957            if ($token->{tag_name} eq 'image') {
4958              !!!parse-error (type => 'image');
4959              $token->{tag_name} = 'img';
4960            }
4961    
4962            ## NOTE: There is an "as if <br>" code clone.
4963            $reconstruct_active_formatting_elements->($insert_to_current);
4964            
4965            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4966            pop @{$self->{open_elements}};
4967            
4968            !!!next-token;
4969            redo B;
4970          } elsif ($token->{tag_name} eq 'hr') {
4971            ## has a p element in scope
4972            INSCOPE: for (reverse @{$self->{open_elements}}) {
4973              if ($_->[1] eq 'p') {
4974                !!!back-token;
4975                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4976                redo B;
4977              } elsif ({
4978                        table => 1, caption => 1, td => 1, th => 1,
4979                        button => 1, marquee => 1, object => 1, html => 1,
4980                       }->{$_->[1]}) {
4981                last INSCOPE;
4982              }
4983            } # INSCOPE
4984                        
4985            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4986            pop @{$self->{open_elements}};
4987                        
4988            !!!next-token;
4989            redo B;
4990          } elsif ($token->{tag_name} eq 'input') {
4991            $reconstruct_active_formatting_elements->($insert_to_current);
4992            
4993            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4994            ## TODO: associate with $self->{form_element} if defined
4995            pop @{$self->{open_elements}};
4996            
4997            !!!next-token;
4998            redo B;
4999          } elsif ($token->{tag_name} eq 'isindex') {
5000            !!!parse-error (type => 'isindex');
5001            
5002            if (defined $self->{form_element}) {
5003              ## Ignore the token
5004              !!!next-token;
5005              redo B;
5006            } else {
5007              my $at = $token->{attributes};
5008              my $form_attrs;
5009              $form_attrs->{action} = $at->{action} if $at->{action};
5010              my $prompt_attr = $at->{prompt};
5011              $at->{name} = {name => 'name', value => 'isindex'};
5012              delete $at->{action};
5013              delete $at->{prompt};
5014              my @tokens = (
5015                            {type => START_TAG_TOKEN, tag_name => 'form',
5016                             attributes => $form_attrs},
5017                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5018                            {type => START_TAG_TOKEN, tag_name => 'p'},
5019                            {type => START_TAG_TOKEN, tag_name => 'label'},
5020                           );
5021              if ($prompt_attr) {
5022                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5023              } else {
5024                push @tokens, {type => CHARACTER_TOKEN,
5025                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5026                ## TODO: make this configurable
5027              }
5028              push @tokens,
5029                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5030                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5031                            {type => END_TAG_TOKEN, tag_name => 'label'},
5032                            {type => END_TAG_TOKEN, tag_name => 'p'},
5033                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5034                            {type => END_TAG_TOKEN, tag_name => 'form'};
5035              $token = shift @tokens;
5036              !!!back-token (@tokens);
5037              redo B;
5038            }
5039          } elsif ($token->{tag_name} eq 'textarea') {
5040            my $tag_name = $token->{tag_name};
5041            my $el;
5042            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5043            
5044            ## TODO: $self->{form_element} if defined
5045            $self->{content_model} = RCDATA_CONTENT_MODEL;
5046            delete $self->{escape}; # MUST
5047            
5048            $insert->($el);
5049            
5050            my $text = '';
5051            !!!next-token;
5052            if ($token->{type} == CHARACTER_TOKEN) {
5053              $token->{data} =~ s/^\x0A//;
5054            unless (length $token->{data}) {            unless (length $token->{data}) {
5055              !!!next-token;              !!!next-token;
             redo B;  
5056            }            }
5057          }          }
5058            while ($token->{type} == CHARACTER_TOKEN) {
5059              $text .= $token->{data};
5060              !!!next-token;
5061            }
5062            if (length $text) {
5063              $el->manakai_append_text ($text);
5064            }
5065            
5066            $self->{content_model} = PCDATA_CONTENT_MODEL;
5067            
5068            if ($token->{type} == END_TAG_TOKEN and
5069                $token->{tag_name} eq $tag_name) {
5070              ## Ignore the token
5071            } else {
5072              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5073            }
5074            !!!next-token;
5075            redo B;
5076          } elsif ({
5077                    iframe => 1,
5078                    noembed => 1,
5079                    noframes => 1,
5080                    noscript => 0, ## TODO: 1 if scripting is enabled
5081                   }->{$token->{tag_name}}) {
5082            ## NOTE: There is an "as if in body" code clone.
5083            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5084            redo B;
5085          } elsif ($token->{tag_name} eq 'select') {
5086            $reconstruct_active_formatting_elements->($insert_to_current);
5087            
5088            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5089            
5090            $self->{insertion_mode} = IN_SELECT_IM;
5091            !!!next-token;
5092            redo B;
5093          } elsif ({
5094                    caption => 1, col => 1, colgroup => 1, frame => 1,
5095                    frameset => 1, head => 1, option => 1, optgroup => 1,
5096                    tbody => 1, td => 1, tfoot => 1, th => 1,
5097                    thead => 1, tr => 1,
5098                   }->{$token->{tag_name}}) {
5099            !!!parse-error (type => 'in body:'.$token->{tag_name});
5100            ## Ignore the token
5101            !!!next-token;
5102            redo B;
5103            
5104            ## ISSUE: An issue on HTML5 new elements in the spec.
5105          } else {
5106            $reconstruct_active_formatting_elements->($insert_to_current);
5107            
5108            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5109            
5110            !!!next-token;
5111            redo B;
5112          }
5113        } elsif ($token->{type} == END_TAG_TOKEN) {
5114          if ($token->{tag_name} eq 'body') {
5115            if (@{$self->{open_elements}} > 1 and
5116                $self->{open_elements}->[1]->[1] eq 'body') {
5117              for (@{$self->{open_elements}}) {
5118                unless ({
5119                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
5120                           th => 1, tr => 1, body => 1, html => 1,
5121                         tbody => 1, tfoot => 1, thead => 1,
5122                        }->{$_->[1]}) {
5123                  !!!parse-error (type => 'not closed:'.$_->[1]);
5124                }
5125              }
5126    
5127          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
5128          $phase = 'main';            !!!next-token;
5129          ## reprocess            redo B;
5130            } else {
5131              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5132              ## Ignore the token
5133              !!!next-token;
5134              redo B;
5135            }
5136          } elsif ($token->{tag_name} eq 'html') {
5137            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5138              ## ISSUE: There is an issue in the spec.
5139              if ($self->{open_elements}->[-1]->[1] ne 'body') {
5140                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5141              }
5142              $self->{insertion_mode} = AFTER_BODY_IM;
5143              ## reprocess
5144              redo B;
5145            } else {
5146              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5147              ## Ignore the token
5148              !!!next-token;
5149              redo B;
5150            }
5151          } elsif ({
5152                    address => 1, blockquote => 1, center => 1, dir => 1,
5153                    div => 1, dl => 1, fieldset => 1, listing => 1,
5154                    menu => 1, ol => 1, pre => 1, ul => 1,
5155                    p => 1,
5156                    dd => 1, dt => 1, li => 1,
5157                    button => 1, marquee => 1, object => 1,
5158                   }->{$token->{tag_name}}) {
5159            ## has an element in scope
5160            my $i;
5161            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5162              my $node = $self->{open_elements}->[$_];
5163              if ($node->[1] eq $token->{tag_name}) {
5164                ## generate implied end tags
5165                if ({
5166                     dd => ($token->{tag_name} ne 'dd'),
5167                     dt => ($token->{tag_name} ne 'dt'),
5168                     li => ($token->{tag_name} ne 'li'),
5169                     p => ($token->{tag_name} ne 'p'),
5170                     td => 1, th => 1, tr => 1,
5171                     tbody => 1, tfoot=> 1, thead => 1,
5172                    }->{$self->{open_elements}->[-1]->[1]}) {
5173                  !!!back-token;
5174                  $token = {type => END_TAG_TOKEN,
5175                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5176                  redo B;
5177                }
5178                $i = $_;
5179                last INSCOPE unless $token->{tag_name} eq 'p';
5180              } elsif ({
5181                        table => 1, caption => 1, td => 1, th => 1,
5182                        button => 1, marquee => 1, object => 1, html => 1,
5183                       }->{$node->[1]}) {
5184                last INSCOPE;
5185              }
5186            } # INSCOPE
5187            
5188            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5189              if (defined $i) {
5190                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5191              } else {
5192                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5193              }
5194            }
5195            
5196            if (defined $i) {
5197              splice @{$self->{open_elements}}, $i;
5198            } elsif ($token->{tag_name} eq 'p') {
5199              ## As if <p>, then reprocess the current token
5200              my $el;
5201              !!!create-element ($el, 'p');
5202              $insert->($el);
5203            }
5204            $clear_up_to_marker->()
5205              if {
5206                button => 1, marquee => 1, object => 1,
5207              }->{$token->{tag_name}};
5208            !!!next-token;
5209          redo B;          redo B;
5210        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
5211                 $token->{type} eq 'end tag') {          ## has an element in scope
5212          !!!parse-error (type => 'after html:'.$token->{tag_name});          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213          $phase = 'main';            my $node = $self->{open_elements}->[$_];
5214          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
5215                ## generate implied end tags
5216                if ({
5217                     dd => 1, dt => 1, li => 1, p => 1,
5218                     td => 1, th => 1, tr => 1,
5219                     tbody => 1, tfoot=> 1, thead => 1,
5220                    }->{$self->{open_elements}->[-1]->[1]}) {
5221                  !!!back-token;
5222                  $token = {type => END_TAG_TOKEN,
5223                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5224                  redo B;
5225                }
5226                last INSCOPE;
5227              } elsif ({
5228                        table => 1, caption => 1, td => 1, th => 1,
5229                        button => 1, marquee => 1, object => 1, html => 1,
5230                       }->{$node->[1]}) {
5231                last INSCOPE;
5232              }
5233            } # INSCOPE
5234            
5235            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5236              pop @{$self->{open_elements}};
5237            } else {
5238              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5239            }
5240    
5241            undef $self->{form_element};
5242            !!!next-token;
5243            redo B;
5244          } elsif ({
5245                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5246                   }->{$token->{tag_name}}) {
5247            ## has an element in scope
5248            my $i;
5249            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5250              my $node = $self->{open_elements}->[$_];
5251              if ({
5252                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5253                  }->{$node->[1]}) {
5254                ## generate implied end tags
5255                if ({
5256                     dd => 1, dt => 1, li => 1, p => 1,
5257                     td => 1, th => 1, tr => 1,
5258                     tbody => 1, tfoot=> 1, thead => 1,
5259                    }->{$self->{open_elements}->[-1]->[1]}) {
5260                  !!!back-token;
5261                  $token = {type => END_TAG_TOKEN,
5262                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5263                  redo B;
5264                }
5265                $i = $_;
5266                last INSCOPE;
5267              } elsif ({
5268                        table => 1, caption => 1, td => 1, th => 1,
5269                        button => 1, marquee => 1, object => 1, html => 1,
5270                       }->{$node->[1]}) {
5271                last INSCOPE;
5272              }
5273            } # INSCOPE
5274            
5275            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5276              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5277            }
5278            
5279            splice @{$self->{open_elements}}, $i if defined $i;
5280            !!!next-token;
5281            redo B;
5282          } elsif ({
5283                    a => 1,
5284                    b => 1, big => 1, em => 1, font => 1, i => 1,
5285                    nobr => 1, s => 1, small => 1, strile => 1,
5286                    strong => 1, tt => 1, u => 1,
5287                   }->{$token->{tag_name}}) {
5288            $formatting_end_tag->($token->{tag_name});
5289            redo B;
5290          } elsif ($token->{tag_name} eq 'br') {
5291            !!!parse-error (type => 'unmatched end tag:br');
5292    
5293            ## As if <br>
5294            $reconstruct_active_formatting_elements->($insert_to_current);
5295            
5296            my $el;
5297            !!!create-element ($el, 'br');
5298            $insert->($el);
5299            
5300            ## Ignore the token.
5301            !!!next-token;
5302            redo B;
5303          } elsif ({
5304                    caption => 1, col => 1, colgroup => 1, frame => 1,
5305                    frameset => 1, head => 1, option => 1, optgroup => 1,
5306                    tbody => 1, td => 1, tfoot => 1, th => 1,
5307                    thead => 1, tr => 1,
5308                    area => 1, basefont => 1, bgsound => 1,
5309                    embed => 1, hr => 1, iframe => 1, image => 1,
5310                    img => 1, input => 1, isindex => 1, noembed => 1,
5311                    noframes => 1, param => 1, select => 1, spacer => 1,
5312                    table => 1, textarea => 1, wbr => 1,
5313                    noscript => 0, ## TODO: if scripting is enabled
5314                   }->{$token->{tag_name}}) {
5315            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5316            ## Ignore the token
5317            !!!next-token;
5318          redo B;          redo B;
5319        } elsif ($token->{type} eq 'end-of-file') {          
5320          ## Stop parsing          ## ISSUE: Issue on HTML5 new elements in spec
5321          last B;          
5322        } else {        } else {
5323          die "$0: $token->{type}: Unknown token";          ## Step 1
5324            my $node_i = -1;
5325            my $node = $self->{open_elements}->[$node_i];
5326    
5327            ## Step 2
5328            S2: {
5329              if ($node->[1] eq $token->{tag_name}) {
5330                ## Step 1
5331                ## generate implied end tags
5332                if ({
5333                     dd => 1, dt => 1, li => 1, p => 1,
5334                     td => 1, th => 1, tr => 1,
5335                     tbody => 1, tfoot => 1, thead => 1,
5336                    }->{$self->{open_elements}->[-1]->[1]}) {
5337                  !!!back-token;
5338                  $token = {type => END_TAG_TOKEN,
5339                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5340                  redo B;
5341                }
5342            
5343                ## Step 2
5344                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5345                  ## NOTE: <x><y></x>
5346                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5347                }
5348                
5349                ## Step 3
5350                splice @{$self->{open_elements}}, $node_i;
5351    
5352                !!!next-token;
5353                last S2;
5354              } else {
5355                ## Step 3
5356                if (not $formatting_category->{$node->[1]} and
5357                    #not $phrasing_category->{$node->[1]} and
5358                    ($special_category->{$node->[1]} or
5359                     $scoping_category->{$node->[1]})) {
5360                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5361                  ## Ignore the token
5362                  !!!next-token;
5363                  last S2;
5364                }
5365              }
5366              
5367              ## Step 4
5368              $node_i--;
5369              $node = $self->{open_elements}->[$node_i];
5370              
5371              ## Step 5;
5372              redo S2;
5373            } # S2
5374            redo B;
5375        }        }
5376      }      }
5377        redo B;
5378    } # B    } # B
5379    
5380      ## NOTE: The "trailing end" phase in HTML5 is split into
5381      ## two insertion modes: "after html body" and "after html frameset".
5382      ## NOTE: States in the main stage is preserved while
5383      ## the parser stays in the trailing end phase. # MUST
5384    
5385    ## Stop parsing # MUST    ## Stop parsing # MUST
5386        
5387    ## TODO: script stuffs    ## TODO: script stuffs
# Line 4824  sub set_inner_html ($$$) { Line 5393  sub set_inner_html ($$$) {
5393    my $s = \$_[0];    my $s = \$_[0];
5394    my $onerror = $_[1];    my $onerror = $_[1];
5395    
5396      ## ISSUE: Should {confident} be true?
5397    
5398    my $nt = $node->node_type;    my $nt = $node->node_type;
5399    if ($nt == 9) {    if ($nt == 9) {
5400      # MUST      # MUST
# Line 4846  sub set_inner_html ($$$) { Line 5417  sub set_inner_html ($$$) {
5417      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5418    
5419      ## Step 1 # MUST      ## Step 1 # MUST
5420      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5421      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5422        $doc->manakai_is_html (1);
5423      my $p = $class->new;      my $p = $class->new;
5424      $p->{document} = $doc;      $p->{document} = $doc;
5425    
# Line 4857  sub set_inner_html ($$$) { Line 5429  sub set_inner_html ($$$) {
5429      my $column = 0;      my $column = 0;
5430      $p->{set_next_input_character} = sub {      $p->{set_next_input_character} = sub {
5431        my $self = shift;        my $self = shift;
5432    
5433          pop @{$self->{prev_input_character}};
5434          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5435    
5436        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_input_character} = -1 and return if $i >= length $$s;
5437        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_input_character} = ord substr $$s, $i++, 1;
5438        $column++;        $column++;
# Line 4865  sub set_inner_html ($$$) { Line 5441  sub set_inner_html ($$$) {
5441          $line++;          $line++;
5442          $column = 0;          $column = 0;
5443        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_input_character} == 0x000D) { # CR
5444          if ($i >= length $$s) {          $i++ if substr ($$s, $i, 1) eq "\x0A";
           #  
         } else {  
           my $next_char = ord substr $$s, $i++, 1;  
           if ($next_char == 0x000A) { # LF  
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
         }  
5445          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_input_character} = 0x000A; # LF # MUST
5446          $line++;          $line++;
5447          $column = 0;          $column = 0;
5448        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_input_character} > 0x10FFFF) {
5449          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5450        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_input_character} == 0x0000) { # NULL
5451            !!!parse-error (type => 'NULL');
5452          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5453        }        }
5454      };      };
5455        $p->{prev_input_character} = [-1, -1, -1];
5456        $p->{next_input_character} = -1;
5457            
5458      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5459        my (%opt) = @_;        my (%opt) = @_;
# Line 4897  sub set_inner_html ($$$) { Line 5467  sub set_inner_html ($$$) {
5467      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5468    
5469      ## Step 2      ## Step 2
5470      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5471      $p->{content_model_flag} = {      $p->{content_model} = {
5472        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5473        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5474        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5475        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5476        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5477        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5478        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5479        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5480        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5481        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5482      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5483         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5484            unless defined $p->{content_model};
5485            ## ISSUE: What is "the name of the element"? local name?
5486    
5487      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5488    
# Line 4935  sub set_inner_html ($$$) { Line 5507  sub set_inner_html ($$$) {
5507        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5508          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5509          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5510            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5511              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5512              last AN;              last AN;
5513            }            }
# Line 4962  sub set_inner_html ($$$) { Line 5534  sub set_inner_html ($$$) {
5534      ## Step 12 # MUST      ## Step 12 # MUST
5535      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5536      for (@cn) {      for (@cn) {
5537          $this_doc->adopt_node ($_);
5538        $node->append_child ($_);        $node->append_child ($_);
5539      }      }
5540      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5541    
5542      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5543    } else {    } else {
# Line 4974  sub set_inner_html ($$$) { Line 5547  sub set_inner_html ($$$) {
5547    
5548  } # tree construction stage  } # tree construction stage
5549    
5550  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5551    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  
5552    
5553  1;  1;
5554  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24