/[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.67 by wakaba, Sat Feb 16 03:47:33 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    ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT
12    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
13    ## is not yet clear.
14    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
15    ## "{U+FEFF}..." in GB18030?
16    
17  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
18    base => 1,    base => 1,
# Line 18  my $permitted_slash_tag_name = { Line 28  my $permitted_slash_tag_name = {
28    input => 1,    input => 1,
29  };  };
30    
 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>  
31  my $c1_entity_char = {  my $c1_entity_char = {
32       128, 8364,    0x80 => 0x20AC,
33       129, 65533,    0x81 => 0xFFFD,
34       130, 8218,    0x82 => 0x201A,
35       131, 402,    0x83 => 0x0192,
36       132, 8222,    0x84 => 0x201E,
37       133, 8230,    0x85 => 0x2026,
38       134, 8224,    0x86 => 0x2020,
39       135, 8225,    0x87 => 0x2021,
40       136, 710,    0x88 => 0x02C6,
41       137, 8240,    0x89 => 0x2030,
42       138, 352,    0x8A => 0x0160,
43       139, 8249,    0x8B => 0x2039,
44       140, 338,    0x8C => 0x0152,
45       141, 65533,    0x8D => 0xFFFD,
46       142, 381,    0x8E => 0x017D,
47       143, 65533,    0x8F => 0xFFFD,
48       144, 65533,    0x90 => 0xFFFD,
49       145, 8216,    0x91 => 0x2018,
50       146, 8217,    0x92 => 0x2019,
51       147, 8220,    0x93 => 0x201C,
52       148, 8221,    0x94 => 0x201D,
53       149, 8226,    0x95 => 0x2022,
54       150, 8211,    0x96 => 0x2013,
55       151, 8212,    0x97 => 0x2014,
56       152, 732,    0x98 => 0x02DC,
57       153, 8482,    0x99 => 0x2122,
58       154, 353,    0x9A => 0x0161,
59       155, 8250,    0x9B => 0x203A,
60       156, 339,    0x9C => 0x0153,
61       157, 65533,    0x9D => 0xFFFD,
62       158, 382,    0x9E => 0x017E,
63       159, 376,    0x9F => 0x0178,
64  }; # $c1_entity_char  }; # $c1_entity_char
65    
66  my $special_category = {  my $special_category = {
# Line 338  my $formatting_category = { Line 85  my $formatting_category = {
85  };  };
86  # $phrasing_category: all other elements  # $phrasing_category: all other elements
87    
88    sub parse_byte_string ($$$$;$) {
89      my $self = ref $_[0] ? shift : shift->new;
90      my $charset = shift;
91      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
92      my $s;
93      
94      if (defined $charset) {
95        require Encode; ## TODO: decode(utf8) don't delete BOM
96        $s = \ (Encode::decode ($charset, $$bytes_s));
97        $self->{input_encoding} = lc $charset; ## TODO: normalize name
98        $self->{confident} = 1;
99      } else {
100        ## TODO: Implement HTML5 detection algorithm
101        require Whatpm::Charset::UniversalCharDet;
102        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
103            (substr ($$bytes_s, 0, 1024));
104        $charset ||= 'windows-1252';
105        $s = \ (Encode::decode ($charset, $$bytes_s));
106        $self->{input_encoding} = $charset;
107        $self->{confident} = 0;
108      }
109    
110      $self->{change_encoding} = sub {
111        my $self = shift;
112        my $charset = lc shift;
113        ## TODO: if $charset is supported
114        ## TODO: normalize charset name
115    
116        ## "Change the encoding" algorithm:
117    
118        ## Step 1    
119        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
120          $charset = 'utf-8';
121        }
122    
123        ## Step 2
124        if (defined $self->{input_encoding} and
125            $self->{input_encoding} eq $charset) {
126          $self->{confident} = 1;
127          return;
128        }
129    
130        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
131            ':'.$charset, level => 'w');
132    
133        ## Step 3
134        # if (can) {
135          ## change the encoding on the fly.
136          #$self->{confident} = 1;
137          #return;
138        # }
139    
140        ## Step 4
141        throw Whatpm::HTML::RestartParser (charset => $charset);
142      }; # $self->{change_encoding}
143    
144      my @args = @_; shift @args; # $s
145      my $return;
146      try {
147        $return = $self->parse_char_string ($s, @args);  
148      } catch Whatpm::HTML::RestartParser with {
149        my $charset = shift->{charset};
150        $s = \ (Encode::decode ($charset, $$bytes_s));    
151        $self->{input_encoding} = $charset; ## TODO: normalize
152        $self->{confident} = 1;
153        $return = $self->parse_char_string ($s, @args);
154      };
155      return $return;
156    } # parse_byte_string
157    
158    *parse_char_string = \&parse_string;
159    
160  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
161    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
162    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
163    $self->{document} = $_[1];    $self->{document} = $_[1];
164      @{$self->{document}->child_nodes} = ();
165    
166    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
167    
168      $self->{confident} = 1 unless exists $self->{confident};
169      $self->{document}->input_encoding ($self->{input_encoding})
170          if defined $self->{input_encoding};
171    
172    my $i = 0;    my $i = 0;
173    my $line = 1;    my $line = 1;
174    my $column = 0;    my $column = 0;
175    $self->{set_next_input_character} = sub {    $self->{set_next_input_character} = sub {
176      my $self = shift;      my $self = shift;
177    
178        pop @{$self->{prev_input_character}};
179        unshift @{$self->{prev_input_character}}, $self->{next_input_character};
180    
181      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_input_character} = -1 and return if $i >= length $$s;
182      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_input_character} = ord substr $$s, $i++, 1;
183      $column++;      $column++;
# Line 358  sub parse_string ($$$;$) { Line 186  sub parse_string ($$$;$) {
186        $line++;        $line++;
187        $column = 0;        $column = 0;
188      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_input_character} == 0x000D) { # CR
189        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;  
         }  
       }  
190        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_input_character} = 0x000A; # LF # MUST
191        $line++;        $line++;
192        $column = 0;        $column = 0;
193      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_input_character} > 0x10FFFF) {
194        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
195      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_input_character} == 0x0000) { # NULL
196          !!!parse-error (type => 'NULL');
197        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
198      }      }
199    };    };
200      $self->{prev_input_character} = [-1, -1, -1];
201      $self->{next_input_character} = -1;
202    
203    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
204      my (%opt) = @_;      my (%opt) = @_;
# Line 403  sub new ($) { Line 225  sub new ($) {
225    $self->{parse_error} = sub {    $self->{parse_error} = sub {
226      #      #
227    };    };
228      $self->{change_encoding} = sub {
229        # if ($_[0] is a supported encoding) {
230        #   run "change the encoding" algorithm;
231        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
232        # }
233      };
234      $self->{application_cache_selection} = sub {
235        #
236      };
237    return $self;    return $self;
238  } # new  } # new
239    
240    sub CM_ENTITY () { 0b001 } # & markup in data
241    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
242    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
243    
244    sub PLAINTEXT_CONTENT_MODEL () { 0 }
245    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
246    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
247    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
248    
249    sub DATA_STATE () { 0 }
250    sub ENTITY_DATA_STATE () { 1 }
251    sub TAG_OPEN_STATE () { 2 }
252    sub CLOSE_TAG_OPEN_STATE () { 3 }
253    sub TAG_NAME_STATE () { 4 }
254    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
255    sub ATTRIBUTE_NAME_STATE () { 6 }
256    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
257    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
258    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
259    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
260    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
261    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
262    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
263    sub COMMENT_START_STATE () { 14 }
264    sub COMMENT_START_DASH_STATE () { 15 }
265    sub COMMENT_STATE () { 16 }
266    sub COMMENT_END_STATE () { 17 }
267    sub COMMENT_END_DASH_STATE () { 18 }
268    sub BOGUS_COMMENT_STATE () { 19 }
269    sub DOCTYPE_STATE () { 20 }
270    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
271    sub DOCTYPE_NAME_STATE () { 22 }
272    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
273    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
274    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
275    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
276    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
277    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
278    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
279    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
280    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
281    sub BOGUS_DOCTYPE_STATE () { 32 }
282    
283    sub DOCTYPE_TOKEN () { 1 }
284    sub COMMENT_TOKEN () { 2 }
285    sub START_TAG_TOKEN () { 3 }
286    sub END_TAG_TOKEN () { 4 }
287    sub END_OF_FILE_TOKEN () { 5 }
288    sub CHARACTER_TOKEN () { 6 }
289    
290    sub AFTER_HTML_IMS () { 0b100 }
291    sub HEAD_IMS ()       { 0b1000 }
292    sub BODY_IMS ()       { 0b10000 }
293    sub BODY_TABLE_IMS () { 0b100000 }
294    sub TABLE_IMS ()      { 0b1000000 }
295    sub ROW_IMS ()        { 0b10000000 }
296    sub BODY_AFTER_IMS () { 0b100000000 }
297    sub FRAME_IMS ()      { 0b1000000000 }
298    
299    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
300    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
301    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
302    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
303    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
304    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
305    sub IN_BODY_IM () { BODY_IMS }
306    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
307    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
308    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
309    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
310    sub IN_TABLE_IM () { TABLE_IMS }
311    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
312    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
313    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
314    sub IN_SELECT_IM () { 0b01 }
315    sub IN_COLUMN_GROUP_IM () { 0b10 }
316    
317  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
318    
319  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
320    my $self = shift;    my $self = shift;
321    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
322    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
323    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
324    undef $self->{current_attribute};    undef $self->{current_attribute};
325    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 420  sub _initialize_tokenizer ($) { Line 328  sub _initialize_tokenizer ($) {
328    # $self->{next_input_character}    # $self->{next_input_character}
329    !!!next-input-character;    !!!next-input-character;
330    $self->{token} = [];    $self->{token} = [];
331      # $self->{escape}
332  } # _initialize_tokenizer  } # _initialize_tokenizer
333    
334  ## A token has:  ## A token has:
335  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
336  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
337  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
338      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
339  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
340  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
341  ##   ->{data} (comment, character)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
342    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
343  ## Macros  ##        ->{name}
344  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
345  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
346  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
347    
348  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
349    
# Line 444  sub _initialize_tokenizer ($) { Line 353  sub _initialize_tokenizer ($) {
353  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
354  ## and removed from the list.  ## and removed from the list.
355    
356    ## NOTE: HTML5 "Writing HTML documents" section, applied to
357    ## documents and not to user agents and conformance checkers,
358    ## contains some requirements that are not detected by the
359    ## parsing algorithm:
360    ## - Some requirements on character encoding declarations. ## TODO
361    ## - "Elements MUST NOT contain content that their content model disallows."
362    ##   ... Some are parse error, some are not (will be reported by c.c.).
363    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
364    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
365    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
366    
367    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
368    ## be detected by the HTML5 parsing algorithm:
369    ## - Text,
370    
371  sub _get_next_token ($) {  sub _get_next_token ($) {
372    my $self = shift;    my $self = shift;
373    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 451  sub _get_next_token ($) { Line 375  sub _get_next_token ($) {
375    }    }
376    
377    A: {    A: {
378      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
379        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
380          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
381              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
382            !!!next-input-character;            !!!next-input-character;
383            redo A;            redo A;
384          } else {          } else {
385            #            #
386          }          }
387          } elsif ($self->{next_input_character} == 0x002D) { # -
388            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
389              unless ($self->{escape}) {
390                if ($self->{prev_input_character}->[0] == 0x002D and # -
391                    $self->{prev_input_character}->[1] == 0x0021 and # !
392                    $self->{prev_input_character}->[2] == 0x003C) { # <
393                  $self->{escape} = 1;
394                }
395              }
396            }
397            
398            #
399        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
400          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
401            $self->{state} = 'tag open';              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
402                 not $self->{escape})) {
403              $self->{state} = TAG_OPEN_STATE;
404            !!!next-input-character;            !!!next-input-character;
405            redo A;            redo A;
406          } else {          } else {
407            #            #
408          }          }
409          } elsif ($self->{next_input_character} == 0x003E) { # >
410            if ($self->{escape} and
411                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
412              if ($self->{prev_input_character}->[0] == 0x002D and # -
413                  $self->{prev_input_character}->[1] == 0x002D) { # -
414                delete $self->{escape};
415              }
416            }
417            
418            #
419        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
420          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
421          last A; ## TODO: ok?          last A; ## TODO: ok?
422        }        }
423        # Anything else        # Anything else
424        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
425                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
426        ## Stay in the data state        ## Stay in the data state
427        !!!next-input-character;        !!!next-input-character;
# Line 482  sub _get_next_token ($) { Line 429  sub _get_next_token ($) {
429        !!!emit ($token);        !!!emit ($token);
430    
431        redo A;        redo A;
432      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
433        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
434                
435        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
436    
437        $self->{state} = 'data';        $self->{state} = DATA_STATE;
438        # next-input-character is already done        # next-input-character is already done
439    
440        unless (defined $token) {        unless (defined $token) {
441          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
442        } else {        } else {
443          !!!emit ($token);          !!!emit ($token);
444        }        }
445    
446        redo A;        redo A;
447      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
448        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
449          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
450            !!!next-input-character;            !!!next-input-character;
451            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
452            redo A;            redo A;
453          } else {          } else {
454            ## reconsume            ## reconsume
455            $self->{state} = 'data';            $self->{state} = DATA_STATE;
456    
457            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
458    
459            redo A;            redo A;
460          }          }
461        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
462          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
463            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
464            !!!next-input-character;            !!!next-input-character;
465            redo A;            redo A;
466          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
467            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
468            !!!next-input-character;            !!!next-input-character;
469            redo A;            redo A;
470          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
471                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
472            $self->{current_token}            $self->{current_token}
473              = {type => 'start tag',              = {type => START_TAG_TOKEN,
474                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
475            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
476            !!!next-input-character;            !!!next-input-character;
477            redo A;            redo A;
478          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
479                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
480            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
481                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
482            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
483            !!!next-input-character;            !!!next-input-character;
484            redo A;            redo A;
485          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
486            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
487            $self->{state} = 'data';            $self->{state} = DATA_STATE;
488            !!!next-input-character;            !!!next-input-character;
489    
490            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
491    
492            redo A;            redo A;
493          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
494            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
495            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
496            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
497            redo A;            redo A;
498          } else {          } else {
499            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
500            $self->{state} = 'data';            $self->{state} = DATA_STATE;
501            ## reconsume            ## reconsume
502    
503            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
504    
505            redo A;            redo A;
506          }          }
507        } else {        } else {
508          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
509        }        }
510      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
511        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
512            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
513          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
514          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
515              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
516                push @next_char, $self->{next_input_character};
517                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
518                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
519                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
520                  !!!next-input-character;
521                  next TAGNAME;
522                } else {
523                  $self->{next_input_character} = shift @next_char; # reconsume
524                  !!!back-next-input-character (@next_char);
525                  $self->{state} = DATA_STATE;
526    
527                  !!!emit ({type => CHARACTER_TOKEN, data => '</'});
528      
529                  redo A;
530                }
531              }
532            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
533            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
534            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
535            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
536              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
537              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
538            } else {                    $self->{next_input_character} == 0x0020 or # SP
539              !!!parse-error (type => 'unmatched end tag');                    $self->{next_input_character} == 0x003E or # >
540                      $self->{next_input_character} == 0x002F or # /
541                      $self->{next_input_character} == -1) {
542              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
543              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
544              $self->{state} = 'data';              $self->{state} = DATA_STATE;
545                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
             !!!emit ({type => 'character', data => '</'});  
   
546              redo A;              redo A;
547              } else {
548                $self->{next_input_character} = shift @next_char;
549                !!!back-next-input-character (@next_char);
550                # and consume...
551            }            }
         }  
         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;  
