/[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.10 by wakaba, Sat Jun 23 03:30:06 2007 UTC revision 1.210 by wakaba, Tue Oct 14 13:24:52 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  use Whatpm::HTML::Tokenizer;
7    
8  my $permitted_slash_tag_name = {  ## NOTE: This module don't check all HTML5 parse errors; character
9    base => 1,  ## encoding related parse errors are expected to be handled by relevant
10    link => 1,  ## modules.
11    meta => 1,  ## Parse errors for control characters that are not allowed in HTML5
12    hr => 1,  ## documents, for surrogate code points, and for noncharacter code
13    br => 1,  ## points, as well as U+FFFD substitions for characters whose code points
14    img=> 1,  ## is higher than U+10FFFF may be detected by combining the parser with
15    embed => 1,  ## the checker implemented by Whatpm::Charset::UnicodeChecker (for its
16    param => 1,  ## usage example, see |t/HTML-tree.t| in the Whatpm package or the
17    area => 1,  ## WebHACC::Language::HTML module in the WebHACC package).
18    col => 1,  
19    input => 1,  ## ISSUE:
20    ## var doc = implementation.createDocument (null, null, null);
21    ## doc.write ('');
22    ## alert (doc.compatMode);
23    
24    require IO::Handle;
25    
26    ## Namespace URLs
27    
28    my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
29    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
30    my $SVG_NS = q<http://www.w3.org/2000/svg>;
31    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
32    my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
33    my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
34    
35    ## Element categories
36    
37    ## Bits 12-15
38    sub SPECIAL_EL () { 0b1_000000000000000 }
39    sub SCOPING_EL () { 0b1_00000000000000 }
40    sub FORMATTING_EL () { 0b1_0000000000000 }
41    sub PHRASING_EL () { 0b1_000000000000 }
42    
43    ## Bits 10-11
44    #sub FOREIGN_EL () { 0b1_00000000000 } # see Whatpm::HTML::Tokenizer
45    sub FOREIGN_FLOW_CONTENT_EL () { 0b1_0000000000 }
46    
47    ## Bits 6-9
48    sub TABLE_SCOPING_EL () { 0b1_000000000 }
49    sub TABLE_ROWS_SCOPING_EL () { 0b1_00000000 }
50    sub TABLE_ROW_SCOPING_EL () { 0b1_0000000 }
51    sub TABLE_ROWS_EL () { 0b1_000000 }
52    
53    ## Bit 5
54    sub ADDRESS_DIV_P_EL () { 0b1_00000 }
55    
56    ## NOTE: Used in </body> and EOF algorithms.
57    ## Bit 4
58    sub ALL_END_TAG_OPTIONAL_EL () { 0b1_0000 }
59    
60    ## NOTE: Used in "generate implied end tags" algorithm.
61    ## NOTE: There is a code where a modified version of
62    ## END_TAG_OPTIONAL_EL is used in "generate implied end tags"
63    ## implementation (search for the algorithm name).
64    ## Bit 3
65    sub END_TAG_OPTIONAL_EL () { 0b1_000 }
66    
67    ## Bits 0-2
68    
69    sub MISC_SPECIAL_EL () { SPECIAL_EL | 0b000 }
70    sub FORM_EL () { SPECIAL_EL | 0b001 }
71    sub FRAMESET_EL () { SPECIAL_EL | 0b010 }
72    sub HEADING_EL () { SPECIAL_EL | 0b011 }
73    sub SELECT_EL () { SPECIAL_EL | 0b100 }
74    sub SCRIPT_EL () { SPECIAL_EL | 0b101 }
75    
76    sub ADDRESS_DIV_EL () { SPECIAL_EL | ADDRESS_DIV_P_EL | 0b001 }
77    sub BODY_EL () { SPECIAL_EL | ALL_END_TAG_OPTIONAL_EL | 0b001 }
78    
79    sub DTDD_EL () {
80      SPECIAL_EL |
81      END_TAG_OPTIONAL_EL |
82      ALL_END_TAG_OPTIONAL_EL |
83      0b010
84    }
85    sub LI_EL () {
86      SPECIAL_EL |
87      END_TAG_OPTIONAL_EL |
88      ALL_END_TAG_OPTIONAL_EL |
89      0b100
90    }
91    sub P_EL () {
92      SPECIAL_EL |
93      ADDRESS_DIV_P_EL |
94      END_TAG_OPTIONAL_EL |
95      ALL_END_TAG_OPTIONAL_EL |
96      0b001
97    }
98    
99    sub TABLE_ROW_EL () {
100      SPECIAL_EL |
101      TABLE_ROWS_EL |
102      TABLE_ROW_SCOPING_EL |
103      ALL_END_TAG_OPTIONAL_EL |
104      0b001
105    }
106    sub TABLE_ROW_GROUP_EL () {
107      SPECIAL_EL |
108      TABLE_ROWS_EL |
109      TABLE_ROWS_SCOPING_EL |
110      ALL_END_TAG_OPTIONAL_EL |
111      0b001
112    }
113    
114    sub MISC_SCOPING_EL () { SCOPING_EL | 0b000 }
115    sub BUTTON_EL () { SCOPING_EL | 0b001 }
116    sub CAPTION_EL () { SCOPING_EL | 0b010 }
117    sub HTML_EL () {
118      SCOPING_EL |
119      TABLE_SCOPING_EL |
120      TABLE_ROWS_SCOPING_EL |
121      TABLE_ROW_SCOPING_EL |
122      ALL_END_TAG_OPTIONAL_EL |
123      0b001
124    }
125    sub TABLE_EL () {
126      SCOPING_EL |
127      TABLE_ROWS_EL |
128      TABLE_SCOPING_EL |
129      0b001
130    }
131    sub TABLE_CELL_EL () {
132      SCOPING_EL |
133      TABLE_ROW_SCOPING_EL |
134      ALL_END_TAG_OPTIONAL_EL |
135      0b001
136    }
137    
138    sub MISC_FORMATTING_EL () { FORMATTING_EL | 0b000 }
139    sub A_EL () { FORMATTING_EL | 0b001 }
140    sub NOBR_EL () { FORMATTING_EL | 0b010 }
141    
142    sub RUBY_EL () { PHRASING_EL | 0b001 }
143    
144    ## ISSUE: ALL_END_TAG_OPTIONAL_EL?
145    sub OPTGROUP_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b001 }
146    sub OPTION_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b010 }
147    sub RUBY_COMPONENT_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b100 }
148    
149    sub MML_AXML_EL () { PHRASING_EL | FOREIGN_EL | 0b001 }
150    
151    my $el_category = {
152      a => A_EL,
153      address => ADDRESS_DIV_EL,
154      applet => MISC_SCOPING_EL,
155      area => MISC_SPECIAL_EL,
156      article => MISC_SPECIAL_EL,
157      aside => MISC_SPECIAL_EL,
158      b => FORMATTING_EL,
159      base => MISC_SPECIAL_EL,
160      basefont => MISC_SPECIAL_EL,
161      bgsound => MISC_SPECIAL_EL,
162      big => FORMATTING_EL,
163      blockquote => MISC_SPECIAL_EL,
164      body => BODY_EL,
165      br => MISC_SPECIAL_EL,
166      button => BUTTON_EL,
167      caption => CAPTION_EL,
168      center => MISC_SPECIAL_EL,
169      col => MISC_SPECIAL_EL,
170      colgroup => MISC_SPECIAL_EL,
171      command => MISC_SPECIAL_EL,
172      datagrid => MISC_SPECIAL_EL,
173      dd => DTDD_EL,
174      details => MISC_SPECIAL_EL,
175      dialog => MISC_SPECIAL_EL,
176      dir => MISC_SPECIAL_EL,
177      div => ADDRESS_DIV_EL,
178      dl => MISC_SPECIAL_EL,
179      dt => DTDD_EL,
180      em => FORMATTING_EL,
181      embed => MISC_SPECIAL_EL,
182      eventsource => MISC_SPECIAL_EL,
183      fieldset => MISC_SPECIAL_EL,
184      figure => MISC_SPECIAL_EL,
185      font => FORMATTING_EL,
186      footer => MISC_SPECIAL_EL,
187      form => FORM_EL,
188      frame => MISC_SPECIAL_EL,
189      frameset => FRAMESET_EL,
190      h1 => HEADING_EL,
191      h2 => HEADING_EL,
192      h3 => HEADING_EL,
193      h4 => HEADING_EL,
194      h5 => HEADING_EL,
195      h6 => HEADING_EL,
196      head => MISC_SPECIAL_EL,
197      header => MISC_SPECIAL_EL,
198      hr => MISC_SPECIAL_EL,
199      html => HTML_EL,
200      i => FORMATTING_EL,
201      iframe => MISC_SPECIAL_EL,
202      img => MISC_SPECIAL_EL,
203      #image => MISC_SPECIAL_EL, ## NOTE: Commented out in the spec.
204      input => MISC_SPECIAL_EL,
205      isindex => MISC_SPECIAL_EL,
206      li => LI_EL,
207      link => MISC_SPECIAL_EL,
208      listing => MISC_SPECIAL_EL,
209      marquee => MISC_SCOPING_EL,
210      menu => MISC_SPECIAL_EL,
211      meta => MISC_SPECIAL_EL,
212      nav => MISC_SPECIAL_EL,
213      nobr => NOBR_EL,
214      noembed => MISC_SPECIAL_EL,
215      noframes => MISC_SPECIAL_EL,
216      noscript => MISC_SPECIAL_EL,
217      object => MISC_SCOPING_EL,
218      ol => MISC_SPECIAL_EL,
219      optgroup => OPTGROUP_EL,
220      option => OPTION_EL,
221      p => P_EL,
222      param => MISC_SPECIAL_EL,
223      plaintext => MISC_SPECIAL_EL,
224      pre => MISC_SPECIAL_EL,
225      rp => RUBY_COMPONENT_EL,
226      rt => RUBY_COMPONENT_EL,
227      ruby => RUBY_EL,
228      s => FORMATTING_EL,
229      script => MISC_SPECIAL_EL,
230      select => SELECT_EL,
231      section => MISC_SPECIAL_EL,
232      small => FORMATTING_EL,
233      spacer => MISC_SPECIAL_EL,
234      strike => FORMATTING_EL,
235      strong => FORMATTING_EL,
236      style => MISC_SPECIAL_EL,
237      table => TABLE_EL,
238      tbody => TABLE_ROW_GROUP_EL,
239      td => TABLE_CELL_EL,
240      textarea => MISC_SPECIAL_EL,
241      tfoot => TABLE_ROW_GROUP_EL,
242      th => TABLE_CELL_EL,
243      thead => TABLE_ROW_GROUP_EL,
244      title => MISC_SPECIAL_EL,
245      tr => TABLE_ROW_EL,
246      tt => FORMATTING_EL,
247      u => FORMATTING_EL,
248      ul => MISC_SPECIAL_EL,
249      wbr => MISC_SPECIAL_EL,
250  };  };
251    
252  my $entity_char = {  my $el_category_f = {
253    AElig => "\x{00C6}",    $MML_NS => {
254    Aacute => "\x{00C1}",      'annotation-xml' => MML_AXML_EL,
255    Acirc => "\x{00C2}",      mi => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
256    Agrave => "\x{00C0}",      mo => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
257    Alpha => "\x{0391}",      mn => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
258    Aring => "\x{00C5}",      ms => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
259    Atilde => "\x{00C3}",      mtext => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
260    Auml => "\x{00C4}",    },
261    Beta => "\x{0392}",    $SVG_NS => {
262    Ccedil => "\x{00C7}",      foreignObject => SCOPING_EL | FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
263    Chi => "\x{03A7}",      desc => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
264    Dagger => "\x{2021}",      title => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
265    Delta => "\x{0394}",    },
266    ETH => "\x{00D0}",    ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
   Eacute => "\x{00C9}",  
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 }; # $entity_char  
   
 my $c1_entity_char = {  
   0x80 => 0x20AC,  
   0x81 => 0xFFFD,  
   0x82 => 0x201A,  
   0x83 => 0x0192,  
   0x84 => 0x201E,  
   0x85 => 0x2026,  
   0x86 => 0x2020,  
   0x87 => 0x2021,  
   0x88 => 0x02C6,  
   0x89 => 0x2030,  
   0x8A => 0x0160,  
   0x8B => 0x2039,  
   0x8C => 0x0152,  
   0x8D => 0xFFFD,  
   0x8E => 0x017D,  
   0x8F => 0xFFFD,  
   0x90 => 0xFFFD,  
   0x91 => 0x2018,  
   0x92 => 0x2019,  
   0x93 => 0x201C,  
   0x94 => 0x201D,  
   0x95 => 0x2022,  
   0x96 => 0x2013,  
   0x97 => 0x2014,  
   0x98 => 0x02DC,  
   0x99 => 0x2122,  
   0x9A => 0x0161,  
   0x9B => 0x203A,  
   0x9C => 0x0153,  
   0x9D => 0xFFFD,  
   0x9E => 0x017E,  
   0x9F => 0x0178,  
 }; # $c1_entity_char  
   
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
267  };  };
268  my $scoping_category = {  
269    button => 1, caption => 1, html => 1, marquee => 1, object => 1,  my $svg_attr_name = {
270    table => 1, td => 1, th => 1,    attributename => 'attributeName',
271      attributetype => 'attributeType',
272      basefrequency => 'baseFrequency',
273      baseprofile => 'baseProfile',
274      calcmode => 'calcMode',
275      clippathunits => 'clipPathUnits',
276      contentscripttype => 'contentScriptType',
277      contentstyletype => 'contentStyleType',
278      diffuseconstant => 'diffuseConstant',
279      edgemode => 'edgeMode',
280      externalresourcesrequired => 'externalResourcesRequired',
281      filterres => 'filterRes',
282      filterunits => 'filterUnits',
283      glyphref => 'glyphRef',
284      gradienttransform => 'gradientTransform',
285      gradientunits => 'gradientUnits',
286      kernelmatrix => 'kernelMatrix',
287      kernelunitlength => 'kernelUnitLength',
288      keypoints => 'keyPoints',
289      keysplines => 'keySplines',
290      keytimes => 'keyTimes',
291      lengthadjust => 'lengthAdjust',
292      limitingconeangle => 'limitingConeAngle',
293      markerheight => 'markerHeight',
294      markerunits => 'markerUnits',
295      markerwidth => 'markerWidth',
296      maskcontentunits => 'maskContentUnits',
297      maskunits => 'maskUnits',
298      numoctaves => 'numOctaves',
299      pathlength => 'pathLength',
300      patterncontentunits => 'patternContentUnits',
301      patterntransform => 'patternTransform',
302      patternunits => 'patternUnits',
303      pointsatx => 'pointsAtX',
304      pointsaty => 'pointsAtY',
305      pointsatz => 'pointsAtZ',
306      preservealpha => 'preserveAlpha',
307      preserveaspectratio => 'preserveAspectRatio',
308      primitiveunits => 'primitiveUnits',
309      refx => 'refX',
310      refy => 'refY',
311      repeatcount => 'repeatCount',
312      repeatdur => 'repeatDur',
313      requiredextensions => 'requiredExtensions',
314      requiredfeatures => 'requiredFeatures',
315      specularconstant => 'specularConstant',
316      specularexponent => 'specularExponent',
317      spreadmethod => 'spreadMethod',
318      startoffset => 'startOffset',
319      stddeviation => 'stdDeviation',
320      stitchtiles => 'stitchTiles',
321      surfacescale => 'surfaceScale',
322      systemlanguage => 'systemLanguage',
323      tablevalues => 'tableValues',
324      targetx => 'targetX',
325      targety => 'targetY',
326      textlength => 'textLength',
327      viewbox => 'viewBox',
328      viewtarget => 'viewTarget',
329      xchannelselector => 'xChannelSelector',
330      ychannelselector => 'yChannelSelector',
331      zoomandpan => 'zoomAndPan',
332  };  };
333  my $formatting_category = {  
334    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  my $foreign_attr_xname = {
335    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
336      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
337      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
338      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
339      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
340      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
341      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
342      'xml:base' => [$XML_NS, ['xml', 'base']],
343      'xml:lang' => [$XML_NS, ['xml', 'lang']],
344      'xml:space' => [$XML_NS, ['xml', 'space']],
345      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
346      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
347  };  };
 # $phrasing_category: all other elements  
348    
349  sub parse_string ($$$;$) {  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
   my $self = shift->new;  
   my $s = \$_[0];  
   $self->{document} = $_[1];  
350    
351    ## NOTE: |set_inner_html| copies most of this method's code  ## TODO: Invoke the reset algorithm when a resettable element is
352    ## created (cf. HTML5 revision 2259).
353    
354    my $i = 0;  sub parse_byte_string ($$$$;$) {
355    my $line = 1;    my $self = shift;
356    my $column = 0;    my $charset_name = shift;
357    $self->{set_next_input_character} = sub {    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
358      my $self = shift;    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
359      $self->{next_input_character} = -1 and return if $i >= length $$s;  } # parse_byte_string
360      $self->{next_input_character} = ord substr $$s, $i++, 1;  
361      $column++;  sub parse_byte_stream ($$$$;$$) {
362          # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
363      if ($self->{next_input_character} == 0x000A) { # LF    my $self = ref $_[0] ? shift : shift->new;
364        $line++;    my $charset_name = shift;
365        $column = 0;    my $byte_stream = $_[0];
     } elsif ($self->{next_input_character} == 0x000D) { # CR  
       if ($i >= length $$s) {  
         #  
       } else {  
         my $next_char = ord substr $$s, $i++, 1;  
         if ($next_char == 0x000A) { # LF  
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
       }  
       $self->{next_input_character} = 0x000A; # LF # MUST  
       $line++;  
       $column = 0;  
     } elsif ($self->{next_input_character} > 0x10FFFF) {  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_input_character} == 0x0000) { # NULL  
       !!!parse-error (type => 'NULL');  
 ## TODO: test  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     }  
   };  
366    
367    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
368      my (%opt) = @_;      my (%opt) = @_;
369      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      warn "Parse error ($opt{type})\n";
   };  
   $self->{parse_error} = sub {  
     $onerror->(@_, line => $line, column => $column);  
370    };    };
371      $self->{parse_error} = $onerror; # updated later by parse_char_string
372    
373    $self->_initialize_tokenizer;    my $get_wrapper = $_[3] || sub ($) {
374    $self->_initialize_tree_constructor;      return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
   $self->_construct_tree;  
   $self->_terminate_tree_constructor;  
   
   return $self->{document};  
 } # parse_string  
   
 sub new ($) {  
   my $class = shift;  
   my $self = bless {}, $class;  
   $self->{set_next_input_character} = sub {  
     $self->{next_input_character} = -1;  
375    };    };
   $self->{parse_error} = sub {  
     #  
   };  
   return $self;  
 } # new  
376    
377  ## Implementations MUST act as if state machine in the spec    ## HTML5 encoding sniffing algorithm
378      require Message::Charset::Info;
379  sub _initialize_tokenizer ($) {    my $charset;
380    my $self = shift;    my $buffer;
381    $self->{state} = 'data'; # MUST    my ($char_stream, $e_status);
382    $self->{content_model_flag} = 'PCDATA'; # be  
383    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    SNIFFING: {
384    undef $self->{current_attribute};      ## NOTE: By setting |allow_fallback| option true when the
385    undef $self->{last_emitted_start_tag_name};      ## |get_decode_handle| method is invoked, we ignore what the HTML5
386    undef $self->{last_attribute_value_state};      ## spec requires, i.e. unsupported encoding should be ignored.
387    $self->{char} = [];        ## TODO: We should not do this unless the parser is invoked
388    # $self->{next_input_character}        ## in the conformance checking mode, in which this behavior
389    !!!next-input-character;        ## would be useful.
   $self->{token} = [];  
 } # _initialize_tokenizer  
   
 ## A token has:  
 ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  
 ##       'character', or 'end-of-file'  
 ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  
     ## ISSUE: the spec need s/tagname/tag name/  
 ##   ->{error} == 1 or 0 (DOCTYPE)  
 ##   ->{attributes} isa HASH (start tag, end tag)  
 ##   ->{data} (comment, character)  
   
 ## Macros  
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
   
 ## Emitted token MUST immediately be handled by the tree construction state.  
   
 ## Before each step, UA MAY check to see if either one of the scripts in  
 ## "list of scripts that will execute as soon as possible" or the first  
 ## script in the "list of scripts that will execute asynchronously",  
 ## has completed loading.  If one has, then it MUST be executed  
 ## and removed from the list.  
   
 ## ISSUE: <http://html5.org/tools/web-apps-tracker?from=874&to=876>  
   
 sub _get_next_token ($) {  
   my $self = shift;  
   if (@{$self->{token}}) {  
     return shift @{$self->{token}};  
   }  
390    
391    A: {      ## Step 1
392      if ($self->{state} eq 'data') {      if (defined $charset_name) {
393        if ($self->{next_input_character} == 0x0026) { # &        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
394          if ($self->{content_model_flag} eq 'PCDATA' or            ## TODO: Is this ok?  Transfer protocol's parameter should be
395              $self->{content_model_flag} eq 'RCDATA') {            ## interpreted in its semantics?
396            $self->{state} = 'entity data';  
397            !!!next-input-character;        ($char_stream, $e_status) = $charset->get_decode_handle
398            redo A;            ($byte_stream, allow_error_reporting => 1,
399          } else {             allow_fallback => 1);
400            #        if ($char_stream) {
401          }          $self->{confident} = 1;
402        } elsif ($self->{next_input_character} == 0x003C) { # <          last SNIFFING;
         if ($self->{content_model_flag} ne 'PLAINTEXT') {  
           $self->{state} = 'tag open';  
           !!!next-input-character;  
           redo A;  
         } else {  
           #  
         }  
       } elsif ($self->{next_input_character} == -1) {  
         !!!emit ({type => 'end-of-file'});  
         last A; ## TODO: ok?  
       }  
       # Anything else  
       my $token = {type => 'character',  
                    data => chr $self->{next_input_character}};  
       ## Stay in the data state  
       !!!next-input-character;  
   
       !!!emit ($token);  
   
       redo A;  
     } elsif ($self->{state} eq 'entity data') {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity;  
   
       $self->{state} = 'data';  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!emit ({type => 'character', data => '&'});  
403        } else {        } else {
404          !!!emit ($token);          !!!parse-error (type => 'charset:not supported',
405                            layer => 'encode',
406                            line => 1, column => 1,
407                            value => $charset_name,
408                            level => $self->{level}->{uncertain});
409        }        }
410        }
411    
412        redo A;      ## Step 2
413      } elsif ($self->{state} eq 'tag open') {      my $byte_buffer = '';
414        if ($self->{content_model_flag} eq 'RCDATA' or      for (1..1024) {
415            $self->{content_model_flag} eq 'CDATA') {        my $char = $byte_stream->getc;
416          if ($self->{next_input_character} == 0x002F) { # /        last unless defined $char;
417            !!!next-input-character;        $byte_buffer .= $char;
418            $self->{state} = 'close tag open';      } ## TODO: timeout
           redo A;  
         } else {  
           ## reconsume  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '<'});  
   
           redo A;  
         }  
       } elsif ($self->{content_model_flag} eq 'PCDATA') {  
         if ($self->{next_input_character} == 0x0021) { # !  
           $self->{state} = 'markup declaration open';  
           !!!next-input-character;  
           redo A;  
         } elsif ($self->{next_input_character} == 0x002F) { # /  
           $self->{state} = 'close tag open';  
           !!!next-input-character;  
           redo A;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x005A) { # A..Z  
           $self->{current_token}  
             = {type => 'start tag',  
                tag_name => chr ($self->{next_input_character} + 0x0020)};  
           $self->{state} = 'tag name';  
           !!!next-input-character;  
           redo A;  
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x007A) { # a..z  
           $self->{current_token} = {type => 'start tag',  
                             tag_name => chr ($self->{next_input_character})};  
           $self->{state} = 'tag name';  
           !!!next-input-character;  
           redo A;  
         } elsif ($self->{next_input_character} == 0x003E) { # >  
           !!!parse-error (type => 'empty start tag');  
           $self->{state} = 'data';  
           !!!next-input-character;  
   
           !!!emit ({type => 'character', data => '<>'});  
   
           redo A;  
         } elsif ($self->{next_input_character} == 0x003F) { # ?  
           !!!parse-error (type => 'pio');  
           $self->{state} = 'bogus comment';  
           ## $self->{next_input_character} is intentionally left as is  
           redo A;  
         } else {  
           !!!parse-error (type => 'bare stago');  
           $self->{state} = 'data';  
           ## reconsume  
419    
420            !!!emit ({type => 'character', data => '<'});      ## Step 3
421        if ($byte_buffer =~ /^\xFE\xFF/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
436          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
437          ($char_stream, $e_status) = $charset->get_decode_handle
438              ($byte_stream, allow_error_reporting => 1,
439               allow_fallback => 1, byte_buffer => \$byte_buffer);
440          $self->{confident} = 1;
441          last SNIFFING;
442        }
443    
444            redo A;      ## Step 4
445          }      ## TODO: <meta charset>
       } else {  
         die "$0: $self->{content_model_flag}: Unknown content model flag";  
       }  
     } elsif ($self->{state} eq 'close tag open') {  
       if ($self->{content_model_flag} eq 'RCDATA' or  
           $self->{content_model_flag} eq 'CDATA') {  
         my @next_char;  
         TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
           push @next_char, $self->{next_input_character};  
           my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
           my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
           if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {  
             !!!next-input-character;  
             next TAGNAME;  
           } else {  
             !!!parse-error (type => 'unmatched end tag');  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = 'data';  
446    
447              !!!emit ({type => 'character', data => '</'});      ## Step 5
448        ## TODO: from history
449    
450              redo A;      ## Step 6
451            }      require Whatpm::Charset::UniversalCharDet;
452          }      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
453          push @next_char, $self->{next_input_character};          ($byte_buffer);
454            if (defined $charset_name) {
455          unless ($self->{next_input_character} == 0x0009 or # HT        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
456                  $self->{next_input_character} == 0x000A or # LF  
457                  $self->{next_input_character} == 0x000B or # VT        require Whatpm::Charset::DecodeHandle;
458                  $self->{next_input_character} == 0x000C or # FF        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
459                  $self->{next_input_character} == 0x0020 or # SP            ($byte_stream);
460                  $self->{next_input_character} == 0x003E or # >        ($char_stream, $e_status) = $charset->get_decode_handle
461                  $self->{next_input_character} == 0x002F or # /            ($buffer, allow_error_reporting => 1,
462                  $self->{next_input_character} == 0x003C or # <             allow_fallback => 1, byte_buffer => \$byte_buffer);
463                  $self->{next_input_character} == -1) {        if ($char_stream) {
464            !!!parse-error (type => 'unmatched end tag');          $buffer->{buffer} = $byte_buffer;
465            $self->{next_input_character} = shift @next_char; # reconsume          !!!parse-error (type => 'sniffing:chardet',
466            !!!back-next-input-character (@next_char);                          text => $charset_name,
467            $self->{state} = 'data';                          level => $self->{level}->{info},
468                            layer => 'encode',
469            !!!emit ({type => 'character', data => '</'});                          line => 1, column => 1);
470            $self->{confident} = 0;
471            redo A;          last SNIFFING;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
         }  
472        }        }
473              }
       if (0x0041 <= $self->{next_input_character} and  
           $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_token} = {type => 'end tag',  
                           tag_name => chr ($self->{next_input_character} + 0x0020)};  
         $self->{state} = 'tag name';  
         !!!next-input-character;  
         redo A;  
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
         $self->{current_token} = {type => 'end tag',  
                           tag_name => chr ($self->{next_input_character})};  
         $self->{state} = 'tag name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'empty end tag');  
         $self->{state} = 'data';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'bare etago');  
         $self->{state} = 'data';  
         # reconsume  
   
         !!!emit ({type => 'character', data => '</'});  
   
         redo A;  
       } else {  
         !!!parse-error (type => 'bogus end tag');  
         $self->{state} = 'bogus comment';  
         ## $self->{next_input_character} is intentionally left as is  
         redo A;  
       }  
     } elsif ($self->{state} eq 'tag name') {  
       if ($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) { # SP  
         $self->{state} = 'before attribute name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);  
           # start tag or end tag  
         ## Stay in this state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } elsif ($self->{next_input_character} == 0x002F) { # /  
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } else {  
         $self->{current_token}->{tag_name} .= chr $self->{next_input_character};  
           # start tag or end tag  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'before attribute name') {  
       if ($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) { # SP  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),  
                               value => ''};  
         $self->{state} = 'attribute name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x002F) { # /  
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } else {  
         $self->{current_attribute} = {name => chr ($self->{next_input_character}),  
                               value => ''};  
         $self->{state} = 'attribute name';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'attribute name') {  
       my $before_leave = sub {  
         if (exists $self->{current_token}->{attributes} # start tag or end tag  
             ->{$self->{current_attribute}->{name}}) { # MUST  
           !!!parse-error (type => 'dupulicate attribute');  
           ## Discard $self->{current_attribute} # MUST  
         } else {  
           $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}  
             = $self->{current_attribute};  
         }  
       }; # $before_leave  
   
       if ($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) { # SP  
         $before_leave->();  
         $self->{state} = 'after attribute name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003D) { # =  
         $before_leave->();  
         $self->{state} = 'before attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $before_leave->();  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x002F) { # /  
         $before_leave->();  
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         $before_leave->();  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } else {  
         $self->{current_attribute}->{name} .= chr ($self->{next_input_character});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'after attribute name') {  
       if ($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) { # SP  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003D) { # =  
         $self->{state} = 'before attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } elsif (0x0041 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x005A) { # A..Z  
         $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),  
                               value => ''};  
         $self->{state} = 'attribute name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x002F) { # /  
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         # reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } else {  
         $self->{current_attribute} = {name => chr ($self->{next_input_character}),  
                               value => ''};  
         $self->{state} = 'attribute name';  
         !!!next-input-character;  
         redo A;          
       }  
     } elsif ($self->{state} eq 'before attribute value') {  
       if ($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) { # SP        
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0022) { # "  
         $self->{state} = 'attribute value (double-quoted)';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0026) { # &  
         $self->{state} = 'attribute value (unquoted)';  
         ## reconsume  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0027) { # '  
         $self->{state} = 'attribute value (single-quoted)';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } else {  
         $self->{current_attribute}->{value} .= chr ($self->{next_input_character});  
         $self->{state} = 'attribute value (unquoted)';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'attribute value (double-quoted)') {  
       if ($self->{next_input_character} == 0x0022) { # "  
         $self->{state} = 'before attribute name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0026) { # &  
         $self->{last_attribute_value_state} = 'attribute value (double-quoted)';  
         $self->{state} = 'entity in attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed attribute value');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
   
         redo A;  
       } else {  
         $self->{current_attribute}->{value} .= chr ($self->{next_input_character});  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'attribute value (single-quoted)') {  
       if ($self->{next_input_character} == 0x0027) { # '  
         $self->{state} = 'before attribute name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x0026) { # &  
         $self->{last_attribute_value_state} = 'attribute value (single-quoted)';  
         $self->{state} = 'entity in attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed attribute value');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         ## reconsume  
   
         !!!emit ($self->{current_token}); # start tag or end tag  
         undef $self->{current_token};  
474    
475          redo A;      ## Step 7: default
476        } else {      ## TODO: Make this configurable.
477          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});      $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
478          ## Stay in the state          ## NOTE: We choose |windows-1252| here, since |utf-8| should be
479          !!!next-input-character;          ## detectable in the step 6.
480          redo A;      require Whatpm::Charset::DecodeHandle;
481        }      $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
482      } elsif ($self->{state} eq 'attribute value (unquoted)') {          ($byte_stream);
483        if ($self->{next_input_character} == 0x0009 or # HT      ($char_stream, $e_status)
484            $self->{next_input_character} == 0x000A or # LF          = $charset->get_decode_handle ($buffer,
485            $self->{next_input_character} == 0x000B or # HT                                         allow_error_reporting => 1,
486            $self->{next_input_character} == 0x000C or # FF                                         allow_fallback => 1,
487            $self->{next_input_character} == 0x0020) { # SP                                         byte_buffer => \$byte_buffer);
488          $self->{state} = 'before attribute name';      $buffer->{buffer} = $byte_buffer;
489          !!!next-input-character;      !!!parse-error (type => 'sniffing:default',
490          redo A;                      text => 'windows-1252',
491        } elsif ($self->{next_input_character} == 0x0026) { # &                      level => $self->{level}->{info},
492          $self->{last_attribute_value_state} = 'attribute value (unquoted)';                      line => 1, column => 1,
493          $self->{state} = 'entity in attribute value';                      layer => 'encode');
494          !!!next-input-character;      $self->{confident} = 0;
495          redo A;    } # SNIFFING
496        } elsif ($self->{next_input_character} == 0x003E) { # >  
497          if ($self->{current_token}->{type} eq 'start tag') {    if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
498            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};      $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
499          } elsif ($self->{current_token}->{type} eq 'end tag') {      !!!parse-error (type => 'chardecode:fallback',
500            $self->{content_model_flag} = 'PCDATA'; # MUST                      #text => $self->{input_encoding},
501            if ($self->{current_token}->{attributes}) {                      level => $self->{level}->{uncertain},
502              !!!parse-error (type => 'end tag attribute');                      line => 1, column => 1,
503            }                      layer => 'encode');
504          } else {    } elsif (not ($e_status &
505            die "$0: $self->{current_token}->{type}: Unknown token type";                  Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
506          }      $self->{input_encoding} = $charset->get_iana_name;
507          $self->{state} = 'data';      !!!parse-error (type => 'chardecode:no error',
508          !!!next-input-character;                      text => $self->{input_encoding},
509                        level => $self->{level}->{uncertain},
510          !!!emit ($self->{current_token}); # start tag or end tag                      line => 1, column => 1,
511          undef $self->{current_token};                      layer => 'encode');
512      } else {
513          redo A;      $self->{input_encoding} = $charset->get_iana_name;
514        } elsif ($self->{next_input_character} == 0x003C or # <    }
                $self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed tag');  
         if ($self->{current_token}->{type} eq 'start tag') {  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
           $self->{content_model_flag} = 'PCDATA'; # MUST  
           if ($self->{current_token}->{attributes}) {  
             !!!parse-error (type => 'end tag attribute');  
           }  
         } else {  
           die "$0: $self->{current_token}->{type}: Unknown token type";  
         }  
         $self->{state} = 'data';  
         ## reconsume  
515    
516          !!!emit ($self->{current_token}); # start tag or end tag    $self->{change_encoding} = sub {
517          undef $self->{current_token};      my $self = shift;
518        $charset_name = shift;
519        my $token = shift;
520    
521          redo A;      $charset = Message::Charset::Info->get_by_html_name ($charset_name);
522        } else {      ($char_stream, $e_status) = $charset->get_decode_handle
523          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
524          ## Stay in the state           byte_buffer => \ $buffer->{buffer});
525          !!!next-input-character;      
526          redo A;      if ($char_stream) { # if supported
527        }        ## "Change the encoding" algorithm:
     } elsif ($self->{state} eq 'entity in attribute value') {  
       my $token = $self->_tokenize_attempt_to_consume_an_entity;  
528    
529        unless (defined $token) {        ## Step 1    
530          $self->{current_attribute}->{value} .= '&';        if ($charset->{category} &
531        } else {            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
532          $self->{current_attribute}->{value} .= $token->{data};          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
533          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ($char_stream, $e_status) = $charset->get_decode_handle
534                ($byte_stream,
535                 byte_buffer => \ $buffer->{buffer});
536        }        }
537          $charset_name = $charset->get_iana_name;
       $self->{state} = $self->{last_attribute_value_state};  
       # next-input-character is already done  
       redo A;  
     } elsif ($self->{state} eq 'bogus comment') {  
       ## (only happen if PCDATA state)  
538                
539        my $token = {type => 'comment', data => ''};        ## Step 2
540          if (defined $self->{input_encoding} and
541        BC: {            $self->{input_encoding} eq $charset_name) {
542          if ($self->{next_input_character} == 0x003E) { # >          !!!parse-error (type => 'charset label:matching',
543            $self->{state} = 'data';                          text => $charset_name,
544            !!!next-input-character;                          level => $self->{level}->{info});
545            $self->{confident} = 1;
546            !!!emit ($token);          return;
   
           redo A;  
         } elsif ($self->{next_input_character} == -1) {  
           $self->{state} = 'data';  
           ## reconsume  
   
           !!!emit ($token);  
   
           redo A;  
         } else {  
           $token->{data} .= chr ($self->{next_input_character});  
           !!!next-input-character;  
           redo BC;  
         }  
       } # BC  
     } elsif ($self->{state} eq 'markup declaration open') {  
       ## (only happen if PCDATA state)  
   
       my @next_char;  
       push @next_char, $self->{next_input_character};  
         
       if ($self->{next_input_character} == 0x002D) { # -  
         !!!next-input-character;  
         push @next_char, $self->{next_input_character};  
         if ($self->{next_input_character} == 0x002D) { # -  
           $self->{current_token} = {type => 'comment', data => ''};  
           $self->{state} = 'comment';  
           !!!next-input-character;  
           redo A;  
         }  
       } elsif ($self->{next_input_character} == 0x0044 or # D  
                $self->{next_input_character} == 0x0064) { # d  
         !!!next-input-character;  
         push @next_char, $self->{next_input_character};  
         if ($self->{next_input_character} == 0x004F or # O  
             $self->{next_input_character} == 0x006F) { # o  
           !!!next-input-character;  
           push @next_char, $self->{next_input_character};  
           if ($self->{next_input_character} == 0x0043 or # C  
               $self->{next_input_character} == 0x0063) { # c  
             !!!next-input-character;  
             push @next_char, $self->{next_input_character};  
             if ($self->{next_input_character} == 0x0054 or # T  
                 $self->{next_input_character} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_input_character};  
               if ($self->{next_input_character} == 0x0059 or # Y  
                   $self->{next_input_character} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_input_character};  
                 if ($self->{next_input_character} == 0x0050 or # P  
                     $self->{next_input_character} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_input_character};  
                   if ($self->{next_input_character} == 0x0045 or # E  
                       $self->{next_input_character} == 0x0065) { # e  
                     ## ISSUE: What a stupid code this is!  
                     $self->{state} = 'DOCTYPE';  
                     !!!next-input-character;  
                     redo A;  
                   }  
                 }  
               }  
             }  
           }  
         }  
