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

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

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

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

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24