552          } else {          } else {
553            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
554            !!!back-next-input-character (@next_char);            # next-input-character is already done
555            # and consume...            $self->{state} = DATA_STATE;
556              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
557              redo A;
558          }          }
559        }        }
560                
561        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
562            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
563          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
564                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
565          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
566          !!!next-input-character;          !!!next-input-character;
567          redo A;          redo A;
568        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
569                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
570          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
571                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
572          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
573          !!!next-input-character;          !!!next-input-character;
574          redo A;          redo A;
575        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
576          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
577          $self->{state} = 'data';          $self->{state} = DATA_STATE;
578          !!!next-input-character;          !!!next-input-character;
579          redo A;          redo A;
580        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
581          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
582          $self->{state} = 'data';          $self->{state} = DATA_STATE;
583          # reconsume          # reconsume
584    
585          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
586    
587          redo A;          redo A;
588        } else {        } else {
589          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
590          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
591          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
592          redo A;          redo A;
593        }        }
594      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
595        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
596            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
597            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
598            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
599            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
600          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
601          !!!next-input-character;          !!!next-input-character;
602          redo A;          redo A;
603        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
604          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
605              $self->{current_token}->{first_start_tag}
606                  = not defined $self->{last_emitted_start_tag_name};
607            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
608          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
609            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
610            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
611              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
612            }            }
613          } else {          } else {
614            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
615          }          }
616          $self->{state} = 'data';          $self->{state} = DATA_STATE;
617          !!!next-input-character;          !!!next-input-character;
618    
619          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
620    
621          redo A;          redo A;
622        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 676  sub _get_next_token ($) { Line 626  sub _get_next_token ($) {
626          ## Stay in this state          ## Stay in this state
627          !!!next-input-character;          !!!next-input-character;
628          redo A;          redo A;
629        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
630          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
631          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
632              $self->{current_token}->{first_start_tag}
633                  = not defined $self->{last_emitted_start_tag_name};
634            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
635          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
636            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
637            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
638              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
639            }            }
640          } else {          } else {
641            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
642          }          }
643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
644          # reconsume          # reconsume
645    
646          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
647    
648          redo A;          redo A;
649        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
650          !!!next-input-character;          !!!next-input-character;
651          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
652              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
653              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
654            # permitted slash            # permitted slash
655            #            #
656          } else {          } else {
657            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
658          }          }
659          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
660          # next-input-character is already done          # next-input-character is already done
661          redo A;          redo A;
662        } else {        } else {
# Line 716  sub _get_next_token ($) { Line 666  sub _get_next_token ($) {
666          !!!next-input-character;          !!!next-input-character;
667          redo A;          redo A;
668        }        }
669      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
670        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
671            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
672            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 726  sub _get_next_token ($) { Line 676  sub _get_next_token ($) {
676          !!!next-input-character;          !!!next-input-character;
677          redo A;          redo A;
678        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
679          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
680              $self->{current_token}->{first_start_tag}
681                  = not defined $self->{last_emitted_start_tag_name};
682            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
683          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
684            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
685            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
686              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
687            }            }
688          } else {          } else {
689            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
690          }          }
691          $self->{state} = 'data';          $self->{state} = DATA_STATE;
692          !!!next-input-character;          !!!next-input-character;
693    
694          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
695    
696          redo A;          redo A;
697        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
698                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
699          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
700                                value => ''};                                value => ''};
701          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
702          !!!next-input-character;          !!!next-input-character;
703          redo A;          redo A;
704        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
705          !!!next-input-character;          !!!next-input-character;
706          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
707              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
708              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
709            # permitted slash            # permitted slash
710            #            #
# Line 763  sub _get_next_token ($) { Line 714  sub _get_next_token ($) {
714          ## Stay in the state          ## Stay in the state
715          # next-input-character is already done          # next-input-character is already done
716          redo A;          redo A;
717        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
718          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
719          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
720              $self->{current_token}->{first_start_tag}
721                  = not defined $self->{last_emitted_start_tag_name};
722            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
723          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
724            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
725            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
726              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
727            }            }
728          } else {          } else {
729            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
730          }          }
731          $self->{state} = 'data';          $self->{state} = DATA_STATE;
732          # reconsume          # reconsume
733    
734          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
735    
736          redo A;          redo A;
737        } else {        } else {
738          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
739                                value => ''};                                value => ''};
740          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
741          !!!next-input-character;          !!!next-input-character;
742          redo A;          redo A;
743        }        }
744      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
745        my $before_leave = sub {        my $before_leave = sub {
746          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
747              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
748            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
749            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
750          } else {          } else {
751            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 808  sub _get_next_token ($) { Line 759  sub _get_next_token ($) {
759            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
760            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
761          $before_leave->();          $before_leave->();
762          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
763          !!!next-input-character;          !!!next-input-character;
764          redo A;          redo A;
765        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
766          $before_leave->();          $before_leave->();
767          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
768          !!!next-input-character;          !!!next-input-character;
769          redo A;          redo A;
770        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
771          $before_leave->();          $before_leave->();
772          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
773              $self->{current_token}->{first_start_tag}
774                  = not defined $self->{last_emitted_start_tag_name};
775            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
776          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
777            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
778            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
779              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
780            }            }
781          } else {          } else {
782            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
783          }          }
784          $self->{state} = 'data';          $self->{state} = DATA_STATE;
785          !!!next-input-character;          !!!next-input-character;
786    
787          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
788    
789          redo A;          redo A;
790        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 845  sub _get_next_token ($) { Line 797  sub _get_next_token ($) {
797          $before_leave->();          $before_leave->();
798          !!!next-input-character;          !!!next-input-character;
799          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
800              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
801              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
802            # permitted slash            # permitted slash
803            #            #
804          } else {          } else {
805            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
806          }          }
807          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
808          # next-input-character is already done          # next-input-character is already done
809          redo A;          redo A;
810        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
811          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
812          $before_leave->();          $before_leave->();
813          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
814              $self->{current_token}->{first_start_tag}
815                  = not defined $self->{last_emitted_start_tag_name};
816            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
817          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
818            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
819            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
820              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
821            }            }
822          } else {          } else {
823            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
824          }          }
825          $self->{state} = 'data';          $self->{state} = DATA_STATE;
826          # reconsume          # reconsume
827    
828          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
829    
830          redo A;          redo A;
831        } else {        } else {
# Line 882  sub _get_next_token ($) { Line 834  sub _get_next_token ($) {
834          !!!next-input-character;          !!!next-input-character;
835          redo A;          redo A;
836        }        }
837      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
838        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
839            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
840            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 892  sub _get_next_token ($) { Line 844  sub _get_next_token ($) {
844          !!!next-input-character;          !!!next-input-character;
845          redo A;          redo A;
846        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
847          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
848          !!!next-input-character;          !!!next-input-character;
849          redo A;          redo A;
850        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
851          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
852              $self->{current_token}->{first_start_tag}
853                  = not defined $self->{last_emitted_start_tag_name};
854            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
855          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
856            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
857            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
858              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
859            }            }
860          } else {          } else {
861            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
862          }          }
863          $self->{state} = 'data';          $self->{state} = DATA_STATE;
864          !!!next-input-character;          !!!next-input-character;
865    
866          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
867    
868          redo A;          redo A;
869        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
870                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
871          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
872                                value => ''};                                value => ''};
873          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
874          !!!next-input-character;          !!!next-input-character;
875          redo A;          redo A;
876        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
877          !!!next-input-character;          !!!next-input-character;
878          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
879              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
880              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
881            # permitted slash            # permitted slash
882            #            #
883          } else {          } else {
884            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
885              ## TODO: Different error type for <aa / bb> than <aa/>
886          }          }
887          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
888          # next-input-character is already done          # next-input-character is already done
889          redo A;          redo A;
890        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
891          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
892          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
893              $self->{current_token}->{first_start_tag}
894                  = not defined $self->{last_emitted_start_tag_name};
895            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
896          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
897            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
898            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
899              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
900            }            }
901          } else {          } else {
902            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
903          }          }
904          $self->{state} = 'data';          $self->{state} = DATA_STATE;
905          # reconsume          # reconsume
906    
907          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
908    
909          redo A;          redo A;
910        } else {        } else {
911          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
912                                value => ''};                                value => ''};
913          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
914          !!!next-input-character;          !!!next-input-character;
915          redo A;                  redo A;        
916        }        }
917      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
918        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
919            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
920            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 970  sub _get_next_token ($) { Line 924  sub _get_next_token ($) {
924          !!!next-input-character;          !!!next-input-character;
925          redo A;          redo A;
926        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
927          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
928          !!!next-input-character;          !!!next-input-character;
929          redo A;          redo A;
930        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
931          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
932          ## reconsume          ## reconsume
933          redo A;          redo A;
934        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
935          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
936          !!!next-input-character;          !!!next-input-character;
937          redo A;          redo A;
938        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
939          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
940              $self->{current_token}->{first_start_tag}
941                  = not defined $self->{last_emitted_start_tag_name};
942            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
943          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
944            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
945            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
946              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
947            }            }
948          } else {          } else {
949            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
950          }          }
951          $self->{state} = 'data';          $self->{state} = DATA_STATE;
952          !!!next-input-character;          !!!next-input-character;
953    
954          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
955    
956          redo A;          redo A;
957        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
958          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
959          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
960              $self->{current_token}->{first_start_tag}
961                  = not defined $self->{last_emitted_start_tag_name};
962            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
963          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
964            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
965            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
966              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
967            }            }
968          } else {          } else {
969            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
970          }          }
971          $self->{state} = 'data';          $self->{state} = DATA_STATE;
972          ## reconsume          ## reconsume
973    
974          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
975    
976          redo A;          redo A;
977        } else {        } else {
978          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
979          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
980          !!!next-input-character;          !!!next-input-character;
981          redo A;          redo A;
982        }        }
983      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
984        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
985          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
986          !!!next-input-character;          !!!next-input-character;
987          redo A;          redo A;
988        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
989          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
990          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
991          !!!next-input-character;          !!!next-input-character;
992          redo A;          redo A;
993        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
994          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
995          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
996              $self->{current_token}->{first_start_tag}
997                  = not defined $self->{last_emitted_start_tag_name};
998            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
999          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1000            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1001            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1002              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1003            }            }
1004          } else {          } else {
1005            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1006          }          }
1007          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1008          ## reconsume          ## reconsume
1009    
1010          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1011    
1012          redo A;          redo A;
1013        } else {        } else {
# Line 1060  sub _get_next_token ($) { Line 1016  sub _get_next_token ($) {
1016          !!!next-input-character;          !!!next-input-character;
1017          redo A;          redo A;
1018        }        }
1019      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1020        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1021          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1022          !!!next-input-character;          !!!next-input-character;
1023          redo A;          redo A;
1024        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1025          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1026          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1027          !!!next-input-character;          !!!next-input-character;
1028          redo A;          redo A;
1029        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1030          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1031          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1032              $self->{current_token}->{first_start_tag}
1033                  = not defined $self->{last_emitted_start_tag_name};
1034            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1035          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1036            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1037            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1038              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1039            }            }
1040          } else {          } else {
1041            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1042          }          }
1043          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1044          ## reconsume          ## reconsume
1045    
1046          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1047    
1048          redo A;          redo A;
1049        } else {        } else {
# Line 1095  sub _get_next_token ($) { Line 1052  sub _get_next_token ($) {
1052          !!!next-input-character;          !!!next-input-character;
1053          redo A;          redo A;
1054        }        }
1055      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1056        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1057            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1058            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1059            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1060            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1061          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1062          !!!next-input-character;          !!!next-input-character;
1063          redo A;          redo A;
1064        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1065          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1066          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1067          !!!next-input-character;          !!!next-input-character;
1068          redo A;          redo A;
1069        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1070          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1071              $self->{current_token}->{first_start_tag}
1072                  = not defined $self->{last_emitted_start_tag_name};
1073            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1074          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1075            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1076            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1077              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1078            }            }
1079          } else {          } else {
1080            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1081          }          }
1082          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1083          !!!next-input-character;          !!!next-input-character;
1084    
1085          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1086    
1087          redo A;          redo A;
1088        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
1089          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1090          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1091              $self->{current_token}->{first_start_tag}
1092                  = not defined $self->{last_emitted_start_tag_name};
1093            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1094          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1095            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1096            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1097              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1098            }            }
1099          } else {          } else {
1100            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1101          }          }
1102          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1103          ## reconsume          ## reconsume
1104    
1105          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1106    
1107          redo A;          redo A;
1108        } else {        } else {
# Line 1153  sub _get_next_token ($) { Line 1111  sub _get_next_token ($) {
1111          !!!next-input-character;          !!!next-input-character;
1112          redo A;          redo A;
1113        }        }
1114      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1115        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1116    
1117        unless (defined $token) {        unless (defined $token) {
1118          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1119        } else {        } else {
1120          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1121            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1122          ## 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"
1123        }        }
1124    
1125        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1126        # next-input-character is already done        # next-input-character is already done
1127        redo A;        redo A;
1128      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1129        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1130                
1131        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1132    
1133        BC: {        BC: {
1134          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1135            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1136            !!!next-input-character;            !!!next-input-character;
1137    
1138            !!!emit ($token);            !!!emit ($token);
1139    
1140            redo A;            redo A;
1141          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1142            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1143            ## reconsume            ## reconsume
1144    
1145            !!!emit ($token);            !!!emit ($token);
# Line 1192  sub _get_next_token ($) { Line 1151  sub _get_next_token ($) {
1151            redo BC;            redo BC;
1152          }          }
1153        } # BC        } # BC
1154      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1155        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1156    
1157        my @next_char;        my @next_char;
# Line 1202  sub _get_next_token ($) { Line 1161  sub _get_next_token ($) {
1161          !!!next-input-character;          !!!next-input-character;
1162          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1163          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1164            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1165            $self->{state} = 'comment';            $self->{state} = COMMENT_START_STATE;
1166            !!!next-input-character;            !!!next-input-character;
1167            redo A;            redo A;
1168          }          }
# Line 1234  sub _get_next_token ($) { Line 1193  sub _get_next_token ($) {
1193                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1194                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1195                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1196                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1197                      !!!next-input-character;                      !!!next-input-character;
1198                      redo A;                      redo A;
1199                    }                    }
# Line 1245  sub _get_next_token ($) { Line 1204  sub _get_next_token ($) {
1204          }          }
1205        }        }
1206    
1207        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1208        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1209        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1210        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1211        redo A;        redo A;
1212                
1213        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1214        ## 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?
1215      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1216        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1217          $self->{state} = 'comment dash';          $self->{state} = COMMENT_START_DASH_STATE;
1218            !!!next-input-character;
1219            redo A;
1220          } elsif ($self->{next_input_character} == 0x003E) { # >
1221            !!!parse-error (type => 'bogus comment');
1222            $self->{state} = DATA_STATE;
1223          !!!next-input-character;          !!!next-input-character;
1224    
1225            !!!emit ($self->{current_token}); # comment
1226    
1227          redo A;          redo A;
1228        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1229          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1230          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1231            ## reconsume
1232    
1233            !!!emit ($self->{current_token}); # comment
1234    
1235            redo A;
1236          } else {
1237            $self->{current_token}->{data} # comment
1238                .= chr ($self->{next_input_character});
1239            $self->{state} = COMMENT_STATE;
1240            !!!next-input-character;
1241            redo A;
1242          }
1243        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1244          if ($self->{next_input_character} == 0x002D) { # -
1245            $self->{state} = COMMENT_END_STATE;
1246            !!!next-input-character;
1247            redo A;
1248          } elsif ($self->{next_input_character} == 0x003E) { # >
1249            !!!parse-error (type => 'bogus comment');
1250            $self->{state} = DATA_STATE;
1251            !!!next-input-character;
1252    
1253            !!!emit ($self->{current_token}); # comment
1254    
1255            redo A;
1256          } elsif ($self->{next_input_character} == -1) {
1257            !!!parse-error (type => 'unclosed comment');
1258            $self->{state} = DATA_STATE;
1259            ## reconsume
1260    
1261            !!!emit ($self->{current_token}); # comment
1262    
1263            redo A;
1264          } else {
1265            $self->{current_token}->{data} # comment
1266                .= '-' . chr ($self->{next_input_character});
1267            $self->{state} = COMMENT_STATE;
1268            !!!next-input-character;
1269            redo A;
1270          }
1271        } elsif ($self->{state} == COMMENT_STATE) {
1272          if ($self->{next_input_character} == 0x002D) { # -
1273            $self->{state} = COMMENT_END_DASH_STATE;
1274            !!!next-input-character;
1275            redo A;
1276          } elsif ($self->{next_input_character} == -1) {
1277            !!!parse-error (type => 'unclosed comment');
1278            $self->{state} = DATA_STATE;
1279          ## reconsume          ## reconsume
1280    
1281          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1282    
1283          redo A;          redo A;
1284        } else {        } else {
# Line 1273  sub _get_next_token ($) { Line 1287  sub _get_next_token ($) {
1287          !!!next-input-character;          !!!next-input-character;
1288          redo A;          redo A;
1289        }        }
1290      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1291        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1292          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294          redo A;          redo A;
1295        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1296          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1297          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1298          ## reconsume          ## reconsume
1299    
1300          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1301    
1302          redo A;          redo A;
1303        } else {        } else {
1304          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1305          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        }        }
1309      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1310        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1311          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1312          !!!next-input-character;          !!!next-input-character;
1313    
1314          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1315    
1316          redo A;          redo A;
1317        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1310  sub _get_next_token ($) { Line 1322  sub _get_next_token ($) {
1322          redo A;          redo A;
1323        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1324          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1325          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1326          ## reconsume          ## reconsume
1327    
1328          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1329    
1330          redo A;          redo A;
1331        } else {        } else {
1332          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1333          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1334          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1335          !!!next-input-character;          !!!next-input-character;
1336          redo A;          redo A;
1337        }        }
1338      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1339        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1340            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1341            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1342            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1343            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1344          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1345          !!!next-input-character;          !!!next-input-character;
1346          redo A;          redo A;
1347        } else {        } else {
1348          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1349          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1350          ## reconsume          ## reconsume
1351          redo A;          redo A;
1352        }        }
1353      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1354        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1355            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1356            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1348  sub _get_next_token ($) { Line 1359  sub _get_next_token ($) {
1359          ## Stay in the state          ## Stay in the state
1360          !!!next-input-character;          !!!next-input-character;
1361          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;  
1362        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1363          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1364          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1365          !!!next-input-character;          !!!next-input-character;
1366    
1367          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1368    
1369          redo A;          redo A;
1370        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1371          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1372          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1373          ## reconsume          ## reconsume
1374    
1375          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1376    
1377          redo A;          redo A;
1378        } else {        } else {
1379          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1380                            name => chr ($self->{next_input_character}),              = {type => DOCTYPE_TOKEN,
1381                            error => 1};                 name => chr ($self->{next_input_character}),
1382                   correct => 1};
1383  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1384          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1385          !!!next-input-character;          !!!next-input-character;
1386          redo A;          redo A;
1387        }        }
1388      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1389    ## ISSUE: Redundant "First," in the spec.
1390        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1391            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1392            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1393            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1394            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1395          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
         $self->{state} = 'after DOCTYPE name';  