547        }        }
548    
549        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'charset label detected',
550        $self->{next_input_character} = shift @next_char;                        text => $self->{input_encoding},
551        !!!back-next-input-character (@next_char);                        value => $charset_name,
552        $self->{state} = 'bogus comment';                        level => $self->{level}->{warn},
553        redo A;                        token => $token);
554                
555        ## ISSUE: typos in spec: chacacters, is is a parse error        ## Step 3
556        ## 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?        # if (can) {
557      } elsif ($self->{state} eq 'comment') {          ## change the encoding on the fly.
558        if ($self->{next_input_character} == 0x002D) { # -          #$self->{confident} = 1;
559          $self->{state} = 'comment dash';          #return;
560          !!!next-input-character;        # }
561          redo A;        
562        } elsif ($self->{next_input_character} == -1) {        ## Step 4
563          !!!parse-error (type => 'unclosed comment');        throw Whatpm::HTML::RestartParser ();
564          $self->{state} = 'data';      }
565          ## reconsume    }; # $self->{change_encoding}
   
         !!!emit ($self->{current_token}); # comment  
         undef $self->{current_token};  
   
         redo A;  
       } else {  
         $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'comment dash') {  
       if ($self->{next_input_character} == 0x002D) { # -  
         $self->{state} = 'comment end';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = 'data';  
         ## reconsume  
566    
567          !!!emit ($self->{current_token}); # comment    my $char_onerror = sub {
568          undef $self->{current_token};      my (undef, $type, %opt) = @_;
569        !!!parse-error (layer => 'encode',
570                        line => $self->{line}, column => $self->{column} + 1,
571                        %opt, type => $type);
572        if ($opt{octets}) {
573          ${$opt{octets}} = "\x{FFFD}"; # relacement character
574        }
575      };
576    
577          redo A;    my $wrapped_char_stream = $get_wrapper->($char_stream);
578        } else {    $wrapped_char_stream->onerror ($char_onerror);
         $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment  
         $self->{state} = 'comment';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'comment end') {  
       if ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # comment  
         undef $self->{current_token};  
   
         redo A;  
       } elsif ($self->{next_input_character} == 0x002D) { # -  
         !!!parse-error (type => 'dash in comment');  
         $self->{current_token}->{data} .= '-'; # comment  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed comment');  
         $self->{state} = 'data';  
         ## reconsume  
579    
580          !!!emit ($self->{current_token}); # comment    my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
581          undef $self->{current_token};    my $return;
582      try {
583        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
584      } catch Whatpm::HTML::RestartParser with {
585        ## NOTE: Invoked after {change_encoding}.
586    
587        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
588          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
589          !!!parse-error (type => 'chardecode:fallback',
590                          level => $self->{level}->{uncertain},
591                          #text => $self->{input_encoding},
592                          line => 1, column => 1,
593                          layer => 'encode');
594        } elsif (not ($e_status &
595                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
596          $self->{input_encoding} = $charset->get_iana_name;
597          !!!parse-error (type => 'chardecode:no error',
598                          text => $self->{input_encoding},
599                          level => $self->{level}->{uncertain},
600                          line => 1, column => 1,
601                          layer => 'encode');
602        } else {
603          $self->{input_encoding} = $charset->get_iana_name;
604        }
605        $self->{confident} = 1;
606    
607          redo A;      $wrapped_char_stream = $get_wrapper->($char_stream);
608        } else {      $wrapped_char_stream->onerror ($char_onerror);
         !!!parse-error (type => 'dash in comment');  
         $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment  
         $self->{state} = 'comment';  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'DOCTYPE') {  
       if ($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) { # SP  
         $self->{state} = 'before DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
       } else {  
         !!!parse-error (type => 'no space before DOCTYPE name');  
         $self->{state} = 'before DOCTYPE name';  
         ## reconsume  
         redo A;  
       }  
     } elsif ($self->{state} eq 'before DOCTYPE name') {  
       if ($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) { # SP  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'no DOCTYPE name');  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ({type => 'DOCTYPE', name => '', error => 1});  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'no DOCTYPE name');  
         $self->{state} = 'data';  
         ## reconsume  
609    
610          !!!emit ({type => 'DOCTYPE', name => '', error => 1});      $return = $self->parse_char_stream ($wrapped_char_stream, @args);
611      };
612      return $return;
613    } # parse_byte_stream
614    
615          redo A;  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
616        } else {  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
617          $self->{current_token} = {type => 'DOCTYPE',  ## the encoding layer and the HTML layer does not ignore any U+FEFF,
618                            name => chr ($self->{next_input_character}),  ## because the core part of our HTML parser expects a string of character,
619                            error => 1};  ## not a string of bytes or code units or anything which might contain a BOM.
620  ## ISSUE: "Set the token's name name to the" in the spec  ## Therefore, any parser interface that accepts a string of bytes,
621          $self->{state} = 'DOCTYPE name';  ## such as |parse_byte_string| in this module, must ensure that it does
622          !!!next-input-character;  ## strip the BOM and never strip any ZWNBSP.
         redo A;  
       }  
     } elsif ($self->{state} eq 'DOCTYPE name') {  
       if ($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) { # SP  
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
         $self->{state} = 'after DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
         undef $self->{current_token};  
   
         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;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
         $self->{state} = 'data';  
         ## reconsume  
623    
624          !!!emit ($self->{current_token});  sub parse_char_string ($$$;$$) {
625          undef $self->{current_token};    #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
626      my $self = shift;
627      my $s = ref $_[0] ? $_[0] : \($_[0]);
628      require Whatpm::Charset::DecodeHandle;
629      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
630      return $self->parse_char_stream ($input, @_[1..$#_]);
631    } # parse_char_string
632    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
633    
634    sub parse_char_stream ($$$;$$) {
635      my $self = ref $_[0] ? shift : shift->new;
636      my $input = $_[0];
637      $self->{document} = $_[1];
638      @{$self->{document}->child_nodes} = ();
639    
640          redo A;    ## NOTE: |set_inner_html| copies most of this method's code
       } else {  
         $self->{current_token}->{name}  
           .= chr ($self->{next_input_character}); # DOCTYPE  
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       }  
     } elsif ($self->{state} eq 'after DOCTYPE name') {  
       if ($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) { # SP  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
         undef $self->{current_token};  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{state} = 'data';  
         ## reconsume  
641    
642          !!!emit ($self->{current_token}); # DOCTYPE    $self->{confident} = 1 unless exists $self->{confident};
643          undef $self->{current_token};    $self->{document}->input_encoding ($self->{input_encoding})
644          if defined $self->{input_encoding};
645    ## TODO: |{input_encoding}| is needless?
646    
647      $self->{line_prev} = $self->{line} = 1;
648      $self->{column_prev} = -1;
649      $self->{column} = 0;
650      $self->{set_nc} = sub {
651        my $self = shift;
652    
653          redo A;      my $char = '';
654        } else {      if (defined $self->{next_nc}) {
655          !!!parse-error (type => 'string after DOCTYPE name');        $char = $self->{next_nc};
656          $self->{current_token}->{error} = 1; # DOCTYPE        delete $self->{next_nc};
657          $self->{state} = 'bogus DOCTYPE';        $self->{nc} = ord $char;
658          !!!next-input-character;      } else {
659          redo A;        $self->{char_buffer} = '';
660        }        $self->{char_buffer_pos} = 0;
     } elsif ($self->{state} eq 'bogus DOCTYPE') {  
       if ($self->{next_input_character} == 0x003E) { # >  
         $self->{state} = 'data';  
         !!!next-input-character;  
   
         !!!emit ($self->{current_token}); # DOCTYPE  
         undef $self->{current_token};  
   
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
         !!!parse-error (type => 'unclosed DOCTYPE');  
         $self->{state} = 'data';  
         ## reconsume  
661    
662          !!!emit ($self->{current_token}); # DOCTYPE        my $count = $input->manakai_read_until
663          undef $self->{current_token};           ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
664          if ($count) {
665            $self->{line_prev} = $self->{line};
666            $self->{column_prev} = $self->{column};
667            $self->{column}++;
668            $self->{nc}
669                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
670            return;
671          }
672    
673          redo A;        if ($input->read ($char, 1)) {
674            $self->{nc} = ord $char;
675        } else {        } else {
676          ## Stay in the state          $self->{nc} = -1;
677          !!!next-input-character;          return;
         redo A;  
678        }        }
     } else {  
       die "$0: $self->{state}: Unknown state";  
679      }      }
   } # A    
   
   die "$0: _get_next_token: unexpected case";  
 } # _get_next_token  
680    
681  sub _tokenize_attempt_to_consume_an_entity ($) {      ($self->{line_prev}, $self->{column_prev})
682    my $self = shift;          = ($self->{line}, $self->{column});
683          $self->{column}++;
684    if ($self->{next_input_character} == 0x0023) { # #      
685      !!!next-input-character;      if ($self->{nc} == 0x000A) { # LF
686      if ($self->{next_input_character} == 0x0078 or # x        !!!cp ('j1');
687          $self->{next_input_character} == 0x0058) { # X        $self->{line}++;
688        my $num;        $self->{column} = 0;
689        X: {      } elsif ($self->{nc} == 0x000D) { # CR
690          my $x_char = $self->{next_input_character};        !!!cp ('j2');
691          !!!next-input-character;  ## TODO: support for abort/streaming
692          if (0x0030 <= $self->{next_input_character} and        my $next = '';
693              $self->{next_input_character} <= 0x0039) { # 0..9        if ($input->read ($next, 1) and $next ne "\x0A") {
694            $num ||= 0;          $self->{next_nc} = $next;
695            $num *= 0x10;        }
696            $num += $self->{next_input_character} - 0x0030;        $self->{nc} = 0x000A; # LF # MUST
697            redo X;        $self->{line}++;
698          } elsif (0x0061 <= $self->{next_input_character} and        $self->{column} = 0;
699                   $self->{next_input_character} <= 0x0066) { # a..f      } elsif ($self->{nc} == 0x0000) { # NULL
700            ## ISSUE: the spec says U+0078, which is apparently incorrect        !!!cp ('j4');
701            $num ||= 0;        !!!parse-error (type => 'NULL');
702            $num *= 0x10;        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
703            $num += $self->{next_input_character} - 0x0060 + 9;      }
704            redo X;    };
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           ## ISSUE: the spec says U+0058, which is apparently incorrect  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $num) { # no hexadecimal digit  
           !!!parse-error (type => 'bare hcro');  
           $self->{next_input_character} = 0x0023; # #  
           !!!back-next-input-character ($x_char);  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
         } else {  
           !!!parse-error (type => 'no refc');  
         }  
   
         ## TODO: check the definition for |a valid Unicode character|.  
         ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>  
         if ($num > 1114111 or $num == 0) {  
           $num = 0xFFFD; # REPLACEMENT CHARACTER  
           ## ISSUE: Why this is not an error?  
         } elsif (0x80 <= $num and $num <= 0x9F) {  
           !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);  
           $num = $c1_entity_char->{$num};  
         }  
   
         return {type => 'character', data => chr $num};  
       } # X  
     } elsif (0x0030 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x0039) { # 0..9  
       my $code = $self->{next_input_character} - 0x0030;  
       !!!next-input-character;  
         
       while (0x0030 <= $self->{next_input_character} and  
                 $self->{next_input_character} <= 0x0039) { # 0..9  
         $code *= 10;  
         $code += $self->{next_input_character} - 0x0030;  
           
         !!!next-input-character;  
       }  
705    
706        if ($self->{next_input_character} == 0x003B) { # ;    $self->{read_until} = sub {
707          !!!next-input-character;      #my ($scalar, $specials_range, $offset) = @_;
708        return 0 if defined $self->{next_nc};
709    
710        my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
711        my $offset = $_[2] || 0;
712    
713        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
714          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
715          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
716            substr ($_[0], $offset)
717                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
718            my $count = $+[0] - $-[0];
719            if ($count) {
720              $self->{column} += $count;
721              $self->{char_buffer_pos} += $count;
722              $self->{line_prev} = $self->{line};
723              $self->{column_prev} = $self->{column} - 1;
724              $self->{nc} = -1;
725            }
726            return $count;
727        } else {        } else {
728          !!!parse-error (type => 'no refc');          return 0;
729        }        }
   
       ## TODO: check the definition for |a valid Unicode character|.  
       if ($code > 1114111 or $code == 0) {  
         $code = 0xFFFD; # REPLACEMENT CHARACTER  
         ## ISSUE: Why this is not an error?  
       } elsif (0x80 <= $code and $code <= 0x9F) {  
         !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);  
         $code = $c1_entity_char->{$code};  
       }  
         
       return {type => 'character', data => chr $code};  