1396          !!!next-input-character;          !!!next-input-character;
1397          redo A;          redo A;
1398        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1399          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
1400          !!!next-input-character;          !!!next-input-character;
1401    
1402          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1403    
1404          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;  
1405        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1406          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1407          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
1408          ## reconsume          ## reconsume
1409    
1410          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1411          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1412    
1413          redo A;          redo A;
1414        } else {        } else {
1415          $self->{current_token}->{name}          $self->{current_token}->{name}
1416            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1417          ## Stay in the state          ## Stay in the state
1418          !!!next-input-character;          !!!next-input-character;
1419          redo A;          redo A;
1420        }        }
1421      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1422        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1423            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1424            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1436  sub _get_next_token ($) { Line 1428  sub _get_next_token ($) {
1428          !!!next-input-character;          !!!next-input-character;
1429          redo A;          redo A;
1430        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1431          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1432            !!!next-input-character;
1433    
1434            !!!emit ($self->{current_token}); # DOCTYPE
1435    
1436            redo A;
1437          } elsif ($self->{next_input_character} == -1) {
1438            !!!parse-error (type => 'unclosed DOCTYPE');
1439            $self->{state} = DATA_STATE;
1440            ## reconsume
1441    
1442            delete $self->{current_token}->{correct};
1443            !!!emit ($self->{current_token}); # DOCTYPE
1444    
1445            redo A;
1446          } elsif ($self->{next_input_character} == 0x0050 or # P
1447                   $self->{next_input_character} == 0x0070) { # p
1448            !!!next-input-character;
1449            if ($self->{next_input_character} == 0x0055 or # U
1450                $self->{next_input_character} == 0x0075) { # u
1451              !!!next-input-character;
1452              if ($self->{next_input_character} == 0x0042 or # B
1453                  $self->{next_input_character} == 0x0062) { # b
1454                !!!next-input-character;
1455                if ($self->{next_input_character} == 0x004C or # L
1456                    $self->{next_input_character} == 0x006C) { # l
1457                  !!!next-input-character;
1458                  if ($self->{next_input_character} == 0x0049 or # I
1459                      $self->{next_input_character} == 0x0069) { # i
1460                    !!!next-input-character;
1461                    if ($self->{next_input_character} == 0x0043 or # C
1462                        $self->{next_input_character} == 0x0063) { # c
1463                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1464                      !!!next-input-character;
1465                      redo A;
1466                    }
1467                  }
1468                }
1469              }
1470            }
1471    
1472            #
1473          } elsif ($self->{next_input_character} == 0x0053 or # S
1474                   $self->{next_input_character} == 0x0073) { # s
1475            !!!next-input-character;
1476            if ($self->{next_input_character} == 0x0059 or # Y
1477                $self->{next_input_character} == 0x0079) { # y
1478              !!!next-input-character;
1479              if ($self->{next_input_character} == 0x0053 or # S
1480                  $self->{next_input_character} == 0x0073) { # s
1481                !!!next-input-character;
1482                if ($self->{next_input_character} == 0x0054 or # T
1483                    $self->{next_input_character} == 0x0074) { # t
1484                  !!!next-input-character;
1485                  if ($self->{next_input_character} == 0x0045 or # E
1486                      $self->{next_input_character} == 0x0065) { # e
1487                    !!!next-input-character;
1488                    if ($self->{next_input_character} == 0x004D or # M
1489                        $self->{next_input_character} == 0x006D) { # m
1490                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1491                      !!!next-input-character;
1492                      redo A;
1493                    }
1494                  }
1495                }
1496              }
1497            }
1498    
1499            #
1500          } else {
1501            !!!next-input-character;
1502            #
1503          }
1504    
1505          !!!parse-error (type => 'string after DOCTYPE name');
1506          $self->{state} = BOGUS_DOCTYPE_STATE;
1507          # next-input-character is already done
1508          redo A;
1509        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1510          if ({
1511                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1512                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1513              }->{$self->{next_input_character}}) {
1514            ## Stay in the state
1515            !!!next-input-character;
1516            redo A;
1517          } elsif ($self->{next_input_character} eq 0x0022) { # "
1518            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1519            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1520            !!!next-input-character;
1521            redo A;
1522          } elsif ($self->{next_input_character} eq 0x0027) { # '
1523            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1524            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1525            !!!next-input-character;
1526            redo A;
1527          } elsif ($self->{next_input_character} eq 0x003E) { # >
1528            !!!parse-error (type => 'no PUBLIC literal');
1529    
1530            $self->{state} = DATA_STATE;
1531            !!!next-input-character;
1532    
1533            delete $self->{current_token}->{correct};
1534            !!!emit ($self->{current_token}); # DOCTYPE
1535    
1536            redo A;
1537          } elsif ($self->{next_input_character} == -1) {
1538            !!!parse-error (type => 'unclosed DOCTYPE');
1539    
1540            $self->{state} = DATA_STATE;
1541            ## reconsume
1542    
1543            delete $self->{current_token}->{correct};
1544            !!!emit ($self->{current_token}); # DOCTYPE
1545    
1546            redo A;
1547          } else {
1548            !!!parse-error (type => 'string after PUBLIC');
1549            $self->{state} = BOGUS_DOCTYPE_STATE;
1550            !!!next-input-character;
1551            redo A;
1552          }
1553        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1554          if ($self->{next_input_character} == 0x0022) { # "
1555            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1556            !!!next-input-character;
1557            redo A;
1558          } elsif ($self->{next_input_character} == -1) {
1559            !!!parse-error (type => 'unclosed PUBLIC literal');
1560    
1561            $self->{state} = DATA_STATE;
1562            ## reconsume
1563    
1564            delete $self->{current_token}->{correct};
1565            !!!emit ($self->{current_token}); # DOCTYPE
1566    
1567            redo A;
1568          } else {
1569            $self->{current_token}->{public_identifier} # DOCTYPE
1570                .= chr $self->{next_input_character};
1571            ## Stay in the state
1572            !!!next-input-character;
1573            redo A;
1574          }
1575        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1576          if ($self->{next_input_character} == 0x0027) { # '
1577            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1578            !!!next-input-character;
1579            redo A;
1580          } elsif ($self->{next_input_character} == -1) {
1581            !!!parse-error (type => 'unclosed PUBLIC literal');
1582    
1583            $self->{state} = DATA_STATE;
1584            ## reconsume
1585    
1586            delete $self->{current_token}->{correct};
1587            !!!emit ($self->{current_token}); # DOCTYPE
1588    
1589            redo A;
1590          } else {
1591            $self->{current_token}->{public_identifier} # DOCTYPE
1592                .= chr $self->{next_input_character};
1593            ## Stay in the state
1594            !!!next-input-character;
1595            redo A;
1596          }
1597        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1598          if ({
1599                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1600                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1601              }->{$self->{next_input_character}}) {
1602            ## Stay in the state
1603            !!!next-input-character;
1604            redo A;
1605          } elsif ($self->{next_input_character} == 0x0022) { # "
1606            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1607            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1608            !!!next-input-character;
1609            redo A;
1610          } elsif ($self->{next_input_character} == 0x0027) { # '
1611            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1612            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1613            !!!next-input-character;
1614            redo A;
1615          } elsif ($self->{next_input_character} == 0x003E) { # >
1616            $self->{state} = DATA_STATE;
1617            !!!next-input-character;
1618    
1619            !!!emit ($self->{current_token}); # DOCTYPE
1620    
1621            redo A;
1622          } elsif ($self->{next_input_character} == -1) {
1623            !!!parse-error (type => 'unclosed DOCTYPE');
1624    
1625            $self->{state} = DATA_STATE;
1626            ## reconsume
1627    
1628            delete $self->{current_token}->{correct};
1629            !!!emit ($self->{current_token}); # DOCTYPE
1630    
1631            redo A;
1632          } else {
1633            !!!parse-error (type => 'string after PUBLIC literal');
1634            $self->{state} = BOGUS_DOCTYPE_STATE;
1635            !!!next-input-character;
1636            redo A;
1637          }
1638        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1639          if ({
1640                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1641                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1642              }->{$self->{next_input_character}}) {
1643            ## Stay in the state
1644            !!!next-input-character;
1645            redo A;
1646          } elsif ($self->{next_input_character} == 0x0022) { # "
1647            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1648            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1649            !!!next-input-character;
1650            redo A;
1651          } elsif ($self->{next_input_character} == 0x0027) { # '
1652            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1653            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1654            !!!next-input-character;
1655            redo A;
1656          } elsif ($self->{next_input_character} == 0x003E) { # >
1657            !!!parse-error (type => 'no SYSTEM literal');
1658            $self->{state} = DATA_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660    
1661            delete $self->{current_token}->{correct};
1662          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1663    
1664          redo A;          redo A;
1665        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1666          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1667          $self->{state} = 'data';  
1668            $self->{state} = DATA_STATE;
1669          ## reconsume          ## reconsume
1670    
1671            delete $self->{current_token}->{correct};
1672          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1673    
1674          redo A;          redo A;
1675        } else {        } else {
1676          !!!parse-error (type => 'string after DOCTYPE name');          !!!parse-error (type => 'string after SYSTEM');
1677          $self->{current_token}->{error} = 1; # DOCTYPE          $self->{state} = BOGUS_DOCTYPE_STATE;
         $self->{state} = 'bogus DOCTYPE';  
1678          !!!next-input-character;          !!!next-input-character;
1679          redo A;          redo A;
1680        }        }
1681      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1682          if ($self->{next_input_character} == 0x0022) { # "
1683            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1684            !!!next-input-character;
1685            redo A;
1686          } elsif ($self->{next_input_character} == -1) {
1687            !!!parse-error (type => 'unclosed SYSTEM literal');
1688    
1689            $self->{state} = DATA_STATE;
1690            ## reconsume
1691    
1692            delete $self->{current_token}->{correct};
1693            !!!emit ($self->{current_token}); # DOCTYPE
1694    
1695            redo A;
1696          } else {
1697            $self->{current_token}->{system_identifier} # DOCTYPE
1698                .= chr $self->{next_input_character};
1699            ## Stay in the state
1700            !!!next-input-character;
1701            redo A;
1702          }
1703        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1704          if ($self->{next_input_character} == 0x0027) { # '
1705            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1706            !!!next-input-character;
1707            redo A;
1708          } elsif ($self->{next_input_character} == -1) {
1709            !!!parse-error (type => 'unclosed SYSTEM literal');
1710    
1711            $self->{state} = DATA_STATE;
1712            ## reconsume
1713    
1714            delete $self->{current_token}->{correct};
1715            !!!emit ($self->{current_token}); # DOCTYPE
1716    
1717            redo A;
1718          } else {
1719            $self->{current_token}->{system_identifier} # DOCTYPE
1720                .= chr $self->{next_input_character};
1721            ## Stay in the state
1722            !!!next-input-character;
1723            redo A;
1724          }
1725        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1726          if ({
1727                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1728                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1729              }->{$self->{next_input_character}}) {
1730            ## Stay in the state
1731            !!!next-input-character;
1732            redo A;
1733          } elsif ($self->{next_input_character} == 0x003E) { # >
1734            $self->{state} = DATA_STATE;
1735            !!!next-input-character;
1736    
1737            !!!emit ($self->{current_token}); # DOCTYPE
1738    
1739            redo A;
1740          } elsif ($self->{next_input_character} == -1) {
1741            !!!parse-error (type => 'unclosed DOCTYPE');
1742    
1743            $self->{state} = DATA_STATE;
1744            ## reconsume
1745    
1746            delete $self->{current_token}->{correct};
1747            !!!emit ($self->{current_token}); # DOCTYPE
1748    
1749            redo A;
1750          } else {
1751            !!!parse-error (type => 'string after SYSTEM literal');
1752            $self->{state} = BOGUS_DOCTYPE_STATE;
1753            !!!next-input-character;
1754            redo A;
1755          }
1756        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1757        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1758          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1759          !!!next-input-character;          !!!next-input-character;
1760    
1761            delete $self->{current_token}->{correct};
1762          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1763    
1764          redo A;          redo A;
1765        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1766          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1767          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1768          ## reconsume          ## reconsume
1769    
1770            delete $self->{current_token}->{correct};
1771          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1772    
1773          redo A;          redo A;
1774        } else {        } else {
# Line 1490  sub _get_next_token ($) { Line 1784  sub _get_next_token ($) {
1784    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1785  } # _get_next_token  } # _get_next_token
1786    
1787  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1788    my $self = shift;    my ($self, $in_attr) = @_;
1789      
1790    if ($self->{next_input_character} == 0x0023) { # #    if ({
1791           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1792           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1793          }->{$self->{next_input_character}}) {
1794        ## Don't consume
1795        ## No error
1796        return undef;
1797      } elsif ($self->{next_input_character} == 0x0023) { # #
1798      !!!next-input-character;      !!!next-input-character;
1799      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1800          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1801        my $num;        my $code;
1802        X: {        X: {
1803          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1804          !!!next-input-character;          !!!next-input-character;
1805          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1806              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1807            $num ||= 0;            $code ||= 0;
1808            $num *= 0x10;            $code *= 0x10;
1809            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1810            redo X;            redo X;
1811          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1812                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1813            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1814            $num ||= 0;            $code *= 0x10;
1815            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1816            redo X;            redo X;
1817          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1818                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1819            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1820            $num ||= 0;            $code *= 0x10;
1821            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1822            redo X;            redo X;
1823          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1824            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1825              !!!back-next-input-character ($x_char, $self->{next_input_character});
1826            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1827            return undef;            return undef;
1828          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1829            !!!next-input-character;            !!!next-input-character;
# Line 1532  sub _tokenize_attempt_to_consume_an_enti Line 1831  sub _tokenize_attempt_to_consume_an_enti
1831            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1832          }          }
1833    
1834          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1835          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1836          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1837            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1838            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1839          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1840            ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>          } elsif ($code == 0x000D) {
1841            ## ISSUE: Not in the spec yet; parse error?            !!!parse-error (type => 'CR character reference');
1842            $num = $c1_entity_char->{$num};            $code = 0x000A;
1843            } elsif (0x80 <= $code and $code <= 0x9F) {
1844              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1845              $code = $c1_entity_char->{$code};
1846          }          }
1847    
1848          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code,
1849                    has_reference => 1};
1850        } # X        } # X
1851      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1852               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1564  sub _tokenize_attempt_to_consume_an_enti Line 1867  sub _tokenize_attempt_to_consume_an_enti
1867          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1868        }        }
1869    
1870        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1871        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1872          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1873          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1874            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1875            $code = 0xFFFD;
1876          } elsif ($code == 0x000D) {
1877            !!!parse-error (type => 'CR character reference');
1878            $code = 0x000A;
1879        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1880          ## 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?  
1881          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1882        }        }
1883                
1884        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1885      } else {      } else {
1886        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1887        !!!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 1896  sub _tokenize_attempt_to_consume_an_enti
1896      !!!next-input-character;      !!!next-input-character;
1897    
1898      my $value = $entity_name;      my $value = $entity_name;
1899      my $match;      my $match = 0;
1900        require Whatpm::_NamedEntityList;
1901        our $EntityChar;
1902    
1903      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1904             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1905             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1906               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1907              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1908               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1909              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1910               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1911                $self->{next_input_character} == 0x003B)) { # ;
1912        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1913        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1914          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1915          $match = 1;            $value = $EntityChar->{$entity_name};
1916              $match = 1;
1917              !!!next-input-character;
1918              last;
1919            } else {
1920              $value = $EntityChar->{$entity_name};
1921              $match = -1;
1922              !!!next-input-character;
1923            }
1924        } else {        } else {
1925          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1926            $match *= 2;
1927            !!!next-input-character;
1928        }        }
       !!!next-input-character;  
1929      }      }
1930            
1931      if ($match) {      if ($match > 0) {
1932        if ($self->{next_input_character} == 0x003B) { # ;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1933          !!!next-input-character;      } elsif ($match < 0) {
1934          !!!parse-error (type => 'no refc');
1935          if ($in_attr and $match < -1) {
1936            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1937        } else {        } else {
1938          !!!parse-error (type => 'refc');          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1939        }        }
   
       return {type => 'character', data => $value};  
1940      } else {      } else {
1941        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1942        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1943        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1944      }      }
1945    } else {    } else {
1946      ## no characters are consumed      ## no characters are consumed
# Line 1636  sub _initialize_tree_constructor ($) { Line 1955  sub _initialize_tree_constructor ($) {
1955    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1956    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1957    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1958    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1959  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1960    
1961  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1663  sub _construct_tree ($) { Line 1982  sub _construct_tree ($) {
1982        
1983    !!!next-token;    !!!next-token;
1984    
1985    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1986    undef $self->{form_element};    undef $self->{form_element};
1987    undef $self->{head_element};    undef $self->{head_element};
1988    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1676  sub _construct_tree ($) { Line 1995  sub _construct_tree ($) {
1995    
1996  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1997    my $self = shift;    my $self = shift;
1998    B: {    INITIAL: {
1999        if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2000          if ($token->{error}) {        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2001            ## ISSUE: Spec currently left this case undefined.        ## error, switch to a conformance checking mode for another
2002            !!!parse-error (type => 'bogus DOCTYPE');        ## language.
2003          }        my $doctype_name = $token->{name};
2004          my $doctype = $self->{document}->create_document_type_definition        $doctype_name = '' unless defined $doctype_name;
2005            ($token->{name});        $doctype_name =~ tr/a-z/A-Z/;
2006          $self->{document}->append_child ($doctype);        if (not defined $token->{name} or # <!DOCTYPE>
2007          #$phase = 'root element';            defined $token->{public_identifier} or
2008          !!!next-token;            defined $token->{system_identifier}) {
2009          #redo B;          !!!parse-error (type => 'not HTML5');
2010          return;        } elsif ($doctype_name ne 'HTML') {
2011        } elsif ({          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2012                  comment => 1,          !!!parse-error (type => 'not HTML5');
2013                  'start tag' => 1,        }
2014                  'end tag' => 1,        
2015                  'end-of-file' => 1,        my $doctype = $self->{document}->create_document_type_definition
2016                 }->{$token->{type}}) {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2017          ## ISSUE: Spec currently left this case undefined.        $doctype->public_id ($token->{public_identifier})
2018          !!!parse-error (type => 'missing DOCTYPE');            if defined $token->{public_identifier};
2019          #$phase = 'root element';        $doctype->system_id ($token->{system_identifier})
2020          ## reprocess            if defined $token->{system_identifier};
2021          #redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
2022          return;        ## ISSUE: internalSubset = null??
2023        } elsif ($token->{type} eq 'character') {        $self->{document}->append_child ($doctype);
2024          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        
2025            $self->{document}->manakai_append_text ($1);        if (not $token->{correct} or $doctype_name ne 'HTML') {
2026            ## ISSUE: DOM3 Core does not allow Document > Text          $self->{document}->manakai_compat_mode ('quirks');
2027            unless (length $token->{data}) {        } elsif (defined $token->{public_identifier}) {
2028              ## Stay in the phase          my $pubid = $token->{public_identifier};
2029              !!!next-token;          $pubid =~ tr/a-z/A-z/;
2030              redo B;          if ({
2031              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
2032              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2033              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2034              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
2035              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
2036              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
2037              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
2038              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
2039              "-//IETF//DTD HTML 2.0//EN" => 1,
2040              "-//IETF//DTD HTML 2.1E//EN" => 1,
2041              "-//IETF//DTD HTML 3.0//EN" => 1,
2042              "-//IETF//DTD HTML 3.0//EN//" => 1,
2043              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
2044              "-//IETF//DTD HTML 3.2//EN" => 1,
2045              "-//IETF//DTD HTML 3//EN" => 1,
2046              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
2047              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
2048              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
2049              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
2050              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
2051              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
2052              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
2053              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
2054              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
2055              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
2056              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
2057              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
2058              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
2059              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
2060              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
2061              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
2062              "-//IETF//DTD HTML STRICT//EN" => 1,
2063              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
2064              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
2065              "-//IETF//DTD HTML//EN" => 1,
2066              "-//IETF//DTD HTML//EN//2.0" => 1,
2067              "-//IETF//DTD HTML//EN//3.0" => 1,
2068              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
2069              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
2070              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
2071              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
2072              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
2073              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
2074              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
2075              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
2076              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2077              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2078              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2079              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2080              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2081              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
2082              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
2083              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
2084              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
2085              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
2086              "-//W3C//DTD HTML 3.2//EN" => 1,
2087              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
2088              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
2089              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
2090              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
2091              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
2092              "-//W3C//DTD W3 HTML//EN" => 1,
2093              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
2094              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
2095              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
2096              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
2097              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
2098              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2099              "HTML" => 1,
2100            }->{$pubid}) {
2101              $self->{document}->manakai_compat_mode ('quirks');
2102            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2103                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2104              if (defined $token->{system_identifier}) {
2105                $self->{document}->manakai_compat_mode ('quirks');
2106              } else {
2107                $self->{document}->manakai_compat_mode ('limited quirks');
2108            }            }
2109            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
2110                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
2111              $self->{document}->manakai_compat_mode ('limited quirks');
2112            }
2113          }
2114          if (defined $token->{system_identifier}) {
2115            my $sysid = $token->{system_identifier};
2116            $sysid =~ tr/A-Z/a-z/;
2117            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2118              $self->{document}->manakai_compat_mode ('quirks');
2119            }
2120          }
2121          
2122          ## Go to the root element phase.
2123          !!!next-token;
2124          return;
2125        } elsif ({
2126                  START_TAG_TOKEN, 1,
2127                  END_TAG_TOKEN, 1,
2128                  END_OF_FILE_TOKEN, 1,
2129                 }->{$token->{type}}) {
2130          !!!parse-error (type => 'no DOCTYPE');
2131          $self->{document}->manakai_compat_mode ('quirks');
2132          ## Go to the root element phase
2133          ## reprocess
2134          return;
2135        } elsif ($token->{type} == CHARACTER_TOKEN) {
2136          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2137            ## Ignore the token
2138    
2139            unless (length $token->{data}) {
2140              ## Stay in the phase
2141              !!!next-token;
2142              redo INITIAL;
2143          }          }
         ## 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";  
2144        }        }
2145      } # B  
2146          !!!parse-error (type => 'no DOCTYPE');
2147          $self->{document}->manakai_compat_mode ('quirks');
2148          ## Go to the root element phase
2149          ## reprocess
2150          return;
2151        } elsif ($token->{type} == COMMENT_TOKEN) {
2152          my $comment = $self->{document}->create_comment ($token->{data});
2153          $self->{document}->append_child ($comment);
2154          
2155          ## Stay in the phase.
2156          !!!next-token;
2157          redo INITIAL;
2158        } else {
2159          die "$0: $token->{type}: Unknown token type";
2160        }
2161      } # INITIAL
2162  } # _tree_construction_initial  } # _tree_construction_initial
2163    
2164  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2165    my $self = shift;    my $self = shift;
2166        
2167    B: {    B: {
2168        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2169          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2170          ## Ignore the token          ## Ignore the token
2171          ## Stay in the phase          ## Stay in the phase
2172          !!!next-token;          !!!next-token;
2173          redo B;          redo B;
2174        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2175          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2176          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2177          ## Stay in the phase          ## Stay in the phase
2178          !!!next-token;          !!!next-token;
2179          redo B;          redo B;
2180        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2181          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2182            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2183            ## ISSUE: DOM3 Core does not allow Document > Text  
2184            unless (length $token->{data}) {            unless (length $token->{data}) {
2185              ## Stay in the phase              ## Stay in the phase
2186              !!!next-token;              !!!next-token;
2187              redo B;              redo B;
2188            }            }
2189          }          }
2190    
2191            $self->{application_cache_selection}->(undef);
2192    
2193            #
2194          } elsif ($token->{type} == START_TAG_TOKEN) {
2195            if ($token->{tag_name} eq 'html' and
2196                $token->{attributes}->{manifest}) {
2197              $self->{application_cache_selection}
2198                   ->($token->{attributes}->{manifest}->{value});
2199              ## ISSUE: No relative reference resolution?
2200            } else {
2201              $self->{application_cache_selection}->(undef);
2202            }
2203    
2204            ## ISSUE: There is an issue in the spec
2205          #          #
2206        } elsif ({        } elsif ({
2207                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2208                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2209                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2210            $self->{application_cache_selection}->(undef);
2211    
2212          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2213          #          #
2214        } else {        } else {
2215          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2216        }        }
2217    
2218        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2219        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2220        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2221        ## reprocess        ## reprocess
2222        #redo B;        #redo B;
2223        return;        return; ## Go to the main phase.
2224    } # B    } # B
2225  } # _tree_construction_root_element  } # _tree_construction_root_element
2226    
# Line 1782  sub _reset_insertion_mode ($) { Line 2236  sub _reset_insertion_mode ($) {
2236            
2237      ## Step 3      ## Step 3
2238      S3: {      S3: {
2239        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2240        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2241          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2242              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2243            #        ## The second "if" is in the scope of the first "if"!?
2244          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2245            $node = $self->{inner_html_node};          $last = 1;
2246            if (defined $self->{inner_html_node}) {
2247              if ($self->{inner_html_node}->[1] eq 'td' or
2248                  $self->{inner_html_node}->[1] eq 'th') {
2249                #
2250              } else {
2251                $node = $self->{inner_html_node};
2252              }
2253          }          }
2254        }        }
2255            
2256        ## Step 4..13        ## Step 4..13
2257        my $new_mode = {        my $new_mode = {
2258                        select => 'in select',                        select => IN_SELECT_IM,
2259                        td => 'in cell',                        td => IN_CELL_IM,
2260                        th => 'in cell',                        th => IN_CELL_IM,
2261                        tr => 'in row',                        tr => IN_ROW_IM,
2262                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2263                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2264                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2265                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2266                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2267                        table => 'in table',                        table => IN_TABLE_IM,
2268                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2269                        body => 'in body',                        body => IN_BODY_IM,
2270                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2271                       }->{$node->[1]};                       }->{$node->[1]};
2272        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2273                
2274        ## Step 14        ## Step 14
2275        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2276          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2277            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2278          } else {          } else {
2279            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2280          }          }
2281          return;          return;
2282        }        }
2283                
2284        ## Step 15        ## Step 15
2285        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2286                
2287        ## Step 16        ## Step 16
2288        $i--;        $i--;
# Line 1835  sub _reset_insertion_mode ($) { Line 2296  sub _reset_insertion_mode ($) {
2296  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2297    my $self = shift;    my $self = shift;
2298    
   my $phase = 'main';  
   
2299    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2300    
2301    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1919  sub _tree_construction_main ($) { Line 2378  sub _tree_construction_main ($) {
2378      }      }
2379    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2380    
2381    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2382      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2383      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2384      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2385       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2386        ->append_child ($style_el);      my $el;
2387      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2388                  
2389        ## Step 2
2390        $insert->($el); # /context node/->append_child ($el)
2391    
2392        ## Step 3
2393        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2394        delete $self->{escape}; # MUST
2395    
2396        ## Step 4
2397      my $text = '';      my $text = '';
2398      !!!next-token;      !!!next-token;
2399      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2400        $text .= $token->{data};        $text .= $token->{data};
2401        !!!next-token;        !!!next-token;
2402      } # stop if non-character token or tokenizer stops tokenising      }
2403    
2404        ## Step 5
2405      if (length $text) {      if (length $text) {
2406        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2407          $el->append_child ($text);
2408      }      }
2409        
2410      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2411                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2412      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2413        ## Step 7
2414        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2415        ## Ignore the token        ## Ignore the token
2416      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2417        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2418        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2419          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2420        } else {
2421          die "$0: $content_model_flag in parse_rcdata";
2422      }      }
2423      !!!next-token;      !!!next-token;
2424    }; # $style_start_tag    }; # $parse_rcdata
2425    
2426    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2427        my $insert = $_[0];
2428      my $script_el;      my $script_el;
2429      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2430      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2431    
2432      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2433        delete $self->{escape}; # MUST
2434            
2435      my $text = '';      my $text = '';
2436      !!!next-token;      !!!next-token;
2437      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2438        $text .= $token->{data};        $text .= $token->{data};
2439        !!!next-token;        !!!next-token;
2440      } # 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 2442  sub _tree_construction_main ($) {
2442        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2443      }      }
2444                                
2445      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2446    
2447      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2448          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2449        ## Ignore the token        ## Ignore the token
2450      } else {      } else {
# Line 1981  sub _tree_construction_main ($) { Line 2458  sub _tree_construction_main ($) {
2458      } else {      } else {
2459        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2460        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2461          
2462        (($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);  
2463                
2464        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2465                
# Line 2177  sub _tree_construction_main ($) { Line 2653  sub _tree_construction_main ($) {
2653    }; # $formatting_end_tag    }; # $formatting_end_tag
2654    
2655    my $insert_to_current = sub {    my $insert_to_current = sub {
2656      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2657    }; # $insert_to_current    }; # $insert_to_current
2658    
2659    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2211  sub _tree_construction_main ($) { Line 2687  sub _tree_construction_main ($) {
2687                         }                         }
2688    }; # $insert_to_foster    }; # $insert_to_foster
2689    
2690    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  
2691    
2692    B: {    B: {
2693      if ($phase eq 'main') {      if ($token->{type} == DOCTYPE_TOKEN) {
2694        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
2695          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
2696          ## Ignore the token        ## Stay in the phase
2697          ## Stay in the phase        !!!next-token;
2698          !!!next-token;        redo B;
2699          redo B;      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2700        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2701                 $token->{tag_name} eq 'html') {          #
2702          ## 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') {  
2703          ## Generate implied end tags          ## Generate implied end tags
2704          if ({          if ({
2705               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,
2706                 tbody => 1, tfoot=> 1, thead => 1,
2707              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2708            !!!back-token;            !!!back-token;
2709            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2710            redo B;            redo B;
2711          }          }
2712                    
# Line 2990  sub _tree_construction_main ($) { Line 2719  sub _tree_construction_main ($) {
2719            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2720          }          }
2721    
         ## Stop parsing  
         last B;  
   
2722          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
2723          }
2724    
2725          ## Stop parsing
2726          last B;
2727        } elsif ($token->{type} == START_TAG_TOKEN and
2728                 $token->{tag_name} eq 'html') {
2729          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2730            ## Turn into the main phase
2731            !!!parse-error (type => 'after html:html');
2732            $self->{insertion_mode} = AFTER_BODY_IM;
2733          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2734            ## Turn into the main phase
2735            !!!parse-error (type => 'after html:html');
2736            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2737          }
2738    
2739    ## ISSUE: "aa<html>" is not a parse error.
2740    ## ISSUE: "<html>" in fragment is not a parse error.
2741          unless ($token->{first_start_tag}) {
2742            !!!parse-error (type => 'not first start tag');
2743          }
2744          my $top_el = $self->{open_elements}->[0]->[0];
2745          for my $attr_name (keys %{$token->{attributes}}) {
2746            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2747              $top_el->set_attribute_ns
2748                (undef, [undef, $attr_name],
2749                 $token->{attributes}->{$attr_name}->{value});
2750            }
2751          }
2752          !!!next-token;
2753          redo B;
2754        } elsif ($token->{type} == COMMENT_TOKEN) {
2755          my $comment = $self->{document}->create_comment ($token->{data});
2756          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2757            $self->{document}->append_child ($comment);
2758          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2759            $self->{open_elements}->[0]->[0]->append_child ($comment);
2760        } else {        } else {
2761          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2762            if ($token->{type} eq 'character') {        }
2763              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        !!!next-token;
2764                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);        redo B;
2765                unless (length $token->{data}) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2766                  !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
2767                  redo B;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2768                }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2769              }            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);  
2770              !!!next-token;              !!!next-token;
2771              redo B;              redo B;
2772            } elsif ($token->{type} eq 'start tag') {            }
2773              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};          }
2774              !!!create-element ($self->{head_element}, 'head', $attr);  
2775              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2776              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            ## As if <head>
2777              $self->{insertion_mode} = 'in head';            !!!create-element ($self->{head_element}, 'head');
2778              if ($token->{tag_name} eq 'head') {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2779                !!!next-token;            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2780              #} elsif ({  
2781              #          base => 1, link => 1, meta => 1,            ## Reprocess in the "in head" insertion mode...
2782              #          script => 1, style => 1, title => 1,            pop @{$self->{open_elements}};
2783              #         }->{$token->{tag_name}}) {  
2784              #  ## reprocess            ## Reprocess in the "after head" insertion mode...
2785              } else {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2786                ## reprocess            ## As if </noscript>
2787              }            pop @{$self->{open_elements}};
2788              !!!parse-error (type => 'in noscript:#character');
2789              
2790              ## Reprocess in the "in head" insertion mode...
2791              ## As if </head>
2792              pop @{$self->{open_elements}};
2793    
2794              ## Reprocess in the "after head" insertion mode...
2795            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2796              pop @{$self->{open_elements}};
2797    
2798              ## Reprocess in the "after head" insertion mode...
2799            }
2800    
2801                ## "after head" insertion mode
2802                ## As if <body>
2803                !!!insert-element ('body');
2804                $self->{insertion_mode} = IN_BODY_IM;
2805                ## reprocess
2806              redo B;              redo B;
2807            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2808              if ($token->{tag_name} eq 'html') {              if ($token->{tag_name} eq 'head') {
2809                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2810                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2811                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2812                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2813                    $self->{insertion_mode} = IN_HEAD_IM;
2814                    !!!next-token;
2815                    redo B;
2816                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2817                    #
2818                  } else {
2819                    !!!parse-error (type => 'in head:head'); # or in head noscript
2820                    ## Ignore the token
2821                    !!!next-token;
2822                    redo B;
2823                  }
2824                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2825                ## As if <head>                ## As if <head>
2826                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2827                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2828                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2829                $self->{insertion_mode} = 'in head';  
2830                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2831                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;  
2832              }              }
2833            } else {  
2834              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
2835            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2836          } elsif ($self->{insertion_mode} eq 'in head') {                  ## As if </noscript>
2837            if ($token->{type} eq 'character') {                  pop @{$self->{open_elements}};
2838              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!parse-error (type => 'in noscript:base');
2839                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                
2840                unless (length $token->{data}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2841                  !!!next-token;                  ## Reprocess in the "in head" insertion mode...
                 redo B;  
2842                }                }
             }  
               
             #  
           } 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';  
2843    
2844                my $text = '';                ## NOTE: There is a "as if in head" code clone.
2845                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2846                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2847                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2848                  }
2849                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2850                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2851                  pop @{$self->{open_elements}}
2852                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2853                !!!next-token;                !!!next-token;
2854                while ($token->{type} eq 'character') {                redo B;
2855                  $text .= $token->{data};              } elsif ($token->{tag_name} eq 'link') {
2856                  !!!next-token;                ## NOTE: There is a "as if in head" code clone.
2857                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2858                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2859                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2860                }                }
2861                if (length $text) {                !!!insert-element ($token->{tag_name}, $token->{attributes});
2862                  $title_el->manakai_append_text ($text);                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2863                  pop @{$self->{open_elements}}
2864                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2865                  !!!next-token;
2866                  redo B;
2867                } elsif ($token->{tag_name} eq 'meta') {
2868                  ## NOTE: There is a "as if in head" code clone.
2869                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2870                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2871                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2872                }                }
2873                                !!!insert-element ($token->{tag_name}, $token->{attributes});
2874                $self->{content_model_flag} = 'PCDATA';                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2875                  
2876                if ($token->{type} eq 'end tag' and                unless ($self->{confident}) {
2877                    $token->{tag_name} eq 'title') {                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2878                  ## Ignore the token                    $self->{change_encoding}
2879                          ->($self, $token->{attributes}->{charset}->{value});
2880                      
2881                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2882                          ->set_user_data (manakai_has_reference =>
2883                                               $token->{attributes}->{charset}
2884                                                   ->{has_reference});
2885                    } elsif ($token->{attributes}->{content}) {
2886                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2887                      if ($token->{attributes}->{content}->{value}
2888                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2889                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2890                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2891                        $self->{change_encoding}
2892                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2893                      }
2894                    }
2895                } else {                } else {
2896                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  if ($token->{attributes}->{charset}) {
2897                  ## ISSUE: And ignore?                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2898                          ->set_user_data (manakai_has_reference =>
2899                                               $token->{attributes}->{charset}
2900                                                   ->{has_reference});
2901                    }
2902                }                }
               !!!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);  