730      } else {      } else {
731        !!!parse-error (type => 'bare nero');        my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
732        !!!back-next-input-character ($self->{next_input_character});        if ($count) {
733        $self->{next_input_character} = 0x0023; # #          $self->{column} += $count;
734        return undef;          $self->{line_prev} = $self->{line};
735      }          $self->{column_prev} = $self->{column} - 1;
736    } elsif ((0x0041 <= $self->{next_input_character} and          $self->{nc} = -1;
             $self->{next_input_character} <= 0x005A) or  
            (0x0061 <= $self->{next_input_character} and  
             $self->{next_input_character} <= 0x007A)) {  
     my $entity_name = chr $self->{next_input_character};  
     !!!next-input-character;  
   
     my $value = $entity_name;  
     my $match;  
   
     while (length $entity_name < 10 and  
            ## NOTE: Some number greater than the maximum length of entity name  
            ((0x0041 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x005A) or  
             (0x0061 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x007A) or  
             (0x0030 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x0039))) {  
       $entity_name .= chr $self->{next_input_character};  
       if (defined $entity_char->{$entity_name}) {  
         $value = $entity_char->{$entity_name};  
         $match = 1;  
       } else {  
         $value .= chr $self->{next_input_character};  
737        }        }
738        !!!next-input-character;        return $count;
739      }      }
740          }; # $self->{read_until}
     if ($match) {  
       if ($self->{next_input_character} == 0x003B) { # ;  
         !!!next-input-character;  
       } else {  
         !!!parse-error (type => 'refc');  
       }  
741    
742        return {type => 'character', data => $value};    my $onerror = $_[2] || sub {
743      } else {      my (%opt) = @_;
744        !!!parse-error (type => 'bare ero');      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
745        ## NOTE: No characters are consumed in the spec.      my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
746        !!!back-token ({type => 'character', data => $value});      warn "Parse error ($opt{type}) at line $line column $column\n";
747        return undef;    };
748      }    $self->{parse_error} = sub {
749        $onerror->(line => $self->{line}, column => $self->{column}, @_);
750      };
751    
752      my $char_onerror = sub {
753        my (undef, $type, %opt) = @_;
754        !!!parse-error (layer => 'encode',
755                        line => $self->{line}, column => $self->{column} + 1,
756                        %opt, type => $type);
757      }; # $char_onerror
758    
759      if ($_[3]) {
760        $input = $_[3]->($input);
761        $input->onerror ($char_onerror);
762    } else {    } else {
763      ## no characters are consumed      $input->onerror ($char_onerror) unless defined $input->onerror;
     !!!parse-error (type => 'bare ero');  
     return undef;  
764    }    }
765  } # _tokenize_attempt_to_consume_an_entity  
766      $self->_initialize_tokenizer;
767      $self->_initialize_tree_constructor;
768      $self->_construct_tree;
769      $self->_terminate_tree_constructor;
770    
771      delete $self->{parse_error}; # remove loop
772    
773      return $self->{document};
774    } # parse_char_stream
775    
776    sub new ($) {
777      my $class = shift;
778      my $self = bless {
779        level => {must => 'm',
780                  should => 's',
781                  warn => 'w',
782                  info => 'i',
783                  uncertain => 'u'},
784      }, $class;
785      $self->{set_nc} = sub {
786        $self->{nc} = -1;
787      };
788      $self->{parse_error} = sub {
789        #
790      };
791      $self->{change_encoding} = sub {
792        # if ($_[0] is a supported encoding) {
793        #   run "change the encoding" algorithm;
794        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
795        # }
796      };
797      $self->{application_cache_selection} = sub {
798        #
799      };
800      return $self;
801    } # new
802    
803    ## Insertion modes
804    
805    sub AFTER_HTML_IMS () { 0b100 }
806    sub HEAD_IMS ()       { 0b1000 }
807    sub BODY_IMS ()       { 0b10000 }
808    sub BODY_TABLE_IMS () { 0b100000 }
809    sub TABLE_IMS ()      { 0b1000000 }
810    sub ROW_IMS ()        { 0b10000000 }
811    sub BODY_AFTER_IMS () { 0b100000000 }
812    sub FRAME_IMS ()      { 0b1000000000 }
813    sub SELECT_IMS ()     { 0b10000000000 }
814    #sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 } # see Whatpm::HTML::Tokenizer
815        ## NOTE: "in foreign content" insertion mode is special; it is combined
816        ## with the secondary insertion mode.  In this parser, they are stored
817        ## together in the bit-or'ed form.
818    sub IN_CDATA_RCDATA_IM () { 0b1000000000000 }
819        ## NOTE: "in CDATA/RCDATA" insertion mode is also special; it is
820        ## combined with the original insertion mode.  In thie parser,
821        ## they are stored together in the bit-or'ed form.
822    
823    sub IM_MASK () { 0b11111111111 }
824    
825    ## NOTE: "initial" and "before html" insertion modes have no constants.
826    
827    ## NOTE: "after after body" insertion mode.
828    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
829    
830    ## NOTE: "after after frameset" insertion mode.
831    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
832    
833    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
834    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
835    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
836    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
837    sub IN_BODY_IM () { BODY_IMS }
838    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
839    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
840    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
841    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
842    sub IN_TABLE_IM () { TABLE_IMS }
843    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
844    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
845    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
846    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
847    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
848    sub IN_COLUMN_GROUP_IM () { 0b10 }
849    
850  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
851    my $self = shift;    my $self = shift;
# Line 1637  sub _initialize_tree_constructor ($) { Line 853  sub _initialize_tree_constructor ($) {
853    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
854    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
855    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
856    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
857      $self->{document}->set_user_data (manakai_source_line => 1);
858      $self->{document}->set_user_data (manakai_source_column => 1);
859  } # _initialize_tree_constructor  } # _initialize_tree_constructor
860    
861  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1657  sub _construct_tree ($) { Line 875  sub _construct_tree ($) {
875    ## When an interactive UA render the $self->{document} available    ## When an interactive UA render the $self->{document} available
876    ## to the user, or when it begin accepting user input, are    ## to the user, or when it begin accepting user input, are
877    ## not defined.    ## not defined.
   
   ## Append a character: collect it and all subsequent consecutive  
   ## characters and insert one Text node whose data is concatenation  
   ## of all those characters. # MUST  
878        
879    !!!next-token;    !!!next-token;
880    
   $self->{insertion_mode} = 'before head';  
881    undef $self->{form_element};    undef $self->{form_element};
882    undef $self->{head_element};    undef $self->{head_element};
883      undef $self->{head_element_inserted};
884    $self->{open_elements} = [];    $self->{open_elements} = [];
885    undef $self->{inner_html_node};    undef $self->{inner_html_node};
886      undef $self->{ignore_newline};
887    
888      ## NOTE: The "initial" insertion mode.
889    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
890    
891      ## NOTE: The "before html" insertion mode.
892    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
893      $self->{insertion_mode} = BEFORE_HEAD_IM;
894    
895      ## NOTE: The "before head" insertion mode and so on.
896    $self->_tree_construction_main;    $self->_tree_construction_main;
897  } # _construct_tree  } # _construct_tree
898    
899  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
900    my $self = shift;    my $self = shift;
901    B: {  
902        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
903          if ($token->{error}) {  
904            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
905            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
906          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
907          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
908            ($token->{name});        ## language.
909          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
910          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
911          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
912          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
913          return;            defined $token->{sysid}) {
914        } elsif ({          !!!cp ('t1');
915                  comment => 1,          !!!parse-error (type => 'not HTML5', token => $token);
916                  'start tag' => 1,        } elsif ($doctype_name ne 'HTML') {
917                  'end tag' => 1,          !!!cp ('t2');
918                  'end-of-file' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
919                 }->{$token->{type}}) {        } elsif (defined $token->{pubid}) {
920          ## ISSUE: Spec currently left this case undefined.          if ($token->{pubid} eq 'XSLT-compat') {
921          !!!parse-error (type => 'missing DOCTYPE');            !!!cp ('t1.2');
922          #$phase = 'root element';            !!!parse-error (type => 'XSLT-compat', token => $token,
923          ## reprocess                            level => $self->{level}->{should});
924          #redo B;          } else {
925          return;            !!!parse-error (type => 'not HTML5', token => $token);
926        } elsif ($token->{type} eq 'character') {          }
927          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } else {
928            $self->{document}->manakai_append_text ($1);          !!!cp ('t3');
929            ## ISSUE: DOM3 Core does not allow Document > Text          #
930            unless (length $token->{data}) {        }
931              ## Stay in the phase        
932              !!!next-token;        my $doctype = $self->{document}->create_document_type_definition
933              redo B;          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
934          ## NOTE: Default value for both |public_id| and |system_id| attributes
935          ## are empty strings, so that we don't set any value in missing cases.
936          $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
937          $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
938          ## NOTE: Other DocumentType attributes are null or empty lists.
939          ## ISSUE: internalSubset = null??
940          $self->{document}->append_child ($doctype);
941          
942          if ($token->{quirks} or $doctype_name ne 'HTML') {
943            !!!cp ('t4');
944            $self->{document}->manakai_compat_mode ('quirks');
945          } elsif (defined $token->{pubid}) {
946            my $pubid = $token->{pubid};
947            $pubid =~ tr/a-z/A-z/;
948            my $prefix = [
949              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
950              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
951              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
952              "-//IETF//DTD HTML 2.0 LEVEL 1//",
953              "-//IETF//DTD HTML 2.0 LEVEL 2//",
954              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
955              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
956              "-//IETF//DTD HTML 2.0 STRICT//",
957              "-//IETF//DTD HTML 2.0//",
958              "-//IETF//DTD HTML 2.1E//",
959              "-//IETF//DTD HTML 3.0//",
960              "-//IETF//DTD HTML 3.2 FINAL//",
961              "-//IETF//DTD HTML 3.2//",
962              "-//IETF//DTD HTML 3//",
963              "-//IETF//DTD HTML LEVEL 0//",
964              "-//IETF//DTD HTML LEVEL 1//",
965              "-//IETF//DTD HTML LEVEL 2//",
966              "-//IETF//DTD HTML LEVEL 3//",
967              "-//IETF//DTD HTML STRICT LEVEL 0//",
968              "-//IETF//DTD HTML STRICT LEVEL 1//",
969              "-//IETF//DTD HTML STRICT LEVEL 2//",
970              "-//IETF//DTD HTML STRICT LEVEL 3//",
971              "-//IETF//DTD HTML STRICT//",
972              "-//IETF//DTD HTML//",
973              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
974              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
975              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
976              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
977              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
978              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
979              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
980              "-//NETSCAPE COMM. CORP.//DTD HTML//",
981              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
982              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
983              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
984              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
985              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
986              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
987              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
988              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
989              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
990              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
991              "-//W3C//DTD HTML 3 1995-03-24//",
992              "-//W3C//DTD HTML 3.2 DRAFT//",
993              "-//W3C//DTD HTML 3.2 FINAL//",
994              "-//W3C//DTD HTML 3.2//",
995              "-//W3C//DTD HTML 3.2S DRAFT//",
996              "-//W3C//DTD HTML 4.0 FRAMESET//",
997              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
998              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
999              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
1000              "-//W3C//DTD W3 HTML//",
1001              "-//W3O//DTD W3 HTML 3.0//",
1002              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
1003              "-//WEBTECHS//DTD MOZILLA HTML//",
1004            ]; # $prefix
1005            my $match;
1006            for (@$prefix) {
1007              if (substr ($prefix, 0, length $_) eq $_) {
1008                $match = 1;
1009                last;
1010              }
1011            }
1012            if ($match or
1013                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
1014                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
1015                $pubid eq "HTML") {
1016              !!!cp ('t5');
1017              $self->{document}->manakai_compat_mode ('quirks');
1018            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
1019                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
1020              if (defined $token->{sysid}) {
1021                !!!cp ('t6');
1022                $self->{document}->manakai_compat_mode ('quirks');
1023              } else {
1024                !!!cp ('t7');
1025                $self->{document}->manakai_compat_mode ('limited quirks');
1026            }            }
1027            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
1028                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
1029              !!!cp ('t8');
1030              $self->{document}->manakai_compat_mode ('limited quirks');
1031            } else {
1032              !!!cp ('t9');
1033            }
1034          } else {
1035            !!!cp ('t10');
1036          }
1037          if (defined $token->{sysid}) {
1038            my $sysid = $token->{sysid};
1039            $sysid =~ tr/A-Z/a-z/;
1040            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1041              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
1042              ## marked as quirks.
1043              $self->{document}->manakai_compat_mode ('quirks');
1044              !!!cp ('t11');
1045            } else {
1046              !!!cp ('t12');
1047            }
1048          } else {
1049            !!!cp ('t13');
1050          }
1051          
1052          ## Go to the "before html" insertion mode.
1053          !!!next-token;
1054          return;
1055        } elsif ({
1056                  START_TAG_TOKEN, 1,
1057                  END_TAG_TOKEN, 1,
1058                  END_OF_FILE_TOKEN, 1,
1059                 }->{$token->{type}}) {
1060          !!!cp ('t14');
1061          !!!parse-error (type => 'no DOCTYPE', token => $token);
1062          $self->{document}->manakai_compat_mode ('quirks');
1063          ## Go to the "before html" insertion mode.
1064          ## reprocess
1065          !!!ack-later;
1066          return;
1067        } elsif ($token->{type} == CHARACTER_TOKEN) {
1068          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1069            ## Ignore the token
1070    
1071            unless (length $token->{data}) {
1072              !!!cp ('t15');
1073              ## Stay in the insertion mode.
1074              !!!next-token;
1075              redo INITIAL;
1076            } else {
1077              !!!cp ('t16');
1078          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
1079        } else {        } else {
1080          die "$0: $token->{type}: Unknown token";          !!!cp ('t17');
1081        }        }
1082      } # B  
1083          !!!parse-error (type => 'no DOCTYPE', token => $token);
1084          $self->{document}->manakai_compat_mode ('quirks');
1085          ## Go to the "before html" insertion mode.
1086          ## reprocess
1087          return;
1088        } elsif ($token->{type} == COMMENT_TOKEN) {
1089          !!!cp ('t18');
1090          my $comment = $self->{document}->create_comment ($token->{data});
1091          $self->{document}->append_child ($comment);
1092          
1093          ## Stay in the insertion mode.
1094          !!!next-token;
1095          redo INITIAL;
1096        } else {
1097          die "$0: $token->{type}: Unknown token type";
1098        }
1099      } # INITIAL
1100    
1101      die "$0: _tree_construction_initial: This should be never reached";
1102  } # _tree_construction_initial  } # _tree_construction_initial
1103    
1104  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
1105    my $self = shift;    my $self = shift;
1106    
1107      ## NOTE: "before html" insertion mode.
1108        
1109    B: {    B: {
1110        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
1111          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
1112            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
1113          ## Ignore the token          ## Ignore the token
1114          ## Stay in the phase          ## Stay in the insertion mode.
1115          !!!next-token;          !!!next-token;
1116          redo B;          redo B;
1117        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
1118            !!!cp ('t20');
1119          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
1120          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
1121          ## Stay in the phase          ## Stay in the insertion mode.
1122          !!!next-token;          !!!next-token;
1123          redo B;          redo B;
1124        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
1125          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1126            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
1127            ## ISSUE: DOM3 Core does not allow Document > Text  
1128            unless (length $token->{data}) {            unless (length $token->{data}) {
1129              ## Stay in the phase              !!!cp ('t21');
1130                ## Stay in the insertion mode.
1131              !!!next-token;              !!!next-token;
1132              redo B;              redo B;
1133              } else {
1134                !!!cp ('t22');
1135            }            }
1136            } else {
1137              !!!cp ('t23');
1138          }          }
1139    
1140            $self->{application_cache_selection}->(undef);
1141    
1142          #          #
1143          } elsif ($token->{type} == START_TAG_TOKEN) {
1144            if ($token->{tag_name} eq 'html') {
1145              my $root_element;
1146              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
1147              $self->{document}->append_child ($root_element);
1148              push @{$self->{open_elements}},
1149                  [$root_element, $el_category->{html}];
1150    
1151              if ($token->{attributes}->{manifest}) {
1152                !!!cp ('t24');
1153                $self->{application_cache_selection}
1154                    ->($token->{attributes}->{manifest}->{value});
1155                ## ISSUE: Spec is unclear on relative references.
1156                ## According to Hixie (#whatwg 2008-03-19), it should be
1157                ## resolved against the base URI of the document in HTML
1158                ## or xml:base of the element in XHTML.
1159              } else {
1160                !!!cp ('t25');
1161                $self->{application_cache_selection}->(undef);
1162              }
1163    
1164              !!!nack ('t25c');
1165    
1166              !!!next-token;
1167              return; ## Go to the "before head" insertion mode.
1168            } else {
1169              !!!cp ('t25.1');
1170              #
1171            }
1172        } elsif ({        } elsif ({
1173                  'start tag' => 1,                  END_TAG_TOKEN, 1,
1174                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
1175                 }->{$token->{type}}) {                 }->{$token->{type}}) {
1176          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
1177          #          #
1178        } else {        } else {
1179          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
1180        }        }
1181        my $root_element; !!!create-element ($root_element, 'html');  
1182        $self->{document}->append_child ($root_element);      my $root_element;
1183        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
1184        #$phase = 'main';      $self->{document}->append_child ($root_element);
1185        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
1186        #redo B;  
1187        return;      $self->{application_cache_selection}->(undef);
1188    
1189        ## NOTE: Reprocess the token.
1190        !!!ack-later;
1191        return; ## Go to the "before head" insertion mode.
1192    } # B    } # B
1193    
1194      die "$0: _tree_construction_root_element: This should never be reached";
1195  } # _tree_construction_root_element  } # _tree_construction_root_element
1196    
1197  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1783  sub _reset_insertion_mode ($) { Line 1206  sub _reset_insertion_mode ($) {
1206            
1207      ## Step 3      ## Step 3
1208      S3: {      S3: {
1209        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
1210        if (defined $self->{inner_html_node}) {          $last = 1;
1211          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
1212              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
1213              $node = $self->{inner_html_node};
1214            } else {
1215              die "_reset_insertion_mode: t27";
1216            }
1217          }
1218          
1219          ## Step 4..14
1220          my $new_mode;
1221          if ($node->[1] & FOREIGN_EL) {
1222            !!!cp ('t28.1');
1223            ## NOTE: Strictly spaking, the line below only applies to MathML and
1224            ## SVG elements.  Currently the HTML syntax supports only MathML and
1225            ## SVG elements as foreigners.
1226            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
1227          } elsif ($node->[1] == TABLE_CELL_EL) {
1228            if ($last) {
1229              !!!cp ('t28.2');
1230            #            #
1231          } else {          } else {
1232            $node = $self->{inner_html_node};            !!!cp ('t28.3');
1233              $new_mode = IN_CELL_IM;
1234          }          }
1235          } else {
1236            !!!cp ('t28.4');
1237            $new_mode = {
1238                          select => IN_SELECT_IM,
1239                          ## NOTE: |option| and |optgroup| do not set
1240                          ## insertion mode to "in select" by themselves.
1241                          tr => IN_ROW_IM,
1242                          tbody => IN_TABLE_BODY_IM,
1243                          thead => IN_TABLE_BODY_IM,
1244                          tfoot => IN_TABLE_BODY_IM,
1245                          caption => IN_CAPTION_IM,
1246                          colgroup => IN_COLUMN_GROUP_IM,
1247                          table => IN_TABLE_IM,
1248                          head => IN_BODY_IM, # not in head!
1249                          body => IN_BODY_IM,
1250                          frameset => IN_FRAMESET_IM,
1251                         }->{$node->[0]->manakai_local_name};
1252        }        }
       
       ## Step 4..13  
       my $new_mode = {  
                       select => 'in select',  
                       td => 'in cell',  
                       th => 'in cell',  
                       tr => 'in row',  
                       tbody => 'in table body',  
                       thead => 'in table head',  
                       tfoot => 'in table foot',  
                       caption => 'in caption',  
                       colgroup => 'in column group',  
                       table => 'in table',  
                       head => 'in body', # not in head!  
                       body => 'in body',  
                       frameset => 'in frameset',  
                      }->{$node->[1]};  
1253        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
1254                
1255        ## Step 14        ## Step 15
1256        if ($node->[1] eq 'html') {        if ($node->[1] == HTML_EL) {
1257          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
1258            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
1259              $self->{insertion_mode} = BEFORE_HEAD_IM;
1260          } else {          } else {
1261            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
1262              !!!cp ('t30');
1263              $self->{insertion_mode} = AFTER_HEAD_IM;
1264          }          }
1265          return;          return;
1266          } else {
1267            !!!cp ('t31');
1268        }        }
1269                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
1270        ## Step 16        ## Step 16
1271          $self->{insertion_mode} = IN_BODY_IM and return if $last;
1272          
1273          ## Step 17
1274        $i--;        $i--;
1275        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
1276                
1277        ## Step 17        ## Step 18
1278        redo S3;        redo S3;
1279      } # S3      } # S3
1280    
1281      die "$0: _reset_insertion_mode: This line should never be reached";
1282  } # _reset_insertion_mode  } # _reset_insertion_mode
1283    
1284  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
1285    my $self = shift;    my $self = shift;
1286    
   my $phase = 'main';  
   
1287    my $active_formatting_elements = [];    my $active_formatting_elements = [];
1288    
1289    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1854  sub _tree_construction_main ($) { Line 1300  sub _tree_construction_main ($) {
1300      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
1301      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
1302        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
1303            !!!cp ('t32');
1304          return;          return;
1305        }        }
1306      }      }
# Line 1868  sub _tree_construction_main ($) { Line 1315  sub _tree_construction_main ($) {
1315    
1316        ## Step 6        ## Step 6
1317        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
1318            !!!cp ('t33_1');
1319          #          #
1320        } else {        } else {
1321          my $in_open_elements;          my $in_open_elements;
1322          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
1323            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
1324                !!!cp ('t33');
1325              $in_open_elements = 1;              $in_open_elements = 1;
1326              last OE;              last OE;
1327            }            }
1328          }          }
1329          if ($in_open_elements) {          if ($in_open_elements) {
1330              !!!cp ('t34');
1331            #            #
1332          } else {          } else {
1333              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
1334              !!!cp ('t35');
1335            redo S4;            redo S4;
1336          }          }
1337        }        }
# Line 1902  sub _tree_construction_main ($) { Line 1354  sub _tree_construction_main ($) {
1354    
1355        ## Step 11        ## Step 11
1356        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
1357            !!!cp ('t36');
1358          ## Step 7'          ## Step 7'
1359          $i++;          $i++;
1360          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
1361                    
1362          redo S7;          redo S7;
1363        }        }
1364    
1365          !!!cp ('t37');
1366      } # S7      } # S7
1367    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
1368    
1369    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
1370      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
1371        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
1372            !!!cp ('t38');
1373          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
1374          return;          return;
1375        }        }
1376      }      }
1377    
1378        !!!cp ('t39');
1379    }; # $clear_up_to_marker    }; # $clear_up_to_marker
1380    
1381    my $style_start_tag = sub {    my $insert;
1382      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
1383      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
1384      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
1385       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
1386        ->append_child ($style_el);      ## Step 1
1387      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
1388                      !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
1389      my $text = '';  
1390      !!!next-token;      ## Step 2
1391      while ($token->{type} eq 'character') {      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
1392        $text .= $token->{data};      delete $self->{escape}; # MUST
1393        !!!next-token;  
1394      } # stop if non-character token or tokenizer stops tokenising      ## Step 3, 4
1395      if (length $text) {      $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
1396        $style_el->manakai_append_text ($text);  
1397      }      !!!nack ('t40.1');
       
     $self->{content_model_flag} = 'PCDATA';  
                 
     if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
       ## Ignore the token  
     } else {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
       ## ISSUE: And ignore?  
     }  
1398      !!!next-token;      !!!next-token;
1399    }; # $style_start_tag    }; # $parse_rcdata
1400    
1401    my $script_start_tag = sub {    my $script_start_tag = sub () {
1402        ## Step 1
1403      my $script_el;      my $script_el;
1404      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
1405    
1406        ## Step 2
1407      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
1408    
1409      $self->{content_model_flag} = 'CDATA';      ## Step 3
1410            ## TODO: Mark as "already executed", if ...
     my $text = '';  
     !!!next-token;  
     while ($token->{type} eq 'character') {  
       $text .= $token->{data};  
       !!!next-token;  
     } # stop if non-character token or tokenizer stops tokenising  
     if (length $text) {  
       $script_el->manakai_append_text ($text);  
     }  
                 
     $self->{content_model_flag} = 'PCDATA';  
1411    
1412      if ($token->{type} eq 'end tag' and      ## Step 4
1413          $token->{tag_name} eq 'script') {      $insert->($script_el);
1414        ## Ignore the token  
1415      } else {      ## ISSUE: $script_el is not put into the stack
1416        !!!parse-error (type => 'in CDATA:#'.$token->{type});      push @{$self->{open_elements}}, [$script_el, $el_category->{script}];
1417        ## ISSUE: And ignore?  
1418        ## TODO: mark as "already executed"      ## Step 5
1419      }      $self->{content_model} = CDATA_CONTENT_MODEL;
1420            delete $self->{escape}; # MUST
1421      if (defined $self->{inner_html_node}) {  
1422        ## TODO: mark as "already executed"      ## Step 6-7
1423      } else {      $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
1424        ## TODO: $old_insertion_point = current insertion point  
1425        ## TODO: insertion point = just before the next input character      !!!nack ('t40.2');
         
       (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})  
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
         
       ## TODO: insertion point = $old_insertion_point (might be "undefined")  
         
       ## TODO: if there is a script that will execute as soon as the parser resume, then...  
     }  
       
1426      !!!next-token;      !!!next-token;
1427    }; # $script_start_tag    }; # $script_start_tag
1428    
1429      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
1430      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
1431      ## NOTE: $open_tables->[-1]->[2] is set false when non-Text node inserted.
1432      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
1433    
1434    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
1435      my $tag_name = shift;      my $end_tag_token = shift;
1436        my $tag_name = $end_tag_token->{tag_name};
1437    
1438        ## NOTE: The adoption agency algorithm (AAA).
1439    
1440      FET: {      FET: {
1441        ## Step 1        ## Step 1
1442        my $formatting_element;        my $formatting_element;
1443        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
1444        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
1445          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
1446              !!!cp ('t52');
1447              last AFE;
1448            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
1449                         eq $tag_name) {
1450              !!!cp ('t51');
1451            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
1452            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
1453            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
1454          }          }
1455        } # AFE        } # AFE
1456        unless (defined $formatting_element) {        unless (defined $formatting_element) {
1457          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
1458            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
1459          ## Ignore the token          ## Ignore the token
1460          !!!next-token;          !!!next-token;
1461          return;          return;
# Line 2023  sub _tree_construction_main ($) { Line 1467  sub _tree_construction_main ($) {
1467          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
1468          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
1469            if ($in_scope) {            if ($in_scope) {
1470                !!!cp ('t54');
1471              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
1472              last INSCOPE;              last INSCOPE;
1473            } else { # in open elements but not in scope            } else { # in open elements but not in scope
1474              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
1475                !!!parse-error (type => 'unmatched end tag',
1476                                text => $token->{tag_name},
1477                                token => $end_tag_token);
1478              ## Ignore the token              ## Ignore the token
1479              !!!next-token;              !!!next-token;
1480              return;              return;
1481            }            }
1482          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
1483                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
1484            $in_scope = 0;            $in_scope = 0;
1485          }          }
1486        } # INSCOPE        } # INSCOPE
1487        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
1488          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
1489            !!!parse-error (type => 'unmatched end tag',
1490                            text => $token->{tag_name},
1491                            token => $end_tag_token);
1492          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
1493          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
1494          return;          return;
1495        }        }
1496        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
1497          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
1498            !!!parse-error (type => 'not closed',
1499                            text => $self->{open_elements}->[-1]->[0]
1500                                ->manakai_local_name,
1501                            token => $end_tag_token);
1502        }        }
1503                
1504        ## Step 2        ## Step 2
# Line 2053  sub _tree_construction_main ($) { Line 1506  sub _tree_construction_main ($) {
1506        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
1507        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
1508          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
1509          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
1510              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
1511              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
1512               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
1513              !!!cp ('t59');
1514            $furthest_block = $node;            $furthest_block = $node;
1515            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
1516              ## NOTE: The topmost (eldest) node.
1517          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
1518              !!!cp ('t60');
1519            last OE;            last OE;
1520          }          }
1521        } # OE        } # OE
1522                
1523        ## Step 3        ## Step 3
1524        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
1525            !!!cp ('t61');
1526          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
1527          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
1528          !!!next-token;          !!!next-token;
# Line 2078  sub _tree_construction_main ($) { Line 1535  sub _tree_construction_main ($) {
1535        ## Step 5        ## Step 5
1536        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
1537        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
1538            !!!cp ('t62');
1539          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
1540        }        }
1541                
# Line 2100  sub _tree_construction_main ($) { Line 1558  sub _tree_construction_main ($) {
1558          S7S2: {          S7S2: {
1559            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
1560              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
1561                  !!!cp ('t63');
1562                $node_i_in_active = $_;                $node_i_in_active = $_;
1563                last S7S2;                last S7S2;
1564              }              }
# Line 2113  sub _tree_construction_main ($) { Line 1572  sub _tree_construction_main ($) {
1572                    
1573          ## Step 4          ## Step 4
1574          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
1575              !!!cp ('t64');
1576            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
1577          }          }
1578                    
1579          ## Step 5          ## Step 5
1580          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
1581              !!!cp ('t65');
1582            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
1583            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
1584            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2135  sub _tree_construction_main ($) { Line 1596  sub _tree_construction_main ($) {
1596        } # S7          } # S7  
1597                
1598        ## Step 8        ## Step 8
1599        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
1600            my $foster_parent_element;
1601            my $next_sibling;
1602            OE: for (reverse 0..$#{$self->{open_elements}}) {
1603              if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
1604                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
1605                                 if (defined $parent and $parent->node_type == 1) {
1606                                   !!!cp ('t65.1');
1607                                   $foster_parent_element = $parent;
1608                                   $next_sibling = $self->{open_elements}->[$_]->[0];
1609                                 } else {
1610                                   !!!cp ('t65.2');
1611                                   $foster_parent_element
1612                                     = $self->{open_elements}->[$_ - 1]->[0];
1613                                 }
1614                                 last OE;
1615                               }
1616                             } # OE
1617                             $foster_parent_element = $self->{open_elements}->[0]->[0]
1618                               unless defined $foster_parent_element;
1619            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
1620            $open_tables->[-1]->[1] = 1; # tainted
1621          } else {
1622            !!!cp ('t65.3');
1623            $common_ancestor_node->[0]->append_child ($last_node->[0]);
1624          }
1625                
1626        ## Step 9        ## Step 9
1627        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2152  sub _tree_construction_main ($) { Line 1638  sub _tree_construction_main ($) {
1638        my $i;        my $i;
1639        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
1640          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
1641              !!!cp ('t66');
1642            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
1643            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
1644          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
1645              !!!cp ('t67');
1646            $i = $_;            $i = $_;
1647          }          }
1648        } # AFE        } # AFE
# Line 2164  sub _tree_construction_main ($) { Line 1652  sub _tree_construction_main ($) {
1652        undef $i;        undef $i;
1653        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
1654          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
1655              !!!cp ('t68');
1656            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
1657            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
1658          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
1659              !!!cp ('t69');
1660            $i = $_;            $i = $_;
1661          }          }
1662        } # OE        } # OE
1663        splice @{$self->{open_elements}}, $i + 1, 1, $clone;        splice @{$self->{open_elements}}, $i + 1, 0, $clone;
1664                
1665        ## Step 14        ## Step 14
1666        redo FET;        redo FET;
1667      } # FET      } # FET
1668    }; # $formatting_end_tag    }; # $formatting_end_tag
1669    
1670    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
1671      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
1672    }; # $insert_to_current    }; # $insert_to_current
1673    
1674    my $insert_to_foster = sub {    my $insert_to_foster = sub {
1675                         my $child = shift;      my $child = shift;
1676                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
1677                              table => 1, tbody => 1, tfoot => 1,        # MUST
1678                              thead => 1, tr => 1,        my $foster_parent_element;
1679                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
1680                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
1681                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
1682                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
1683                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
1684                                   !!!cp ('t70');
1685                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
1686                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
1687                               } else {                               } else {
1688                                   !!!cp ('t71');
1689                                 $foster_parent_element                                 $foster_parent_element
1690                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
1691                               }                               }
# Line 2207  sub _tree_construction_main ($) { Line 1696  sub _tree_construction_main ($) {
1696                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
1697                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
1698                             ($child, $next_sibling);                             ($child, $next_sibling);
1699                         } else {        $open_tables->[-1]->[1] = 1; # tainted
1700                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
1701                         }        !!!cp ('t72');
1702          $self->{open_elements}->[-1]->[0]->append_child ($child);
1703        }
1704    }; # $insert_to_foster    }; # $insert_to_foster
1705    
1706    my $in_body = sub {    ## NOTE: Insert a character (MUST): When a character is inserted, if
1707      my $insert = shift;    ## the last node that was inserted by the parser is a Text node and
1708      if ($token->{type} eq 'start tag') {    ## the character has to be inserted after that node, then the
1709        if ($token->{tag_name} eq 'script') {    ## character is appended to the Text node.  However, if any other
1710          $script_start_tag->();    ## node is inserted by the parser, then a new Text node is created
1711          return;    ## and the character is appended as that Text node.  If I'm not
1712        } elsif ($token->{tag_name} eq 'style') {    ## wrong, for a parser with scripting disabled, there are only two
1713          $style_start_tag->();    ## cases where this occurs.  One is the case where an element node
1714          return;    ## is inserted to the |head| element.  This is covered by using the
1715        } elsif ({    ## |$self->{head_element_inserted}| flag.  Another is the case where
1716                  base => 1, link => 1, meta => 1,    ## an element or comment is inserted into the |table| subtree while
1717                 }->{$token->{tag_name}}) {    ## foster parenting happens.  This is covered by using the [2] flag
1718          !!!parse-error (type => 'in body:'.$token->{tag_name});    ## of the |$open_tables| structure.  All other cases are handled
1719          ## NOTE: This is an "as if in head" code clone    ## simply by calling |manakai_append_text| method.
1720          my $el;  
1721          !!!create-element ($el, $token->{tag_name}, $token->{attributes});    ## TODO: |<body><script>document.write("a<br>");
1722          if (defined $self->{head_element}) {    ## document.body.removeChild (document.body.lastChild);
1723            $self->{head_element}->append_child ($el);    ## document.write ("b")</script>|
1724          } else {  
1725            $insert->($el);    B: while (1) {
1726          }      if ($token->{type} == DOCTYPE_TOKEN) {
1727                  !!!cp ('t73');
1728          !!!next-token;        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
1729          return;        ## Ignore the token
1730        } elsif ($token->{tag_name} eq 'title') {        ## Stay in the phase
1731          !!!parse-error (type => 'in body:title');        !!!next-token;
1732          ## NOTE: There is an "as if in head" code clone        next B;
1733          my $title_el;      } elsif ($token->{type} == START_TAG_TOKEN and
1734          !!!create-element ($title_el, 'title', $token->{attributes});               $token->{tag_name} eq 'html') {
1735          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
1736            ->append_child ($title_el);          !!!cp ('t79');
1737          $self->{content_model_flag} = 'RCDATA';          !!!parse-error (type => 'after html', text => 'html', token => $token);
1738                    $self->{insertion_mode} = AFTER_BODY_IM;
1739          my $text = '';        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
1740          !!!next-token;          !!!cp ('t80');
1741          while ($token->{type} eq 'character') {          !!!parse-error (type => 'after html', text => 'html', token => $token);
1742            $text .= $token->{data};          $self->{insertion_mode} = AFTER_FRAMESET_IM;
1743            !!!next-token;        } else {
1744          }          !!!cp ('t81');
1745          if (length $text) {        }
1746            $title_el->manakai_append_text ($text);  
1747          }        !!!cp ('t82');
1748                  !!!parse-error (type => 'not first start tag', token => $token);
1749          $self->{content_model_flag} = 'PCDATA';        my $top_el = $self->{open_elements}->[0]->[0];
1750                  for my $attr_name (keys %{$token->{attributes}}) {
1751          if ($token->{type} eq 'end tag' and          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
1752              $token->{tag_name} eq 'title') {            !!!cp ('t84');
1753            ## Ignore the token            $top_el->set_attribute_ns
1754          } else {              (undef, [undef, $attr_name],
1755            !!!parse-error (type => 'in RCDATA:#'.$token->{type});               $token->{attributes}->{$attr_name}->{value});
           ## ISSUE: And ignore?  
1756          }          }
1757          !!!next-token;        }
1758          return;        !!!nack ('t84.1');
1759        } elsif ($token->{tag_name} eq 'body') {        !!!next-token;
1760          !!!parse-error (type => 'in body:body');        next B;
1761                      } elsif ($token->{type} == COMMENT_TOKEN) {
1762          if (@{$self->{open_elements}} == 1 or        my $comment = $self->{document}->create_comment ($token->{data});
1763              $self->{open_elements}->[1]->[1] ne 'body') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
1764            ## Ignore the token          !!!cp ('t85');
1765            $self->{document}->append_child ($comment);
1766          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
1767            !!!cp ('t86');
1768            $self->{open_elements}->[0]->[0]->append_child ($comment);
1769          } else {
1770            !!!cp ('t87');
1771            $self->{open_elements}->[-1]->[0]->append_child ($comment);
1772            $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
1773          }
1774          !!!next-token;
1775          next B;
1776        } elsif ($self->{insertion_mode} & IN_CDATA_RCDATA_IM) {
1777          if ($token->{type} == CHARACTER_TOKEN) {
1778            $token->{data} =~ s/^\x0A// if $self->{ignore_newline};
1779            delete $self->{ignore_newline};
1780    
1781            if (length $token->{data}) {
1782              !!!cp ('t43');
1783              $self->{open_elements}->[-1]->[0]->manakai_append_text
1784                  ($token->{data});
1785          } else {          } else {
1786            my $body_el = $self->{open_elements}->[1]->[0];            !!!cp ('t43.1');
           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});  
             }  
           }  
1787          }          }
1788          !!!next-token;          !!!next-token;
1789          return;          next B;
1790        } elsif ({        } elsif ($token->{type} == END_TAG_TOKEN) {
1791                  address => 1, blockquote => 1, center => 1, dir => 1,          delete $self->{ignore_newline};
1792                  div => 1, dl => 1, fieldset => 1, listing => 1,  
1793                  menu => 1, ol => 1, p => 1, ul => 1,          if ($token->{tag_name} eq 'script') {
1794                  pre => 1,            !!!cp ('t50');
                }->{$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  
1795                        
1796          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            ## Para 1-2
1797          if ($token->{tag_name} eq 'pre') {            my $script = pop @{$self->{open_elements}};
1798            !!!next-token;            
1799            if ($token->{type} eq 'character') {            ## Para 3
1800              $token->{data} =~ s/^\x0A//;            $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1801              unless (length $token->{data}) {  
1802                !!!next-token;            ## Para 4
1803              }            ## TODO: $old_insertion_point = $current_insertion_point;
1804            }            ## TODO: $current_insertion_point = just before $self->{nc};
1805          } else {  
1806            !!!next-token;            ## Para 5
1807          }            ## TODO: Run the $script->[0].
1808          return;  
1809        } elsif ($token->{tag_name} eq 'form') {            ## Para 6
1810          if (defined $self->{form_element}) {            ## TODO: $current_insertion_point = $old_insertion_point;
1811            !!!parse-error (type => 'in form:form');  
1812            ## Ignore the token            ## Para 7
1813              ## TODO: if ($pending_external_script) {
1814                ## TODO: ...
1815              ## TODO: }
1816    
1817            !!!next-token;            !!!next-token;
1818            return;            next B;
1819          } else {          } else {
1820            ## has a p element in scope            !!!cp ('t42');
1821            INSCOPE: for (reverse @{$self->{open_elements}}) {  
1822              if ($_->[1] eq 'p') {            pop @{$self->{open_elements}};
1823                !!!back-token;  
1824                $token = {type => 'end tag', tag_name => 'p'};            $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
               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];  
1825            !!!next-token;            !!!next-token;
1826            return;            next B;
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
1827          }          }
1828                    } elsif ($token->{type} == END_OF_FILE_TOKEN) {
1829          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          delete $self->{ignore_newline};
             
         !!!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);  
1830    
1831          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t44');
1832          push @$active_formatting_elements, $self->{open_elements}->[-1];          !!!parse-error (type => 'not closed',
1833                            text => $self->{open_elements}->[-1]->[0]
1834                                ->manakai_local_name,
1835                            token => $token);
1836    
1837            #if ($self->{open_elements}->[-1]->[1] == SCRIPT_EL) {
1838            #  ## TODO: Mark as "already executed"
1839            #}
1840    
         !!!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});  