2903    
2904                  pop @{$self->{open_elements}}
2905                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2906                !!!next-token;                !!!next-token;
2907                redo B;                redo B;
2908              } elsif ($token->{tag_name} eq 'head') {              } elsif ($token->{tag_name} eq 'title') {
2909                !!!parse-error (type => 'in head:head');                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2910                ## Ignore the token                  ## As if </noscript>
2911                !!!next-token;                  pop @{$self->{open_elements}};
2912                    !!!parse-error (type => 'in noscript:title');
2913                  
2914                    $self->{insertion_mode} = IN_HEAD_IM;
2915                    ## Reprocess in the "in head" insertion mode...
2916                  } elsif ($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    
2921                  ## NOTE: There is a "as if in head" code clone.
2922                  my $parent = defined $self->{head_element} ? $self->{head_element}
2923                      : $self->{open_elements}->[-1]->[0];
2924                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2925                                  sub { $parent->append_child ($_[0]) });
2926                  pop @{$self->{open_elements}}
2927                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2928                redo B;                redo B;
2929              } else {              } elsif ($token->{tag_name} eq 'style') {
2930                #                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2931              }                ## insertion mode IN_HEAD_IM)
2932            } elsif ($token->{type} eq 'end tag') {                ## NOTE: There is a "as if in head" code clone.
2933              if ($token->{tag_name} eq 'head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2934                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2935                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2936                  }
2937                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2938                  pop @{$self->{open_elements}}
2939                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2940                  redo B;
2941                } elsif ($token->{tag_name} eq 'noscript') {
2942                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2943                    ## NOTE: and scripting is disalbed
2944                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2945                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2946                    !!!next-token;
2947                    redo B;
2948                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2949                    !!!parse-error (type => 'in noscript:noscript');
2950                    ## Ignore the token
2951                    !!!next-token;
2952                    redo B;
2953                  } else {
2954                    #
2955                  }
2956                } elsif ($token->{tag_name} eq 'script') {
2957                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2958                    ## As if </noscript>
2959                    pop @{$self->{open_elements}};
2960                    !!!parse-error (type => 'in noscript:script');
2961                  
2962                    $self->{insertion_mode} = IN_HEAD_IM;
2963                    ## Reprocess in the "in head" insertion mode...
2964                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2965                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2966                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2967                  }
2968    
2969                  ## NOTE: There is a "as if in head" code clone.
2970                  $script_start_tag->($insert_to_current);
2971                  pop @{$self->{open_elements}}
2972                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2973                  redo B;
2974                } elsif ($token->{tag_name} eq 'body' or
2975                         $token->{tag_name} eq 'frameset') {
2976                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2977                    ## As if </noscript>
2978                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2979                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2980                    
2981                    ## Reprocess in the "in head" insertion mode...
2982                    ## As if </head>
2983                    pop @{$self->{open_elements}};
2984                    
2985                    ## Reprocess in the "after head" insertion mode...
2986                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2987                    pop @{$self->{open_elements}};
2988                    
2989                    ## Reprocess in the "after head" insertion mode...
2990                  }
2991    
2992                  ## "after head" insertion mode
2993                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2994                  if ($token->{tag_name} eq 'body') {
2995                    $self->{insertion_mode} = IN_BODY_IM;
2996                  } elsif ($token->{tag_name} eq 'frameset') {
2997                    $self->{insertion_mode} = IN_FRAMESET_IM;
2998                } else {                } else {
2999                  !!!parse-error (type => 'unmatched end tag:head');                  die "$0: tag name: $self->{tag_name}";
3000                }                }
               $self->{insertion_mode} = 'after head';  
3001                !!!next-token;                !!!next-token;
3002                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'html') {  
               #  
3003              } else {              } else {
3004                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                #
               ## Ignore the token  
               !!!next-token;  
               redo B;  
3005              }              }
           } else {  
             #  
           }  
3006    
3007            if ($self->{open_elements}->[-1]->[1] eq 'head') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3008              ## As if </head>                ## As if </noscript>
3009              pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3010            }                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3011            $self->{insertion_mode} = 'after head';                
3012            ## reprocess                ## Reprocess in the "in head" insertion mode...
3013            redo B;                ## As if </head>
3014                  pop @{$self->{open_elements}};
3015    
3016            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
3017          } elsif ($self->{insertion_mode} eq 'after head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3018            if ($token->{type} eq 'character') {                ## As if </head>
3019              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                pop @{$self->{open_elements}};
3020                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
3021                unless (length $token->{data}) {                ## Reprocess in the "after head" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
3022              }              }
               
             #  
           } 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});  
3023    
3024              !!!next-token;              ## "after head" insertion mode
3025              redo B;              ## As if <body>
3026            } elsif ($token->{type} eq 'comment') {              !!!insert-element ('body');
3027              ## NOTE: There is a code clone of "comment in body".              $self->{insertion_mode} = IN_BODY_IM;
3028              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);  
3029              redo B;              redo B;
3030            }            } elsif ($token->{type} == END_TAG_TOKEN) {
3031          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3032            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3033              ## NOTE: There are "character in table" code clones.                  ## As if <head>
3034              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
3035                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3036                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3037                unless (length $token->{data}) {  
3038                    ## Reprocess in the "in head" insertion mode...
3039                    pop @{$self->{open_elements}};
3040                    $self->{insertion_mode} = AFTER_HEAD_IM;
3041                    !!!next-token;
3042                    redo B;
3043                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3044                    ## As if </noscript>
3045                    pop @{$self->{open_elements}};
3046                    !!!parse-error (type => 'in noscript:script');
3047                    
3048                    ## Reprocess in the "in head" insertion mode...
3049                    pop @{$self->{open_elements}};
3050                    $self->{insertion_mode} = AFTER_HEAD_IM;
3051                    !!!next-token;
3052                    redo B;
3053                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3054                    pop @{$self->{open_elements}};
3055                    $self->{insertion_mode} = AFTER_HEAD_IM;
3056                  !!!next-token;                  !!!next-token;
3057                  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});  
3058                } else {                } else {
3059                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
3060                }                }
3061              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3062                $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]);  
3063                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3064                    $self->{insertion_mode} = IN_HEAD_IM;
3065                    !!!next-token;
3066                    redo B;
3067                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3068                    !!!parse-error (type => 'unmatched end tag:noscript');
3069                    ## Ignore the token ## ISSUE: An issue in the spec.
3070                    !!!next-token;
3071                    redo B;
3072                  } else {
3073                    #
3074                }                }
   
               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;  
3075              } elsif ({              } elsif ({
3076                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3077                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3078                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3079                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
3080                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
3081                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3082                  pop @{$self->{open_elements}};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3083                }  
3084                    $self->{insertion_mode} = IN_HEAD_IM;
3085                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
3086                $self->{insertion_mode} = $token->{tag_name} eq 'col'                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3087                  ? 'in column group' : 'in table body';                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3088                ## 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>  
3089                  !!!next-token;                  !!!next-token;
3090                  redo B;                  redo B;
3091                }                }
3092                                
3093                ## generate implied end tags                #
3094                if ({              } elsif ({
3095                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3096                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3097                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3098                  !!!back-token; # <table>                  ## As if <head>
3099                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3100                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3101                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3102    
3103                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3104                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3105                }                }
3106    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3107                #                #
3108              }              } else {
3109            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3110              if ($token->{tag_name} eq 'table') {                  #
3111                ## 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) {  
3112                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3113                  ## Ignore the token                  ## Ignore the token
3114                  !!!next-token;                  !!!next-token;
3115                  redo B;                  redo B;
3116                }                }
3117                              }
               ## 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]);  
               }  
3118    
3119                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3120                  ## As if </noscript>
3121                  pop @{$self->{open_elements}};
3122                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3123                  
3124                  ## Reprocess in the "in head" insertion mode...
3125                  ## As if </head>
3126                  pop @{$self->{open_elements}};
3127    
3128                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3129                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3130                  ## As if </head>
3131                  pop @{$self->{open_elements}};
3132    
3133                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3134                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}}) {  
3135                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3136                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3137                !!!next-token;                !!!next-token;
3138                redo B;                redo B;
             } else {  
               #  
3139              }              }
3140    
3141                ## "after head" insertion mode
3142                ## As if <body>
3143                !!!insert-element ('body');
3144                $self->{insertion_mode} = IN_BODY_IM;
3145                ## reprocess
3146                redo B;
3147            } else {            } else {
3148              #              die "$0: $token->{type}: Unknown token type";
3149            }            }
3150    
3151            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3152            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
3153            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3154          } 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".  
3155              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3156                            
3157              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3158    
3159              !!!next-token;              !!!next-token;
3160              redo B;              redo B;
3161            } 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') {  
3162              if ({              if ({
3163                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3164                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3165                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3166                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3167                    ## have an element in table scope
3168                    my $tn;
3169                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3170                      my $node = $self->{open_elements}->[$_];
3171                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3172                        $tn = $node->[1];
3173                        last INSCOPE;
3174                      } elsif ({
3175                                table => 1, html => 1,
3176                               }->{$node->[1]}) {
3177                        last INSCOPE;
3178                      }
3179                    } # INSCOPE
3180                      unless (defined $tn) {
3181                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3182                        ## Ignore the token
3183                        !!!next-token;
3184                        redo B;
3185                      }
3186                    
3187                    ## Close the cell
3188                    !!!back-token; # <?>
3189                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3190                    redo B;
3191                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3192                    !!!parse-error (type => 'not closed:caption');
3193                    
3194                    ## As if </caption>
3195                    ## have a table element in table scope
3196                    my $i;
3197                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3198                      my $node = $self->{open_elements}->[$_];
3199                      if ($node->[1] eq 'caption') {
3200                        $i = $_;
3201                        last INSCOPE;
3202                      } elsif ({
3203                                table => 1, html => 1,
3204                               }->{$node->[1]}) {
3205                        last INSCOPE;
3206                      }
3207                    } # INSCOPE
3208                      unless (defined $i) {
3209                        !!!parse-error (type => 'unmatched end tag:caption');
3210                        ## Ignore the token
3211                        !!!next-token;
3212                        redo B;
3213                      }
3214                    
3215                    ## generate implied end tags
3216                    if ({
3217                         dd => 1, dt => 1, li => 1, p => 1,
3218                         td => 1, th => 1, tr => 1,
3219                         tbody => 1, tfoot=> 1, thead => 1,
3220                        }->{$self->{open_elements}->[-1]->[1]}) {
3221                      !!!back-token; # <?>
3222                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3223                      !!!back-token;
3224                      $token = {type => END_TAG_TOKEN,
3225                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3226                      redo B;
3227                    }
3228    
3229                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3230                ## 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;  
3231                  }                  }
3232                } # INSCOPE                  
3233                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3234                  !!!parse-error (type => 'unmatched end tag:caption');                  
3235                    $clear_up_to_marker->();
3236                    
3237                    $self->{insertion_mode} = IN_TABLE_IM;
3238                    
3239                    ## reprocess
3240                    redo B;
3241                  } else {
3242                    #
3243                  }
3244                } else {
3245                  #
3246                }
3247              } elsif ($token->{type} == END_TAG_TOKEN) {
3248                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3249                  if ($self->{insertion_mode} == IN_CELL_IM) {
3250                    ## have an element in table scope
3251                    my $i;
3252                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3253                      my $node = $self->{open_elements}->[$_];
3254                      if ($node->[1] eq $token->{tag_name}) {
3255                        $i = $_;
3256                        last INSCOPE;
3257                      } elsif ({
3258                                table => 1, html => 1,
3259                               }->{$node->[1]}) {
3260                        last INSCOPE;
3261                      }
3262                    } # INSCOPE
3263                      unless (defined $i) {
3264                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3265                        ## Ignore the token
3266                        !!!next-token;
3267                        redo B;
3268                      }
3269                    
3270                    ## generate implied end tags
3271                    if ({
3272                         dd => 1, dt => 1, li => 1, p => 1,
3273                         td => ($token->{tag_name} eq 'th'),
3274                         th => ($token->{tag_name} eq 'td'),
3275                         tr => 1,
3276                         tbody => 1, tfoot=> 1, thead => 1,
3277                        }->{$self->{open_elements}->[-1]->[1]}) {
3278                      !!!back-token;
3279                      $token = {type => END_TAG_TOKEN,
3280                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3281                      redo B;
3282                    }
3283                    
3284                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3285                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3286                    }
3287                    
3288                    splice @{$self->{open_elements}}, $i;
3289                    
3290                    $clear_up_to_marker->();
3291                    
3292                    $self->{insertion_mode} = IN_ROW_IM;
3293                    
3294                    !!!next-token;
3295                    redo B;
3296                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3297                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3298                  ## Ignore the token                  ## Ignore the token
3299                  !!!next-token;                  !!!next-token;
3300                  redo B;                  redo B;
3301                  } else {
3302                    #
3303                }                }
3304                              } elsif ($token->{tag_name} eq 'caption') {
3305                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3306                if ({                  ## have a table element in table scope
3307                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3308                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3309                    }->{$self->{open_elements}->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3310                  !!!back-token; # <?>                    if ($node->[1] eq $token->{tag_name}) {
3311                  $token = {type => 'end tag', tag_name => 'caption'};                      $i = $_;
3312                  !!!back-token;                      last INSCOPE;
3313                  $token = {type => 'end tag',                    } elsif ({
3314                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              table => 1, html => 1,
3315                               }->{$node->[1]}) {
3316                        last INSCOPE;
3317                      }
3318                    } # INSCOPE
3319                      unless (defined $i) {
3320                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3321                        ## Ignore the token
3322                        !!!next-token;
3323                        redo B;
3324                      }
3325                    
3326                    ## generate implied end tags
3327                    if ({
3328                         dd => 1, dt => 1, li => 1, p => 1,
3329                         td => 1, th => 1, tr => 1,
3330                         tbody => 1, tfoot=> 1, thead => 1,
3331                        }->{$self->{open_elements}->[-1]->[1]}) {
3332                      !!!back-token;
3333                      $token = {type => END_TAG_TOKEN,
3334                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3335                      redo B;
3336                    }
3337                    
3338                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3339                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3340                    }
3341                    
3342                    splice @{$self->{open_elements}}, $i;
3343                    
3344                    $clear_up_to_marker->();
3345                    
3346                    $self->{insertion_mode} = IN_TABLE_IM;
3347                    
3348                    !!!next-token;
3349                  redo B;                  redo B;
3350                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3351                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3352                    ## Ignore the token
3353                    !!!next-token;
3354                    redo B;
3355                  } else {
3356                    #
3357                }                }
3358                } elsif ({
3359                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3360                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3361                }                       }->{$token->{tag_name}} and
3362                         $self->{insertion_mode} == IN_CELL_IM) {
3363                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  
3364                my $i;                my $i;
3365                  my $tn;
3366                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3367                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3368                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3369                    $i = $_;                    $i = $_;
3370                    last INSCOPE;                    last INSCOPE;
3371                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3372                      $tn = $node->[1];
3373                      ## NOTE: There is exactly one |td| or |th| element
3374                      ## in scope in the stack of open elements by definition.
3375                  } elsif ({                  } elsif ({
3376                            table => 1, html => 1,                            table => 1, html => 1,
3377                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3528  sub _tree_construction_main ($) { Line 3384  sub _tree_construction_main ($) {
3384                  !!!next-token;                  !!!next-token;
3385                  redo B;                  redo B;
3386                }                }
                 
               ## 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]);  
               }  
3387    
3388                splice @{$self->{open_elements}}, $i;                ## Close the cell
3389                  !!!back-token; # </?>
3390                $clear_up_to_marker->();                $token = {type => END_TAG_TOKEN, tag_name => $tn};
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
3391                redo B;                redo B;
3392              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3393                         $self->{insertion_mode} == IN_CAPTION_IM) {
3394                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3395    
3396                ## As if </caption>                ## As if </caption>
# Line 3580  sub _tree_construction_main ($) { Line 3418  sub _tree_construction_main ($) {
3418                if ({                if ({
3419                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3420                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3421                       tbody => 1, tfoot=> 1, thead => 1,
3422                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3423                  !!!back-token; # </table>                  !!!back-token; # </table>
3424                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3425                  !!!back-token;                  !!!back-token;
3426                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3427                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3428                  redo B;                  redo B;
3429                }                }
# Line 3597  sub _tree_construction_main ($) { Line 3436  sub _tree_construction_main ($) {
3436    
3437                $clear_up_to_marker->();                $clear_up_to_marker->();
3438    
3439                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3440    
3441                ## reprocess                ## reprocess
3442                redo B;                redo B;
3443              } elsif ({              } elsif ({
3444                        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,  
3445                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3446                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3447                ## 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');  
3448                  ## Ignore the token                  ## Ignore the token
3449                  !!!next-token;                  !!!next-token;
3450                  redo B;                  redo B;
3451                } else {                } else {
3452                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3453                }                }
3454              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3455                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3456                          thead => 1, tr => 1,
3457                         }->{$token->{tag_name}} and
3458                         $self->{insertion_mode} == IN_CAPTION_IM) {
3459                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3460                ## Ignore the token                ## Ignore the token
3461                !!!next-token;                !!!next-token;
3462                redo B;                redo B;
3463              } else {              } else {
3464                #                #
3465              }              }
3466            } else {        } else {
3467              #          die "$0: $token->{type}: Unknown token type";
3468            }        }
3469    
3470            ## As if </colgroup>        $insert = $insert_to_current;
3471            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3472              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3473              ## 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.  
3474              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3475                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3476                                
# Line 3699  sub _tree_construction_main ($) { Line 3487  sub _tree_construction_main ($) {
3487              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3488              ## result in a new Text node.              ## result in a new Text node.
3489              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3490                
3491              if ({              if ({
3492                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3493                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3739  sub _tree_construction_main ($) { Line 3527  sub _tree_construction_main ($) {
3527                            
3528              !!!next-token;              !!!next-token;
3529              redo B;              redo B;
3530            } 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') {  
3531              if ({              if ({
3532                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3533                   th => 1, td => 1,                   th => 1, td => 1,
3534                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3535                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3536                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3537                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3538                           $self->{open_elements}->[-1]->[1] ne 'html') {
3539                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3540                      pop @{$self->{open_elements}};
3541                    }
3542                    
3543                    !!!insert-element ('tbody');
3544                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3545                    ## reprocess in the "in table body" insertion mode...
3546                }                }
3547    
3548                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3549                    unless ($token->{tag_name} eq 'tr') {
3550                      !!!parse-error (type => 'missing start tag:tr');
3551                    }
3552                    
3553                    ## Clear back to table body context
3554                    while (not {
3555                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3556                    }->{$self->{open_elements}->[-1]->[1]}) {
3557                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3558                      pop @{$self->{open_elements}};
3559                    }
3560                    
3561                    $self->{insertion_mode} = IN_ROW_IM;
3562                    if ($token->{tag_name} eq 'tr') {
3563                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3564                      !!!next-token;
3565                      redo B;
3566                    } else {
3567                      !!!insert-element ('tr');
3568                      ## reprocess in the "in row" insertion mode
3569                    }
3570                  }
3571    
3572                  ## Clear back to table row context
3573                while (not {                while (not {
3574                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3575                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3576                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3577                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3578                }                }
3579                                
3580                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3581                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
3582                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3583                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3584                } else {                
3585                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3586                redo B;                redo B;
3587              } elsif ({              } elsif ({
3588                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3589                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3590                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3591                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3592                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3593                my $i;                  ## As if </tr>
3594                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3595                  my $node = $self->{open_elements}->[$_];                  my $i;
3596                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3597                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3598                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3599                    $i = $_;                      $i = $_;
3600                    last INSCOPE;                      last INSCOPE;
3601                  } elsif ({                    } elsif ({
3602                            table => 1, html => 1,                              table => 1, html => 1,
3603                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3604                    last INSCOPE;                      last INSCOPE;
3605                      }
3606                    } # INSCOPE
3607                    unless (defined $i) {
3608                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3609                      ## Ignore the token
3610                      !!!next-token;
3611                      redo B;
3612                    }
3613                    
3614                    ## Clear back to table row context
3615                    while (not {
3616                      tr => 1, html => 1,
3617                    }->{$self->{open_elements}->[-1]->[1]}) {
3618                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3619                      pop @{$self->{open_elements}};
3620                    }
3621                    
3622                    pop @{$self->{open_elements}}; # tr
3623                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3624                    if ($token->{tag_name} eq 'tr') {
3625                      ## reprocess
3626                      redo B;
3627                    } else {
3628                      ## reprocess in the "in table body" insertion mode...
3629                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3630                }                }
3631    
3632                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3633                while (not {                  ## have an element in table scope
3634                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3635                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3636                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3637                      if ({
3638                           tbody => 1, thead => 1, tfoot => 1,
3639                          }->{$node->[1]}) {
3640                        $i = $_;
3641                        last INSCOPE;
3642                      } elsif ({
3643                                table => 1, html => 1,
3644                               }->{$node->[1]}) {
3645                        last INSCOPE;
3646                      }
3647                    } # INSCOPE
3648                    unless (defined $i) {
3649                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3650                      ## Ignore the token
3651                      !!!next-token;
3652                      redo B;
3653                    }
3654    
3655                    ## Clear back to table body context
3656                    while (not {
3657                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3658                    }->{$self->{open_elements}->[-1]->[1]}) {
3659                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3660                      pop @{$self->{open_elements}};
3661                    }
3662                    
3663                    ## As if <{current node}>
3664                    ## have an element in table scope
3665                    ## true by definition
3666                    
3667                    ## Clear back to table body context
3668                    ## nop by definition
3669                    
3670                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3671                    $self->{insertion_mode} = IN_TABLE_IM;
3672                    ## reprocess in "in table" insertion mode...
3673                }                }
3674    
3675                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3676                ## have an element in table scope                  ## Clear back to table context
3677                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3678                           $self->{open_elements}->[-1]->[1] ne 'html') {
3679                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3680                ## nop by definition                    pop @{$self->{open_elements}};
3681                    }
3682                pop @{$self->{open_elements}};                  
3683                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3684                ## reprocess                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3685                redo B;                  ## reprocess
3686                    redo B;
3687                  } elsif ({
3688                            caption => 1,
3689                            colgroup => 1,
3690                            tbody => 1, tfoot => 1, thead => 1,
3691                           }->{$token->{tag_name}}) {
3692                    ## Clear back to table context
3693                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3694                           $self->{open_elements}->[-1]->[1] ne 'html') {
3695                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3696                      pop @{$self->{open_elements}};
3697                    }
3698                    
3699                    push @$active_formatting_elements, ['#marker', '']
3700                        if $token->{tag_name} eq 'caption';
3701                    
3702                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3703                    $self->{insertion_mode} = {
3704                                               caption => IN_CAPTION_IM,
3705                                               colgroup => IN_COLUMN_GROUP_IM,
3706                                               tbody => IN_TABLE_BODY_IM,
3707                                               tfoot => IN_TABLE_BODY_IM,
3708                                               thead => IN_TABLE_BODY_IM,
3709                                              }->{$token->{tag_name}};
3710                    !!!next-token;
3711                    redo B;
3712                  } else {
3713                    die "$0: in table: <>: $token->{tag_name}";
3714                  }
3715              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3716                ## 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');  
3717    
3718                ## As if </table>                ## As if </table>
3719                ## have a table element in table scope                ## have a table element in table scope
# Line 3845  sub _tree_construction_main ($) { Line 3740  sub _tree_construction_main ($) {
3740                if ({                if ({
3741                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3742                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3743                       tbody => 1, tfoot=> 1, thead => 1,
3744                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3745                  !!!back-token; # <table>                  !!!back-token; # <table>
3746                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3747                  !!!back-token;                  !!!back-token;
3748                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3749                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3750                  redo B;                  redo B;
3751                }                }
# Line 3860  sub _tree_construction_main ($) { Line 3756  sub _tree_construction_main ($) {
3756    
3757                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3758    
3759                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3760    
3761                ## reprocess                ## reprocess
3762                redo B;                redo B;
3763              } else {          } else {
3764                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3765              }  
3766            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3767              if ({            #
3768                   tbody => 1, tfoot => 1, thead => 1,          }
3769                  }->{$token->{tag_name}}) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3770                if ($token->{tag_name} eq 'tr' and
3771                    $self->{insertion_mode} == IN_ROW_IM) {
3772                ## have an element in table scope                ## have an element in table scope
3773                my $i;                my $i;
3774                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3891  sub _tree_construction_main ($) { Line 3789  sub _tree_construction_main ($) {
3789                  redo B;                  redo B;
3790                }                }
3791    
3792                ## Clear back to table body context                ## Clear back to table row context
3793                while (not {                while (not {
3794                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3795                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3796                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3797                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3798                }                }
3799    
3800                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3801                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3802                !!!next-token;                !!!next-token;
3803                redo B;                redo B;
3804              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3805                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3806                my $i;                  ## As if </tr>
3807                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3808                  my $node = $self->{open_elements}->[$_];                  my $i;
3809                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3810                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3811                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3812                    $i = $_;                      $i = $_;
3813                    last INSCOPE;                      last INSCOPE;
3814                  } elsif ({                    } elsif ({
3815                            table => 1, html => 1,                              table => 1, html => 1,
3816                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3817                    last INSCOPE;                      last INSCOPE;
3818                      }
3819                    } # INSCOPE
3820                    unless (defined $i) {
3821                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3822                      ## Ignore the token
3823                      !!!next-token;
3824                      redo B;
3825                  }                  }
3826                } # INSCOPE                  
3827                unless (defined $i) {                  ## Clear back to table row context
3828                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3829                  ## 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,  
3830                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3831                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3832                my $foster_parent_element;                    pop @{$self->{open_elements}};
3833                my $next_sibling;                  }
3834                my $prev_sibling;                  
3835                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3836                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3837                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3838                    if (defined $parent and $parent->node_type == 1) {                }
3839                      $foster_parent_element = $parent;  
3840                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3841                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3842                    } else {                  my $i;
3843                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3844                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
3845                      if ({
3846                           tbody => 1, thead => 1, tfoot => 1,
3847                          }->{$node->[1]}) {
3848                        $i = $_;
3849                        last INSCOPE;
3850                      } elsif ({
3851                                table => 1, html => 1,
3852                               }->{$node->[1]}) {
3853                        last INSCOPE;
3854                    }                    }
3855                    last OE;                  } # INSCOPE
3856                    unless (defined $i) {
3857                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3858                      ## Ignore the token
3859                      !!!next-token;
3860                      redo B;
3861                  }                  }
3862                } # OE                  
3863                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3864                $prev_sibling = $foster_parent_element->last_child                  while (not {
3865                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3866                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3867                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3868                  $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;  
3869                  }                  }
3870                } # INSCOPE                  
3871                unless (defined $i) {                  ## As if <{current node}>
3872                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3873                  ## Ignore the token                  ## true by definition
3874                  !!!next-token;                  
3875                  redo B;                  ## Clear back to table body context
3876                }                  ## nop by definition
3877                    
               ## 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]);  
3878                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3879                    $self->{insertion_mode} = IN_TABLE_IM;
3880                    ## reprocess in the "in table" insertion mode...
3881                }                }
3882    
               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>  
3883                ## have a table element in table scope                ## have a table element in table scope
3884                my $i;                my $i;
3885                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3886                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3887                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3888                    $i = $_;                    $i = $_;
3889                    last INSCOPE;                    last INSCOPE;
3890                  } elsif ({                  } elsif ({
# Line 4103  sub _tree_construction_main ($) { Line 3894  sub _tree_construction_main ($) {
3894                  }                  }
3895                } # INSCOPE                } # INSCOPE
3896                unless (defined $i) {                unless (defined $i) {
3897                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3898                  ## Ignore tokens </table><table>                  ## Ignore the token
3899                  !!!next-token;                  !!!next-token;
3900                  redo B;                  redo B;
3901                }                }
3902                  
3903                ## generate implied end tags                ## generate implied end tags
3904                if ({                if ({
3905                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3906                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3907                       tbody => 1, tfoot=> 1, thead => 1,
3908                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3909                  !!!back-token;                  !!!back-token;
3910                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3911                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3912                  redo B;                  redo B;
3913                }                }
3914                  
3915                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3916                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3917                }                }
3918                    
3919                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3920                  
3921                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3922                  
               ## 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';  
3923                !!!next-token;                !!!next-token;
3924                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;  
3925              } elsif ({              } elsif ({
3926                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3927                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3928                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
3929                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
3930                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3931                  my $node = $self->{open_elements}->[$_];                  my $i;
3932                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3933                    $i = $_;                    my $node = $self->{open_elements}->[$_];
3934                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
3935                  } elsif ({                      $i = $_;
3936                            table => 1, html => 1,                      last INSCOPE;
3937                           }->{$node->[1]}) {                    } elsif ({
3938                    last INSCOPE;                              table => 1, html => 1,
3939                               }->{$node->[1]}) {
3940                        last INSCOPE;
3941                      }
3942                    } # INSCOPE
3943                      unless (defined $i) {
3944                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3945                        ## Ignore the token
3946                        !!!next-token;
3947                        redo B;
3948                      }
3949                    
3950                    ## As if </tr>
3951                    ## have an element in table scope
3952                    my $i;
3953                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3954                      my $node = $self->{open_elements}->[$_];
3955                      if ($node->[1] eq 'tr') {
3956                        $i = $_;
3957                        last INSCOPE;
3958                      } elsif ({
3959                                table => 1, html => 1,
3960                               }->{$node->[1]}) {
3961                        last INSCOPE;
3962                      }
3963                    } # INSCOPE
3964                      unless (defined $i) {
3965                        !!!parse-error (type => 'unmatched end tag:tr');
3966                        ## Ignore the token
3967                        !!!next-token;
3968                        redo B;
3969                      }
3970                    
3971                    ## Clear back to table row context
3972                    while (not {
3973                      tr => 1, html => 1,
3974                    }->{$self->{open_elements}->[-1]->[1]}) {
3975                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3976                      pop @{$self->{open_elements}};
3977                  }                  }
3978                } # INSCOPE                  
3979                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3980                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3981                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3982                }                }
3983    
               ## As if </tr>  
3984                ## have an element in table scope                ## have an element in table scope
3985                my $i;                my $i;
3986                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3987                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3988                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3989                    $i = $_;                    $i = $_;
3990                    last INSCOPE;                    last INSCOPE;
3991                  } elsif ({                  } elsif ({
# Line 4241  sub _tree_construction_main ($) { Line 3995  sub _tree_construction_main ($) {
3995                  }                  }
3996                } # INSCOPE                } # INSCOPE
3997                unless (defined $i) {                unless (defined $i) {
3998                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3999                  ## Ignore the token                  ## Ignore the token
4000                  !!!next-token;                  !!!next-token;
4001                  redo B;                  redo B;
4002                }                }
4003    
4004                ## Clear back to table row context                ## Clear back to table body context
4005                while (not {                while (not {
4006                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4007                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4008                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4009                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4010                }                }
4011    
4012                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4013                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4014                ## reprocess                !!!next-token;
4015                redo B;                redo B;
4016              } elsif ({              } elsif ({
4017                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4018                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4019                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4020                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4021                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4022                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4023                ## Ignore the token                ## Ignore the token
4024                !!!next-token;                !!!next-token;
4025                redo B;                redo B;
4026              } else {          } else {
4027                #            !!!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});  
4028    
4029              !!!next-token;            $insert = $insert_to_foster;
4030              redo B;            #
4031            } elsif ($token->{type} eq 'comment') {          }
4032              ## NOTE: This is a code clone of "comment in body".        } else {
4033              my $comment = $self->{document}->create_comment ($token->{data});          die "$0: $token->{type}: Unknown token type";
4034              $self->{open_elements}->[-1]->[0]->append_child ($comment);        }
4035              !!!next-token;      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4036              redo B;            if ($token->{type} == CHARACTER_TOKEN) {
4037            } elsif ($token->{type} eq 'start tag') {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4038              if ({                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4039                   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  
4040                  !!!next-token;                  !!!next-token;
4041                  redo B;                  redo B;
4042                }                }
4043                }
4044                ## Close the cell              
4045                !!!back-token; # <?>              #
4046                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
4047                if ($token->{tag_name} eq 'col') {
4048                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4049                  pop @{$self->{open_elements}};
4050                  !!!next-token;
4051                redo B;                redo B;
4052              } else {              } else {
4053                #                #
4054              }              }
4055            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4056              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4057                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4058                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});  
4059                  ## Ignore the token                  ## Ignore the token
4060                  !!!next-token;                  !!!next-token;
4061                  redo B;                  redo B;
4062                  } else {
4063                    pop @{$self->{open_elements}}; # colgroup
4064                    $self->{insertion_mode} = IN_TABLE_IM;
4065                    !!!next-token;
4066                    redo B;            
4067                }                }
4068                              } elsif ($token->{tag_name} eq 'col') {
4069                ## 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});  
4070                ## Ignore the token                ## Ignore the token
4071                !!!next-token;                !!!next-token;
4072                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;  
4073              } else {              } else {
4074                #                #
4075              }              }
4076            } else {            } else {
4077              #              #
4078            }            }
4079              
4080            $in_body->($insert_to_current);            ## As if </colgroup>
4081            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4082          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
4083            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4084              !!!next-token;              !!!next-token;
4085              redo B;              redo B;
4086            } elsif ($token->{type} eq 'comment') {            } else {
4087              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
4088              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = IN_TABLE_IM;
4089              !!!next-token;              ## reprocess
4090              redo B;              redo B;
4091            } elsif ($token->{type} eq 'start tag') {            }
4092        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4093          if ($token->{type} == CHARACTER_TOKEN) {
4094            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4095            !!!next-token;
4096            redo B;
4097          } elsif ($token->{type} == START_TAG_TOKEN) {
4098              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4099                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4100                  ## As if </option>                  ## As if </option>
# Line 4486  sub _tree_construction_main ($) { Line 4147  sub _tree_construction_main ($) {
4147    
4148                !!!next-token;                !!!next-token;
4149                redo B;                redo B;
4150              } else {          } else {
4151                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4152              }            ## Ignore the token
4153            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4154              redo B;
4155            }
4156          } elsif ($token->{type} == END_TAG_TOKEN) {
4157              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4158                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4159                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4591  sub _tree_construction_main ($) { Line 4255  sub _tree_construction_main ($) {
4255    
4256                ## reprocess                ## reprocess
4257                redo B;                redo B;
4258              } else {          } else {
4259                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4260            ## Ignore the token            ## Ignore the token
4261            !!!next-token;            !!!next-token;
4262            redo B;            redo B;
4263          } elsif ($self->{insertion_mode} eq 'after body') {          }
4264            if ($token->{type} eq 'character') {        } else {
4265              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4266                ## As if in body        }
4267                $reconstruct_active_formatting_elements->($insert_to_current);      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4268          if ($token->{type} == CHARACTER_TOKEN) {
4269            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4270              my $data = $1;
4271              ## As if in body
4272              $reconstruct_active_formatting_elements->($insert_to_current);
4273                                
4274                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4275              
4276              unless (length $token->{data}) {
4277                !!!next-token;
4278                redo B;
4279              }
4280            }
4281            
4282            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4283              !!!parse-error (type => 'after html:#character');
4284    
4285                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4286                  !!!next-token;          }
4287                  redo B;          
4288                }          ## "after body" insertion mode
4289              }          !!!parse-error (type => 'after body:#character');
4290                
4291              #          $self->{insertion_mode} = IN_BODY_IM;
4292              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4293            } elsif ($token->{type} eq 'comment') {          redo B;
4294              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} == START_TAG_TOKEN) {
4295              $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4296              !!!parse-error (type => 'after html:'.$token->{tag_name});
4297              
4298              ## Reprocess in the "main" phase, "after body" insertion mode...
4299            }
4300    
4301            ## "after body" insertion mode
4302            !!!parse-error (type => 'after body:'.$token->{tag_name});
4303    
4304            $self->{insertion_mode} = IN_BODY_IM;
4305            ## reprocess
4306            redo B;
4307          } elsif ($token->{type} == END_TAG_TOKEN) {
4308            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4309              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4310              
4311              $self->{insertion_mode} = AFTER_BODY_IM;
4312              ## Reprocess in the "main" phase, "after body" insertion mode...
4313            }
4314    
4315            ## "after body" insertion mode
4316            if ($token->{tag_name} eq 'html') {
4317              if (defined $self->{inner_html_node}) {
4318                !!!parse-error (type => 'unmatched end tag:html');
4319                ## Ignore the token
4320              !!!next-token;              !!!next-token;
4321              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});  
             }  