1841          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
1842              
1843          !!!next-token;          $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1844          return;          ## Reprocess.
1845        } elsif ($token->{tag_name} eq 'input') {          next B;
1846          $reconstruct_active_formatting_elements->($insert_to_current);        } else {
1847                    die "$0: $token->{type}: In CDATA/RCDATA: Unknown token type";        
1848          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        }
1849          ## TODO: associate with $self->{form_element} if defined      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
1850          pop @{$self->{open_elements}};        if ($token->{type} == CHARACTER_TOKEN) {
1851                    !!!cp ('t87.1');
1852            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
1853          !!!next-token;          !!!next-token;
1854          return;          next B;
1855        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{type} == START_TAG_TOKEN) {
1856          !!!parse-error (type => 'isindex');          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
1857                         $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
1858          if (defined $self->{form_element}) {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
1859            ## Ignore the token              ($token->{tag_name} eq 'svg' and
1860            !!!next-token;               $self->{open_elements}->[-1]->[1] == MML_AXML_EL)) {
1861            return;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
1862          } else {            !!!cp ('t87.2');
1863            my $at = $token->{attributes};            #
1864            $at->{name} = {name => 'name', value => 'isindex'};          } elsif ({
1865            my @tokens = (                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
1866                          {type => 'start tag', tag_name => 'form'},                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
1867                          {type => 'start tag', tag_name => 'hr'},                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
1868                          {type => 'start tag', tag_name => 'p'},                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
1869                          {type => 'start tag', tag_name => 'label'},                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
1870                          {type => 'character',                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
1871                           data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
1872                          ## TODO: make this configurable                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
1873                          {type => 'start tag', tag_name => 'input', attributes => $at},                   }->{$token->{tag_name}}) {
1874                          #{type => 'character', data => ''}, # SHOULD            !!!cp ('t87.2');
1875                          {type => 'end tag', tag_name => 'label'},            !!!parse-error (type => 'not closed',
1876                          {type => 'end tag', tag_name => 'p'},                            text => $self->{open_elements}->[-1]->[0]
1877                          {type => 'start tag', tag_name => 'hr'},                                ->manakai_local_name,
1878                          {type => 'end tag', tag_name => 'form'},                            token => $token);
1879                         );  
1880            $token = shift @tokens;            pop @{$self->{open_elements}}
1881            !!!back-token (@tokens);                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
1882            return;  
1883          }            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
1884        } elsif ({            ## Reprocess.
1885                  textarea => 1,            next B;
1886                  iframe => 1,          } else {
1887                  noembed => 1,            my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
1888                  noframes => 1,            my $tag_name = $token->{tag_name};
1889                  noscript => 0, ## TODO: 1 if scripting is enabled            if ($nsuri eq $SVG_NS) {
1890                 }->{$token->{tag_name}}) {              $tag_name = {
1891          my $tag_name = $token->{tag_name};                 altglyph => 'altGlyph',
1892          my $el;                 altglyphdef => 'altGlyphDef',
1893          !!!create-element ($el, $token->{tag_name}, $token->{attributes});                 altglyphitem => 'altGlyphItem',
1894                           animatecolor => 'animateColor',
1895          if ($token->{tag_name} eq 'textarea') {                 animatemotion => 'animateMotion',
1896            ## TODO: $self->{form_element} if defined                 animatetransform => 'animateTransform',
1897            $self->{content_model_flag} = 'RCDATA';                 clippath => 'clipPath',
1898          } else {                 feblend => 'feBlend',
1899            $self->{content_model_flag} = 'CDATA';                 fecolormatrix => 'feColorMatrix',
1900          }                 fecomponenttransfer => 'feComponentTransfer',
1901                           fecomposite => 'feComposite',
1902          $insert->($el);                 feconvolvematrix => 'feConvolveMatrix',
1903                           fediffuselighting => 'feDiffuseLighting',
1904          my $text = '';                 fedisplacementmap => 'feDisplacementMap',
1905          if ($token->{tag_name} eq 'textarea') {                 fedistantlight => 'feDistantLight',
1906            !!!next-token;                 feflood => 'feFlood',
1907            if ($token->{type} eq 'character') {                 fefunca => 'feFuncA',
1908              $token->{data} =~ s/^\x0A//;                 fefuncb => 'feFuncB',
1909              unless (length $token->{data}) {                 fefuncg => 'feFuncG',
1910                !!!next-token;                 fefuncr => 'feFuncR',
1911              }                 fegaussianblur => 'feGaussianBlur',
1912                   feimage => 'feImage',
1913                   femerge => 'feMerge',
1914                   femergenode => 'feMergeNode',
1915                   femorphology => 'feMorphology',
1916                   feoffset => 'feOffset',
1917                   fepointlight => 'fePointLight',
1918                   fespecularlighting => 'feSpecularLighting',
1919                   fespotlight => 'feSpotLight',
1920                   fetile => 'feTile',
1921                   feturbulence => 'feTurbulence',
1922                   foreignobject => 'foreignObject',
1923                   glyphref => 'glyphRef',
1924                   lineargradient => 'linearGradient',
1925                   radialgradient => 'radialGradient',
1926                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
1927                   textpath => 'textPath',  
1928                }->{$tag_name} || $tag_name;
1929            }            }
1930          } else {  
1931            !!!next-token;            ## "adjust SVG attributes" (SVG only) - done in insert-element-f
1932          }  
1933          while ($token->{type} eq 'character') {            ## "adjust foreign attributes" - done in insert-element-f
1934            $text .= $token->{data};  
1935            !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
1936          }  
1937          if (length $text) {            if ($self->{self_closing}) {
1938            $el->manakai_append_text ($text);              pop @{$self->{open_elements}};
1939          }              !!!ack ('t87.3');
           
         $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 RCDATA:#'.$token->{type});  
1940            } else {            } else {
1941              !!!parse-error (type => 'in CDATA:#'.$token->{type});              !!!cp ('t87.4');
1942            }            }
1943            ## ISSUE: And ignore?  
1944              !!!next-token;
1945              next B;
1946          }          }
1947          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
1948          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
1949        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
1950          $reconstruct_active_formatting_elements->($insert_to_current);          #
1951                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
1952          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
1953                    !!!parse-error (type => 'not closed',
1954          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
1955          !!!next-token;                              ->manakai_local_name,
1956          return;                          token => $token);
1957        } elsif ({  
1958                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
1959                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
1960                  tbody => 1, td => 1, tfoot => 1, th => 1,  
1961                  thead => 1, tr => 1,          ## NOTE: |<span><svg>| ... two parse errors, |<svg>| ... a parse error.
1962                 }->{$token->{tag_name}}) {  
1963          !!!parse-error (type => 'in body:'.$token->{tag_name});          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
1964          ## Ignore the token          ## Reprocess.
1965          !!!next-token;          next B;
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
1966        } else {        } else {
1967          $reconstruct_active_formatting_elements->($insert_to_current);          die "$0: $token->{type}: Unknown token type";        
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
1968        }        }
1969      } elsif ($token->{type} eq 'end tag') {      }
1970        if ($token->{tag_name} eq 'body') {  
1971          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
1972            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
1973            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1974              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
1975            }              if ($self->{head_element_inserted}) {
1976            $self->{insertion_mode} = 'after body';                !!!cp ('t88.3');
1977            !!!next-token;                $self->{open_elements}->[-1]->[0]->append_child
1978            return;                  ($self->{document}->create_text_node ($1));
1979          } else {                delete $self->{head_element_inserted};
1980            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                ## NOTE: |</head> <link> |
1981            ## Ignore the token                #
1982            !!!next-token;              } else {
1983            return;                !!!cp ('t88.2');
1984          }                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
1985        } elsif ($token->{tag_name} eq 'html') {                ## NOTE: |</head> &#x20;|
1986          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;  
1987              }              }
1988              $i = $_;            } else {
1989              last INSCOPE unless $token->{tag_name} eq 'p';              !!!cp ('t88.1');
1990            } elsif ({              ## Ignore the token.
1991                      table => 1, caption => 1, td => 1, th => 1,              #
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
1992            }            }
1993          } # INSCOPE            unless (length $token->{data}) {
1994                        !!!cp ('t88');
1995          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
1996            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              next B;
         }  
           
         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;  
1997            }            }
1998          } # INSCOPE  ## TODO: set $token->{column} appropriately
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
1999          }          }
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
2000    
2001          ## Step 2          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2002          S2: {            !!!cp ('t89');
2003            if ($node->[1] eq $token->{tag_name}) {            ## As if <head>
2004              ## Step 1            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2005              ## generate implied end tags            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2006              if ({            push @{$self->{open_elements}},
2007                   dd => 1, dt => 1, li => 1, p => 1,                [$self->{head_element}, $el_category->{head}];
2008                   td => 1, th => 1, tr => 1,  
2009                  }->{$self->{open_elements}->[-1]->[1]}) {            ## Reprocess in the "in head" insertion mode...
2010                !!!back-token;            pop @{$self->{open_elements}};
2011                $token = {type => 'end tag',  
2012                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST            ## Reprocess in the "after head" insertion mode...
2013                return;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2014              }            !!!cp ('t90');
2015                      ## As if </noscript>
2016              ## Step 2            pop @{$self->{open_elements}};
2017              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {            !!!parse-error (type => 'in noscript:#text', token => $token);
2018                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            
2019              }            ## Reprocess in the "in head" insertion mode...
2020                          ## As if </head>
2021              ## Step 3            pop @{$self->{open_elements}};
             splice @{$self->{open_elements}}, $node_i;  
2022    
2023              ## Reprocess in the "after head" insertion mode...
2024            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2025              !!!cp ('t91');
2026              pop @{$self->{open_elements}};
2027    
2028              ## Reprocess in the "after head" insertion mode...
2029            } else {
2030              !!!cp ('t92');
2031            }
2032    
2033            ## "after head" insertion mode
2034            ## As if <body>
2035            !!!insert-element ('body',, $token);
2036            $self->{insertion_mode} = IN_BODY_IM;
2037            ## reprocess
2038            next B;
2039          } elsif ($token->{type} == START_TAG_TOKEN) {
2040            if ($token->{tag_name} eq 'head') {
2041              if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2042                !!!cp ('t93');
2043                !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
2044                $self->{open_elements}->[-1]->[0]->append_child
2045                    ($self->{head_element});
2046                push @{$self->{open_elements}},
2047                    [$self->{head_element}, $el_category->{head}];
2048                $self->{insertion_mode} = IN_HEAD_IM;
2049                !!!nack ('t93.1');
2050              !!!next-token;              !!!next-token;
2051              last S2;              next B;
2052              } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2053                !!!cp ('t93.2');
2054                !!!parse-error (type => 'after head', text => 'head',
2055                                token => $token);
2056                ## Ignore the token
2057                !!!nack ('t93.3');
2058                !!!next-token;
2059                next B;
2060            } else {            } else {
2061              ## Step 3              !!!cp ('t95');
2062              if (not $formatting_category->{$node->[1]} and              !!!parse-error (type => 'in head:head',
2063                  #not $phrasing_category->{$node->[1]} and                              token => $token); # or in head noscript
2064                  ($special_category->{$node->[1]} or              ## Ignore the token
2065                   $scoping_category->{$node->[1]})) {              !!!nack ('t95.1');
2066                !!!parse-error (type => 'not closed:'.$node->[1]);              !!!next-token;
2067                ## Ignore the token              next B;
               !!!next-token;  
               last S2;  
             }  
2068            }            }
2069            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2070              !!!cp ('t96');
2071              ## As if <head>
2072              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2073              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2074              push @{$self->{open_elements}},
2075                  [$self->{head_element}, $el_category->{head}];
2076    
2077              $self->{insertion_mode} = IN_HEAD_IM;
2078              ## Reprocess in the "in head" insertion mode...
2079            } else {
2080              !!!cp ('t97');
2081            }
2082    
2083            if ($token->{tag_name} eq 'base') {
2084              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2085                !!!cp ('t98');
2086                ## As if </noscript>
2087                pop @{$self->{open_elements}};
2088                !!!parse-error (type => 'in noscript', text => 'base',
2089                                token => $token);
2090                        
2091            ## Step 4              $self->{insertion_mode} = IN_HEAD_IM;
2092            $node_i--;              ## Reprocess in the "in head" insertion mode...
2093            $node = $self->{open_elements}->[$node_i];            } else {
2094                          !!!cp ('t99');
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($phase eq 'main') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'in html:#DOCTYPE');  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         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});  
2095            }            }
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## 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]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
2096    
2097          ## Stop parsing            ## NOTE: There is a "as if in head" code clone.
2098          last B;            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2099                !!!cp ('t100');
2100          ## ISSUE: There is an issue in the spec.              !!!parse-error (type => 'after head',
2101        } else {                              text => $token->{tag_name}, token => $token);
2102          if ($self->{insertion_mode} eq 'before head') {              push @{$self->{open_elements}},
2103            if ($token->{type} eq 'character') {                  [$self->{head_element}, $el_category->{head}];
2104              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $self->{head_element_inserted} = 1;
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
             ## 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);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               ## 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;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
2105            } else {            } else {
2106              die "$0: $token->{type}: Unknown type";              !!!cp ('t101');
2107            }            }
2108          } elsif ($self->{insertion_mode} eq 'in head') {            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2109            if ($token->{type} eq 'character') {            pop @{$self->{open_elements}};
2110              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            pop @{$self->{open_elements}} # <head>
2111                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                if $self->{insertion_mode} == AFTER_HEAD_IM;
2112                unless (length $token->{data}) {            !!!nack ('t101.1');
2113                  !!!next-token;            !!!next-token;
2114                  redo B;            next B;
2115                }          } elsif ($token->{tag_name} eq 'link') {
2116              }            ## NOTE: There is a "as if in head" code clone.
2117                          if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2118              #              !!!cp ('t102');
2119            } elsif ($token->{type} eq 'comment') {              !!!parse-error (type => 'after head',
2120              my $comment = $self->{document}->create_comment ($token->{data});                              text => $token->{tag_name}, token => $token);
2121              $self->{open_elements}->[-1]->[0]->append_child ($comment);              push @{$self->{open_elements}},
2122              !!!next-token;                  [$self->{head_element}, $el_category->{head}];
2123              redo B;              $self->{head_element_inserted} = 1;
           } 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';  
   
               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;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'head') {  
               !!!parse-error (type => 'in head:head');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{open_elements}->[-1]->[1] eq 'head') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:head');  
               }  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'html') {  
               #  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
2124            } else {            } else {
2125              #              !!!cp ('t103');
2126            }            }
2127              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2128              pop @{$self->{open_elements}};
2129              pop @{$self->{open_elements}} # <head>
2130                  if $self->{insertion_mode} == AFTER_HEAD_IM;
2131              !!!ack ('t103.1');
2132              !!!next-token;
2133              next B;
2134            } elsif ($token->{tag_name} eq 'command' or
2135                     $token->{tag_name} eq 'eventsource') {
2136              if ($self->{insertion_mode} == IN_HEAD_IM) {
2137                ## NOTE: If the insertion mode at the time of the emission
2138                ## of the token was "before head", $self->{insertion_mode}
2139                ## is already changed to |IN_HEAD_IM|.
2140    
2141            if ($self->{open_elements}->[-1]->[1] eq 'head') {              ## NOTE: There is a "as if in head" code clone.
2142              ## As if </head>              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2143              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
2144            }              pop @{$self->{open_elements}} # <head>
2145            $self->{insertion_mode} = 'after head';                  if $self->{insertion_mode} == AFTER_HEAD_IM;
2146            ## reprocess              !!!ack ('t103.2');
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
2147              !!!next-token;              !!!next-token;
2148              redo B;              next 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 {  
               #  
             }  
2149            } else {            } else {
2150                ## NOTE: "in head noscript" or "after head" insertion mode
2151                ## - in these cases, these tags are treated as same as
2152                ## normal in-body tags.
2153                !!!cp ('t103.3');
2154              #              #
2155            }            }
2156                      } elsif ($token->{tag_name} eq 'meta') {
2157            ## As if <body>            ## NOTE: There is a "as if in head" code clone.
2158            !!!insert-element ('body');            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2159            $self->{insertion_mode} = 'in body';              !!!cp ('t104');
2160            ## reprocess              !!!parse-error (type => 'after head',
2161            redo B;                              text => $token->{tag_name}, token => $token);
2162          } elsif ($self->{insertion_mode} eq 'in body') {              push @{$self->{open_elements}},
2163            if ($token->{type} eq 'character') {                  [$self->{head_element}, $el_category->{head}];
2164              ## NOTE: There is a code clone of "character in body".              $self->{head_element_inserted} = 1;
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
2165            } else {            } else {
2166              $in_body->($insert_to_current);              !!!cp ('t105');
             redo B;  
2167            }            }
2168          } elsif ($self->{insertion_mode} eq 'in table') {            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2169            if ($token->{type} eq 'character') {            my $meta_el = pop @{$self->{open_elements}};
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
2170    
2171              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
2172              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) {
2173              ## into the current node" while characters might not be                    !!!cp ('t106');
2174              ## result in a new Text node.                    ## NOTE: Whether the encoding is supported or not is handled
2175              $reconstruct_active_formatting_elements->($insert_to_foster);                    ## in the {change_encoding} callback.
2176                                  $self->{change_encoding}
2177              if ({                        ->($self, $token->{attributes}->{charset}->{value},
2178                   table => 1, tbody => 1, tfoot => 1,                           $token);
2179                   thead => 1, tr => 1,                    
2180                  }->{$self->{open_elements}->[-1]->[1]}) {                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2181                # MUST                        ->set_user_data (manakai_has_reference =>
2182                my $foster_parent_element;                                             $token->{attributes}->{charset}
2183                my $next_sibling;                                                 ->{has_reference});
2184                my $prev_sibling;                  } elsif ($token->{attributes}->{content}) {
2185                OE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($token->{attributes}->{content}->{value}
2186                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2187                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09\x0A\x0C\x0D\x20]*=
2188                    if (defined $parent and $parent->node_type == 1) {                            [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2189                      $foster_parent_element = $parent;                            ([^"'\x09\x0A\x0C\x0D\x20]
2190                      $next_sibling = $self->{open_elements}->[$_]->[0];                             [^\x09\x0A\x0C\x0D\x20\x3B]*))/x) {
2191                      $prev_sibling = $next_sibling->previous_sibling;                      !!!cp ('t107');
2192                        ## NOTE: Whether the encoding is supported or not is handled
2193                        ## in the {change_encoding} callback.
2194                        $self->{change_encoding}
2195                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
2196                               $token);
2197                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2198                            ->set_user_data (manakai_has_reference =>
2199                                                 $token->{attributes}->{content}
2200                                                       ->{has_reference});
2201                    } else {                    } else {
2202                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
2203                    }                    }
                   last OE;  
2204                  }                  }
               } # 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});  
2205                } else {                } else {
2206                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
2207                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
2208                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2209                }                        ->set_user_data (manakai_has_reference =>
2210              } else {                                             $token->{attributes}->{charset}
2211                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
2212              }                  }
2213                                if ($token->{attributes}->{content}) {
2214              !!!next-token;                    !!!cp ('t110');
2215              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2216            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
2217              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
2218              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
2219              !!!next-token;                  }
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
2220                }                }
2221    
2222                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
2223                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2224                  !!!ack ('t110.1');
               !!!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}};  
2225                !!!next-token;                !!!next-token;
2226                redo B;                next B;
2227              } elsif ({          } elsif ($token->{tag_name} eq 'title') {
2228                        col => 1,            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2229                        td => 1, th => 1, tr => 1,              !!!cp ('t111');
2230                       }->{$token->{tag_name}}) {              ## As if </noscript>
2231                ## Clear back to table context              pop @{$self->{open_elements}};
2232                while ($self->{open_elements}->[-1]->[1] ne 'table' and              !!!parse-error (type => 'in noscript', text => 'title',
2233                       $self->{open_elements}->[-1]->[1] ne 'html') {                              token => $token);
2234                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            
2235                  pop @{$self->{open_elements}};              $self->{insertion_mode} = IN_HEAD_IM;
2236                }              ## Reprocess in the "in head" insertion mode...
2237              } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2238                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');              !!!cp ('t112');
2239                $self->{insertion_mode} = $token->{tag_name} eq 'col'              !!!parse-error (type => 'after head',
2240                  ? 'in column group' : 'in table body';                              text => $token->{tag_name}, token => $token);
2241                ## reprocess              push @{$self->{open_elements}},
2242                redo B;                  [$self->{head_element}, $el_category->{head}];
2243              } elsif ($token->{tag_name} eq 'table') {              $self->{head_element_inserted} = 1;
2244                ## NOTE: There are code clones for this "table in table"            } else {
2245                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!cp ('t113');
2246              }
2247    
2248                ## As if </table>            ## NOTE: There is a "as if in head" code clone.
2249                ## have a table element in table scope            $parse_rcdata->(RCDATA_CONTENT_MODEL);
2250                my $i;            ## ISSUE: A spec bug [Bug 6038]
2251                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            splice @{$self->{open_elements}}, -2, 1, () # <head>
2252                  my $node = $self->{open_elements}->[$_];                if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2253                  if ($node->[1] eq 'table') {            next B;
2254                    $i = $_;          } elsif ($token->{tag_name} eq 'style' or
2255                    last INSCOPE;                   $token->{tag_name} eq 'noframes') {
2256                  } elsif ({            ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2257                            table => 1, html => 1,            ## insertion mode IN_HEAD_IM)
2258                           }->{$node->[1]}) {            ## NOTE: There is a "as if in head" code clone.
2259                    last INSCOPE;            if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2260                  }              !!!cp ('t114');
2261                } # INSCOPE              !!!parse-error (type => 'after head',
2262                unless (defined $i) {                              text => $token->{tag_name}, token => $token);
2263                  !!!parse-error (type => 'unmatched end tag:table');              push @{$self->{open_elements}},
2264                  ## Ignore tokens </table><table>                  [$self->{head_element}, $el_category->{head}];
2265                $self->{head_element_inserted} = 1;
2266              } else {
2267                !!!cp ('t115');
2268              }
2269              $parse_rcdata->(CDATA_CONTENT_MODEL);
2270              ## ISSUE: A spec bug [Bug 6038]
2271              splice @{$self->{open_elements}}, -2, 1, () # <head>
2272                  if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2273              next B;
2274            } elsif ($token->{tag_name} eq 'noscript') {
2275                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2276                    !!!cp ('t116');
2277                    ## NOTE: and scripting is disalbed
2278                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2279                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2280                    !!!nack ('t116.1');
2281                  !!!next-token;                  !!!next-token;
2282                  redo B;                  next B;
2283                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2284                    !!!cp ('t117');
2285                    !!!parse-error (type => 'in noscript', text => 'noscript',
2286                                    token => $token);
2287                    ## Ignore the token
2288                    !!!nack ('t117.1');
2289                    !!!next-token;
2290                    next B;
2291                  } else {
2292                    !!!cp ('t118');
2293                    #
2294                }                }
2295                          } elsif ($token->{tag_name} eq 'script') {
2296                ## generate implied end tags            if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2297                if ({              !!!cp ('t119');
2298                     dd => 1, dt => 1, li => 1, p => 1,              ## As if </noscript>
2299                     td => 1, th => 1, tr => 1,              pop @{$self->{open_elements}};
2300                    }->{$self->{open_elements}->[-1]->[1]}) {              !!!parse-error (type => 'in noscript', text => 'script',
2301                  !!!back-token; # <table>                              token => $token);
2302                  $token = {type => 'end tag', tag_name => 'table'};            
2303                  !!!back-token;              $self->{insertion_mode} = IN_HEAD_IM;
2304                  $token = {type => 'end tag',              ## Reprocess in the "in head" insertion mode...
2305                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2306                  redo B;              !!!cp ('t120');
2307                !!!parse-error (type => 'after head',
2308                                text => $token->{tag_name}, token => $token);
2309                push @{$self->{open_elements}},
2310                    [$self->{head_element}, $el_category->{head}];
2311                $self->{head_element_inserted} = 1;
2312              } else {
2313                !!!cp ('t121');
2314              }
2315    
2316              ## NOTE: There is a "as if in head" code clone.
2317              $script_start_tag->();
2318              ## ISSUE: A spec bug  [Bug 6038]
2319              splice @{$self->{open_elements}}, -2, 1 # <head>
2320                  if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2321              next B;
2322            } elsif ($token->{tag_name} eq 'body' or
2323                     $token->{tag_name} eq 'frameset') {
2324                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2325                    !!!cp ('t122');
2326                    ## As if </noscript>
2327                    pop @{$self->{open_elements}};
2328                    !!!parse-error (type => 'in noscript',
2329                                    text => $token->{tag_name}, token => $token);
2330                    
2331                    ## Reprocess in the "in head" insertion mode...
2332                    ## As if </head>
2333                    pop @{$self->{open_elements}};
2334                    
2335                    ## Reprocess in the "after head" insertion mode...
2336                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2337                    !!!cp ('t124');
2338                    pop @{$self->{open_elements}};
2339                    
2340                    ## Reprocess in the "after head" insertion mode...
2341                  } else {
2342                    !!!cp ('t125');
2343                }                }
2344    
2345                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## "after head" insertion mode
2346                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
2347                  if ($token->{tag_name} eq 'body') {
2348                    !!!cp ('t126');
2349                    $self->{insertion_mode} = IN_BODY_IM;
2350                  } elsif ($token->{tag_name} eq 'frameset') {
2351                    !!!cp ('t127');
2352                    $self->{insertion_mode} = IN_FRAMESET_IM;
2353                  } else {
2354                    die "$0: tag name: $self->{tag_name}";
2355                }                }
2356                  !!!nack ('t127.1');
2357                  !!!next-token;
2358                  next B;
2359                } else {
2360                  !!!cp ('t128');
2361                  #
2362                }
2363    
2364                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2365                  !!!cp ('t129');
2366                  ## As if </noscript>
2367                  pop @{$self->{open_elements}};
2368                  !!!parse-error (type => 'in noscript:/',
2369                                  text => $token->{tag_name}, token => $token);
2370                  
2371                  ## Reprocess in the "in head" insertion mode...
2372                  ## As if </head>
2373                  pop @{$self->{open_elements}};
2374    
2375                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
2376                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2377                  !!!cp ('t130');
2378                  ## As if </head>
2379                  pop @{$self->{open_elements}};
2380    
2381                ## reprocess                ## Reprocess in the "after head" insertion mode...
               redo B;  