4322            } else {            } else {
4323              !!!parse-error (type => 'after body:#'.$token->{type});              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4324                !!!next-token;
4325                redo B;
4326            }            }
4327            } else {
4328              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4329    
4330            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4331            ## reprocess            ## reprocess
4332            redo B;            redo B;
4333          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4334            if ($token->{type} eq 'character') {        } else {
4335              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4336                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4337        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4338                unless (length $token->{data}) {        if ($token->{type} == CHARACTER_TOKEN) {
4339                  !!!next-token;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4340                  redo B;            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4341                }            
4342              }            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);  
4343              !!!next-token;              !!!next-token;
4344              redo B;              redo B;
4345            } elsif ($token->{type} eq 'start tag') {            }
4346              if ($token->{tag_name} eq 'frameset') {          }
4347                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4348                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4349                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4350              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4351                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4352                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
4353                !!!next-token;            } else { # "after html frameset"
4354                redo B;              !!!parse-error (type => 'after html:#character');
4355              } elsif ($token->{tag_name} eq 'noframes') {  
4356                $in_body->($insert_to_current);              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4357                redo B;              ## Reprocess in the "main" phase, "after frameset"...
4358              } 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 {  
             #  
4359            }            }
4360                        
4361            if (defined $token->{tag_name}) {            ## Ignore the token.
4362              if (length $token->{data}) {
4363                ## reprocess the rest of characters
4364              } else {
4365                !!!next-token;
4366              }
4367              redo B;
4368            }
4369            
4370            die qq[$0: Character "$token->{data}"];
4371          } elsif ($token->{type} == START_TAG_TOKEN) {
4372            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4373              !!!parse-error (type => 'after html:'.$token->{tag_name});
4374    
4375              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4376              ## Process in the "main" phase, "after frameset" insertion mode...
4377            }
4378    
4379            if ($token->{tag_name} eq 'frameset' and
4380                $self->{insertion_mode} == IN_FRAMESET_IM) {
4381              !!!insert-element ($token->{tag_name}, $token->{attributes});
4382              !!!next-token;
4383              redo B;
4384            } elsif ($token->{tag_name} eq 'frame' and
4385                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4386              !!!insert-element ($token->{tag_name}, $token->{attributes});
4387              pop @{$self->{open_elements}};
4388              !!!next-token;
4389              redo B;
4390            } elsif ($token->{tag_name} eq 'noframes') {
4391              ## NOTE: As if in body.
4392              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4393              redo B;
4394            } else {
4395              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4396              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4397            } else {            } else {
4398              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4399            }            }
4400            ## Ignore the token            ## Ignore the token
4401            !!!next-token;            !!!next-token;
4402            redo B;            redo B;
4403          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4404            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4405              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4406                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4407    
4408                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4409                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
4410                  redo B;          }
               }  
             }  
4411    
4412              #          if ($token->{tag_name} eq 'frameset' and
4413            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4414              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4415              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4416                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4417                ## Ignore the token
4418              !!!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 {  
               #  
             }  
4419            } else {            } else {
4420              #              pop @{$self->{open_elements}};
4421                !!!next-token;
4422            }            }
4423              
4424            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
4425              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4426                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4427              }
4428              redo B;
4429            } elsif ($token->{tag_name} eq 'html' and
4430                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4431              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4432              !!!next-token;
4433              redo B;
4434            } else {
4435              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4436                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4437            } else {            } else {
4438              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4439            }            }
4440            ## Ignore the token            ## Ignore the token
4441            !!!next-token;            !!!next-token;
4442            redo B;            redo B;
4443            }
4444          } else {
4445            die "$0: $token->{type}: Unknown token type";
4446          }
4447    
4448          ## ISSUE: An issue in spec here
4449        } else {
4450          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4451        }
4452    
4453            ## ISSUE: An issue in spec there      ## "in body" insertion mode
4454        if ($token->{type} == START_TAG_TOKEN) {
4455          if ($token->{tag_name} eq 'script') {
4456            ## NOTE: This is an "as if in head" code clone
4457            $script_start_tag->($insert);
4458            redo B;
4459          } elsif ($token->{tag_name} eq 'style') {
4460            ## NOTE: This is an "as if in head" code clone
4461            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4462            redo B;
4463          } elsif ({
4464                    base => 1, link => 1,
4465                   }->{$token->{tag_name}}) {
4466            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4467            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4468            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4469            !!!next-token;
4470            redo B;
4471          } elsif ($token->{tag_name} eq 'meta') {
4472            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4473            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4474            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4475    
4476            unless ($self->{confident}) {
4477              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4478                $self->{change_encoding}
4479                    ->($self, $token->{attributes}->{charset}->{value});
4480                
4481                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4482                    ->set_user_data (manakai_has_reference =>
4483                                         $token->{attributes}->{charset}
4484                                             ->{has_reference});
4485              } elsif ($token->{attributes}->{content}) {
4486                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4487                if ($token->{attributes}->{content}->{value}
4488                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4489                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4490                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4491                  $self->{change_encoding}
4492                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4493                }
4494              }
4495          } else {          } else {
4496            die "$0: $self->{insertion_mode}: Unknown insertion mode";            if ($token->{attributes}->{charset}) {
4497                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4498                    ->set_user_data (manakai_has_reference =>
4499                                         $token->{attributes}->{charset}
4500                                             ->{has_reference});
4501              }
4502          }          }
4503        }  
     } 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  
4504          !!!next-token;          !!!next-token;
4505          redo B;          redo B;
4506        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'title') {
4507          my $comment = $self->{document}->create_comment ($token->{data});          !!!parse-error (type => 'in body:title');
4508          $self->{document}->append_child ($comment);          ## NOTE: This is an "as if in head" code clone
4509            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4510              if (defined $self->{head_element}) {
4511                $self->{head_element}->append_child ($_[0]);
4512              } else {
4513                $insert->($_[0]);
4514              }
4515            });
4516            redo B;
4517          } elsif ($token->{tag_name} eq 'body') {
4518            !!!parse-error (type => 'in body:body');
4519                  
4520            if (@{$self->{open_elements}} == 1 or
4521                $self->{open_elements}->[1]->[1] ne 'body') {
4522              ## Ignore the token
4523            } else {
4524              my $body_el = $self->{open_elements}->[1]->[0];
4525              for my $attr_name (keys %{$token->{attributes}}) {
4526                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4527                  $body_el->set_attribute_ns
4528                    (undef, [undef, $attr_name],
4529                     $token->{attributes}->{$attr_name}->{value});
4530                }
4531              }
4532            }
4533          !!!next-token;          !!!next-token;
4534          redo B;          redo B;
4535        } elsif ($token->{type} eq 'character') {        } elsif ({
4536          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  address => 1, blockquote => 1, center => 1, dir => 1,
4537            my $data = $1;                  div => 1, dl => 1, fieldset => 1, listing => 1,
4538            ## As if in the main phase.                  menu => 1, ol => 1, p => 1, ul => 1,
4539            ## NOTE: The insertion mode in the main phase                  pre => 1,
4540            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4541            ## end phase is either "after body" or "after frameset".          ## has a p element in scope
4542            $reconstruct_active_formatting_elements->($insert_to_current)          INSCOPE: for (reverse @{$self->{open_elements}}) {
4543              if $phase eq 'main';            if ($_->[1] eq 'p') {
4544                !!!back-token;
4545                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4546                redo B;
4547              } elsif ({
4548                        table => 1, caption => 1, td => 1, th => 1,
4549                        button => 1, marquee => 1, object => 1, html => 1,
4550                       }->{$_->[1]}) {
4551                last INSCOPE;
4552              }
4553            } # INSCOPE
4554              
4555            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4556            if ($token->{tag_name} eq 'pre') {
4557              !!!next-token;
4558              if ($token->{type} == CHARACTER_TOKEN) {
4559                $token->{data} =~ s/^\x0A//;
4560                unless (length $token->{data}) {
4561                  !!!next-token;
4562                }
4563              }
4564            } else {
4565              !!!next-token;
4566            }
4567            redo B;
4568          } elsif ($token->{tag_name} eq 'form') {
4569            if (defined $self->{form_element}) {
4570              !!!parse-error (type => 'in form:form');
4571              ## Ignore the token
4572              !!!next-token;
4573              redo B;
4574            } else {
4575              ## has a p element in scope
4576              INSCOPE: for (reverse @{$self->{open_elements}}) {
4577                if ($_->[1] eq 'p') {
4578                  !!!back-token;
4579                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4580                  redo B;
4581                } elsif ({
4582                          table => 1, caption => 1, td => 1, th => 1,
4583                          button => 1, marquee => 1, object => 1, html => 1,
4584                         }->{$_->[1]}) {
4585                  last INSCOPE;
4586                }
4587              } # INSCOPE
4588                
4589              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4590              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4591              !!!next-token;
4592              redo B;
4593            }
4594          } elsif ($token->{tag_name} eq 'li') {
4595            ## has a p element in scope
4596            INSCOPE: for (reverse @{$self->{open_elements}}) {
4597              if ($_->[1] eq 'p') {
4598                !!!back-token;
4599                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4600                redo B;
4601              } elsif ({
4602                        table => 1, caption => 1, td => 1, th => 1,
4603                        button => 1, marquee => 1, object => 1, html => 1,
4604                       }->{$_->[1]}) {
4605                last INSCOPE;
4606              }
4607            } # INSCOPE
4608              
4609            ## Step 1
4610            my $i = -1;
4611            my $node = $self->{open_elements}->[$i];
4612            LI: {
4613              ## Step 2
4614              if ($node->[1] eq 'li') {
4615                if ($i != -1) {
4616                  !!!parse-error (type => 'end tag missing:'.
4617                                  $self->{open_elements}->[-1]->[1]);
4618                }
4619                splice @{$self->{open_elements}}, $i;
4620                last LI;
4621              }
4622              
4623              ## Step 3
4624              if (not $formatting_category->{$node->[1]} and
4625                  #not $phrasing_category->{$node->[1]} and
4626                  ($special_category->{$node->[1]} or
4627                   $scoping_category->{$node->[1]}) and
4628                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4629                last LI;
4630              }
4631              
4632              ## Step 4
4633              $i--;
4634              $node = $self->{open_elements}->[$i];
4635              redo LI;
4636            } # LI
4637              
4638            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4639            !!!next-token;
4640            redo B;
4641          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4642            ## has a p element in scope
4643            INSCOPE: for (reverse @{$self->{open_elements}}) {
4644              if ($_->[1] eq 'p') {
4645                !!!back-token;
4646                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4647                redo B;
4648              } elsif ({
4649                        table => 1, caption => 1, td => 1, th => 1,
4650                        button => 1, marquee => 1, object => 1, html => 1,
4651                       }->{$_->[1]}) {
4652                last INSCOPE;
4653              }
4654            } # INSCOPE
4655              
4656            ## Step 1
4657            my $i = -1;
4658            my $node = $self->{open_elements}->[$i];
4659            LI: {
4660              ## Step 2
4661              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4662                if ($i != -1) {
4663                  !!!parse-error (type => 'end tag missing:'.
4664                                  $self->{open_elements}->[-1]->[1]);
4665                }
4666                splice @{$self->{open_elements}}, $i;
4667                last LI;
4668              }
4669              
4670              ## Step 3
4671              if (not $formatting_category->{$node->[1]} and
4672                  #not $phrasing_category->{$node->[1]} and
4673                  ($special_category->{$node->[1]} or
4674                   $scoping_category->{$node->[1]}) and
4675                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4676                last LI;
4677              }
4678              
4679              ## Step 4
4680              $i--;
4681              $node = $self->{open_elements}->[$i];
4682              redo LI;
4683            } # LI
4684              
4685            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4686            !!!next-token;
4687            redo B;
4688          } elsif ($token->{tag_name} eq 'plaintext') {
4689            ## has a p element in scope
4690            INSCOPE: for (reverse @{$self->{open_elements}}) {
4691              if ($_->[1] eq 'p') {
4692                !!!back-token;
4693                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4694                redo B;
4695              } elsif ({
4696                        table => 1, caption => 1, td => 1, th => 1,
4697                        button => 1, marquee => 1, object => 1, html => 1,
4698                       }->{$_->[1]}) {
4699                last INSCOPE;
4700              }
4701            } # INSCOPE
4702              
4703            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4704                        
4705            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4706              
4707            !!!next-token;
4708            redo B;
4709          } elsif ({
4710                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4711                   }->{$token->{tag_name}}) {
4712            ## has a p element in scope
4713            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4714              my $node = $self->{open_elements}->[$_];
4715              if ($node->[1] eq 'p') {
4716                !!!back-token;
4717                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4718                redo B;
4719              } elsif ({
4720                        table => 1, caption => 1, td => 1, th => 1,
4721                        button => 1, marquee => 1, object => 1, html => 1,
4722                       }->{$node->[1]}) {
4723                last INSCOPE;
4724              }
4725            } # INSCOPE
4726              
4727            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4728            ## has an element in scope
4729            #my $i;
4730            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4731            #  my $node = $self->{open_elements}->[$_];
4732            #  if ({
4733            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4734            #      }->{$node->[1]}) {
4735            #    $i = $_;
4736            #    last INSCOPE;
4737            #  } elsif ({
4738            #            table => 1, caption => 1, td => 1, th => 1,
4739            #            button => 1, marquee => 1, object => 1, html => 1,
4740            #           }->{$node->[1]}) {
4741            #    last INSCOPE;
4742            #  }
4743            #} # INSCOPE
4744            #  
4745            #if (defined $i) {
4746            #  !!! parse-error (type => 'in hn:hn');
4747            #  splice @{$self->{open_elements}}, $i;
4748            #}
4749              
4750            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4751              
4752            !!!next-token;
4753            redo B;
4754          } elsif ($token->{tag_name} eq 'a') {
4755            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4756              my $node = $active_formatting_elements->[$i];
4757              if ($node->[1] eq 'a') {
4758                !!!parse-error (type => 'in a:a');
4759                
4760                !!!back-token;
4761                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4762                $formatting_end_tag->($token->{tag_name});
4763                
4764                AFE2: for (reverse 0..$#$active_formatting_elements) {
4765                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4766                    splice @$active_formatting_elements, $_, 1;
4767                    last AFE2;
4768                  }
4769                } # AFE2
4770                OE: for (reverse 0..$#{$self->{open_elements}}) {
4771                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4772                    splice @{$self->{open_elements}}, $_, 1;
4773                    last OE;
4774                  }
4775                } # OE
4776                last AFE;
4777              } elsif ($node->[0] eq '#marker') {
4778                last AFE;
4779              }
4780            } # AFE
4781              
4782            $reconstruct_active_formatting_elements->($insert_to_current);
4783    
4784            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4785            push @$active_formatting_elements, $self->{open_elements}->[-1];
4786    
4787            !!!next-token;
4788            redo B;
4789          } elsif ({
4790                    b => 1, big => 1, em => 1, font => 1, i => 1,
4791                    s => 1, small => 1, strile => 1,
4792                    strong => 1, tt => 1, u => 1,
4793                   }->{$token->{tag_name}}) {
4794            $reconstruct_active_formatting_elements->($insert_to_current);
4795            
4796            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4797            push @$active_formatting_elements, $self->{open_elements}->[-1];
4798            
4799            !!!next-token;
4800            redo B;
4801          } elsif ($token->{tag_name} eq 'nobr') {
4802            $reconstruct_active_formatting_elements->($insert_to_current);
4803    
4804            ## has a |nobr| element in scope
4805            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4806              my $node = $self->{open_elements}->[$_];
4807              if ($node->[1] eq 'nobr') {
4808                !!!parse-error (type => 'in nobr:nobr');
4809                !!!back-token;
4810                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4811                redo B;
4812              } elsif ({
4813                        table => 1, caption => 1, td => 1, th => 1,
4814                        button => 1, marquee => 1, object => 1, html => 1,
4815                       }->{$node->[1]}) {
4816                last INSCOPE;
4817              }
4818            } # INSCOPE
4819            
4820            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4821            push @$active_formatting_elements, $self->{open_elements}->[-1];
4822            
4823            !!!next-token;
4824            redo B;
4825          } elsif ($token->{tag_name} eq 'button') {
4826            ## has a button element in scope
4827            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4828              my $node = $self->{open_elements}->[$_];
4829              if ($node->[1] eq 'button') {
4830                !!!parse-error (type => 'in button:button');
4831                !!!back-token;
4832                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4833                redo B;
4834              } elsif ({
4835                        table => 1, caption => 1, td => 1, th => 1,
4836                        button => 1, marquee => 1, object => 1, html => 1,
4837                       }->{$node->[1]}) {
4838                last INSCOPE;
4839              }
4840            } # INSCOPE
4841              
4842            $reconstruct_active_formatting_elements->($insert_to_current);
4843              
4844            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4845            push @$active_formatting_elements, ['#marker', ''];
4846    
4847            !!!next-token;
4848            redo B;
4849          } elsif ($token->{tag_name} eq 'marquee' or
4850                   $token->{tag_name} eq 'object') {
4851            $reconstruct_active_formatting_elements->($insert_to_current);
4852            
4853            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4854            push @$active_formatting_elements, ['#marker', ''];
4855            
4856            !!!next-token;
4857            redo B;
4858          } elsif ($token->{tag_name} eq 'xmp') {
4859            $reconstruct_active_formatting_elements->($insert_to_current);
4860            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4861            redo B;
4862          } elsif ($token->{tag_name} eq 'table') {
4863            ## has a p element in scope
4864            INSCOPE: for (reverse @{$self->{open_elements}}) {
4865              if ($_->[1] eq 'p') {
4866                !!!back-token;
4867                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4868                redo B;
4869              } elsif ({
4870                        table => 1, caption => 1, td => 1, th => 1,
4871                        button => 1, marquee => 1, object => 1, html => 1,
4872                       }->{$_->[1]}) {
4873                last INSCOPE;
4874              }
4875            } # INSCOPE
4876              
4877            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4878                        
4879            $self->{insertion_mode} = IN_TABLE_IM;
4880              
4881            !!!next-token;
4882            redo B;
4883          } elsif ({
4884                    area => 1, basefont => 1, bgsound => 1, br => 1,
4885                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4886                    image => 1,
4887                   }->{$token->{tag_name}}) {
4888            if ($token->{tag_name} eq 'image') {
4889              !!!parse-error (type => 'image');
4890              $token->{tag_name} = 'img';
4891            }
4892    
4893            ## NOTE: There is an "as if <br>" code clone.
4894            $reconstruct_active_formatting_elements->($insert_to_current);
4895            
4896            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4897            pop @{$self->{open_elements}};
4898            
4899            !!!next-token;
4900            redo B;
4901          } elsif ($token->{tag_name} eq 'hr') {
4902            ## has a p element in scope
4903            INSCOPE: for (reverse @{$self->{open_elements}}) {
4904              if ($_->[1] eq 'p') {
4905                !!!back-token;
4906                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4907                redo B;
4908              } elsif ({
4909                        table => 1, caption => 1, td => 1, th => 1,
4910                        button => 1, marquee => 1, object => 1, html => 1,
4911                       }->{$_->[1]}) {
4912                last INSCOPE;
4913              }
4914            } # INSCOPE
4915              
4916            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4917            pop @{$self->{open_elements}};
4918              
4919            !!!next-token;
4920            redo B;
4921          } elsif ($token->{tag_name} eq 'input') {
4922            $reconstruct_active_formatting_elements->($insert_to_current);
4923            
4924            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4925            ## TODO: associate with $self->{form_element} if defined
4926            pop @{$self->{open_elements}};
4927            
4928            !!!next-token;
4929            redo B;
4930          } elsif ($token->{tag_name} eq 'isindex') {
4931            !!!parse-error (type => 'isindex');
4932            
4933            if (defined $self->{form_element}) {
4934              ## Ignore the token
4935              !!!next-token;
4936              redo B;
4937            } else {
4938              my $at = $token->{attributes};
4939              my $form_attrs;
4940              $form_attrs->{action} = $at->{action} if $at->{action};
4941              my $prompt_attr = $at->{prompt};
4942              $at->{name} = {name => 'name', value => 'isindex'};
4943              delete $at->{action};
4944              delete $at->{prompt};
4945              my @tokens = (
4946                            {type => START_TAG_TOKEN, tag_name => 'form',
4947                             attributes => $form_attrs},
4948                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4949                            {type => START_TAG_TOKEN, tag_name => 'p'},
4950                            {type => START_TAG_TOKEN, tag_name => 'label'},
4951                           );
4952              if ($prompt_attr) {
4953                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4954              } else {
4955                push @tokens, {type => CHARACTER_TOKEN,
4956                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4957                ## TODO: make this configurable
4958              }
4959              push @tokens,
4960                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4961                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4962                            {type => END_TAG_TOKEN, tag_name => 'label'},
4963                            {type => END_TAG_TOKEN, tag_name => 'p'},
4964                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4965                            {type => END_TAG_TOKEN, tag_name => 'form'};
4966              $token = shift @tokens;
4967              !!!back-token (@tokens);
4968              redo B;
4969            }
4970          } elsif ($token->{tag_name} eq 'textarea') {
4971            my $tag_name = $token->{tag_name};
4972            my $el;
4973            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4974            
4975            ## TODO: $self->{form_element} if defined
4976            $self->{content_model} = RCDATA_CONTENT_MODEL;
4977            delete $self->{escape}; # MUST
4978            
4979            $insert->($el);
4980            
4981            my $text = '';
4982            !!!next-token;
4983            if ($token->{type} == CHARACTER_TOKEN) {
4984              $token->{data} =~ s/^\x0A//;
4985            unless (length $token->{data}) {            unless (length $token->{data}) {
4986              !!!next-token;              !!!next-token;
             redo B;  
4987            }            }
4988          }          }
4989            while ($token->{type} == CHARACTER_TOKEN) {
4990              $text .= $token->{data};
4991              !!!next-token;
4992            }
4993            if (length $text) {
4994              $el->manakai_append_text ($text);
4995            }
4996            
4997            $self->{content_model} = PCDATA_CONTENT_MODEL;
4998            
4999            if ($token->{type} == END_TAG_TOKEN and
5000                $token->{tag_name} eq $tag_name) {
5001              ## Ignore the token
5002            } else {
5003              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5004            }
5005            !!!next-token;
5006            redo B;
5007          } elsif ({
5008                    iframe => 1,
5009                    noembed => 1,
5010                    noframes => 1,
5011                    noscript => 0, ## TODO: 1 if scripting is enabled
5012                   }->{$token->{tag_name}}) {
5013            ## NOTE: There is an "as if in body" code clone.
5014            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5015            redo B;
5016          } elsif ($token->{tag_name} eq 'select') {
5017            $reconstruct_active_formatting_elements->($insert_to_current);
5018            
5019            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5020            
5021            $self->{insertion_mode} = IN_SELECT_IM;
5022            !!!next-token;
5023            redo B;
5024          } elsif ({
5025                    caption => 1, col => 1, colgroup => 1, frame => 1,
5026                    frameset => 1, head => 1, option => 1, optgroup => 1,
5027                    tbody => 1, td => 1, tfoot => 1, th => 1,
5028                    thead => 1, tr => 1,
5029                   }->{$token->{tag_name}}) {
5030            !!!parse-error (type => 'in body:'.$token->{tag_name});
5031            ## Ignore the token
5032            !!!next-token;
5033            redo B;
5034            
5035            ## ISSUE: An issue on HTML5 new elements in the spec.
5036          } else {
5037            $reconstruct_active_formatting_elements->($insert_to_current);
5038            
5039            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5040            
5041            !!!next-token;
5042            redo B;
5043          }
5044        } elsif ($token->{type} == END_TAG_TOKEN) {
5045          if ($token->{tag_name} eq 'body') {
5046            if (@{$self->{open_elements}} > 1 and
5047                $self->{open_elements}->[1]->[1] eq 'body') {
5048              for (@{$self->{open_elements}}) {
5049                unless ({
5050                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
5051                           th => 1, tr => 1, body => 1, html => 1,
5052                         tbody => 1, tfoot => 1, thead => 1,
5053                        }->{$_->[1]}) {
5054                  !!!parse-error (type => 'not closed:'.$_->[1]);
5055                }
5056              }
5057    
5058          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
5059          $phase = 'main';            !!!next-token;
5060          ## reprocess            redo B;
5061            } else {
5062              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5063              ## Ignore the token
5064              !!!next-token;
5065              redo B;
5066            }
5067          } elsif ($token->{tag_name} eq 'html') {
5068            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5069              ## ISSUE: There is an issue in the spec.
5070              if ($self->{open_elements}->[-1]->[1] ne 'body') {
5071                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5072              }
5073              $self->{insertion_mode} = AFTER_BODY_IM;
5074              ## reprocess
5075              redo B;
5076            } else {
5077              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5078              ## Ignore the token
5079              !!!next-token;
5080              redo B;
5081            }
5082          } elsif ({
5083                    address => 1, blockquote => 1, center => 1, dir => 1,
5084                    div => 1, dl => 1, fieldset => 1, listing => 1,
5085                    menu => 1, ol => 1, pre => 1, ul => 1,
5086                    p => 1,
5087                    dd => 1, dt => 1, li => 1,
5088                    button => 1, marquee => 1, object => 1,
5089                   }->{$token->{tag_name}}) {
5090            ## has an element in scope
5091            my $i;
5092            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5093              my $node = $self->{open_elements}->[$_];
5094              if ($node->[1] eq $token->{tag_name}) {
5095                ## generate implied end tags
5096                if ({
5097                     dd => ($token->{tag_name} ne 'dd'),
5098                     dt => ($token->{tag_name} ne 'dt'),
5099                     li => ($token->{tag_name} ne 'li'),
5100                     p => ($token->{tag_name} ne 'p'),
5101                     td => 1, th => 1, tr => 1,
5102                     tbody => 1, tfoot=> 1, thead => 1,
5103                    }->{$self->{open_elements}->[-1]->[1]}) {
5104                  !!!back-token;
5105                  $token = {type => END_TAG_TOKEN,
5106                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5107                  redo B;
5108                }
5109                $i = $_;
5110                last INSCOPE unless $token->{tag_name} eq 'p';
5111              } elsif ({
5112                        table => 1, caption => 1, td => 1, th => 1,
5113                        button => 1, marquee => 1, object => 1, html => 1,
5114                       }->{$node->[1]}) {
5115                last INSCOPE;
5116              }
5117            } # INSCOPE
5118            
5119            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5120              if (defined $i) {
5121                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5122              } else {
5123                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5124              }
5125            }
5126            
5127            if (defined $i) {
5128              splice @{$self->{open_elements}}, $i;
5129            } elsif ($token->{tag_name} eq 'p') {
5130              ## As if <p>, then reprocess the current token
5131              my $el;
5132              !!!create-element ($el, 'p');
5133              $insert->($el);
5134            }
5135            $clear_up_to_marker->()
5136              if {
5137                button => 1, marquee => 1, object => 1,
5138              }->{$token->{tag_name}};
5139            !!!next-token;
5140          redo B;          redo B;
5141        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
5142                 $token->{type} eq 'end tag') {          ## has an element in scope
5143          !!!parse-error (type => 'after html:'.$token->{tag_name});          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5144          $phase = 'main';            my $node = $self->{open_elements}->[$_];
5145          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
5146                ## generate implied end tags
5147                if ({
5148                     dd => 1, dt => 1, li => 1, p => 1,
5149                     td => 1, th => 1, tr => 1,
5150                     tbody => 1, tfoot=> 1, thead => 1,
5151                    }->{$self->{open_elements}->[-1]->[1]}) {
5152                  !!!back-token;
5153                  $token = {type => END_TAG_TOKEN,
5154                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5155                  redo B;
5156                }
5157                last INSCOPE;
5158              } elsif ({
5159                        table => 1, caption => 1, td => 1, th => 1,
5160                        button => 1, marquee => 1, object => 1, html => 1,
5161                       }->{$node->[1]}) {
5162                last INSCOPE;
5163              }
5164            } # INSCOPE
5165            
5166            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5167              pop @{$self->{open_elements}};
5168            } else {
5169              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5170            }
5171    
5172            undef $self->{form_element};
5173            !!!next-token;
5174            redo B;
5175          } elsif ({
5176                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5177                   }->{$token->{tag_name}}) {
5178            ## has an element in scope
5179            my $i;
5180            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5181              my $node = $self->{open_elements}->[$_];
5182              if ({
5183                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5184                  }->{$node->[1]}) {
5185                ## generate implied end tags
5186                if ({
5187                     dd => 1, dt => 1, li => 1, p => 1,
5188                     td => 1, th => 1, tr => 1,
5189                     tbody => 1, tfoot=> 1, thead => 1,
5190                    }->{$self->{open_elements}->[-1]->[1]}) {
5191                  !!!back-token;
5192                  $token = {type => END_TAG_TOKEN,
5193                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5194                  redo B;
5195                }
5196                $i = $_;
5197                last INSCOPE;
5198              } elsif ({
5199                        table => 1, caption => 1, td => 1, th => 1,
5200                        button => 1, marquee => 1, object => 1, html => 1,
5201                       }->{$node->[1]}) {
5202                last INSCOPE;
5203              }
5204            } # INSCOPE
5205            
5206            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5207              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5208            }
5209            
5210            splice @{$self->{open_elements}}, $i if defined $i;
5211            !!!next-token;
5212            redo B;
5213          } elsif ({
5214                    a => 1,
5215                    b => 1, big => 1, em => 1, font => 1, i => 1,
5216                    nobr => 1, s => 1, small => 1, strile => 1,
5217                    strong => 1, tt => 1, u => 1,
5218                   }->{$token->{tag_name}}) {
5219            $formatting_end_tag->($token->{tag_name});
5220            redo B;
5221          } elsif ($token->{tag_name} eq 'br') {
5222            !!!parse-error (type => 'unmatched end tag:br');
5223    
5224            ## As if <br>
5225            $reconstruct_active_formatting_elements->($insert_to_current);
5226            
5227            my $el;
5228            !!!create-element ($el, 'br');
5229            $insert->($el);
5230            
5231            ## Ignore the token.
5232            !!!next-token;
5233            redo B;
5234          } elsif ({
5235                    caption => 1, col => 1, colgroup => 1, frame => 1,
5236                    frameset => 1, head => 1, option => 1, optgroup => 1,
5237                    tbody => 1, td => 1, tfoot => 1, th => 1,
5238                    thead => 1, tr => 1,
5239                    area => 1, basefont => 1, bgsound => 1,
5240                    embed => 1, hr => 1, iframe => 1, image => 1,
5241                    img => 1, input => 1, isindex => 1, noembed => 1,
5242                    noframes => 1, param => 1, select => 1, spacer => 1,
5243                    table => 1, textarea => 1, wbr => 1,
5244                    noscript => 0, ## TODO: if scripting is enabled
5245                   }->{$token->{tag_name}}) {
5246            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5247            ## Ignore the token
5248            !!!next-token;
5249          redo B;          redo B;
5250        } elsif ($token->{type} eq 'end-of-file') {          
5251          ## Stop parsing          ## ISSUE: Issue on HTML5 new elements in spec
5252          last B;          
5253        } else {        } else {
5254          die "$0: $token->{type}: Unknown token";          ## Step 1
5255            my $node_i = -1;
5256            my $node = $self->{open_elements}->[$node_i];
5257    
5258            ## Step 2
5259            S2: {
5260              if ($node->[1] eq $token->{tag_name}) {
5261                ## Step 1
5262                ## generate implied end tags
5263                if ({
5264                     dd => 1, dt => 1, li => 1, p => 1,
5265                     td => 1, th => 1, tr => 1,
5266                     tbody => 1, tfoot => 1, thead => 1,
5267                    }->{$self->{open_elements}->[-1]->[1]}) {
5268                  !!!back-token;
5269                  $token = {type => END_TAG_TOKEN,
5270                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5271                  redo B;
5272                }
5273            
5274                ## Step 2
5275                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5276                  ## NOTE: <x><y></x>
5277                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5278                }
5279                
5280                ## Step 3
5281                splice @{$self->{open_elements}}, $node_i;
5282    
5283                !!!next-token;
5284                last S2;
5285              } else {
5286                ## Step 3
5287                if (not $formatting_category->{$node->[1]} and
5288                    #not $phrasing_category->{$node->[1]} and
5289                    ($special_category->{$node->[1]} or
5290                     $scoping_category->{$node->[1]})) {
5291                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5292                  ## Ignore the token
5293                  !!!next-token;
5294                  last S2;
5295                }
5296              }
5297              
5298              ## Step 4
5299              $node_i--;
5300              $node = $self->{open_elements}->[$node_i];
5301              
5302              ## Step 5;
5303              redo S2;
5304            } # S2
5305            redo B;
5306        }        }
5307      }      }
5308        redo B;
5309    } # B    } # B
5310    
5311      ## NOTE: The "trailing end" phase in HTML5 is split into
5312      ## two insertion modes: "after html body" and "after html frameset".
5313      ## NOTE: States in the main stage is preserved while
5314      ## the parser stays in the trailing end phase. # MUST
5315    
5316    ## Stop parsing # MUST    ## Stop parsing # MUST
5317        
5318    ## TODO: script stuffs    ## TODO: script stuffs
# Line 4824  sub set_inner_html ($$$) { Line 5324  sub set_inner_html ($$$) {
5324    my $s = \$_[0];    my $s = \$_[0];
5325    my $onerror = $_[1];    my $onerror = $_[1];
5326    
5327      ## ISSUE: Should {confident} be true?
5328    
5329    my $nt = $node->node_type;    my $nt = $node->node_type;
5330    if ($nt == 9) {    if ($nt == 9) {
5331      # MUST      # MUST
# Line 4846  sub set_inner_html ($$$) { Line 5348  sub set_inner_html ($$$) {
5348      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5349    
5350      ## Step 1 # MUST      ## Step 1 # MUST
5351      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5352      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5353        $doc->manakai_is_html (1);
5354      my $p = $class->new;      my $p = $class->new;
5355      $p->{document} = $doc;      $p->{document} = $doc;
5356    
# Line 4857  sub set_inner_html ($$$) { Line 5360  sub set_inner_html ($$$) {
5360      my $column = 0;      my $column = 0;
5361      $p->{set_next_input_character} = sub {      $p->{set_next_input_character} = sub {
5362        my $self = shift;        my $self = shift;
5363    
5364          pop @{$self->{prev_input_character}};
5365          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5366    
5367        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_input_character} = -1 and return if $i >= length $$s;
5368        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_input_character} = ord substr $$s, $i++, 1;
5369        $column++;        $column++;
# Line 4865  sub set_inner_html ($$$) { Line 5372  sub set_inner_html ($$$) {
5372          $line++;          $line++;
5373          $column = 0;          $column = 0;
5374        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_input_character} == 0x000D) { # CR
5375          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;  
           }  
         }  
5376          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_input_character} = 0x000A; # LF # MUST
5377          $line++;          $line++;
5378          $column = 0;          $column = 0;
5379        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_input_character} > 0x10FFFF) {
5380          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5381        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_input_character} == 0x0000) { # NULL
5382            !!!parse-error (type => 'NULL');
5383          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5384        }        }
5385      };      };
5386        $p->{prev_input_character} = [-1, -1, -1];
5387        $p->{next_input_character} = -1;
5388            
5389      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5390        my (%opt) = @_;        my (%opt) = @_;
# Line 4898  sub set_inner_html ($$$) { Line 5399  sub set_inner_html ($$$) {
5399    
5400      ## Step 2      ## Step 2
5401      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5402      $p->{content_model_flag} = {      $p->{content_model} = {
5403        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5404        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5405        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5406        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5407        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5408        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5409        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5410        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5411        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5412        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5413      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5414         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5415            unless defined $p->{content_model};
5416            ## ISSUE: What is "the name of the element"? local name?
5417    
5418      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5419    
# Line 4962  sub set_inner_html ($$$) { Line 5465  sub set_inner_html ($$$) {
5465      ## Step 12 # MUST      ## Step 12 # MUST
5466      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5467      for (@cn) {      for (@cn) {
5468          $this_doc->adopt_node ($_);
5469        $node->append_child ($_);        $node->append_child ($_);
5470      }      }
5471      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5472    
5473      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5474    } else {    } else {
# Line 4974  sub set_inner_html ($$$) { Line 5478  sub set_inner_html ($$$) {
5478    
5479  } # tree construction stage  } # tree construction stage
5480    
5481  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5482    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  
5483    
5484  1;  1;
5485  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24