2382              } else {              } else {
2383                #                !!!cp ('t131');
2384              }              }
2385            } elsif ($token->{type} eq 'end tag') {  
2386              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
2387                ## have a table element in table scope              ## As if <body>
2388                my $i;              !!!insert-element ('body',, $token);
2389                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
2390                  my $node = $self->{open_elements}->[$_];              ## reprocess
2391                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
2392                    $i = $_;              next B;
2393                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
2394                  } elsif ({              if ($token->{tag_name} eq 'head') {
2395                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2396                           }->{$node->[1]}) {                  !!!cp ('t132');
2397                    last INSCOPE;                  ## As if <head>
2398                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2399                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2400                unless (defined $i) {                  push @{$self->{open_elements}},
2401                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
2402    
2403                    ## Reprocess in the "in head" insertion mode...
2404                    pop @{$self->{open_elements}};
2405                    $self->{insertion_mode} = AFTER_HEAD_IM;
2406                    !!!next-token;
2407                    next B;
2408                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2409                    !!!cp ('t133');
2410                    ## As if </noscript>
2411                    pop @{$self->{open_elements}};
2412                    !!!parse-error (type => 'in noscript:/',
2413                                    text => 'head', token => $token);
2414                    
2415                    ## Reprocess in the "in head" insertion mode...
2416                    pop @{$self->{open_elements}};
2417                    $self->{insertion_mode} = AFTER_HEAD_IM;
2418                    !!!next-token;
2419                    next B;
2420                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2421                    !!!cp ('t134');
2422                    pop @{$self->{open_elements}};
2423                    $self->{insertion_mode} = AFTER_HEAD_IM;
2424                    !!!next-token;
2425                    next B;
2426                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2427                    !!!cp ('t134.1');
2428                    !!!parse-error (type => 'unmatched end tag', text => 'head',
2429                                    token => $token);
2430                  ## Ignore the token                  ## Ignore the token
2431                  !!!next-token;                  !!!next-token;
2432                  redo B;                  next B;
2433                  } else {
2434                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
2435                }                }
2436                              } elsif ($token->{tag_name} eq 'noscript') {
2437                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2438                if ({                  !!!cp ('t136');
2439                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
2440                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
2441                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
2442                  !!!back-token;                  next B;
2443                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
2444                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
2445                  redo B;                  !!!cp ('t137');
2446                    !!!parse-error (type => 'unmatched end tag',
2447                                    text => 'noscript', token => $token);
2448                    ## Ignore the token ## ISSUE: An issue in the spec.
2449                    !!!next-token;
2450                    next B;
2451                  } else {
2452                    !!!cp ('t138');
2453                    #
2454                }                }
2455                } elsif ({
2456                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
2457                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
2458                  ## TODO: This branch is entirely redundant.
2459                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
2460                      $self->{insertion_mode} == IN_HEAD_IM or
2461                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2462                    !!!cp ('t140');
2463                    !!!parse-error (type => 'unmatched end tag',
2464                                    text => $token->{tag_name}, token => $token);
2465                    ## Ignore the token
2466                    !!!next-token;
2467                    next B;
2468                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2469                    !!!cp ('t140.1');
2470                    !!!parse-error (type => 'unmatched end tag',
2471                                    text => $token->{tag_name}, token => $token);
2472                    ## Ignore the token
2473                    !!!next-token;
2474                    next B;
2475                  } else {
2476                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
2477                }                }
2478                } elsif ($token->{tag_name} eq 'p') {
2479                  !!!cp ('t142');
2480                  !!!parse-error (type => 'unmatched end tag',
2481                                  text => $token->{tag_name}, token => $token);
2482                  ## Ignore the token
2483                  !!!next-token;
2484                  next B;
2485                } elsif ($token->{tag_name} eq 'br') {
2486                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2487                    !!!cp ('t142.2');
2488                    ## (before head) as if <head>, (in head) as if </head>
2489                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2490                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2491                    $self->{insertion_mode} = AFTER_HEAD_IM;
2492      
2493                    ## Reprocess in the "after head" insertion mode...
2494                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2495                    !!!cp ('t143.2');
2496                    ## As if </head>
2497                    pop @{$self->{open_elements}};
2498                    $self->{insertion_mode} = AFTER_HEAD_IM;
2499      
2500                    ## Reprocess in the "after head" insertion mode...
2501                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2502                    !!!cp ('t143.3');
2503                    ## ISSUE: Two parse errors for <head><noscript></br>
2504                    !!!parse-error (type => 'unmatched end tag',
2505                                    text => 'br', token => $token);
2506                    ## As if </noscript>
2507                    pop @{$self->{open_elements}};
2508                    $self->{insertion_mode} = IN_HEAD_IM;
2509    
2510                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
2511                    ## As if </head>
2512                    pop @{$self->{open_elements}};
2513                    $self->{insertion_mode} = AFTER_HEAD_IM;
2514    
2515                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
2516                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2517                    !!!cp ('t143.4');
2518                    #
2519                  } else {
2520                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
2521                  }
2522    
2523                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
2524                  !!!parse-error (type => 'unmatched end tag',
2525                                  text => 'br', token => $token);
2526                  ## Ignore the token
2527                !!!next-token;                !!!next-token;
2528                redo B;                next B;
2529              } elsif ({              } else {
2530                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
2531                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
2532                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
2533                ## Ignore the token                ## Ignore the token
2534                !!!next-token;                !!!next-token;
2535                redo B;                next B;
2536                }
2537    
2538                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2539                  !!!cp ('t146');
2540                  ## As if </noscript>
2541                  pop @{$self->{open_elements}};
2542                  !!!parse-error (type => 'in noscript:/',
2543                                  text => $token->{tag_name}, token => $token);
2544                  
2545                  ## Reprocess in the "in head" insertion mode...
2546                  ## As if </head>
2547                  pop @{$self->{open_elements}};
2548    
2549                  ## Reprocess in the "after head" insertion mode...
2550                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2551                  !!!cp ('t147');
2552                  ## As if </head>
2553                  pop @{$self->{open_elements}};
2554    
2555                  ## Reprocess in the "after head" insertion mode...
2556                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2557    ## ISSUE: This case cannot be reached?
2558                  !!!cp ('t148');
2559                  !!!parse-error (type => 'unmatched end tag',
2560                                  text => $token->{tag_name}, token => $token);
2561                  ## Ignore the token ## ISSUE: An issue in the spec.
2562                  !!!next-token;
2563                  next B;
2564              } else {              } else {
2565                #                !!!cp ('t149');
2566              }              }
           } else {  
             #  
           }  
2567    
2568            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
2569            $in_body->($insert_to_foster);              ## As if <body>
2570            redo B;              !!!insert-element ('body',, $token);
2571          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
2572            if ($token->{type} eq 'character') {              ## reprocess
2573              ## NOTE: This is a code clone of "character in body".              next B;
2574          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2575            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2576              !!!cp ('t149.1');
2577    
2578              ## NOTE: As if <head>
2579              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
2580              $self->{open_elements}->[-1]->[0]->append_child
2581                  ($self->{head_element});
2582              #push @{$self->{open_elements}},
2583              #    [$self->{head_element}, $el_category->{head}];
2584              #$self->{insertion_mode} = IN_HEAD_IM;
2585              ## NOTE: Reprocess.
2586    
2587              ## NOTE: As if </head>
2588              #pop @{$self->{open_elements}};
2589              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2590              ## NOTE: Reprocess.
2591              
2592              #
2593            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2594              !!!cp ('t149.2');
2595    
2596              ## NOTE: As if </head>
2597              pop @{$self->{open_elements}};
2598              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2599              ## NOTE: Reprocess.
2600    
2601              #
2602            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2603              !!!cp ('t149.3');
2604    
2605              !!!parse-error (type => 'in noscript:#eof', token => $token);
2606    
2607              ## As if </noscript>
2608              pop @{$self->{open_elements}};
2609              #$self->{insertion_mode} = IN_HEAD_IM;
2610              ## NOTE: Reprocess.
2611    
2612              ## NOTE: As if </head>
2613              pop @{$self->{open_elements}};
2614              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2615              ## NOTE: Reprocess.
2616    
2617              #
2618            } else {
2619              !!!cp ('t149.4');
2620              #
2621            }
2622    
2623            ## NOTE: As if <body>
2624            !!!insert-element ('body',, $token);
2625            $self->{insertion_mode} = IN_BODY_IM;
2626            ## NOTE: Reprocess.
2627            next B;
2628          } else {
2629            die "$0: $token->{type}: Unknown token type";
2630          }
2631        } elsif ($self->{insertion_mode} & BODY_IMS) {
2632              if ($token->{type} == CHARACTER_TOKEN) {
2633                !!!cp ('t150');
2634                ## NOTE: There is a code clone of "character in body".
2635              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
2636                            
2637              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2638    
2639              !!!next-token;              !!!next-token;
2640              redo B;              next B;
2641            } 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') {  
2642              if ({              if ({
2643                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
2644                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2645                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
2646                !!!parse-error (type => 'not closed:caption');                if (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2647                    ## have an element in table scope
2648                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
2649                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
2650                my $i;                    if ($node->[1] == TABLE_CELL_EL) {
2651                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
2652                  my $node = $self->{open_elements}->[$_];  
2653                  if ($node->[1] eq 'caption') {                      ## Close the cell
2654                    $i = $_;                      !!!back-token; # <x>
2655                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
2656                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
2657                            table => 1, html => 1,                                line => $token->{line},
2658                           }->{$node->[1]}) {                                column => $token->{column}};
2659                    last INSCOPE;                      next B;
2660                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2661                        !!!cp ('t152');
2662                        ## ISSUE: This case can never be reached, maybe.
2663                        last;
2664                      }
2665                  }                  }
2666                } # INSCOPE  
2667                unless (defined $i) {                  !!!cp ('t153');
2668                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
2669                        text => $token->{tag_name}, token => $token);
2670                  ## Ignore the token                  ## Ignore the token
2671                    !!!nack ('t153.1');
2672                  !!!next-token;                  !!!next-token;
2673                  redo B;                  next B;
2674                }                } elsif (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2675                                  !!!parse-error (type => 'not closed', text => 'caption',
2676                ## generate implied end tags                                  token => $token);
2677                if ({                  
2678                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
2679                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
2680                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
2681                  !!!back-token; # <?>                  INSCOPE: {
2682                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
2683                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
2684                  $token = {type => 'end tag',                      if ($node->[1] == CAPTION_EL) {
2685                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
2686                  redo B;                        $i = $_;
2687                }                        last INSCOPE;
2688                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
2689                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
2690                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
2691                }                      }
2692                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
2693    
2694                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
2695                      !!!parse-error (type => 'start tag not allowed',
2696                                      text => $token->{tag_name}, token => $token);
2697                      ## Ignore the token
2698                      !!!nack ('t157.1');
2699                      !!!next-token;
2700                      next B;
2701                    } # INSCOPE
2702                    
2703                    ## generate implied end tags
2704                    while ($self->{open_elements}->[-1]->[1]
2705                               & END_TAG_OPTIONAL_EL) {
2706                      !!!cp ('t158');
2707                      pop @{$self->{open_elements}};
2708                    }
2709    
2710                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2711                redo B;                    !!!cp ('t159');
2712                      !!!parse-error (type => 'not closed',
2713                                      text => $self->{open_elements}->[-1]->[0]
2714                                          ->manakai_local_name,
2715                                      token => $token);
2716                    } else {
2717                      !!!cp ('t160');
2718                    }
2719                    
2720                    splice @{$self->{open_elements}}, $i;
2721                    
2722                    $clear_up_to_marker->();
2723                    
2724                    $self->{insertion_mode} = IN_TABLE_IM;
2725                    
2726                    ## reprocess
2727                    !!!ack-later;
2728                    next B;
2729                  } else {
2730                    !!!cp ('t161');
2731                    #
2732                  }
2733              } else {              } else {
2734                  !!!cp ('t162');
2735                #                #
2736              }              }
2737            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
2738              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
2739                ## have a table element in table scope                if (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2740                my $i;                  ## have an element in table scope
2741                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
2742                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2743                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
2744                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
2745                    last INSCOPE;                      !!!cp ('t163');
2746                  } elsif ({                      $i = $_;
2747                            table => 1, html => 1,                      last INSCOPE;
2748                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
2749                    last INSCOPE;                      !!!cp ('t164');
2750                        last INSCOPE;
2751                      }
2752                    } # INSCOPE
2753                      unless (defined $i) {
2754                        !!!cp ('t165');
2755                        !!!parse-error (type => 'unmatched end tag',
2756                                        text => $token->{tag_name},
2757                                        token => $token);
2758                        ## Ignore the token
2759                        !!!next-token;
2760                        next B;
2761                      }
2762                    
2763                    ## generate implied end tags
2764                    while ($self->{open_elements}->[-1]->[1]
2765                               & END_TAG_OPTIONAL_EL) {
2766                      !!!cp ('t166');
2767                      pop @{$self->{open_elements}};
2768                  }                  }
2769                } # INSCOPE  
2770                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
2771                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
2772                      !!!cp ('t167');
2773                      !!!parse-error (type => 'not closed',
2774                                      text => $self->{open_elements}->[-1]->[0]
2775                                          ->manakai_local_name,
2776                                      token => $token);
2777                    } else {
2778                      !!!cp ('t168');
2779                    }
2780                    
2781                    splice @{$self->{open_elements}}, $i;
2782                    
2783                    $clear_up_to_marker->();
2784                    
2785                    $self->{insertion_mode} = IN_ROW_IM;
2786                    
2787                    !!!next-token;
2788                    next B;
2789                  } elsif (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2790                    !!!cp ('t169');
2791                    !!!parse-error (type => 'unmatched end tag',
2792                                    text => $token->{tag_name}, token => $token);
2793                  ## Ignore the token                  ## Ignore the token
2794                  !!!next-token;                  !!!next-token;
2795                  redo B;                  next B;
2796                }                } else {
2797                                  !!!cp ('t170');
2798                ## 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;  
2799                }                }
2800                } elsif ($token->{tag_name} eq 'caption') {
2801                  if (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2802                    ## have a table element in table scope
2803                    my $i;
2804                    INSCOPE: {
2805                      for (reverse 0..$#{$self->{open_elements}}) {
2806                        my $node = $self->{open_elements}->[$_];
2807                        if ($node->[1] == CAPTION_EL) {
2808                          !!!cp ('t171');
2809                          $i = $_;
2810                          last INSCOPE;
2811                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
2812                          !!!cp ('t172');
2813                          last;
2814                        }
2815                      }
2816    
2817                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
2818                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
2819                                      text => $token->{tag_name}, token => $token);
2820                      ## Ignore the token
2821                      !!!next-token;
2822                      next B;
2823                    } # INSCOPE
2824                    
2825                    ## generate implied end tags
2826                    while ($self->{open_elements}->[-1]->[1]
2827                               & END_TAG_OPTIONAL_EL) {
2828                      !!!cp ('t174');
2829                      pop @{$self->{open_elements}};
2830                    }
2831                    
2832                    unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2833                      !!!cp ('t175');
2834                      !!!parse-error (type => 'not closed',
2835                                      text => $self->{open_elements}->[-1]->[0]
2836                                          ->manakai_local_name,
2837                                      token => $token);
2838                    } else {
2839                      !!!cp ('t176');
2840                    }
2841                    
2842                    splice @{$self->{open_elements}}, $i;
2843                    
2844                    $clear_up_to_marker->();
2845                    
2846                    $self->{insertion_mode} = IN_TABLE_IM;
2847                    
2848                    !!!next-token;
2849                    next B;
2850                  } elsif (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2851                    !!!cp ('t177');
2852                    !!!parse-error (type => 'unmatched end tag',
2853                                    text => $token->{tag_name}, token => $token);
2854                    ## Ignore the token
2855                    !!!next-token;
2856                    next B;
2857                  } else {
2858                    !!!cp ('t178');
2859                    #
2860                }                }
2861                } elsif ({
2862                          table => 1, tbody => 1, tfoot => 1,
2863                          thead => 1, tr => 1,
2864                         }->{$token->{tag_name}} and
2865                         ($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
2866                  ## have an element in table scope
2867                  my $i;
2868                  my $tn;
2869                  INSCOPE: {
2870                    for (reverse 0..$#{$self->{open_elements}}) {
2871                      my $node = $self->{open_elements}->[$_];
2872                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
2873                        !!!cp ('t179');
2874                        $i = $_;
2875    
2876                        ## Close the cell
2877                        !!!back-token; # </x>
2878                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
2879                                  line => $token->{line},
2880                                  column => $token->{column}};
2881                        next B;
2882                      } elsif ($node->[1] == TABLE_CELL_EL) {
2883                        !!!cp ('t180');
2884                        $tn = $node->[0]->manakai_local_name;
2885                        ## NOTE: There is exactly one |td| or |th| element
2886                        ## in scope in the stack of open elements by definition.
2887                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
2888                        ## ISSUE: Can this be reached?
2889                        !!!cp ('t181');
2890                        last;
2891                      }
2892                    }
2893    
2894                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
2895                    !!!parse-error (type => 'unmatched end tag',
2896                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
2897                    ## Ignore the token
2898                $self->{insertion_mode} = 'in table';                  !!!next-token;
2899                    next B;
2900                !!!next-token;                } # INSCOPE
2901                redo B;              } elsif ($token->{tag_name} eq 'table' and
2902              } elsif ($token->{tag_name} eq 'table') {                       ($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2903                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
2904                                  token => $token);
2905    
2906                ## As if </caption>                ## As if </caption>
2907                ## have a table element in table scope                ## have a table element in table scope
2908                my $i;                my $i;
2909                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2910                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
2911                  if ($node->[1] eq 'caption') {                  if ($node->[1] == CAPTION_EL) {
2912                      !!!cp ('t184');
2913                    $i = $_;                    $i = $_;
2914                    last INSCOPE;                    last INSCOPE;
2915                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
2916                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
2917                    last INSCOPE;                    last INSCOPE;
2918                  }                  }
2919                } # INSCOPE                } # INSCOPE
2920                unless (defined $i) {                unless (defined $i) {
2921                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
2922            ## TODO: Wrong error type?
2923                    !!!parse-error (type => 'unmatched end tag',
2924                                    text => 'caption', token => $token);
2925                  ## Ignore the token                  ## Ignore the token
2926                  !!!next-token;                  !!!next-token;
2927                  redo B;                  next B;
2928                }                }
2929                                
2930                ## generate implied end tags                ## generate implied end tags
2931                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
2932                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
2933                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
2934                }                }
2935    
2936                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
2937                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
2938                    !!!parse-error (type => 'not closed',
2939                                    text => $self->{open_elements}->[-1]->[0]
2940                                        ->manakai_local_name,
2941                                    token => $token);
2942                  } else {
2943                    !!!cp ('t189');
2944                }                }
2945    
2946                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
2947    
2948                $clear_up_to_marker->();                $clear_up_to_marker->();
2949    
2950                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
2951    
2952                ## reprocess                ## reprocess
2953                redo B;                next B;
2954              } elsif ({              } elsif ({
2955                        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,  
2956                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2957                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
2958                ## Ignore the token                  !!!cp ('t190');
2959                redo B;                  !!!parse-error (type => 'unmatched end tag',
2960              } else {                                  text => $token->{tag_name}, token => $token);
               #  
             }  
           } 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');  
2961                  ## Ignore the token                  ## Ignore the token
2962                  !!!next-token;                  !!!next-token;
2963                  redo B;                  next B;
2964                } else {                } else {
2965                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
2966                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
2967                }                }
2968              } elsif ($token->{tag_name} eq 'col') {          } elsif ({
2969                !!!parse-error (type => 'unmatched end tag:col');                    tbody => 1, tfoot => 1,
2970                ## Ignore the token                    thead => 1, tr => 1,
2971                !!!next-token;                   }->{$token->{tag_name}} and
2972                redo B;                   ($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
2973              } else {            !!!cp ('t192');
2974                #            !!!parse-error (type => 'unmatched end tag',
2975              }                            text => $token->{tag_name}, token => $token);
2976            } else {            ## Ignore the token
2977              #            !!!next-token;
2978              next B;
2979            } else {
2980              !!!cp ('t193');
2981              #
2982            }
2983          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2984            for my $entry (@{$self->{open_elements}}) {
2985              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
2986                !!!cp ('t75');
2987                !!!parse-error (type => 'in body:#eof', token => $token);
2988                last;
2989            }            }
2990            }
2991    
2992            ## As if </colgroup>          ## Stop parsing.
2993            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
2994              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
2995              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
2996          }
2997    
2998          $insert = $insert_to_current;
2999          #
3000        } elsif ($self->{insertion_mode} & TABLE_IMS) {
3001          if ($token->{type} == CHARACTER_TOKEN) {
3002            if (not $open_tables->[-1]->[1] and # tainted
3003                $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3004              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3005                  
3006              unless (length $token->{data}) {
3007                !!!cp ('t194');
3008              !!!next-token;              !!!next-token;
3009              redo B;              next B;
3010            } else {            } else {
3011              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
3012            }            }
3013          } elsif ($self->{insertion_mode} eq 'in table body') {          }
           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');  
3014    
3015              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table:#text', token => $token);
             ## 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);  
3016    
3017              if ({          ## NOTE: As if in body, but insert into the foster parent element.
3018                   table => 1, tbody => 1, tfoot => 1,          $reconstruct_active_formatting_elements->($insert_to_foster);
3019                   thead => 1, tr => 1,              
3020                  }->{$self->{open_elements}->[-1]->[1]}) {          if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3021                # MUST            # MUST
3022                my $foster_parent_element;            my $foster_parent_element;
3023                my $next_sibling;            my $next_sibling;
3024                my $prev_sibling;            my $prev_sibling;
3025                OE: for (reverse 0..$#{$self->{open_elements}}) {            OE: for (reverse 0..$#{$self->{open_elements}}) {
3026                  if ($self->{open_elements}->[$_]->[1] eq 'table') {              if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
3027                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3028                    if (defined $parent and $parent->node_type == 1) {                if (defined $parent and $parent->node_type == 1) {
3029                      $foster_parent_element = $parent;                  $foster_parent_element = $parent;
3030                      $next_sibling = $self->{open_elements}->[$_]->[0];                  !!!cp ('t196');
3031                      $prev_sibling = $next_sibling->previous_sibling;                  $next_sibling = $self->{open_elements}->[$_]->[0];
3032                    } else {                  $prev_sibling = $next_sibling->previous_sibling;
3033                      $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});  
3034                } else {                } else {
3035                  $foster_parent_element->insert_before                  !!!cp ('t197');
3036                    ($self->{document}->create_text_node ($token->{data}),                  $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3037                     $next_sibling);                  $prev_sibling = $foster_parent_element->last_child;
3038                }                  #
3039              } else {                }
3040                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                last OE;
3041                }
3042              } # OE
3043              $foster_parent_element = $self->{open_elements}->[0]->[0] and
3044              $prev_sibling = $foster_parent_element->last_child
3045                  unless defined $foster_parent_element;
3046              undef $prev_sibling unless $open_tables->[-1]->[2]; # ~node inserted
3047              if (defined $prev_sibling and
3048                  $prev_sibling->node_type == 3) {
3049                !!!cp ('t198');
3050                $prev_sibling->manakai_append_text ($token->{data});
3051              } else {
3052                !!!cp ('t199');
3053                $foster_parent_element->insert_before
3054                    ($self->{document}->create_text_node ($token->{data}),
3055                     $next_sibling);
3056              }
3057              $open_tables->[-1]->[1] = 1; # tainted
3058              $open_tables->[-1]->[2] = 1; # ~node inserted
3059            } else {
3060              ## NOTE: Fragment case or in a foster parent'ed element
3061              ## (e.g. |<table><span>a|).  In fragment case, whether the
3062              ## character is appended to existing node or a new node is
3063              ## created is irrelevant, since the foster parent'ed nodes
3064              ## are discarded and fragment parsing does not invoke any
3065              ## script.
3066              !!!cp ('t200');
3067              $self->{open_elements}->[-1]->[0]->manakai_append_text
3068                  ($token->{data});
3069            }
3070                
3071            !!!next-token;
3072            next B;
3073          } elsif ($token->{type} == START_TAG_TOKEN) {
3074            if ({
3075                 tr => (($self->{insertion_mode} & IM_MASK) != IN_ROW_IM),
3076                 th => 1, td => 1,
3077                }->{$token->{tag_name}}) {
3078              if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_IM) {
3079                ## Clear back to table context
3080                while (not ($self->{open_elements}->[-1]->[1]
3081                                & TABLE_SCOPING_EL)) {
3082                  !!!cp ('t201');
3083                  pop @{$self->{open_elements}};
3084              }              }
3085                            
3086              !!!next-token;              !!!insert-element ('tbody',, $token);
3087              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
3088            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
3089              ## Copied from 'in table'            }
3090              my $comment = $self->{document}->create_comment ($token->{data});            
3091              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3092              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
3093              redo B;                !!!cp ('t202');
3094            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
3095              if ({              }
3096                   tr => 1,                  
3097                   th => 1, td => 1,              ## Clear back to table body context
3098                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
3099                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
3100                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
3101                }                ## ISSUE: Can this case be reached?
3102                  pop @{$self->{open_elements}};
3103                }
3104                    
3105                $self->{insertion_mode} = IN_ROW_IM;
3106                if ($token->{tag_name} eq 'tr') {
3107                  !!!cp ('t204');
3108                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3109                  $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3110                  !!!nack ('t204');
3111                  !!!next-token;
3112                  next B;
3113                } else {
3114                  !!!cp ('t205');
3115                  !!!insert-element ('tr',, $token);
3116                  ## reprocess in the "in row" insertion mode
3117                }
3118              } else {
3119                !!!cp ('t206');
3120              }
3121    
3122                ## Clear back to table body context                ## Clear back to table row context
3123                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3124                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
3125                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3126                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3127                }                }
3128                                
3129                $self->{insertion_mode} = 'in row';            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3130                if ($token->{tag_name} eq 'tr') {            $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3131                  !!!insert-element ($token->{tag_name}, $token->{attributes});            $self->{insertion_mode} = IN_CELL_IM;
3132                  !!!next-token;  
3133                } else {            push @$active_formatting_elements, ['#marker', ''];
3134                  !!!insert-element ('tr');                
3135                  ## reprocess            !!!nack ('t207.1');
3136                }            !!!next-token;
3137                redo B;            next B;
3138              } elsif ({          } elsif ({
3139                        caption => 1, col => 1, colgroup => 1,                    caption => 1, col => 1, colgroup => 1,
3140                        tbody => 1, tfoot => 1, thead => 1,                    tbody => 1, tfoot => 1, thead => 1,
3141                       }->{$token->{tag_name}}) {                    tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3142                ## have an element in table scope                   }->{$token->{tag_name}}) {
3143                my $i;            if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3144                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              ## As if </tr>
3145                  my $node = $self->{open_elements}->[$_];              ## have an element in table scope
3146                  if ({              my $i;
3147                       tbody => 1, thead => 1, tfoot => 1,              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3148                      }->{$node->[1]}) {                my $node = $self->{open_elements}->[$_];
3149                    $i = $_;                if ($node->[1] == TABLE_ROW_EL) {
3150                    last INSCOPE;                  !!!cp ('t208');
3151                  } elsif ({                  $i = $_;
3152                            table => 1, html => 1,                  last INSCOPE;
3153                           }->{$node->[1]}) {                } elsif ($node->[1] & TABLE_SCOPING_EL) {
3154                    last INSCOPE;                  !!!cp ('t209');
3155                    last INSCOPE;
3156                  }
3157                } # INSCOPE
3158                unless (defined $i) {
3159                  !!!cp ('t210');
3160                  ## TODO: This type is wrong.
3161                  !!!parse-error (type => 'unmacthed end tag',
3162                                  text => $token->{tag_name}, token => $token);
3163                  ## Ignore the token
3164                  !!!nack ('t210.1');
3165                  !!!next-token;
3166                  next B;
3167                }
3168                    
3169                    ## Clear back to table row context
3170                    while (not ($self->{open_elements}->[-1]->[1]
3171                                    & TABLE_ROW_SCOPING_EL)) {
3172                      !!!cp ('t211');
3173                      ## ISSUE: Can this case be reached?
3174                      pop @{$self->{open_elements}};
3175                    }
3176                    
3177                    pop @{$self->{open_elements}}; # tr
3178                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3179                    if ($token->{tag_name} eq 'tr') {
3180                      !!!cp ('t212');
3181                      ## reprocess
3182                      !!!ack-later;
3183                      next B;
3184                    } else {
3185                      !!!cp ('t213');
3186                      ## reprocess in the "in table body" insertion mode...
3187                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3188                }                }
3189    
3190                ## Clear back to table body context                if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3191                while (not {                  ## have an element in table scope
3192                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3193                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3194                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3195                      if ($node->[1] == TABLE_ROW_GROUP_EL) {
3196                        !!!cp ('t214');
3197                        $i = $_;
3198                        last INSCOPE;
3199                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
3200                        !!!cp ('t215');
3201                        last INSCOPE;
3202                      }
3203                    } # INSCOPE
3204                    unless (defined $i) {
3205                      !!!cp ('t216');
3206    ## TODO: This erorr type is wrong.
3207                      !!!parse-error (type => 'unmatched end tag',
3208                                      text => $token->{tag_name}, token => $token);
3209                      ## Ignore the token
3210                      !!!nack ('t216.1');
3211                      !!!next-token;
3212                      next B;
3213                    }
3214    
3215                    ## Clear back to table body context
3216                    while (not ($self->{open_elements}->[-1]->[1]
3217                                    & TABLE_ROWS_SCOPING_EL)) {
3218                      !!!cp ('t217');
3219                      ## ISSUE: Can this state be reached?
3220                      pop @{$self->{open_elements}};
3221                    }
3222                    
3223                    ## As if <{current node}>
3224                    ## have an element in table scope
3225                    ## true by definition
3226                    
3227                    ## Clear back to table body context
3228                    ## nop by definition
3229                    
3230                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3231                    $self->{insertion_mode} = IN_TABLE_IM;
3232                    ## reprocess in "in table" insertion mode...
3233                  } else {
3234                    !!!cp ('t218');
3235                }                }
3236    
3237                ## As if <{current node}>            if ($token->{tag_name} eq 'col') {
3238                ## have an element in table scope              ## Clear back to table context
3239                ## true by definition              while (not ($self->{open_elements}->[-1]->[1]
3240                                & TABLE_SCOPING_EL)) {
3241                ## Clear back to table body context                !!!cp ('t219');
3242                ## nop by definition                ## ISSUE: Can this state be reached?
   
3243                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3244                $self->{insertion_mode} = 'in table';              }
3245                ## reprocess              
3246                redo B;              !!!insert-element ('colgroup',, $token);
3247                $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3248                ## reprocess
3249                $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3250                !!!ack-later;
3251                next B;
3252              } elsif ({
3253                        caption => 1,
3254                        colgroup => 1,
3255                        tbody => 1, tfoot => 1, thead => 1,
3256                       }->{$token->{tag_name}}) {
3257                ## Clear back to table context
3258                    while (not ($self->{open_elements}->[-1]->[1]
3259                                    & TABLE_SCOPING_EL)) {
3260                      !!!cp ('t220');
3261                      ## ISSUE: Can this state be reached?
3262                      pop @{$self->{open_elements}};
3263                    }
3264                    
3265                push @$active_formatting_elements, ['#marker', '']
3266                    if $token->{tag_name} eq 'caption';
3267                    
3268                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3269                $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3270                $self->{insertion_mode} = {
3271                                           caption => IN_CAPTION_IM,
3272                                           colgroup => IN_COLUMN_GROUP_IM,
3273                                           tbody => IN_TABLE_BODY_IM,
3274                                           tfoot => IN_TABLE_BODY_IM,
3275                                           thead => IN_TABLE_BODY_IM,
3276                                          }->{$token->{tag_name}};
3277                !!!next-token;
3278                !!!nack ('t220.1');
3279                next B;
3280              } else {
3281                die "$0: in table: <>: $token->{tag_name}";
3282              }
3283              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3284                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
3285                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
3286                                      ->manakai_local_name,
3287                                  token => $token);
3288    
3289                ## As if </table>                ## As if </table>
3290                ## have a table element in table scope                ## have a table element in table scope
3291                my $i;                my $i;
3292                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3293                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3294                  if ($node->[1] eq 'table') {                  if ($node->[1] == TABLE_EL) {
3295                      !!!cp ('t221');
3296                    $i = $_;                    $i = $_;
3297                    last INSCOPE;                    last INSCOPE;
3298                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
3299                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
3300                    last INSCOPE;                    last INSCOPE;
3301                  }                  }
3302                } # INSCOPE                } # INSCOPE
3303                unless (defined $i) {                unless (defined $i) {
3304                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
3305    ## TODO: The following is wrong, maybe.
3306                    !!!parse-error (type => 'unmatched end tag', text => 'table',
3307                                    token => $token);
3308                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
3309                    !!!nack ('t223.1');
3310                  !!!next-token;                  !!!next-token;
3311                  redo B;                  next B;
3312                }                }
3313                                
3314    ## TODO: Followings are removed from the latest spec.
3315                ## generate implied end tags                ## generate implied end tags
3316                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
3317                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
3318                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
3319                }                }
3320    
3321                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] == TABLE_EL) {
3322                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
3323                    ## NOTE: |<table><tr><table>|
3324                    !!!parse-error (type => 'not closed',
3325                                    text => $self->{open_elements}->[-1]->[0]
3326                                        ->manakai_local_name,
3327                                    token => $token);
3328                  } else {
3329                    !!!cp ('t226');
3330                }                }
3331    
3332                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3333                  pop @{$open_tables};
3334    
3335                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3336    
3337                ## reprocess            ## reprocess
3338                redo B;            !!!ack-later;
3339              } else {            next B;
3340                #          } elsif ($token->{tag_name} eq 'style') {
3341              }            if (not $open_tables->[-1]->[1]) { # tainted
3342            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
3343              if ({              ## NOTE: This is a "as if in head" code clone.
3344                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
3345                  }->{$token->{tag_name}}) {              $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3346                ## have an element in table scope              next B;
3347                my $i;            } else {
3348                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              !!!cp ('t227.7');
3349                  my $node = $self->{open_elements}->[$_];              #
3350                  if ($node->[1] eq $token->{tag_name}) {            }
3351                    $i = $_;          } elsif ($token->{tag_name} eq 'script') {
3352                    last INSCOPE;            if (not $open_tables->[-1]->[1]) { # tainted
3353                  } elsif ({              !!!cp ('t227.6');
3354                            table => 1, html => 1,              ## NOTE: This is a "as if in head" code clone.
3355                           }->{$node->[1]}) {              $script_start_tag->();
3356                    last INSCOPE;              $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3357                  }              next B;
3358                } # INSCOPE            } else {
3359                unless (defined $i) {              !!!cp ('t227.5');
3360                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              #
3361                  ## Ignore the token            }
3362                  !!!next-token;          } elsif ($token->{tag_name} eq 'input') {
3363                  redo B;            if (not $open_tables->[-1]->[1]) { # tainted
3364                }              if ($token->{attributes}->{type}) { ## TODO: case
3365                  my $type = lc $token->{attributes}->{type}->{value};
3366                  if ($type eq 'hidden') {
3367                    !!!cp ('t227.3');
3368                    !!!parse-error (type => 'in table',
3369                                    text => $token->{tag_name}, token => $token);
3370    
3371                ## Clear back to table body context                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3372                while (not {                  $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
                 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}};  
               }  
3373    
3374                pop @{$self->{open_elements}};                  ## TODO: form element pointer
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $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;  
               }  
3375    
               ## 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]);  
3376                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
3377    
3378                ## Clear back to table body context                  !!!next-token;
3379                ## nop by definition                  !!!ack ('t227.2.1');
3380                    next B;
3381                pop @{$self->{open_elements}};                } else {
3382                $self->{insertion_mode} = 'in table';                  !!!cp ('t227.2');
3383                ## reprocess                  #
3384                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;  
3385              } else {              } else {
3386                  !!!cp ('t227.1');
3387                #                #
3388              }              }
3389            } else {            } else {
3390                !!!cp ('t227.4');
3391              #              #
3392            }            }
3393                      } else {
3394            ## As if in table            !!!cp ('t227');
3395            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
3396            $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;  
               }  
             }  
3397    
3398              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table', text => $token->{tag_name},
3399                            token => $token);
3400    
3401              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
3402              ## ISSUE: Spec says that "whenever a node would be inserted          #
3403              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
3404              ## result in a new Text node.          if ($token->{tag_name} eq 'tr' and
3405              $reconstruct_active_formatting_elements->($insert_to_foster);              ($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3406                          ## have an element in table scope
             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});  
               } 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  
3407                my $i;                my $i;
3408                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3409                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3410                  if ($node->[1] eq 'tr') {                  if ($node->[1] == TABLE_ROW_EL) {
3411                      !!!cp ('t228');
3412                    $i = $_;                    $i = $_;
3413                    last INSCOPE;                    last INSCOPE;
3414                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
3415                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
3416                    last INSCOPE;                    last INSCOPE;
3417                  }                  }
3418                } # INSCOPE                } # INSCOPE
3419                unless (defined $i) {                unless (defined $i) {
3420                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
3421                    !!!parse-error (type => 'unmatched end tag',
3422                                    text => $token->{tag_name}, token => $token);
3423                  ## Ignore the token                  ## Ignore the token
3424                    !!!nack ('t230.1');
3425                  !!!next-token;                  !!!next-token;
3426                  redo B;                  next B;
3427                  } else {
3428                    !!!cp ('t232');
3429                }                }
3430    
3431                ## Clear back to table row context                ## Clear back to table row context
3432                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3433                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
3434                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
3435                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
3436                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3437                }                }
3438    
3439                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3440                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3441                ## reprocess                !!!next-token;
3442                redo B;                !!!nack ('t231.1');
3443                  next B;
3444              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3445                ## NOTE: This is a code clone of "table in table"                if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3446                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
3447                    ## have an element in table scope
3448                ## As if </table>                  my $i;
3449                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3450                my $i;                    my $node = $self->{open_elements}->[$_];
3451                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] == TABLE_ROW_EL) {
3452                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
3453                  if ($node->[1] eq 'table') {                      $i = $_;
3454                    $i = $_;                      last INSCOPE;
3455                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
3456                  } elsif ({                      !!!cp ('t234');
3457                            table => 1, html => 1,                      last INSCOPE;
3458                           }->{$node->[1]}) {                    }
3459                    last INSCOPE;                  } # INSCOPE
3460                    unless (defined $i) {
3461                      !!!cp ('t235');
3462    ## TODO: The following is wrong.
3463                      !!!parse-error (type => 'unmatched end tag',
3464                                      text => $token->{type}, token => $token);
3465                      ## Ignore the token
3466                      !!!nack ('t236.1');
3467                      !!!next-token;
3468                      next B;
3469                  }                  }
3470                } # INSCOPE                  
3471                unless (defined $i) {                  ## Clear back to table row context
3472                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
3473                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
3474                  !!!next-token;                    !!!cp ('t236');
3475                  redo B;  ## ISSUE: Can this state be reached?
3476                }                    pop @{$self->{open_elements}};
3477                                  }
3478                ## generate implied end tags                  
3479                if ({                  pop @{$self->{open_elements}}; # tr
3480                     dd => 1, dt => 1, li => 1, p => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3481                     td => 1, th => 1, tr => 1,                  ## reprocess in the "in table body" insertion mode...
3482                    }->{$self->{open_elements}->[-1]->[1]}) {                }
3483                  !!!back-token; # <table>  
3484                  $token = {type => 'end tag', tag_name => 'table'};                if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3485                  !!!back-token;                  ## have an element in table scope
3486                  $token = {type => 'end tag',                  my $i;
3487                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3488                  redo B;                    my $node = $self->{open_elements}->[$_];
3489                }                    if ($node->[1] == TABLE_ROW_GROUP_EL) {
3490                        !!!cp ('t237');
3491                if ($self->{open_elements}->[-1]->[1] ne 'table') {                      $i = $_;
3492                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      last INSCOPE;
3493                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
3494                        !!!cp ('t238');
3495                        last INSCOPE;
3496                      }
3497                    } # INSCOPE
3498                    unless (defined $i) {
3499                      !!!cp ('t239');
3500                      !!!parse-error (type => 'unmatched end tag',
3501                                      text => $token->{tag_name}, token => $token);
3502                      ## Ignore the token
3503                      !!!nack ('t239.1');
3504                      !!!next-token;
3505                      next B;
3506                    }
3507                    
3508                    ## Clear back to table body context
3509                    while (not ($self->{open_elements}->[-1]->[1]
3510                                    & TABLE_ROWS_SCOPING_EL)) {
3511                      !!!cp ('t240');
3512                      pop @{$self->{open_elements}};
3513                    }
3514                    
3515                    ## As if <{current node}>
3516                    ## have an element in table scope
3517                    ## true by definition
3518                    
3519                    ## Clear back to table body context
3520                    ## nop by definition
3521                    
3522                    pop @{$self->{open_elements}};
3523                    $self->{insertion_mode} = IN_TABLE_IM;
3524                    ## reprocess in the "in table" insertion mode...
3525                }                }
3526    
3527                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
3528                  ## When you edit the code fragment below, please ensure that
3529                $self->_reset_insertion_mode;                ## the code for <table> in the "in table" insertion mode
3530                  ## is synced with it.
3531    
3532                ## reprocess                ## have a table element in table scope
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
3533                my $i;                my $i;
3534                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3535                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3536                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] == TABLE_EL) {
3537                      !!!cp ('t241');
3538                    $i = $_;                    $i = $_;
3539                    last INSCOPE;                    last INSCOPE;
3540                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
3541                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
3542                    last INSCOPE;                    last INSCOPE;
3543                  }                  }
3544                } # INSCOPE                } # INSCOPE
3545                unless (defined $i) {                unless (defined $i) {
3546                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
3547                    !!!parse-error (type => 'unmatched end tag',
3548                                    text => $token->{tag_name}, token => $token);
3549                  ## Ignore the token                  ## Ignore the token
3550                    !!!nack ('t243.1');
3551                  !!!next-token;                  !!!next-token;
3552                  redo B;                  next B;
3553                }                }
3554                    
3555                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
3556                while (not {                pop @{$open_tables};
3557                  tr => 1, html => 1,                
3558                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
3559                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
3560                  pop @{$self->{open_elements}};                !!!next-token;
3561                  next B;
3562                } elsif ({
3563                          tbody => 1, tfoot => 1, thead => 1,
3564                         }->{$token->{tag_name}} and
3565                         $self->{insertion_mode} & ROW_IMS) {
3566                  if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3567                    ## have an element in table scope
3568                    my $i;
3569                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3570                      my $node = $self->{open_elements}->[$_];
3571                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3572                        !!!cp ('t247');
3573                        $i = $_;
3574                        last INSCOPE;
3575                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
3576                        !!!cp ('t248');
3577                        last INSCOPE;
3578                      }
3579                    } # INSCOPE
3580                      unless (defined $i) {
3581                        !!!cp ('t249');
3582                        !!!parse-error (type => 'unmatched end tag',
3583                                        text => $token->{tag_name}, token => $token);
3584                        ## Ignore the token
3585                        !!!nack ('t249.1');
3586                        !!!next-token;
3587                        next B;
3588                      }
3589                    
3590                    ## As if </tr>
3591                    ## have an element in table scope
3592                    my $i;
3593                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3594                      my $node = $self->{open_elements}->[$_];
3595                      if ($node->[1] == TABLE_ROW_EL) {
3596                        !!!cp ('t250');
3597                        $i = $_;
3598                        last INSCOPE;
3599                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
3600                        !!!cp ('t251');
3601                        last INSCOPE;
3602                      }
3603                    } # INSCOPE
3604                      unless (defined $i) {
3605                        !!!cp ('t252');
3606                        !!!parse-error (type => 'unmatched end tag',
3607                                        text => 'tr', token => $token);
3608                        ## Ignore the token
3609                        !!!nack ('t252.1');
3610                        !!!next-token;
3611                        next B;
3612                      }
3613                    
3614                    ## Clear back to table row context
3615                    while (not ($self->{open_elements}->[-1]->[1]
3616                                    & TABLE_ROW_SCOPING_EL)) {
3617                      !!!cp ('t253');
3618    ## ISSUE: Can this case be reached?
3619                      pop @{$self->{open_elements}};
3620                    }
3621                    
3622                    pop @{$self->{open_elements}}; # tr
3623                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3624                    ## reprocess in the "in table body" insertion mode...
3625                }                }
3626    
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
3627                ## have an element in table scope                ## have an element in table scope
3628                my $i;                my $i;
3629                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3630                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3631                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3632                      !!!cp ('t254');
3633                    $i = $_;                    $i = $_;
3634                    last INSCOPE;                    last INSCOPE;
3635                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
3636                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
3637                    last INSCOPE;                    last INSCOPE;
3638                  }                  }
3639                } # INSCOPE                } # INSCOPE
3640                unless (defined $i) {                unless (defined $i) {
3641                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t256');
3642                    !!!parse-error (type => 'unmatched end tag',
3643                                    text => $token->{tag_name}, token => $token);
3644                  ## Ignore the token                  ## Ignore the token
3645                    !!!nack ('t256.1');
3646                  !!!next-token;                  !!!next-token;
3647                  redo B;                  next B;
3648                }                }
3649    
3650                ## Clear back to table row context                ## Clear back to table body context
3651                while (not {                while (not ($self->{open_elements}->[-1]->[1]
3652                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
3653                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
3654                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
3655                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3656                }                }
3657    
3658                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
3659                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
3660                ## reprocess                !!!nack ('t257.1');
3661                redo B;                !!!next-token;
3662                  next B;
3663              } elsif ({              } elsif ({
3664                        tbody => 1, tfoot => 1, thead => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3665                          html => 1, td => 1, th => 1,
3666                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3667                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3668                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3669                ## have an element in table scope            !!!cp ('t258');
3670                my $i;            !!!parse-error (type => 'unmatched end tag',
3671                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                            text => $token->{tag_name}, token => $token);
3672                  my $node = $self->{open_elements}->[$_];            ## Ignore the token
3673                  if ($node->[1] eq $token->{tag_name}) {            !!!nack ('t258.1');
3674                    $i = $_;             !!!next-token;
3675                    last INSCOPE;            next B;
3676                  } elsif ({          } else {
3677                            table => 1, html => 1,            !!!cp ('t259');
3678                           }->{$node->[1]}) {            !!!parse-error (type => 'in table:/',
3679                    last INSCOPE;                            text => $token->{tag_name}, token => $token);
3680                  }  
3681                } # INSCOPE            $insert = $insert_to_foster;
3682                unless (defined $i) {            #
3683                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          }
3684                  ## Ignore the token        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3685            unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
3686                    @{$self->{open_elements}} == 1) { # redundant, maybe
3687              !!!parse-error (type => 'in body:#eof', token => $token);
3688              !!!cp ('t259.1');
3689              #
3690            } else {
3691              !!!cp ('t259.2');
3692              #
3693            }
3694    
3695            ## Stop parsing
3696            last B;
3697          } else {
3698            die "$0: $token->{type}: Unknown token type";
3699          }
3700        } elsif (($self->{insertion_mode} & IM_MASK) == IN_COLUMN_GROUP_IM) {
3701              if ($token->{type} == CHARACTER_TOKEN) {
3702                if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3703                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3704                  unless (length $token->{data}) {
3705                    !!!cp ('t260');
3706                  !!!next-token;                  !!!next-token;
3707                  redo B;                  next B;
3708                }                }
3709                }
3710                ## As if </tr>              
3711                ## have an element in table scope              !!!cp ('t261');
3712                my $i;              #
3713                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3714                  my $node = $self->{open_elements}->[$_];              if ($token->{tag_name} eq 'col') {
3715                  if ($node->[1] eq 'tr') {                !!!cp ('t262');
3716                    $i = $_;                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3717                    last INSCOPE;                pop @{$self->{open_elements}};
3718                  } elsif ({                !!!ack ('t262.1');
3719                            table => 1, html => 1,                !!!next-token;
3720                           }->{$node->[1]}) {                next B;
3721                    last INSCOPE;              } else {
3722                  }                !!!cp ('t263');
3723                } # INSCOPE                #
3724                unless (defined $i) {              }
3725                  !!!parse-error (type => 'unmatched end tag:tr');            } elsif ($token->{type} == END_TAG_TOKEN) {
3726                if ($token->{tag_name} eq 'colgroup') {
3727                  if ($self->{open_elements}->[-1]->[1] == HTML_EL) {
3728                    !!!cp ('t264');
3729                    !!!parse-error (type => 'unmatched end tag',
3730                                    text => 'colgroup', token => $token);
3731                  ## Ignore the token                  ## Ignore the token
3732                  !!!next-token;                  !!!next-token;
3733                  redo B;                  next B;
3734                }                } else {
3735                    !!!cp ('t265');
3736                ## Clear back to table row context                  pop @{$self->{open_elements}}; # colgroup
3737                while (not {                  $self->{insertion_mode} = IN_TABLE_IM;
3738                  tr => 1, html => 1,                  !!!next-token;
3739                }->{$self->{open_elements}->[-1]->[1]}) {                  next B;            
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
3740                }                }
3741                } elsif ($token->{tag_name} eq 'col') {
3742                pop @{$self->{open_elements}}; # tr                !!!cp ('t266');
3743                $self->{insertion_mode} = 'in table body';                !!!parse-error (type => 'unmatched end tag',
3744                ## reprocess                                text => 'col', token => $token);
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
3745                ## Ignore the token                ## Ignore the token
3746                !!!next-token;                !!!next-token;
3747                redo B;                next B;
3748              } else {              } else {
3749                #                !!!cp ('t267');
3750                  #
3751              }              }
3752          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3753            if ($self->{open_elements}->[-1]->[1] == HTML_EL and
3754                @{$self->{open_elements}} == 1) { # redundant, maybe
3755              !!!cp ('t270.2');
3756              ## Stop parsing.
3757              last B;
3758            } else {
3759              ## NOTE: As if </colgroup>.
3760              !!!cp ('t270.1');
3761              pop @{$self->{open_elements}}; # colgroup
3762              $self->{insertion_mode} = IN_TABLE_IM;
3763              ## Reprocess.
3764              next B;
3765            }
3766          } else {
3767            die "$0: $token->{type}: Unknown token type";
3768          }
3769    
3770              ## As if </colgroup>
3771              if ($self->{open_elements}->[-1]->[1] == HTML_EL) {
3772                !!!cp ('t269');
3773    ## TODO: Wrong error type?
3774                !!!parse-error (type => 'unmatched end tag',
3775                                text => 'colgroup', token => $token);
3776                ## Ignore the token
3777                !!!nack ('t269.1');
3778                !!!next-token;
3779                next B;
3780            } else {            } else {
3781              #              !!!cp ('t270');
3782                pop @{$self->{open_elements}}; # colgroup
3783                $self->{insertion_mode} = IN_TABLE_IM;
3784                !!!ack-later;
3785                ## reprocess
3786                next B;
3787              }
3788        } elsif ($self->{insertion_mode} & SELECT_IMS) {
3789          if ($token->{type} == CHARACTER_TOKEN) {
3790            !!!cp ('t271');
3791            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3792            !!!next-token;
3793            next B;
3794          } elsif ($token->{type} == START_TAG_TOKEN) {
3795            if ($token->{tag_name} eq 'option') {
3796              if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3797                !!!cp ('t272');
3798                ## As if </option>
3799                pop @{$self->{open_elements}};
3800              } else {
3801                !!!cp ('t273');
3802            }            }
3803    
3804            ## As if in table            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3805            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!nack ('t273.1');
3806            $in_body->($insert_to_foster);            !!!next-token;
3807            redo B;            next B;
3808          } elsif ($self->{insertion_mode} eq 'in cell') {          } elsif ($token->{tag_name} eq 'optgroup') {
3809            if ($token->{type} eq 'character') {            if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3810              ## NOTE: This is a code clone of "character in body".              !!!cp ('t274');
3811              $reconstruct_active_formatting_elements->($insert_to_current);              ## As if </option>
3812                            pop @{$self->{open_elements}};
3813              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            } else {
3814                !!!cp ('t275');
3815              }
3816    
3817              if ($self->{open_elements}->[-1]->[1] == OPTGROUP_EL) {
3818                !!!cp ('t276');
3819                ## As if </optgroup>
3820                pop @{$self->{open_elements}};
3821              } else {
3822                !!!cp ('t277');
3823              }
3824    
3825              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3826              !!!nack ('t277.1');
3827              !!!next-token;
3828              next B;
3829            } elsif ({
3830                       select => 1, input => 1, textarea => 1,
3831                     }->{$token->{tag_name}} or
3832                     (($self->{insertion_mode} & IM_MASK)
3833                          == IN_SELECT_IN_TABLE_IM and
3834                      {
3835                       caption => 1, table => 1,
3836                       tbody => 1, tfoot => 1, thead => 1,
3837                       tr => 1, td => 1, th => 1,
3838                      }->{$token->{tag_name}})) {
3839              ## TODO: The type below is not good - <select> is replaced by </select>
3840              !!!parse-error (type => 'not closed', text => 'select',
3841                              token => $token);
3842              ## NOTE: As if the token were </select> (<select> case) or
3843              ## as if there were </select> (otherwise).
3844              ## have an element in table scope
3845              my $i;
3846              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3847                my $node = $self->{open_elements}->[$_];
3848                if ($node->[1] == SELECT_EL) {
3849                  !!!cp ('t278');
3850                  $i = $_;
3851                  last INSCOPE;
3852                } elsif ($node->[1] & TABLE_SCOPING_EL) {
3853                  !!!cp ('t279');
3854                  last INSCOPE;
3855                }
3856              } # INSCOPE
3857              unless (defined $i) {
3858                !!!cp ('t280');
3859                !!!parse-error (type => 'unmatched end tag',
3860                                text => 'select', token => $token);
3861                ## Ignore the token
3862                !!!nack ('t280.1');
3863              !!!next-token;              !!!next-token;
3864              redo B;              next B;
3865            } elsif ($token->{type} eq 'comment') {            }
3866              ## NOTE: This is a code clone of "comment in body".                
3867              my $comment = $self->{document}->create_comment ($token->{data});            !!!cp ('t281');
3868              $self->{open_elements}->[-1]->[0]->append_child ($comment);            splice @{$self->{open_elements}}, $i;
3869    
3870              $self->_reset_insertion_mode;
3871    
3872              if ($token->{tag_name} eq 'select') {
3873                !!!nack ('t281.2');
3874              !!!next-token;              !!!next-token;
3875              redo B;              next B;
3876            } elsif ($token->{type} eq 'start tag') {            } else {
3877              if ({              !!!cp ('t281.1');
3878                   caption => 1, col => 1, colgroup => 1,              !!!ack-later;
3879                   tbody => 1, td => 1, tfoot => 1, th => 1,              ## Reprocess the token.
3880                   thead => 1, tr => 1,              next B;
3881                  }->{$token->{tag_name}}) {            }
3882                ## have an element in table scope          } else {
3883                my $tn;            !!!cp ('t282');
3884                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            !!!parse-error (type => 'in select',
3885                  my $node = $self->{open_elements}->[$_];                            text => $token->{tag_name}, token => $token);
3886                  if ($node->[1] eq 'td' or $node->[1] eq 'th') {            ## Ignore the token
3887                    $tn = $node->[1];            !!!nack ('t282.1');
3888                    last INSCOPE;            !!!next-token;
3889                  } elsif ({            next B;
3890                            table => 1, html => 1,          }
3891                           }->{$node->[1]}) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3892                    last INSCOPE;          if ($token->{tag_name} eq 'optgroup') {
3893                  }            if ($self->{open_elements}->[-1]->[1] == OPTION_EL and
3894                } # INSCOPE                $self->{open_elements}->[-2]->[1] == OPTGROUP_EL) {
3895                unless (defined $tn) {              !!!cp ('t283');
3896                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              ## As if </option>
3897                  ## Ignore the token              splice @{$self->{open_elements}}, -2;
3898                  !!!next-token;            } elsif ($self->{open_elements}->[-1]->[1] == OPTGROUP_EL) {
3899                  redo B;              !!!cp ('t284');
3900                }              pop @{$self->{open_elements}};
3901              } else {
3902                !!!cp ('t285');
3903                !!!parse-error (type => 'unmatched end tag',
3904                                text => $token->{tag_name}, token => $token);
3905                ## Ignore the token
3906              }
3907              !!!nack ('t285.1');
3908              !!!next-token;
3909              next B;
3910            } elsif ($token->{tag_name} eq 'option') {
3911              if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
3912                !!!cp ('t286');
3913                pop @{$self->{open_elements}};
3914              } else {
3915                !!!cp ('t287');
3916                !!!parse-error (type => 'unmatched end tag',
3917                                text => $token->{tag_name}, token => $token);
3918                ## Ignore the token
3919              }
3920              !!!nack ('t287.1');
3921              !!!next-token;
3922              next B;
3923            } elsif ($token->{tag_name} eq 'select') {
3924              ## have an element in table scope
3925              my $i;
3926              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3927                my $node = $self->{open_elements}->[$_];
3928                if ($node->[1] == SELECT_EL) {
3929                  !!!cp ('t288');
3930                  $i = $_;
3931                  last INSCOPE;
3932                } elsif ($node->[1] & TABLE_SCOPING_EL) {
3933                  !!!cp ('t289');
3934                  last INSCOPE;
3935                }
3936              } # INSCOPE
3937              unless (defined $i) {
3938                !!!cp ('t290');
3939                !!!parse-error (type => 'unmatched end tag',
3940                                text => $token->{tag_name}, token => $token);
3941                ## Ignore the token
3942                !!!nack ('t290.1');
3943                !!!next-token;
3944                next B;
3945              }
3946                  
3947              !!!cp ('t291');
3948              splice @{$self->{open_elements}}, $i;
3949    
3950                ## Close the cell            $self->_reset_insertion_mode;
3951                !!!back-token; # <?>  
3952                $token = {type => 'end tag', tag_name => $tn};            !!!nack ('t291.1');
3953                redo B;            !!!next-token;
3954              } else {            next B;
3955                #          } elsif (($self->{insertion_mode} & IM_MASK)
3956                         == IN_SELECT_IN_TABLE_IM and
3957                     {
3958                      caption => 1, table => 1, tbody => 1,
3959                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
3960                     }->{$token->{tag_name}}) {
3961    ## TODO: The following is wrong?
3962              !!!parse-error (type => 'unmatched end tag',
3963                              text => $token->{tag_name}, token => $token);
3964                  
3965              ## have an element in table scope
3966              my $i;
3967              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3968                my $node = $self->{open_elements}->[$_];
3969                if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3970                  !!!cp ('t292');
3971                  $i = $_;
3972                  last INSCOPE;
3973                } elsif ($node->[1] & TABLE_SCOPING_EL) {
3974                  !!!cp ('t293');
3975                  last INSCOPE;
3976              }              }
3977            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
3978              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {            unless (defined $i) {
3979                ## have an element in table scope              !!!cp ('t294');
3980                my $i;              ## Ignore the token
3981                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              !!!nack ('t294.1');
3982                  my $node = $self->{open_elements}->[$_];              !!!next-token;
3983                  if ($node->[1] eq $token->{tag_name}) {              next B;
3984                    $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;  
               }  
3985                                
3986                ## generate implied end tags            ## As if </select>
3987                if ({            ## have an element in table scope
3988                     dd => 1, dt => 1, li => 1, p => 1,            undef $i;
3989                     td => ($token->{tag_name} eq 'th'),            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3990                     th => ($token->{tag_name} eq 'td'),              my $node = $self->{open_elements}->[$_];
3991                     tr => 1,              if ($node->[1] == SELECT_EL) {
3992                    }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t295');
3993                  !!!back-token;                $i = $_;
3994                  $token = {type => 'end tag',                last INSCOPE;
3995                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST              } elsif ($node->[1] & TABLE_SCOPING_EL) {
3996                  redo B;  ## ISSUE: Can this state be reached?
3997                }                !!!cp ('t296');
3998                  last INSCOPE;
3999                }
4000              } # INSCOPE
4001              unless (defined $i) {
4002                !!!cp ('t297');
4003    ## TODO: The following error type is correct?
4004                !!!parse-error (type => 'unmatched end tag',
4005                                text => 'select', token => $token);
4006                ## Ignore the </select> token
4007                !!!nack ('t297.1');
4008                !!!next-token; ## TODO: ok?
4009                next B;
4010              }
4011                  
4012              !!!cp ('t298');
4013              splice @{$self->{open_elements}}, $i;
4014    
4015                if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            $self->_reset_insertion_mode;
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
4016    
4017                splice @{$self->{open_elements}}, $i;            !!!ack-later;
4018              ## reprocess
4019              next B;
4020            } else {
4021              !!!cp ('t299');
4022              !!!parse-error (type => 'in select:/',
4023                              text => $token->{tag_name}, token => $token);
4024              ## Ignore the token
4025              !!!nack ('t299.3');
4026              !!!next-token;
4027              next B;
4028            }
4029          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4030            unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
4031                    @{$self->{open_elements}} == 1) { # redundant, maybe
4032              !!!cp ('t299.1');
4033              !!!parse-error (type => 'in body:#eof', token => $token);
4034            } else {
4035              !!!cp ('t299.2');
4036            }
4037    
4038                $clear_up_to_marker->();          ## Stop parsing.
4039            last B;
4040          } else {
4041            die "$0: $token->{type}: Unknown token type";
4042          }
4043        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4044          if ($token->{type} == CHARACTER_TOKEN) {
4045            if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4046              my $data = $1;
4047              ## As if in body
4048              $reconstruct_active_formatting_elements->($insert_to_current);
4049                  
4050              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4051              
4052              unless (length $token->{data}) {
4053                !!!cp ('t300');
4054                !!!next-token;
4055                next B;
4056              }
4057            }
4058            
4059            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4060              !!!cp ('t301');
4061              !!!parse-error (type => 'after html:#text', token => $token);
4062              #
4063            } else {
4064              !!!cp ('t302');
4065              ## "after body" insertion mode
4066              !!!parse-error (type => 'after body:#text', token => $token);
4067              #
4068            }
4069    
4070                $self->{insertion_mode} = 'in row';          $self->{insertion_mode} = IN_BODY_IM;
4071            ## reprocess
4072            next B;
4073          } elsif ($token->{type} == START_TAG_TOKEN) {
4074            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4075              !!!cp ('t303');
4076              !!!parse-error (type => 'after html',
4077                              text => $token->{tag_name}, token => $token);
4078              #
4079            } else {
4080              !!!cp ('t304');
4081              ## "after body" insertion mode
4082              !!!parse-error (type => 'after body',
4083                              text => $token->{tag_name}, token => $token);
4084              #
4085            }
4086    
4087                !!!next-token;          $self->{insertion_mode} = IN_BODY_IM;
4088                redo B;          !!!ack-later;
4089              } elsif ({          ## reprocess
4090                        body => 1, caption => 1, col => 1,          next B;
4091                        colgroup => 1, html => 1,        } elsif ($token->{type} == END_TAG_TOKEN) {
4092                       }->{$token->{tag_name}}) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4093                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t305');
4094                ## Ignore the token            !!!parse-error (type => 'after html:/',
4095                !!!next-token;                            text => $token->{tag_name}, token => $token);
4096                redo B;            
4097              } elsif ({            $self->{insertion_mode} = IN_BODY_IM;
4098                        table => 1, tbody => 1, tfoot => 1,            ## Reprocess.
4099                        thead => 1, tr => 1,            next B;
4100                       }->{$token->{tag_name}}) {          } else {
4101                ## have an element in table scope            !!!cp ('t306');
4102                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;  
               }  
4103    
4104                ## Close the cell          ## "after body" insertion mode
4105                !!!back-token; # </?>          if ($token->{tag_name} eq 'html') {
4106                $token = {type => 'end tag', tag_name => $tn};            if (defined $self->{inner_html_node}) {
4107                redo B;              !!!cp ('t307');
4108              } else {              !!!parse-error (type => 'unmatched end tag',
4109                #                              text => 'html', token => $token);
4110              }              ## Ignore the token
4111                !!!next-token;
4112                next B;
4113            } else {            } else {
4114              #              !!!cp ('t308');
4115                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4116                !!!next-token;
4117                next B;
4118            }            }
4119            } else {
4120              !!!cp ('t309');
4121              !!!parse-error (type => 'after body:/',
4122                              text => $token->{tag_name}, token => $token);
4123    
4124              $self->{insertion_mode} = IN_BODY_IM;
4125              ## reprocess
4126              next B;
4127            }
4128          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4129            !!!cp ('t309.2');
4130            ## Stop parsing
4131            last B;
4132          } else {
4133            die "$0: $token->{type}: Unknown token type";
4134          }
4135        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4136          if ($token->{type} == CHARACTER_TOKEN) {
4137            if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4138              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4139                        
4140            $in_body->($insert_to_current);            unless (length $token->{data}) {
4141            redo B;              !!!cp ('t310');
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4142              !!!next-token;              !!!next-token;
4143              redo B;              next B;
4144            } elsif ($token->{type} eq 'comment') {            }
4145              my $comment = $self->{document}->create_comment ($token->{data});          }
4146              $self->{open_elements}->[-1]->[0]->append_child ($comment);          
4147            if ($token->{data} =~ s/^[^\x09\x0A\x0C\x20]+//) {
4148              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4149                !!!cp ('t311');
4150                !!!parse-error (type => 'in frameset:#text', token => $token);
4151              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4152                !!!cp ('t312');
4153                !!!parse-error (type => 'after frameset:#text', token => $token);
4154              } else { # "after after frameset"
4155                !!!cp ('t313');
4156                !!!parse-error (type => 'after html:#text', token => $token);
4157              }
4158              
4159              ## Ignore the token.
4160              if (length $token->{data}) {
4161                !!!cp ('t314');
4162                ## reprocess the rest of characters
4163              } else {
4164                !!!cp ('t315');
4165              !!!next-token;              !!!next-token;
4166              redo B;            }
4167            } elsif ($token->{type} eq 'start tag') {            next B;
4168              if ($token->{tag_name} eq 'option') {          }
4169                if ($self->{open_elements}->[-1]->[1] eq 'option') {          
4170                  ## As if </option>          die qq[$0: Character "$token->{data}"];
4171                  pop @{$self->{open_elements}};        } elsif ($token->{type} == START_TAG_TOKEN) {
4172                }          if ($token->{tag_name} eq 'frameset' and
4173                $self->{insertion_mode} == IN_FRAMESET_IM) {
4174              !!!cp ('t318');
4175              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4176              !!!nack ('t318.1');
4177              !!!next-token;
4178              next B;
4179            } elsif ($token->{tag_name} eq 'frame' and
4180                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4181              !!!cp ('t319');
4182              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4183              pop @{$self->{open_elements}};
4184              !!!ack ('t319.1');
4185              !!!next-token;
4186              next B;
4187            } elsif ($token->{tag_name} eq 'noframes') {
4188              !!!cp ('t320');
4189              ## NOTE: As if in head.
4190              $parse_rcdata->(CDATA_CONTENT_MODEL);
4191              next B;
4192    
4193              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
4194              ## has no parse error.
4195            } else {
4196              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4197                !!!cp ('t321');
4198                !!!parse-error (type => 'in frameset',
4199                                text => $token->{tag_name}, token => $token);
4200              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4201                !!!cp ('t322');
4202                !!!parse-error (type => 'after frameset',
4203                                text => $token->{tag_name}, token => $token);
4204              } else { # "after after frameset"
4205                !!!cp ('t322.2');
4206                !!!parse-error (type => 'after after frameset',
4207                                text => $token->{tag_name}, token => $token);
4208              }
4209              ## Ignore the token
4210              !!!nack ('t322.1');
4211              !!!next-token;
4212              next B;
4213            }
4214          } elsif ($token->{type} == END_TAG_TOKEN) {
4215            if ($token->{tag_name} eq 'frameset' and
4216                $self->{insertion_mode} == IN_FRAMESET_IM) {
4217              if ($self->{open_elements}->[-1]->[1] == HTML_EL and
4218                  @{$self->{open_elements}} == 1) {
4219                !!!cp ('t325');
4220                !!!parse-error (type => 'unmatched end tag',
4221                                text => $token->{tag_name}, token => $token);
4222                ## Ignore the token
4223                !!!next-token;
4224              } else {
4225                !!!cp ('t326');
4226                pop @{$self->{open_elements}};
4227                !!!next-token;
4228              }
4229    
4230                !!!insert-element ($token->{tag_name}, $token->{attributes});            if (not defined $self->{inner_html_node} and
4231                !!!next-token;                not ($self->{open_elements}->[-1]->[1] == FRAMESET_EL)) {
4232                redo B;              !!!cp ('t327');
4233              } elsif ($token->{tag_name} eq 'optgroup') {              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4234                if ($self->{open_elements}->[-1]->[1] eq 'option') {            } else {
4235                  ## As if </option>              !!!cp ('t328');
4236                  pop @{$self->{open_elements}};            }
4237                }            next B;
4238            } elsif ($token->{tag_name} eq 'html' and
4239                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4240              !!!cp ('t329');
4241              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4242              !!!next-token;
4243              next B;
4244            } else {
4245              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4246                !!!cp ('t330');
4247                !!!parse-error (type => 'in frameset:/',
4248                                text => $token->{tag_name}, token => $token);
4249              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4250                !!!cp ('t330.1');
4251                !!!parse-error (type => 'after frameset:/',
4252                                text => $token->{tag_name}, token => $token);
4253              } else { # "after after html"
4254                !!!cp ('t331');
4255                !!!parse-error (type => 'after after frameset:/',
4256                                text => $token->{tag_name}, token => $token);
4257              }
4258              ## Ignore the token
4259              !!!next-token;
4260              next B;
4261            }
4262          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4263            unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
4264                    @{$self->{open_elements}} == 1) { # redundant, maybe
4265              !!!cp ('t331.1');
4266              !!!parse-error (type => 'in body:#eof', token => $token);
4267            } else {
4268              !!!cp ('t331.2');
4269            }
4270            
4271            ## Stop parsing
4272            last B;
4273          } else {
4274            die "$0: $token->{type}: Unknown token type";
4275          }
4276        } else {
4277          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4278        }
4279    
4280                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {      ## "in body" insertion mode
4281                  ## As if </optgroup>      if ($token->{type} == START_TAG_TOKEN) {
4282                  pop @{$self->{open_elements}};        if ($token->{tag_name} eq 'script') {
4283                }          !!!cp ('t332');
4284            ## NOTE: This is an "as if in head" code clone
4285            $script_start_tag->();
4286            next B;
4287          } elsif ($token->{tag_name} eq 'style') {
4288            !!!cp ('t333');
4289            ## NOTE: This is an "as if in head" code clone
4290            $parse_rcdata->(CDATA_CONTENT_MODEL);
4291            next B;
4292          } elsif ({
4293                    base => 1, command => 1, eventsource => 1, link => 1,
4294                   }->{$token->{tag_name}}) {
4295            !!!cp ('t334');
4296            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4297            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4298            pop @{$self->{open_elements}};
4299            !!!ack ('t334.1');
4300            !!!next-token;
4301            next B;
4302          } elsif ($token->{tag_name} eq 'meta') {
4303            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4304            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4305            my $meta_el = pop @{$self->{open_elements}};
4306    
4307            unless ($self->{confident}) {
4308              if ($token->{attributes}->{charset}) {
4309                !!!cp ('t335');
4310                ## NOTE: Whether the encoding is supported or not is handled
4311                ## in the {change_encoding} callback.
4312                $self->{change_encoding}
4313                    ->($self, $token->{attributes}->{charset}->{value}, $token);
4314                
4315                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4316                    ->set_user_data (manakai_has_reference =>
4317                                         $token->{attributes}->{charset}
4318                                             ->{has_reference});
4319              } elsif ($token->{attributes}->{content}) {
4320                if ($token->{attributes}->{content}->{value}
4321                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4322                        [\x09\x0A\x0C\x0D\x20]*=
4323                        [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4324                        ([^"'\x09\x0A\x0C\x0D\x20][^\x09\x0A\x0C\x0D\x20\x3B]*))
4325                       /x) {
4326                  !!!cp ('t336');
4327                  ## NOTE: Whether the encoding is supported or not is handled
4328                  ## in the {change_encoding} callback.
4329                  $self->{change_encoding}
4330                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
4331                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4332                      ->set_user_data (manakai_has_reference =>
4333                                           $token->{attributes}->{content}
4334                                                 ->{has_reference});
4335                }
4336              }
4337            } else {
4338              if ($token->{attributes}->{charset}) {
4339                !!!cp ('t337');
4340                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4341                    ->set_user_data (manakai_has_reference =>
4342                                         $token->{attributes}->{charset}
4343                                             ->{has_reference});
4344              }
4345              if ($token->{attributes}->{content}) {
4346                !!!cp ('t338');
4347                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4348                    ->set_user_data (manakai_has_reference =>
4349                                         $token->{attributes}->{content}
4350                                             ->{has_reference});
4351              }
4352            }
4353    
4354                !!!insert-element ($token->{tag_name}, $token->{attributes});          !!!ack ('t338.1');
4355                !!!next-token;          !!!next-token;
4356                redo B;          next B;
4357              } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'title') {
4358                !!!parse-error (type => 'not closed:select');          !!!cp ('t341');
4359                ## As if </select> instead          ## NOTE: This is an "as if in head" code clone
4360                ## have an element in table scope          $parse_rcdata->(RCDATA_CONTENT_MODEL);
4361                my $i;          next B;
4362                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {        } elsif ($token->{tag_name} eq 'body') {
4363                  my $node = $self->{open_elements}->[$_];          !!!parse-error (type => 'in body', text => 'body', token => $token);
                 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:select');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
4364                                
4365                splice @{$self->{open_elements}}, $i;          if (@{$self->{open_elements}} == 1 or
4366                not ($self->{open_elements}->[1]->[1] == BODY_EL)) {
4367              !!!cp ('t342');
4368              ## Ignore the token
4369            } else {
4370              my $body_el = $self->{open_elements}->[1]->[0];
4371              for my $attr_name (keys %{$token->{attributes}}) {
4372                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4373                  !!!cp ('t343');
4374                  $body_el->set_attribute_ns
4375                    (undef, [undef, $attr_name],
4376                     $token->{attributes}->{$attr_name}->{value});
4377                }
4378              }
4379            }
4380            !!!nack ('t343.1');
4381            !!!next-token;
4382            next B;
4383          } elsif ({
4384                    ## NOTE: Start tags for non-phrasing flow content elements
4385    
4386                $self->_reset_insertion_mode;                  ## NOTE: The normal one
4387                    address => 1, article => 1, aside => 1, blockquote => 1,
4388                    center => 1, datagrid => 1, details => 1, dialog => 1,
4389                    dir => 1, div => 1, dl => 1, fieldset => 1, figure => 1,
4390                    footer => 1, h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1,
4391                    h6 => 1, header => 1, menu => 1, nav => 1, ol => 1, p => 1,
4392                    section => 1, ul => 1,
4393                    ## NOTE: As normal, but drops leading newline
4394                    pre => 1, listing => 1,
4395                    ## NOTE: As normal, but interacts with the form element pointer
4396                    form => 1,
4397                    
4398                    table => 1,
4399                    hr => 1,
4400                   }->{$token->{tag_name}}) {
4401            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
4402              !!!cp ('t350');
4403              !!!parse-error (type => 'in form:form', token => $token);
4404              ## Ignore the token
4405              !!!nack ('t350.1');
4406              !!!next-token;
4407              next B;
4408            }
4409    
4410            ## has a p element in scope
4411            INSCOPE: for (reverse @{$self->{open_elements}}) {
4412              if ($_->[1] == P_EL) {
4413                !!!cp ('t344');
4414                !!!back-token; # <form>
4415                $token = {type => END_TAG_TOKEN, tag_name => 'p',
4416                          line => $token->{line}, column => $token->{column}};
4417                next B;
4418              } elsif ($_->[1] & SCOPING_EL) {
4419                !!!cp ('t345');
4420                last INSCOPE;
4421              }
4422            } # INSCOPE
4423              
4424            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4425            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
4426              !!!nack ('t346.1');
4427              !!!next-token;
4428              if ($token->{type} == CHARACTER_TOKEN) {
4429                $token->{data} =~ s/^\x0A//;
4430                unless (length $token->{data}) {
4431                  !!!cp ('t346');
4432                !!!next-token;                !!!next-token;
               redo B;  
4433              } else {              } else {
4434                #                !!!cp ('t349');
4435              }              }
4436            } elsif ($token->{type} eq 'end tag') {            } else {
4437              if ($token->{tag_name} eq 'optgroup') {              !!!cp ('t348');
4438                if ($self->{open_elements}->[-1]->[1] eq 'option' and            }
4439                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {          } elsif ($token->{tag_name} eq 'form') {
4440                  ## As if </option>            !!!cp ('t347.1');
4441                  splice @{$self->{open_elements}}, -2;            $self->{form_element} = $self->{open_elements}->[-1]->[0];
4442                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
4443                  pop @{$self->{open_elements}};            !!!nack ('t347.2');
4444              !!!next-token;
4445            } elsif ($token->{tag_name} eq 'table') {
4446              !!!cp ('t382');
4447              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
4448              
4449              $self->{insertion_mode} = IN_TABLE_IM;
4450    
4451              !!!nack ('t382.1');
4452              !!!next-token;
4453            } elsif ($token->{tag_name} eq 'hr') {
4454              !!!cp ('t386');
4455              pop @{$self->{open_elements}};
4456            
4457              !!!nack ('t386.1');
4458              !!!next-token;
4459            } else {
4460              !!!nack ('t347.1');
4461              !!!next-token;
4462            }
4463            next B;
4464          } elsif ($token->{tag_name} eq 'li') {
4465            ## NOTE: As normal, but imply </li> when there's another <li> ...
4466    
4467            ## NOTE: Special, Scope (<li><foo><li> == <li><foo><li/></foo></li>)
4468              ## Interpreted as <li><foo/></li><li/> (non-conforming)
4469              ## blockquote (O9.27), center (O), dd (Fx3, O, S3.1.2, IE7),
4470              ## dt (Fx, O, S, IE), dl (O), fieldset (O, S, IE), form (Fx, O, S),
4471              ## hn (O), pre (O), applet (O, S), button (O, S), marquee (Fx, O, S),
4472              ## object (Fx)
4473              ## Generate non-tree (non-conforming)
4474              ## basefont (IE7 (where basefont is non-void)), center (IE),
4475              ## form (IE), hn (IE)
4476            ## address, div, p (<li><foo><li> == <li><foo/></li><li/>)
4477              ## Interpreted as <li><foo><li/></foo></li> (non-conforming)
4478              ## div (Fx, S)
4479    
4480            my $non_optional;
4481            my $i = -1;
4482    
4483            ## 1.
4484            for my $node (reverse @{$self->{open_elements}}) {
4485              if ($node->[1] == LI_EL) {
4486                ## 2. (a) As if </li>
4487                {
4488                  ## If no </li> - not applied
4489                  #
4490    
4491                  ## Otherwise
4492    
4493                  ## 1. generate implied end tags, except for </li>
4494                  #
4495    
4496                  ## 2. If current node != "li", parse error
4497                  if ($non_optional) {
4498                    !!!parse-error (type => 'not closed',
4499                                    text => $non_optional->[0]->manakai_local_name,
4500                                    token => $token);
4501                    !!!cp ('t355');
4502                } else {                } else {
4503                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t356');
                 ## Ignore the token  
4504                }                }
4505                !!!next-token;  
4506                redo B;                ## 3. Pop
4507              } elsif ($token->{tag_name} eq 'option') {                splice @{$self->{open_elements}}, $i;
4508                if ($self->{open_elements}->[-1]->[1] eq 'option') {              }
4509                  pop @{$self->{open_elements}};  
4510                last; ## 2. (b) goto 5.
4511              } elsif (
4512                       ## NOTE: not "formatting" and not "phrasing"
4513                       ($node->[1] & SPECIAL_EL or
4514                        $node->[1] & SCOPING_EL) and
4515                       ## NOTE: "li", "dt", and "dd" are in |SPECIAL_EL|.
4516                       (not $node->[1] & ADDRESS_DIV_P_EL)
4517                      ) {
4518                ## 3.
4519                !!!cp ('t357');
4520                last; ## goto 5.
4521              } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
4522                !!!cp ('t358');
4523                #
4524              } else {
4525                !!!cp ('t359');
4526                $non_optional ||= $node;
4527                #
4528              }
4529              ## 4.
4530              ## goto 2.
4531              $i--;
4532            }
4533    
4534            ## 5. (a) has a |p| element in scope
4535            INSCOPE: for (reverse @{$self->{open_elements}}) {
4536              if ($_->[1] == P_EL) {
4537                !!!cp ('t353');
4538    
4539                ## NOTE: |<p><li>|, for example.
4540    
4541                !!!back-token; # <x>
4542                $token = {type => END_TAG_TOKEN, tag_name => 'p',
4543                          line => $token->{line}, column => $token->{column}};
4544                next B;
4545              } elsif ($_->[1] & SCOPING_EL) {
4546                !!!cp ('t354');
4547                last INSCOPE;
4548              }
4549            } # INSCOPE
4550    
4551            ## 5. (b) insert
4552            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4553            !!!nack ('t359.1');
4554            !!!next-token;
4555            next B;
4556          } elsif ($token->{tag_name} eq 'dt' or
4557                   $token->{tag_name} eq 'dd') {
4558            ## NOTE: As normal, but imply </dt> or </dd> when ...
4559    
4560            my $non_optional;
4561            my $i = -1;
4562    
4563            ## 1.
4564            for my $node (reverse @{$self->{open_elements}}) {
4565              if ($node->[1] == DTDD_EL) {
4566                ## 2. (a) As if </li>
4567                {
4568                  ## If no </li> - not applied
4569                  #
4570    
4571                  ## Otherwise
4572    
4573                  ## 1. generate implied end tags, except for </dt> or </dd>
4574                  #
4575    
4576                  ## 2. If current node != "dt"|"dd", parse error
4577                  if ($non_optional) {
4578                    !!!parse-error (type => 'not closed',
4579                                    text => $non_optional->[0]->manakai_local_name,
4580                                    token => $token);
4581                    !!!cp ('t355.1');
4582                } else {                } else {
4583                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t356.1');
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## 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;  
4584                }                }
4585                  
4586                  ## 3. Pop
4587                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4588                }
4589    
4590                $self->_reset_insertion_mode;              last; ## 2. (b) goto 5.
4591              } elsif (
4592                       ## NOTE: not "formatting" and not "phrasing"
4593                       ($node->[1] & SPECIAL_EL or
4594                        $node->[1] & SCOPING_EL) and
4595                       ## NOTE: "li", "dt", and "dd" are in |SPECIAL_EL|.
4596    
4597                       (not $node->[1] & ADDRESS_DIV_P_EL)
4598                      ) {
4599                ## 3.
4600                !!!cp ('t357.1');
4601                last; ## goto 5.
4602              } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
4603                !!!cp ('t358.1');
4604                #
4605              } else {
4606                !!!cp ('t359.1');
4607                $non_optional ||= $node;
4608                #
4609              }
4610              ## 4.
4611              ## goto 2.
4612              $i--;
4613            }
4614    
4615                !!!next-token;          ## 5. (a) has a |p| element in scope
4616                redo B;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4617              } elsif ({            if ($_->[1] == P_EL) {
4618                        caption => 1, table => 1, tbody => 1,              !!!cp ('t353.1');
4619                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,              !!!back-token; # <x>
4620                       }->{$token->{tag_name}}) {              $token = {type => END_TAG_TOKEN, tag_name => 'p',
4621                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                        line => $token->{line}, column => $token->{column}};
4622                              next B;
4623                ## have an element in table scope            } elsif ($_->[1] & SCOPING_EL) {
4624                my $i;              !!!cp ('t354.1');
4625                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              last INSCOPE;
4626                  my $node = $self->{open_elements}->[$_];            }
4627                  if ($node->[1] eq $token->{tag_name}) {          } # INSCOPE
4628                    $i = $_;  
4629                    last INSCOPE;          ## 5. (b) insert
4630                  } elsif ({          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4631                            table => 1, html => 1,          !!!nack ('t359.2');
4632                           }->{$node->[1]}) {          !!!next-token;
4633                    last INSCOPE;          next B;
4634                  }        } elsif ($token->{tag_name} eq 'plaintext') {
4635                } # INSCOPE          ## NOTE: As normal, but effectively ends parsing
4636                unless (defined $i) {  
4637                  ## Ignore the token          ## has a p element in scope
4638                  !!!next-token;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4639                  redo B;            if ($_->[1] == P_EL) {
4640                !!!cp ('t367');
4641                !!!back-token; # <plaintext>
4642                $token = {type => END_TAG_TOKEN, tag_name => 'p',
4643                          line => $token->{line}, column => $token->{column}};
4644                next B;
4645              } elsif ($_->[1] & SCOPING_EL) {
4646                !!!cp ('t368');
4647                last INSCOPE;
4648              }
4649            } # INSCOPE
4650              
4651            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4652              
4653            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4654              
4655            !!!nack ('t368.1');
4656            !!!next-token;
4657            next B;
4658          } elsif ($token->{tag_name} eq 'a') {
4659            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4660              my $node = $active_formatting_elements->[$i];
4661              if ($node->[1] == A_EL) {
4662                !!!cp ('t371');
4663                !!!parse-error (type => 'in a:a', token => $token);
4664                
4665                !!!back-token; # <a>
4666                $token = {type => END_TAG_TOKEN, tag_name => 'a',
4667                          line => $token->{line}, column => $token->{column}};
4668                $formatting_end_tag->($token);
4669                
4670                AFE2: for (reverse 0..$#$active_formatting_elements) {
4671                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4672                    !!!cp ('t372');
4673                    splice @$active_formatting_elements, $_, 1;
4674                    last AFE2;
4675                }                }
4676                              } # AFE2
4677                ## As if </select>              OE: for (reverse 0..$#{$self->{open_elements}}) {
4678                ## have an element in table scope                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4679                undef $i;                  !!!cp ('t373');
4680                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  splice @{$self->{open_elements}}, $_, 1;
4681                  my $node = $self->{open_elements}->[$_];                  last OE;
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
4682                }                }
4683                              } # OE
4684                splice @{$self->{open_elements}}, $i;              last AFE;
4685              } elsif ($node->[0] eq '#marker') {
4686                !!!cp ('t374');
4687                last AFE;
4688              }
4689            } # AFE
4690              
4691            $reconstruct_active_formatting_elements->($insert_to_current);
4692    
4693                $self->_reset_insertion_mode;          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4694            push @$active_formatting_elements, $self->{open_elements}->[-1];
4695    
4696                ## reprocess          !!!nack ('t374.1');
4697                redo B;          !!!next-token;
4698              } else {          next B;
4699                #        } elsif ($token->{tag_name} eq 'nobr') {
4700              }          $reconstruct_active_formatting_elements->($insert_to_current);
4701            } else {  
4702              #          ## has a |nobr| element in scope
4703            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4704              my $node = $self->{open_elements}->[$_];
4705              if ($node->[1] == NOBR_EL) {
4706                !!!cp ('t376');
4707                !!!parse-error (type => 'in nobr:nobr', token => $token);
4708                !!!back-token; # <nobr>
4709                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
4710                          line => $token->{line}, column => $token->{column}};
4711                next B;
4712              } elsif ($node->[1] & SCOPING_EL) {
4713                !!!cp ('t377');
4714                last INSCOPE;
4715              }
4716            } # INSCOPE
4717            
4718            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4719            push @$active_formatting_elements, $self->{open_elements}->[-1];
4720            
4721            !!!nack ('t377.1');
4722            !!!next-token;
4723            next B;
4724          } elsif ($token->{tag_name} eq 'button') {
4725            ## has a button element in scope
4726            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4727              my $node = $self->{open_elements}->[$_];
4728              if ($node->[1] == BUTTON_EL) {
4729                !!!cp ('t378');
4730                !!!parse-error (type => 'in button:button', token => $token);
4731                !!!back-token; # <button>
4732                $token = {type => END_TAG_TOKEN, tag_name => 'button',
4733                          line => $token->{line}, column => $token->{column}};
4734                next B;
4735              } elsif ($node->[1] & SCOPING_EL) {
4736                !!!cp ('t379');
4737                last INSCOPE;
4738            }            }
4739            } # INSCOPE
4740              
4741            $reconstruct_active_formatting_elements->($insert_to_current);
4742              
4743            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4744    
4745            !!!parse-error (type => 'in select:'.$token->{tag_name});          ## TODO: associate with $self->{form_element} if defined
4746    
4747            push @$active_formatting_elements, ['#marker', ''];
4748    
4749            !!!nack ('t379.1');
4750            !!!next-token;
4751            next B;
4752          } elsif ({
4753                    xmp => 1,
4754                    iframe => 1,
4755                    noembed => 1,
4756                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
4757                    noscript => 0, ## TODO: 1 if scripting is enabled
4758                   }->{$token->{tag_name}}) {
4759            if ($token->{tag_name} eq 'xmp') {
4760              !!!cp ('t381');
4761              $reconstruct_active_formatting_elements->($insert_to_current);
4762            } else {
4763              !!!cp ('t399');
4764            }
4765            ## NOTE: There is an "as if in body" code clone.
4766            $parse_rcdata->(CDATA_CONTENT_MODEL);
4767            next B;
4768          } elsif ($token->{tag_name} eq 'isindex') {
4769            !!!parse-error (type => 'isindex', token => $token);
4770            
4771            if (defined $self->{form_element}) {
4772              !!!cp ('t389');
4773            ## Ignore the token            ## Ignore the token
4774              !!!nack ('t389'); ## NOTE: Not acknowledged.
4775            !!!next-token;            !!!next-token;
4776            redo B;            next B;
4777          } elsif ($self->{insertion_mode} eq 'after body') {          } else {
4778            if ($token->{type} eq 'character') {            !!!ack ('t391.1');
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4779    
4780                unless (length $token->{data}) {            my $at = $token->{attributes};
4781                  !!!next-token;            my $form_attrs;
4782                  redo B;            $form_attrs->{action} = $at->{action} if $at->{action};
4783                }            my $prompt_attr = $at->{prompt};
4784              }            $at->{name} = {name => 'name', value => 'isindex'};
4785                          delete $at->{action};
4786              #            delete $at->{prompt};
4787              !!!parse-error (type => 'after body:#'.$token->{type});            my @tokens = (
4788            } elsif ($token->{type} eq 'comment') {                          {type => START_TAG_TOKEN, tag_name => 'form',
4789              my $comment = $self->{document}->create_comment ($token->{data});                           attributes => $form_attrs,
4790              $self->{open_elements}->[0]->[0]->append_child ($comment);                           line => $token->{line}, column => $token->{column}},
4791              !!!next-token;                          {type => START_TAG_TOKEN, tag_name => 'hr',
4792              redo B;                           line => $token->{line}, column => $token->{column}},
4793            } elsif ($token->{type} eq 'start tag') {                          {type => START_TAG_TOKEN, tag_name => 'p',
4794              !!!parse-error (type => 'after body:'.$token->{tag_name});                           line => $token->{line}, column => $token->{column}},
4795              #                          {type => START_TAG_TOKEN, tag_name => 'label',
4796            } elsif ($token->{type} eq 'end tag') {                           line => $token->{line}, column => $token->{column}},
4797              if ($token->{tag_name} eq 'html') {                         );
4798                if (defined $self->{inner_html_node}) {            if ($prompt_attr) {
4799                  !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t390');
4800                  ## Ignore the token              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
4801                  !!!next-token;                             #line => $token->{line}, column => $token->{column},
4802                  redo B;                            };
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
4803            } else {            } else {
4804              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t391');
4805                push @tokens, {type => CHARACTER_TOKEN,
4806                               data => 'This is a searchable index. Insert your search keywords here: ',
4807                               #line => $token->{line}, column => $token->{column},
4808                              }; # SHOULD
4809                ## TODO: make this configurable
4810              }
4811              push @tokens,
4812                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
4813                             line => $token->{line}, column => $token->{column}},
4814                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4815                            {type => END_TAG_TOKEN, tag_name => 'label',
4816                             line => $token->{line}, column => $token->{column}},
4817                            {type => END_TAG_TOKEN, tag_name => 'p',
4818                             line => $token->{line}, column => $token->{column}},
4819                            {type => START_TAG_TOKEN, tag_name => 'hr',
4820                             line => $token->{line}, column => $token->{column}},
4821                            {type => END_TAG_TOKEN, tag_name => 'form',
4822                             line => $token->{line}, column => $token->{column}};
4823              !!!back-token (@tokens);
4824              !!!next-token;
4825              next B;
4826            }
4827          } elsif ($token->{tag_name} eq 'textarea') {
4828            ## Step 1
4829            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4830            
4831            ## Step 2
4832            ## TODO: $self->{form_element} if defined
4833    
4834            ## Step 3
4835            $self->{ignore_newline} = 1;
4836    
4837            ## Step 4
4838            ## ISSUE: This step is wrong. (r2302 enbugged)
4839    
4840            ## Step 5
4841            $self->{content_model} = RCDATA_CONTENT_MODEL;
4842            delete $self->{escape}; # MUST
4843    
4844            ## Step 6-7
4845            $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
4846    
4847            !!!nack ('t392.1');
4848            !!!next-token;
4849            next B;
4850          } elsif ($token->{tag_name} eq 'optgroup' or
4851                   $token->{tag_name} eq 'option') {
4852            ## has an |option| element in scope
4853            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4854              my $node = $self->{open_elements}->[$_];
4855              if ($node->[1] == OPTION_EL) {
4856                !!!cp ('t397.1');
4857                ## NOTE: As if </option>
4858                !!!back-token; # <option> or <optgroup>
4859                $token = {type => END_TAG_TOKEN, tag_name => 'option',
4860                          line => $token->{line}, column => $token->{column}};
4861                next B;
4862              } elsif ($node->[1] & SCOPING_EL) {
4863                !!!cp ('t397.2');
4864                last INSCOPE;
4865            }            }
4866            } # INSCOPE
4867    
4868            $self->{insertion_mode} = 'in body';          $reconstruct_active_formatting_elements->($insert_to_current);
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4869    
4870                unless (length $token->{data}) {          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
                 !!!next-token;  
                 redo B;  
               }  
             }  
4871    
4872              #          !!!nack ('t397.3');
4873            } elsif ($token->{type} eq 'comment') {          !!!next-token;
4874              my $comment = $self->{document}->create_comment ($token->{data});          redo B;
4875              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'rt' or
4876              !!!next-token;                 $token->{tag_name} eq 'rp') {
4877              redo B;          ## has a |ruby| element in scope
4878            } elsif ($token->{type} eq 'start tag') {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4879              if ($token->{tag_name} eq 'frameset') {            my $node = $self->{open_elements}->[$_];
4880                !!!insert-element ($token->{tag_name}, $token->{attributes});            if ($node->[1] == RUBY_EL) {
4881                !!!next-token;              !!!cp ('t398.1');
4882                redo B;              ## generate implied end tags
4883              } elsif ($token->{tag_name} eq 'frame') {              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4884                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t398.2');
4885                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
4886              }              }
4887            } elsif ($token->{type} eq 'end tag') {              unless ($self->{open_elements}->[-1]->[1] == RUBY_EL) {
4888              if ($token->{tag_name} eq 'frameset') {                !!!cp ('t398.3');
4889                if ($self->{open_elements}->[-1]->[1] eq 'html' and                !!!parse-error (type => 'not closed',
4890                    @{$self->{open_elements}} == 1) {                                text => $self->{open_elements}->[-1]->[0]
4891                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                                    ->manakai_local_name,
4892                  ## Ignore the token                                token => $token);
4893                  !!!next-token;                pop @{$self->{open_elements}}
4894                } else {                    while not $self->{open_elements}->[-1]->[1] == RUBY_EL;
                 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 {  
               #  
4895              }              }
4896                last INSCOPE;
4897              } elsif ($node->[1] & SCOPING_EL) {
4898                !!!cp ('t398.4');
4899                last INSCOPE;
4900              }
4901            } # INSCOPE
4902    
4903            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4904    
4905            !!!nack ('t398.5');
4906            !!!next-token;
4907            redo B;
4908          } elsif ($token->{tag_name} eq 'math' or
4909                   $token->{tag_name} eq 'svg') {
4910            $reconstruct_active_formatting_elements->($insert_to_current);
4911    
4912            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
4913    
4914            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
4915    
4916            ## "adjust foreign attributes" - done in insert-element-f
4917            
4918            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
4919            
4920            if ($self->{self_closing}) {
4921              pop @{$self->{open_elements}};
4922              !!!ack ('t398.6');
4923            } else {
4924              !!!cp ('t398.7');
4925              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
4926              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
4927              ## mode, "in body" (not "in foreign content") secondary insertion
4928              ## mode, maybe.
4929            }
4930    
4931            !!!next-token;
4932            next B;
4933          } elsif ({
4934                    caption => 1, col => 1, colgroup => 1, frame => 1,
4935                    frameset => 1, head => 1,
4936                    tbody => 1, td => 1, tfoot => 1, th => 1,
4937                    thead => 1, tr => 1,
4938                   }->{$token->{tag_name}}) {
4939            !!!cp ('t401');
4940            !!!parse-error (type => 'in body',
4941                            text => $token->{tag_name}, token => $token);
4942            ## Ignore the token
4943            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
4944            !!!next-token;
4945            next B;
4946          } elsif ($token->{tag_name} eq 'param' or
4947                   $token->{tag_name} eq 'source') {
4948            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4949            pop @{$self->{open_elements}};
4950    
4951            !!!ack ('t398.5');
4952            !!!next-token;
4953            redo B;
4954          } else {
4955            if ($token->{tag_name} eq 'image') {
4956              !!!cp ('t384');
4957              !!!parse-error (type => 'image', token => $token);
4958              $token->{tag_name} = 'img';
4959            } else {
4960              !!!cp ('t385');
4961            }
4962    
4963            ## NOTE: There is an "as if <br>" code clone.
4964            $reconstruct_active_formatting_elements->($insert_to_current);
4965            
4966            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
4967    
4968            if ({
4969                 applet => 1, marquee => 1, object => 1,
4970                }->{$token->{tag_name}}) {
4971              !!!cp ('t380');
4972              push @$active_formatting_elements, ['#marker', ''];
4973              !!!nack ('t380.1');
4974            } elsif ({
4975                      b => 1, big => 1, em => 1, font => 1, i => 1,
4976                      s => 1, small => 1, strike => 1,
4977                      strong => 1, tt => 1, u => 1,
4978                     }->{$token->{tag_name}}) {
4979              !!!cp ('t375');
4980              push @$active_formatting_elements, $self->{open_elements}->[-1];
4981              !!!nack ('t375.1');
4982            } elsif ($token->{tag_name} eq 'input') {
4983              !!!cp ('t388');
4984              ## TODO: associate with $self->{form_element} if defined
4985              pop @{$self->{open_elements}};
4986              !!!ack ('t388.2');
4987            } elsif ({
4988                      area => 1, basefont => 1, bgsound => 1, br => 1,
4989                      embed => 1, img => 1, spacer => 1, wbr => 1,
4990                     }->{$token->{tag_name}}) {
4991              !!!cp ('t388.1');
4992              pop @{$self->{open_elements}};
4993              !!!ack ('t388.3');
4994            } elsif ($token->{tag_name} eq 'select') {
4995              ## TODO: associate with $self->{form_element} if defined
4996            
4997              if ($self->{insertion_mode} & TABLE_IMS or
4998                  $self->{insertion_mode} & BODY_TABLE_IMS or
4999                  ($self->{insertion_mode} & IM_MASK) == IN_COLUMN_GROUP_IM) {
5000                !!!cp ('t400.1');
5001                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
5002            } else {            } else {
5003              #              !!!cp ('t400.2');
5004                $self->{insertion_mode} = IN_SELECT_IM;
5005            }            }
5006                        !!!nack ('t400.3');
5007            if (defined $token->{tag_name}) {          } else {
5008              !!!parse-error (type => 'in frameset:'.$token->{tag_name});            !!!nack ('t402');
5009            }
5010            
5011            !!!next-token;
5012            next B;
5013          }
5014        } elsif ($token->{type} == END_TAG_TOKEN) {
5015          if ($token->{tag_name} eq 'body') {
5016            ## has a |body| element in scope
5017            my $i;
5018            INSCOPE: {
5019              for (reverse @{$self->{open_elements}}) {
5020                if ($_->[1] == BODY_EL) {
5021                  !!!cp ('t405');
5022                  $i = $_;
5023                  last INSCOPE;
5024                } elsif ($_->[1] & SCOPING_EL) {
5025                  !!!cp ('t405.1');
5026                  last;
5027                }
5028              }
5029    
5030              ## NOTE: |<marquee></body>|, |<svg><foreignobject></body>|
5031    
5032              !!!parse-error (type => 'unmatched end tag',
5033                              text => $token->{tag_name}, token => $token);
5034              ## NOTE: Ignore the token.
5035              !!!next-token;
5036              next B;
5037            } # INSCOPE
5038    
5039            for (@{$self->{open_elements}}) {
5040              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
5041                !!!cp ('t403');
5042                !!!parse-error (type => 'not closed',
5043                                text => $_->[0]->manakai_local_name,
5044                                token => $token);
5045                last;
5046              } else {
5047                !!!cp ('t404');
5048              }
5049            }
5050    
5051            $self->{insertion_mode} = AFTER_BODY_IM;
5052            !!!next-token;
5053            next B;
5054          } elsif ($token->{tag_name} eq 'html') {
5055            ## TODO: Update this code.  It seems that the code below is not
5056            ## up-to-date, though it has same effect as speced.
5057            if (@{$self->{open_elements}} > 1 and
5058                $self->{open_elements}->[1]->[1] == BODY_EL) {
5059              unless ($self->{open_elements}->[-1]->[1] == BODY_EL) {
5060                !!!cp ('t406');
5061                !!!parse-error (type => 'not closed',
5062                                text => $self->{open_elements}->[1]->[0]
5063                                    ->manakai_local_name,
5064                                token => $token);
5065            } else {            } else {
5066              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t407');
5067            }            }
5068              $self->{insertion_mode} = AFTER_BODY_IM;
5069              ## reprocess
5070              next B;
5071            } else {
5072              !!!cp ('t408');
5073              !!!parse-error (type => 'unmatched end tag',
5074                              text => $token->{tag_name}, token => $token);
5075            ## Ignore the token            ## Ignore the token
5076            !!!next-token;            !!!next-token;
5077            redo B;            next B;
5078          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
5079            if ($token->{type} eq 'character') {        } elsif ({
5080              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  ## NOTE: End tags for non-phrasing flow content elements
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
5081    
5082                unless (length $token->{data}) {                  ## NOTE: The normal ones
5083                  !!!next-token;                  address => 1, article => 1, aside => 1, blockquote => 1,
5084                  redo B;                  center => 1, datagrid => 1, details => 1, dialog => 1,
5085                }                  dir => 1, div => 1, dl => 1, fieldset => 1, figure => 1,
5086              }                  footer => 1, header => 1, listing => 1, menu => 1, nav => 1,
5087                    ol => 1, pre => 1, section => 1, ul => 1,
5088    
5089                    ## NOTE: As normal, but ... optional tags
5090                    dd => 1, dt => 1, li => 1,
5091    
5092                    applet => 1, button => 1, marquee => 1, object => 1,
5093                   }->{$token->{tag_name}}) {
5094            ## NOTE: Code for <li> start tags includes "as if </li>" code.
5095            ## Code for <dt> or <dd> start tags includes "as if </dt> or
5096            ## </dd>" code.
5097    
5098            ## has an element in scope
5099            my $i;
5100            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5101              my $node = $self->{open_elements}->[$_];
5102              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5103                !!!cp ('t410');
5104                $i = $_;
5105                last INSCOPE;
5106              } elsif ($node->[1] & SCOPING_EL) {
5107                !!!cp ('t411');
5108                last INSCOPE;
5109              }
5110            } # INSCOPE
5111    
5112            unless (defined $i) { # has an element in scope
5113              !!!cp ('t413');
5114              !!!parse-error (type => 'unmatched end tag',
5115                              text => $token->{tag_name}, token => $token);
5116              ## NOTE: Ignore the token.
5117            } else {
5118              ## Step 1. generate implied end tags
5119              while ({
5120                      ## END_TAG_OPTIONAL_EL
5121                      dd => ($token->{tag_name} ne 'dd'),
5122                      dt => ($token->{tag_name} ne 'dt'),
5123                      li => ($token->{tag_name} ne 'li'),
5124                      option => 1,
5125                      optgroup => 1,
5126                      p => 1,
5127                      rt => 1,
5128                      rp => 1,
5129                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
5130                !!!cp ('t409');
5131                pop @{$self->{open_elements}};
5132              }
5133    
5134              ## Step 2.
5135              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5136                      ne $token->{tag_name}) {
5137                !!!cp ('t412');
5138                !!!parse-error (type => 'not closed',
5139                                text => $self->{open_elements}->[-1]->[0]
5140                                    ->manakai_local_name,
5141                                token => $token);
5142              } else {
5143                !!!cp ('t414');
5144              }
5145    
5146              ## Step 3.
5147              splice @{$self->{open_elements}}, $i;
5148    
5149              ## Step 4.
5150              $clear_up_to_marker->()
5151                  if {
5152                    applet => 1, button => 1, marquee => 1, object => 1,
5153                  }->{$token->{tag_name}};
5154            }
5155            !!!next-token;
5156            next B;
5157          } elsif ($token->{tag_name} eq 'form') {
5158            ## NOTE: As normal, but interacts with the form element pointer
5159    
5160            undef $self->{form_element};
5161    
5162            ## has an element in scope
5163            my $i;
5164            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5165              my $node = $self->{open_elements}->[$_];
5166              if ($node->[1] == FORM_EL) {
5167                !!!cp ('t418');
5168                $i = $_;
5169                last INSCOPE;
5170              } elsif ($node->[1] & SCOPING_EL) {
5171                !!!cp ('t419');
5172                last INSCOPE;
5173              }
5174            } # INSCOPE
5175    
5176            unless (defined $i) { # has an element in scope
5177              !!!cp ('t421');
5178              !!!parse-error (type => 'unmatched end tag',
5179                              text => $token->{tag_name}, token => $token);
5180              ## NOTE: Ignore the token.
5181            } else {
5182              ## Step 1. generate implied end tags
5183              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5184                !!!cp ('t417');
5185                pop @{$self->{open_elements}};
5186              }
5187              
5188              ## Step 2.
5189              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5190                      ne $token->{tag_name}) {
5191                !!!cp ('t417.1');
5192                !!!parse-error (type => 'not closed',
5193                                text => $self->{open_elements}->[-1]->[0]
5194                                    ->manakai_local_name,
5195                                token => $token);
5196              } else {
5197                !!!cp ('t420');
5198              }  
5199              
5200              ## Step 3.
5201              splice @{$self->{open_elements}}, $i;
5202            }
5203    
5204            !!!next-token;
5205            next B;
5206          } elsif ({
5207                    ## NOTE: As normal, except acts as a closer for any ...
5208                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5209                   }->{$token->{tag_name}}) {
5210            ## has an element in scope
5211            my $i;
5212            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213              my $node = $self->{open_elements}->[$_];
5214              if ($node->[1] == HEADING_EL) {
5215                !!!cp ('t423');
5216                $i = $_;
5217                last INSCOPE;
5218              } elsif ($node->[1] & SCOPING_EL) {
5219                !!!cp ('t424');
5220                last INSCOPE;
5221              }
5222            } # INSCOPE
5223    
5224            unless (defined $i) { # has an element in scope
5225              !!!cp ('t425.1');
5226              !!!parse-error (type => 'unmatched end tag',
5227                              text => $token->{tag_name}, token => $token);
5228              ## NOTE: Ignore the token.
5229            } else {
5230              ## Step 1. generate implied end tags
5231              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5232                !!!cp ('t422');
5233                pop @{$self->{open_elements}};
5234              }
5235              
5236              ## Step 2.
5237              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5238                      ne $token->{tag_name}) {
5239                !!!cp ('t425');
5240                !!!parse-error (type => 'unmatched end tag',
5241                                text => $token->{tag_name}, token => $token);
5242              } else {
5243                !!!cp ('t426');
5244              }
5245    
5246              ## Step 3.
5247              splice @{$self->{open_elements}}, $i;
5248            }
5249            
5250            !!!next-token;
5251            next B;
5252          } elsif ($token->{tag_name} eq 'p') {
5253            ## NOTE: As normal, except </p> implies <p> and ...
5254    
5255            ## has an element in scope
5256            my $non_optional;
5257            my $i;
5258            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5259              my $node = $self->{open_elements}->[$_];
5260              if ($node->[1] == P_EL) {
5261                !!!cp ('t410.1');
5262                $i = $_;
5263                last INSCOPE;
5264              } elsif ($node->[1] & SCOPING_EL) {
5265                !!!cp ('t411.1');
5266                last INSCOPE;
5267              } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
5268                ## NOTE: |END_TAG_OPTIONAL_EL| includes "p"
5269                !!!cp ('t411.2');
5270              #              #
           } 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 '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 {  
               #  
             }  
5271            } else {            } else {
5272                !!!cp ('t411.3');
5273                $non_optional ||= $node;
5274              #              #
5275            }            }
5276                      } # INSCOPE
5277            if (defined $token->{tag_name}) {  
5278              !!!parse-error (type => 'after frameset:'.$token->{tag_name});          if (defined $i) {
5279              ## 1. Generate implied end tags
5280              #
5281    
5282              ## 2. If current node != "p", parse error
5283              if ($non_optional) {
5284                !!!cp ('t412.1');
5285                !!!parse-error (type => 'not closed',
5286                                text => $non_optional->[0]->manakai_local_name,
5287                                token => $token);
5288            } else {            } else {
5289              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t414.1');
5290            }            }
           ## Ignore the token  
           !!!next-token;  
           redo B;  
5291    
5292            ## ISSUE: An issue in spec there            ## 3. Pop
5293              splice @{$self->{open_elements}}, $i;
5294          } else {          } else {
5295            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t413.1');
5296              !!!parse-error (type => 'unmatched end tag',
5297                              text => $token->{tag_name}, token => $token);
5298    
5299              !!!cp ('t415.1');
5300              ## As if <p>, then reprocess the current token
5301              my $el;
5302              !!!create-element ($el, $HTML_NS, 'p',, $token);
5303              $insert->($el);
5304              ## NOTE: Not inserted into |$self->{open_elements}|.
5305          }          }
5306        }  
     } 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  
5307          !!!next-token;          !!!next-token;
5308          redo B;          next B;
5309        } elsif ($token->{type} eq 'comment') {        } elsif ({
5310          my $comment = $self->{document}->create_comment ($token->{data});                  a => 1,
5311          $self->{document}->append_child ($comment);                  b => 1, big => 1, em => 1, font => 1, i => 1,
5312                    nobr => 1, s => 1, small => 1, strike => 1,
5313                    strong => 1, tt => 1, u => 1,
5314                   }->{$token->{tag_name}}) {
5315            !!!cp ('t427');
5316            $formatting_end_tag->($token);
5317            next B;
5318          } elsif ($token->{tag_name} eq 'br') {
5319            !!!cp ('t428');
5320            !!!parse-error (type => 'unmatched end tag',
5321                            text => 'br', token => $token);
5322    
5323            ## As if <br>
5324            $reconstruct_active_formatting_elements->($insert_to_current);
5325            
5326            my $el;
5327            !!!create-element ($el, $HTML_NS, 'br',, $token);
5328            $insert->($el);
5329            
5330            ## Ignore the token.
5331          !!!next-token;          !!!next-token;
5332          redo B;          next B;
5333        } elsif ($token->{type} eq 'character') {        } else {
5334          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'sarcasm') {
5335            my $data = $1;            sleep 0.001; # take a deep breath
5336            ## As if in the main phase.          }
5337            ## NOTE: The insertion mode in the main phase  
5338            ## just before the phase has been changed to the trailing          ## Step 1
5339            ## end phase is either "after body" or "after frameset".          my $node_i = -1;
5340            $reconstruct_active_formatting_elements->($insert_to_current)          my $node = $self->{open_elements}->[$node_i];
5341              if $phase eq 'main';  
5342            ## Step 2
5343            S2: {
5344              my $node_tag_name = $node->[0]->manakai_local_name;
5345              $node_tag_name =~ tr/A-Z/a-z/; # for SVG camelCase tag names
5346              if ($node_tag_name eq $token->{tag_name}) {
5347                ## Step 1
5348                ## generate implied end tags
5349                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5350                  !!!cp ('t430');
5351                  ## NOTE: |<ruby><rt></ruby>|.
5352                  ## ISSUE: <ruby><rt></rt> will also take this code path,
5353                  ## which seems wrong.
5354                  pop @{$self->{open_elements}};
5355                  $node_i++;
5356                }
5357            
5358                ## Step 2
5359                my $current_tag_name
5360                    = $self->{open_elements}->[-1]->[0]->manakai_local_name;
5361                $current_tag_name =~ tr/A-Z/a-z/;
5362                if ($current_tag_name ne $token->{tag_name}) {
5363                  !!!cp ('t431');
5364                  ## NOTE: <x><y></x>
5365                  !!!parse-error (type => 'not closed',
5366                                  text => $self->{open_elements}->[-1]->[0]
5367                                      ->manakai_local_name,
5368                                  token => $token);
5369                } else {
5370                  !!!cp ('t432');
5371                }
5372                
5373                ## Step 3
5374                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
5375    
5376                !!!next-token;
5377                last S2;
5378              } else {
5379                ## Step 3
5380                if (not ($node->[1] & FORMATTING_EL) and
5381                    #not $phrasing_category->{$node->[1]} and
5382                    ($node->[1] & SPECIAL_EL or
5383                     $node->[1] & SCOPING_EL)) {
5384                  !!!cp ('t433');
5385                  !!!parse-error (type => 'unmatched end tag',
5386                                  text => $token->{tag_name}, token => $token);
5387                  ## Ignore the token
5388                  !!!next-token;
5389                  last S2;
5390    
5391                  ## NOTE: |<span><dd></span>a|: In Safari 3.1.2 and Opera
5392                  ## 9.27, "a" is a child of <dd> (conforming).  In
5393                  ## Firefox 3.0.2, "a" is a child of <body>.  In WinIE 7,
5394                  ## "a" is a child of both <body> and <dd>.
5395                }
5396                
5397                !!!cp ('t434');
5398              }
5399                        
5400            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 4
5401              $node_i--;
5402              $node = $self->{open_elements}->[$node_i];
5403                        
5404            unless (length $token->{data}) {            ## Step 5;
5405              !!!next-token;            redo S2;
5406              redo B;          } # S2
5407            next B;
5408          }
5409        }
5410        next B;
5411      } continue { # B
5412        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
5413          ## NOTE: The code below is executed in cases where it does not have
5414          ## to be, but it it is harmless even in those cases.
5415          ## has an element in scope
5416          INSCOPE: {
5417            for (reverse 0..$#{$self->{open_elements}}) {
5418              my $node = $self->{open_elements}->[$_];
5419              if ($node->[1] & FOREIGN_EL) {
5420                last INSCOPE;
5421              } elsif ($node->[1] & SCOPING_EL) {
5422                last;
5423            }            }
5424          }          }
5425            
5426          !!!parse-error (type => 'after html:#character');          ## NOTE: No foreign element in scope.
5427          $phase = 'main';          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
5428          ## reprocess        } # INSCOPE
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.$token->{tag_name});  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
       }  
5429      }      }
5430    } # B    } # B
5431    
# Line 4840  sub _tree_construction_main ($) { Line 5434  sub _tree_construction_main ($) {
5434    ## TODO: script stuffs    ## TODO: script stuffs
5435  } # _tree_construct_main  } # _tree_construct_main
5436    
5437  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
5438    my $class = shift;    my $class = shift;
5439    my $node = shift;    my $node = shift;
5440    my $s = \$_[0];    #my $s = \$_[0];
5441    my $onerror = $_[1];    my $onerror = $_[1];
5442      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
5443    
5444      ## ISSUE: Should {confident} be true?
5445    
5446    my $nt = $node->node_type;    my $nt = $node->node_type;
5447    if ($nt == 9) {    if ($nt == 9) {
# Line 4861  sub set_inner_html ($$$) { Line 5458  sub set_inner_html ($$$) {
5458      }      }
5459    
5460      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
5461      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
5462    } elsif ($nt == 1) {    } elsif ($nt == 1) {
5463      ## TODO: If non-html element      ## TODO: If non-html element
5464    
5465      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5466    
5467    ## TODO: Support for $get_wrapper
5468    
5469      ## Step 1 # MUST      ## Step 1 # MUST
5470      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5471      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5472        $doc->manakai_is_html (1);
5473      my $p = $class->new;      my $p = $class->new;
5474      $p->{document} = $doc;      $p->{document} = $doc;
5475    
5476      ## Step 9 # MUST      ## Step 8 # MUST
5477      my $i = 0;      my $i = 0;
5478      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
5479      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
5480      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
5481        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
5482        $input = $get_wrapper->($input);
5483        $p->{set_nc} = sub {
5484        my $self = shift;        my $self = shift;
5485        $self->{next_input_character} = -1 and return if $i >= length $$s;  
5486        $self->{next_input_character} = ord substr $$s, $i++, 1;        my $char = '';
5487        $column++;        if (defined $self->{next_nc}) {
5488            $char = $self->{next_nc};
5489        if ($self->{next_input_character} == 0x000A) { # LF          delete $self->{next_nc};
5490          $line++;          $self->{nc} = ord $char;
5491          $column = 0;        } else {
5492        } elsif ($self->{next_input_character} == 0x000D) { # CR          $self->{char_buffer} = '';
5493          if ($i >= length $$s) {          $self->{char_buffer_pos} = 0;
5494            #          
5495            my $count = $input->manakai_read_until
5496                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
5497                 $self->{char_buffer_pos});
5498            if ($count) {
5499              $self->{line_prev} = $self->{line};
5500              $self->{column_prev} = $self->{column};
5501              $self->{column}++;
5502              $self->{nc}
5503                  = ord substr ($self->{char_buffer},
5504                                $self->{char_buffer_pos}++, 1);
5505              return;
5506            }
5507            
5508            if ($input->read ($char, 1)) {
5509              $self->{nc} = ord $char;
5510          } else {          } else {
5511            my $next_char = ord substr $$s, $i++, 1;            $self->{nc} = -1;
5512            if ($next_char == 0x000A) { # LF            return;
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
5513          }          }
5514          $self->{next_input_character} = 0x000A; # LF # MUST        }
5515          $line++;  
5516          $column = 0;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
5517        } elsif ($self->{next_input_character} > 0x10FFFF) {        $p->{column}++;
5518          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
5519        } elsif ($self->{next_input_character} == 0x0000) { # NULL        if ($self->{nc} == 0x000A) { # LF
5520          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $p->{line}++;
5521            $p->{column} = 0;
5522            !!!cp ('i1');
5523          } elsif ($self->{nc} == 0x000D) { # CR
5524    ## TODO: support for abort/streaming
5525            my $next = '';
5526            if ($input->read ($next, 1) and $next ne "\x0A") {
5527              $self->{next_nc} = $next;
5528            }
5529            $self->{nc} = 0x000A; # LF # MUST
5530            $p->{line}++;
5531            $p->{column} = 0;
5532            !!!cp ('i2');
5533          } elsif ($self->{nc} == 0x0000) { # NULL
5534            !!!cp ('i4');
5535            !!!parse-error (type => 'NULL');
5536            $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5537        }        }
5538      };      };
5539        
5540        $p->{read_until} = sub {
5541          #my ($scalar, $specials_range, $offset) = @_;
5542          return 0 if defined $p->{next_nc};
5543    
5544          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
5545          my $offset = $_[2] || 0;
5546          
5547          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
5548            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
5549            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
5550              substr ($_[0], $offset)
5551                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
5552              my $count = $+[0] - $-[0];
5553              if ($count) {
5554                $p->{column} += $count;
5555                $p->{char_buffer_pos} += $count;
5556                $p->{line_prev} = $p->{line};
5557                $p->{column_prev} = $p->{column} - 1;
5558                $p->{nc} = -1;
5559              }
5560              return $count;
5561            } else {
5562              return 0;
5563            }
5564          } else {
5565            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
5566            if ($count) {
5567              $p->{column} += $count;
5568              $p->{column_prev} += $count;
5569              $p->{nc} = -1;
5570            }
5571            return $count;
5572          }
5573        }; # $p->{read_until}
5574    
5575      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5576        my (%opt) = @_;        my (%opt) = @_;
5577        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
5578          my $column = $opt{column};
5579          if (defined $opt{token} and defined $opt{token}->{line}) {
5580            $line = $opt{token}->{line};
5581            $column = $opt{token}->{column};
5582          }
5583          warn "Parse error ($opt{type}) at line $line column $column\n";
5584      };      };
5585      $p->{parse_error} = sub {      $p->{parse_error} = sub {
5586        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
5587      };      };
5588            
5589        my $char_onerror = sub {
5590          my (undef, $type, %opt) = @_;
5591          $ponerror->(layer => 'encode',
5592                      line => $p->{line}, column => $p->{column} + 1,
5593                      %opt, type => $type);
5594        }; # $char_onerror
5595        $input->onerror ($char_onerror);
5596    
5597      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
5598      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5599    
5600      ## Step 2      ## Step 2
5601      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5602      $p->{content_model_flag} = {      $p->{content_model} = {
5603        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5604        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5605        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5606        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5607        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5608        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5609        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5610        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5611        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5612        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5613      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5614         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5615            unless defined $p->{content_model};
5616            ## ISSUE: What is "the name of the element"? local name?
5617    
5618      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
5619          ## TODO: Foreign element OK?
5620    
5621      ## Step 4      ## Step 3
5622      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
5623        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
5624    
5625      ## Step 5 # MUST      ## Step 4 # MUST
5626      $doc->append_child ($root);      $doc->append_child ($root);
5627    
5628      ## Step 6 # MUST      ## Step 5 # MUST
5629      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
5630    
5631      undef $p->{head_element};      undef $p->{head_element};
5632        undef $p->{head_element_inserted};
5633    
5634      ## Step 7 # MUST      ## Step 6 # MUST
5635      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
5636    
5637      ## Step 8 # MUST      ## Step 7 # MUST
5638      my $anode = $node;      my $anode = $node;
5639      AN: while (defined $anode) {      AN: while (defined $anode) {
5640        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5641          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5642          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5643            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5644                !!!cp ('i5');
5645              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5646              last AN;              last AN;
5647            }            }
# Line 4966  sub set_inner_html ($$$) { Line 5650  sub set_inner_html ($$$) {
5650        $anode = $anode->parent_node;        $anode = $anode->parent_node;
5651      } # AN      } # AN
5652            
5653      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
5654      {      {
5655        my $self = $p;        my $self = $p;
5656        !!!next-token;        !!!next-token;
5657      }      }
5658      $p->_tree_construction_main;      $p->_tree_construction_main;
5659    
5660      ## Step 11 # MUST      ## Step 10 # MUST
5661      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
5662      for (@cn) {      for (@cn) {
5663        $node->remove_child ($_);        $node->remove_child ($_);
5664      }      }
5665      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
5666    
5667      ## Step 12 # MUST      ## Step 11 # MUST
5668      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5669      for (@cn) {      for (@cn) {
5670          $this_doc->adopt_node ($_);
5671        $node->append_child ($_);        $node->append_child ($_);
5672      }      }
5673      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5674    
5675      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5676    
5677        delete $p->{parse_error}; # delete loop
5678    } else {    } else {
5679      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
5680    }    }
# Line 4996  sub set_inner_html ($$$) { Line 5682  sub set_inner_html ($$$) {
5682    
5683  } # tree construction stage  } # tree construction stage
5684    
5685  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5686    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  
5687    
5688  1;  1;
5689  # $Date$  # $Date$

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.210

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24