/[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.13 by wakaba, Sat Jun 23 05:29:48 2007 UTC revision 1.158 by wakaba, Sun Aug 31 12:11:42 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  ## ISSUE:
7    ## var doc = implementation.createDocument (null, null, null);
8    ## doc.write ('');
9    ## alert (doc.compatMode);
10    
11    require IO::Handle;
12    
13    my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15    my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    
20    sub A_EL () { 0b1 }
21    sub ADDRESS_EL () { 0b10 }
22    sub BODY_EL () { 0b100 }
23    sub BUTTON_EL () { 0b1000 }
24    sub CAPTION_EL () { 0b10000 }
25    sub DD_EL () { 0b100000 }
26    sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233  my $permitted_slash_tag_name = {  my $svg_attr_name = {
234    base => 1,    attributename => 'attributeName',
235    link => 1,    attributetype => 'attributeType',
236    meta => 1,    basefrequency => 'baseFrequency',
237    hr => 1,    baseprofile => 'baseProfile',
238    br => 1,    calcmode => 'calcMode',
239    img=> 1,    clippathunits => 'clipPathUnits',
240    embed => 1,    contentscripttype => 'contentScriptType',
241    param => 1,    contentstyletype => 'contentStyleType',
242    area => 1,    diffuseconstant => 'diffuseConstant',
243    col => 1,    edgemode => 'edgeMode',
244    input => 1,    externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298  my $entity_char = {  my $foreign_attr_xname = {
299    AElig => "\x{00C6}",    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300    Aacute => "\x{00C1}",    'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301    Acirc => "\x{00C2}",    'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302    Agrave => "\x{00C0}",    'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303    Alpha => "\x{0391}",    'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304    Aring => "\x{00C5}",    'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305    Atilde => "\x{00C3}",    'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306    Auml => "\x{00C4}",    'xml:base' => [$XML_NS, ['xml', 'base']],
307    Beta => "\x{0392}",    'xml:lang' => [$XML_NS, ['xml', 'lang']],
308    Ccedil => "\x{00C7}",    'xml:space' => [$XML_NS, ['xml', 'space']],
309    Chi => "\x{03A7}",    'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310    Dagger => "\x{2021}",    'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    Delta => "\x{0394}",  };
312    ETH => "\x{00D0}",  
313    Eacute => "\x{00C9}",  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
   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  
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
# Line 315  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
350  my $special_category = {  sub parse_byte_string ($$$$;$) {
351    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
352    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
353    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
356    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
357    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$) {
358    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    my $self = ref $_[0] ? shift : shift->new;
359    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $charset_name = shift;
360    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $byte_stream = $_[0];
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   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,  
 };  
 # $phrasing_category: all other elements  
361    
362  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
363    my $self = shift->new;      my (%opt) = @_;
364    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
365      };
366      $self->{parse_error} = $onerror; # updated later by parse_char_string
367    
368      ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      my $charset;
371      my $buffer;
372      my ($char_stream, $e_status);
373    
374      SNIFFING: {
375    
376        ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380          ## ISSUE: Unsupported encoding is not ignored according to the spec.
381          ($char_stream, $e_status) = $charset->get_decode_handle
382              ($byte_stream, allow_error_reporting => 1,
383               allow_fallback => 1);
384          if ($char_stream) {
385            $self->{confident} = 1;
386            last SNIFFING;
387          } else {
388            ## TODO: unsupported error
389          }
390        }
391    
392        ## Step 2
393        my $byte_buffer = '';
394        for (1..1024) {
395          my $char = $byte_stream->getc;
396          last unless defined $char;
397          $byte_buffer .= $char;
398        } ## TODO: timeout
399    
400        ## Step 3
401        if ($byte_buffer =~ /^\xFE\xFF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406          $self->{confident} = 1;
407          last SNIFFING;
408        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
409          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
410          ($char_stream, $e_status) = $charset->get_decode_handle
411              ($byte_stream, allow_error_reporting => 1,
412               allow_fallback => 1, byte_buffer => \$byte_buffer);
413          $self->{confident} = 1;
414          last SNIFFING;
415        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
416          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
417          ($char_stream, $e_status) = $charset->get_decode_handle
418              ($byte_stream, allow_error_reporting => 1,
419               allow_fallback => 1, byte_buffer => \$byte_buffer);
420          $self->{confident} = 1;
421          last SNIFFING;
422        }
423    
424        ## Step 4
425        ## TODO: <meta charset>
426    
427        ## Step 5
428        ## TODO: from history
429    
430        ## Step 6
431        require Whatpm::Charset::UniversalCharDet;
432        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
433            ($byte_buffer);
434        if (defined $charset_name) {
435          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
436    
437          ## ISSUE: Unsupported encoding is not ignored according to the spec.
438          require Whatpm::Charset::DecodeHandle;
439          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
440              ($byte_stream);
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($buffer, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          if ($char_stream) {
445            $buffer->{buffer} = $byte_buffer;
446            !!!parse-error (type => 'sniffing:chardet',
447                            text => $charset_name,
448                            level => $self->{level}->{info},
449                            layer => 'encode',
450                            line => 1, column => 1);
451            $self->{confident} = 0;
452            last SNIFFING;
453          }
454        }
455    
456        ## Step 7: default
457        ## TODO: Make this configurable.
458        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
459            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
460            ## detectable in the step 6.
461        require Whatpm::Charset::DecodeHandle;
462        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
463            ($byte_stream);
464        ($char_stream, $e_status)
465            = $charset->get_decode_handle ($buffer,
466                                           allow_error_reporting => 1,
467                                           allow_fallback => 1,
468                                           byte_buffer => \$byte_buffer);
469        $buffer->{buffer} = $byte_buffer;
470        !!!parse-error (type => 'sniffing:default',
471                        text => 'windows-1252',
472                        level => $self->{level}->{info},
473                        line => 1, column => 1,
474                        layer => 'encode');
475        $self->{confident} = 0;
476      } # SNIFFING
477    
478      $self->{input_encoding} = $charset->get_iana_name;
479      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
480        !!!parse-error (type => 'chardecode:fallback',
481                        text => $self->{input_encoding},
482                        level => $self->{level}->{uncertain},
483                        line => 1, column => 1,
484                        layer => 'encode');
485      } elsif (not ($e_status &
486                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
487        !!!parse-error (type => 'chardecode:no error',
488                        text => $self->{input_encoding},
489                        level => $self->{level}->{uncertain},
490                        line => 1, column => 1,
491                        layer => 'encode');
492      }
493    
494      $self->{change_encoding} = sub {
495        my $self = shift;
496        $charset_name = shift;
497        my $token = shift;
498    
499        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
500        ($char_stream, $e_status) = $charset->get_decode_handle
501            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
502             byte_buffer => \ $buffer->{buffer});
503        
504        if ($char_stream) { # if supported
505          ## "Change the encoding" algorithm:
506    
507          ## Step 1    
508          if ($charset->{category} &
509              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
510            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
511            ($char_stream, $e_status) = $charset->get_decode_handle
512                ($byte_stream,
513                 byte_buffer => \ $buffer->{buffer});
514          }
515          $charset_name = $charset->get_iana_name;
516          
517          ## Step 2
518          if (defined $self->{input_encoding} and
519              $self->{input_encoding} eq $charset_name) {
520            !!!parse-error (type => 'charset label:matching',
521                            text => $charset_name,
522                            level => $self->{level}->{info});
523            $self->{confident} = 1;
524            return;
525          }
526    
527          !!!parse-error (type => 'charset label detected',
528                          text => $self->{input_encoding},
529                          value => $charset_name,
530                          level => $self->{level}->{warn},
531                          token => $token);
532          
533          ## Step 3
534          # if (can) {
535            ## change the encoding on the fly.
536            #$self->{confident} = 1;
537            #return;
538          # }
539          
540          ## Step 4
541          throw Whatpm::HTML::RestartParser ();
542        }
543      }; # $self->{change_encoding}
544    
545      my $char_onerror = sub {
546        my (undef, $type, %opt) = @_;
547        !!!parse-error (layer => 'encode',
548                        %opt, type => $type,
549                        line => $self->{line}, column => $self->{column} + 1);
550        if ($opt{octets}) {
551          ${$opt{octets}} = "\x{FFFD}"; # relacement character
552        }
553      };
554      $char_stream->onerror ($char_onerror);
555    
556      my @args = @_; shift @args; # $s
557      my $return;
558      try {
559        $return = $self->parse_char_stream ($char_stream, @args);  
560      } catch Whatpm::HTML::RestartParser with {
561        ## NOTE: Invoked after {change_encoding}.
562    
563        $self->{input_encoding} = $charset->get_iana_name;
564        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
565          !!!parse-error (type => 'chardecode:fallback',
566                          text => $self->{input_encoding},
567                          level => $self->{level}->{uncertain},
568                          line => 1, column => 1,
569                          layer => 'encode');
570        } elsif (not ($e_status &
571                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
572          !!!parse-error (type => 'chardecode:no error',
573                          text => $self->{input_encoding},
574                          level => $self->{level}->{uncertain},
575                          line => 1, column => 1,
576                          layer => 'encode');
577        }
578        $self->{confident} = 1;
579        $char_stream->onerror ($char_onerror);
580        $return = $self->parse_char_stream ($char_stream, @args);
581      };
582      return $return;
583    } # parse_byte_stream
584    
585    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
586    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
587    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
588    ## because the core part of our HTML parser expects a string of character,
589    ## not a string of bytes or code units or anything which might contain a BOM.
590    ## Therefore, any parser interface that accepts a string of bytes,
591    ## such as |parse_byte_string| in this module, must ensure that it does
592    ## strip the BOM and never strip any ZWNBSP.
593    
594    sub parse_char_string ($$$;$) {
595      my $self = shift;
596      require utf8;
597      my $s = ref $_[0] ? $_[0] : \($_[0]);
598      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
599      return $self->parse_char_stream ($input, @_[1..$#_]);
600    } # parse_char_string
601    *parse_string = \&parse_char_string;
602    
603    sub parse_char_stream ($$$;$) {
604      my $self = ref $_[0] ? shift : shift->new;
605      my $input = $_[0];
606    $self->{document} = $_[1];    $self->{document} = $_[1];
607      @{$self->{document}->child_nodes} = ();
608    
609    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
610    
611      $self->{confident} = 1 unless exists $self->{confident};
612      $self->{document}->input_encoding ($self->{input_encoding})
613          if defined $self->{input_encoding};
614    
615    my $i = 0;    my $i = 0;
616    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
617    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
618    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
619      my $self = shift;      my $self = shift;
620    
621      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
622      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
623    
624      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
625      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
626      $column++;        $char = $self->{next_next_char};
627          delete $self->{next_next_char};
628        } else {
629          $char = $input->getc;
630        }
631        $self->{next_char} = -1 and return unless defined $char;
632        $self->{next_char} = ord $char;
633    
634        ($self->{line_prev}, $self->{column_prev})
635            = ($self->{line}, $self->{column});
636        $self->{column}++;
637            
638      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
639        $line++;        !!!cp ('j1');
640        $column = 0;        $self->{line}++;
641      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
642        if ($i >= length $$s) {      } elsif ($self->{next_char} == 0x000D) { # CR
643          #        !!!cp ('j2');
644          my $next = $input->getc;
645          if (defined $next and $next ne "\x0A") {
646            $self->{next_next_char} = $next;
647          }
648          $self->{next_char} = 0x000A; # LF # MUST
649          $self->{line}++;
650          $self->{column} = 0;
651        } elsif ($self->{next_char} > 0x10FFFF) {
652          !!!cp ('j3');
653          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
654        } elsif ($self->{next_char} == 0x0000) { # NULL
655          !!!cp ('j4');
656          !!!parse-error (type => 'NULL');
657          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
658        } elsif ($self->{next_char} <= 0x0008 or
659                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
660                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
661                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
662                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
663                 {
664                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
665                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
666                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
667                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
668                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
669                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
670                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
671                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
672                  0x10FFFE => 1, 0x10FFFF => 1,
673                 }->{$self->{next_char}}) {
674          !!!cp ('j5');
675          if ($self->{next_char} < 0x10000) {
676            !!!parse-error (type => 'control char',
677                            text => (sprintf 'U+%04X', $self->{next_char}));
678        } else {        } else {
679          my $next_char = ord substr $$s, $i++, 1;          !!!parse-error (type => 'control char',
680          if ($next_char == 0x000A) { # LF                          text => (sprintf 'U-%08X', $self->{next_char}));
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
681        }        }
       $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');  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
682      }      }
683    };    };
684    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
685    $self->{next_input_character} = -1;    $self->{next_char} = -1;
686    
687    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
688      my (%opt) = @_;      my (%opt) = @_;
689      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
690        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
691        warn "Parse error ($opt{type}) at line $line column $column\n";
692    };    };
693    $self->{parse_error} = sub {    $self->{parse_error} = sub {
694      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
695    };    };
696    
697    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 397  sub parse_string ($$$;$) { Line 699  sub parse_string ($$$;$) {
699    $self->_construct_tree;    $self->_construct_tree;
700    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
701    
702      delete $self->{parse_error}; # remove loop
703    
704    return $self->{document};    return $self->{document};
705  } # parse_string  } # parse_char_stream
706    
707  sub new ($) {  sub new ($) {
708    my $class = shift;    my $class = shift;
709    my $self = bless {}, $class;    my $self = bless {
710    $self->{set_next_input_character} = sub {      level => {must => 'm',
711      $self->{next_input_character} = -1;                warn => 'w',
712                  info => 'i',
713                  uncertain => 'u'},
714      }, $class;
715      $self->{set_next_char} = sub {
716        $self->{next_char} = -1;
717    };    };
718    $self->{parse_error} = sub {    $self->{parse_error} = sub {
719      #      #
720    };    };
721      $self->{change_encoding} = sub {
722        # if ($_[0] is a supported encoding) {
723        #   run "change the encoding" algorithm;
724        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
725        # }
726      };
727      $self->{application_cache_selection} = sub {
728        #
729      };
730    return $self;    return $self;
731  } # new  } # new
732    
733    sub CM_ENTITY () { 0b001 } # & markup in data
734    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
735    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
736    
737    sub PLAINTEXT_CONTENT_MODEL () { 0 }
738    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
739    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
740    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
741    
742    sub DATA_STATE () { 0 }
743    sub ENTITY_DATA_STATE () { 1 }
744    sub TAG_OPEN_STATE () { 2 }
745    sub CLOSE_TAG_OPEN_STATE () { 3 }
746    sub TAG_NAME_STATE () { 4 }
747    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
748    sub ATTRIBUTE_NAME_STATE () { 6 }
749    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
750    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
751    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
752    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
753    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
754    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
755    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
756    sub COMMENT_START_STATE () { 14 }
757    sub COMMENT_START_DASH_STATE () { 15 }
758    sub COMMENT_STATE () { 16 }
759    sub COMMENT_END_STATE () { 17 }
760    sub COMMENT_END_DASH_STATE () { 18 }
761    sub BOGUS_COMMENT_STATE () { 19 }
762    sub DOCTYPE_STATE () { 20 }
763    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
764    sub DOCTYPE_NAME_STATE () { 22 }
765    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
766    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
767    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
768    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
769    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
770    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
771    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
772    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
773    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
774    sub BOGUS_DOCTYPE_STATE () { 32 }
775    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
776    sub SELF_CLOSING_START_TAG_STATE () { 34 }
777    sub CDATA_BLOCK_STATE () { 35 }
778    
779    sub DOCTYPE_TOKEN () { 1 }
780    sub COMMENT_TOKEN () { 2 }
781    sub START_TAG_TOKEN () { 3 }
782    sub END_TAG_TOKEN () { 4 }
783    sub END_OF_FILE_TOKEN () { 5 }
784    sub CHARACTER_TOKEN () { 6 }
785    
786    sub AFTER_HTML_IMS () { 0b100 }
787    sub HEAD_IMS ()       { 0b1000 }
788    sub BODY_IMS ()       { 0b10000 }
789    sub BODY_TABLE_IMS () { 0b100000 }
790    sub TABLE_IMS ()      { 0b1000000 }
791    sub ROW_IMS ()        { 0b10000000 }
792    sub BODY_AFTER_IMS () { 0b100000000 }
793    sub FRAME_IMS ()      { 0b1000000000 }
794    sub SELECT_IMS ()     { 0b10000000000 }
795    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
796        ## NOTE: "in foreign content" insertion mode is special; it is combined
797        ## with the secondary insertion mode.  In this parser, they are stored
798        ## together in the bit-or'ed form.
799    
800    ## NOTE: "initial" and "before html" insertion modes have no constants.
801    
802    ## NOTE: "after after body" insertion mode.
803    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
804    
805    ## NOTE: "after after frameset" insertion mode.
806    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
807    
808    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
809    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
810    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
811    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
812    sub IN_BODY_IM () { BODY_IMS }
813    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
814    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
815    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
816    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
817    sub IN_TABLE_IM () { TABLE_IMS }
818    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
819    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
820    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
821    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
822    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
823    sub IN_COLUMN_GROUP_IM () { 0b10 }
824    
825  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
826    
827  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
828    my $self = shift;    my $self = shift;
829    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
830    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
831    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
832    undef $self->{current_attribute};    undef $self->{current_attribute};
833    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
834    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
835      delete $self->{self_closing};
836    $self->{char} = [];    $self->{char} = [];
837    # $self->{next_input_character}    # $self->{next_char}
838    !!!next-input-character;    !!!next-input-character;
839    $self->{token} = [];    $self->{token} = [];
840      # $self->{escape}
841  } # _initialize_tokenizer  } # _initialize_tokenizer
842    
843  ## A token has:  ## A token has:
844  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
845  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
846  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
847      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
848  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
849  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
850  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
851    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
852  ## Macros  ##        ->{name}
853  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
854  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
855  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
856    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
857    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
858    ##     while the token is pushed back to the stack.
859    
860  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
861    
# Line 450  sub _initialize_tokenizer ($) { Line 865  sub _initialize_tokenizer ($) {
865  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
866  ## and removed from the list.  ## and removed from the list.
867    
868    ## NOTE: HTML5 "Writing HTML documents" section, applied to
869    ## documents and not to user agents and conformance checkers,
870    ## contains some requirements that are not detected by the
871    ## parsing algorithm:
872    ## - Some requirements on character encoding declarations. ## TODO
873    ## - "Elements MUST NOT contain content that their content model disallows."
874    ##   ... Some are parse error, some are not (will be reported by c.c.).
875    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
876    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
877    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
878    
879    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
880    ## be detected by the HTML5 parsing algorithm:
881    ## - Text,
882    
883  sub _get_next_token ($) {  sub _get_next_token ($) {
884    my $self = shift;    my $self = shift;
885    
886      if ($self->{self_closing}) {
887        !!!parse-error (type => 'nestc', token => $self->{current_token});
888        ## NOTE: The |self_closing| flag is only set by start tag token.
889        ## In addition, when a start tag token is emitted, it is always set to
890        ## |current_token|.
891        delete $self->{self_closing};
892      }
893    
894    if (@{$self->{token}}) {    if (@{$self->{token}}) {
895        $self->{self_closing} = $self->{token}->[0]->{self_closing};
896      return shift @{$self->{token}};      return shift @{$self->{token}};
897    }    }
898    
899    A: {    A: {
900      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
901        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
902          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
903              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
904            $self->{state} = 'entity data';            !!!cp (1);
905              $self->{state} = ENTITY_DATA_STATE;
906            !!!next-input-character;            !!!next-input-character;
907            redo A;            redo A;
908          } else {          } else {
909              !!!cp (2);
910            #            #
911          }          }
912        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
913          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
914            unless ($self->{escape}) {            unless ($self->{escape}) {
915              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
916                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
917                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
918                  !!!cp (3);
919                $self->{escape} = 1;                $self->{escape} = 1;
920                } else {
921                  !!!cp (4);
922              }              }
923              } else {
924                !!!cp (5);
925            }            }
926          }          }
927                    
928          #          #
929        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
930          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
931              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
932               not $self->{escape})) {               not $self->{escape})) {
933            $self->{state} = 'tag open';            !!!cp (6);
934              $self->{state} = TAG_OPEN_STATE;
935            !!!next-input-character;            !!!next-input-character;
936            redo A;            redo A;
937          } else {          } else {
938              !!!cp (7);
939            #            #
940          }          }
941        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
942          if ($self->{escape} and          if ($self->{escape} and
943              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
944               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
945            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
946                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
947              delete $self->{escape};              delete $self->{escape};
948              } else {
949                !!!cp (9);
950            }            }
951            } else {
952              !!!cp (10);
953          }          }
954                    
955          #          #
956        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
957          !!!emit ({type => 'end-of-file'});          !!!cp (11);
958            !!!emit ({type => END_OF_FILE_TOKEN,
959                      line => $self->{line}, column => $self->{column}});
960          last A; ## TODO: ok?          last A; ## TODO: ok?
961          } else {
962            !!!cp (12);
963        }        }
964        # Anything else        # Anything else
965        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
966                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
967                       line => $self->{line}, column => $self->{column},
968                      };
969        ## Stay in the data state        ## Stay in the data state
970        !!!next-input-character;        !!!next-input-character;
971    
972        !!!emit ($token);        !!!emit ($token);
973    
974        redo A;        redo A;
975      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
976        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
977    
978          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
979                
980        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
981    
982        $self->{state} = 'data';        $self->{state} = DATA_STATE;
983        # next-input-character is already done        # next-input-character is already done
984    
985        unless (defined $token) {        unless (defined $token) {
986          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
987            !!!emit ({type => CHARACTER_TOKEN, data => '&',
988                      line => $l, column => $c,
989                     });
990        } else {        } else {
991            !!!cp (14);
992          !!!emit ($token);          !!!emit ($token);
993        }        }
994    
995        redo A;        redo A;
996      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
997        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
998            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
999          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
1000            !!!next-input-character;            !!!next-input-character;
1001            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1002            redo A;            redo A;
1003          } else {          } else {
1004              !!!cp (16);
1005            ## reconsume            ## reconsume
1006            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1007    
1008            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1009                        line => $self->{line_prev},
1010                        column => $self->{column_prev},
1011                       });
1012    
1013            redo A;            redo A;
1014          }          }
1015        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1016          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1017            $self->{state} = 'markup declaration open';            !!!cp (17);
1018              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1019            !!!next-input-character;            !!!next-input-character;
1020            redo A;            redo A;
1021          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1022            $self->{state} = 'close tag open';            !!!cp (18);
1023              $self->{state} = CLOSE_TAG_OPEN_STATE;
1024            !!!next-input-character;            !!!next-input-character;
1025            redo A;            redo A;
1026          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1027                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1028              !!!cp (19);
1029            $self->{current_token}            $self->{current_token}
1030              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1031                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1032            $self->{state} = 'tag name';                 line => $self->{line_prev},
1033                   column => $self->{column_prev}};
1034              $self->{state} = TAG_NAME_STATE;
1035            !!!next-input-character;            !!!next-input-character;
1036            redo A;            redo A;
1037          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1038                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1039            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1040                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1041            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1042                                        line => $self->{line_prev},
1043                                        column => $self->{column_prev}};
1044              $self->{state} = TAG_NAME_STATE;
1045            !!!next-input-character;            !!!next-input-character;
1046            redo A;            redo A;
1047          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1048            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1049            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1050                              line => $self->{line_prev},
1051                              column => $self->{column_prev});
1052              $self->{state} = DATA_STATE;
1053            !!!next-input-character;            !!!next-input-character;
1054    
1055            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1056                        line => $self->{line_prev},
1057                        column => $self->{column_prev},
1058                       });
1059    
1060            redo A;            redo A;
1061          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1062            !!!parse-error (type => 'pio');            !!!cp (22);
1063            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1064            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1065                              column => $self->{column_prev});
1066              $self->{state} = BOGUS_COMMENT_STATE;
1067              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1068                                        line => $self->{line_prev},
1069                                        column => $self->{column_prev},
1070                                       };
1071              ## $self->{next_char} is intentionally left as is
1072            redo A;            redo A;
1073          } else {          } else {
1074            !!!parse-error (type => 'bare stago');            !!!cp (23);
1075            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1076                              line => $self->{line_prev},
1077                              column => $self->{column_prev});
1078              $self->{state} = DATA_STATE;
1079            ## reconsume            ## reconsume
1080    
1081            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1082                        line => $self->{line_prev},
1083                        column => $self->{column_prev},
1084                       });
1085    
1086            redo A;            redo A;
1087          }          }
1088        } else {        } else {
1089          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1090        }        }
1091      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1092        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1093            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1094          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
1095          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
1096            push @next_char, $self->{next_input_character};            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1097            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            my @next_char;
1098            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1099            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              push @next_char, $self->{next_char};
1100              !!!next-input-character;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1101              next TAGNAME;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1102            } else {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1103              !!!parse-error (type => 'unmatched end tag');                !!!cp (24);
1104              $self->{next_input_character} = shift @next_char; # reconsume                !!!next-input-character;
1105                  next TAGNAME;
1106                } else {
1107                  !!!cp (25);
1108                  $self->{next_char} = shift @next_char; # reconsume
1109                  !!!back-next-input-character (@next_char);
1110                  $self->{state} = DATA_STATE;
1111    
1112                  !!!emit ({type => CHARACTER_TOKEN, data => '</',
1113                            line => $l, column => $c,
1114                           });
1115      
1116                  redo A;
1117                }
1118              }
1119              push @next_char, $self->{next_char};
1120          
1121              unless ($self->{next_char} == 0x0009 or # HT
1122                      $self->{next_char} == 0x000A or # LF
1123                      $self->{next_char} == 0x000B or # VT
1124                      $self->{next_char} == 0x000C or # FF
1125                      $self->{next_char} == 0x0020 or # SP
1126                      $self->{next_char} == 0x003E or # >
1127                      $self->{next_char} == 0x002F or # /
1128                      $self->{next_char} == -1) {
1129                !!!cp (26);
1130                $self->{next_char} = shift @next_char; # reconsume
1131              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1132              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1133                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1134              !!!emit ({type => 'character', data => '</'});                        line => $l, column => $c,
1135                         });
1136              redo A;              redo A;
1137              } else {
1138                !!!cp (27);
1139                $self->{next_char} = shift @next_char;
1140                !!!back-next-input-character (@next_char);
1141                # and consume...
1142            }            }
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error (type => 'unmatched end tag');  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
           redo A;  
1143          } else {          } else {
1144            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1145            !!!back-next-input-character (@next_char);            !!!cp (28);
1146            # and consume...            # next-input-character is already done
1147              $self->{state} = DATA_STATE;
1148              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1149                        line => $l, column => $c,
1150                       });
1151              redo A;
1152          }          }
1153        }        }
1154                
1155        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1156            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1157          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1158                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1159          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1160                   tag_name => chr ($self->{next_char} + 0x0020),
1161                   line => $l, column => $c};
1162            $self->{state} = TAG_NAME_STATE;
1163            !!!next-input-character;
1164            redo A;
1165          } elsif (0x0061 <= $self->{next_char} and
1166                   $self->{next_char} <= 0x007A) { # a..z
1167            !!!cp (30);
1168            $self->{current_token} = {type => END_TAG_TOKEN,
1169                                      tag_name => chr ($self->{next_char}),
1170                                      line => $l, column => $c};
1171            $self->{state} = TAG_NAME_STATE;
1172            !!!next-input-character;
1173            redo A;
1174          } elsif ($self->{next_char} == 0x003E) { # >
1175            !!!cp (31);
1176            !!!parse-error (type => 'empty end tag',
1177                            line => $self->{line_prev}, ## "<" in "</>"
1178                            column => $self->{column_prev} - 1);
1179            $self->{state} = DATA_STATE;
1180          !!!next-input-character;          !!!next-input-character;
1181          redo A;          redo A;
1182        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
1183                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (32);
         $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) {  
1184          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1185          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1186          # reconsume          # reconsume
1187    
1188          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1189                      line => $l, column => $c,
1190                     });
1191    
1192          redo A;          redo A;
1193        } else {        } else {
1194            !!!cp (33);
1195          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1196          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1197          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1198          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1199        }                                    column => $self->{column_prev} - 1,
1200      } elsif ($self->{state} eq 'tag name') {                                   };
1201        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1202            $self->{next_input_character} == 0x000A or # LF          redo A;
1203            $self->{next_input_character} == 0x000B or # VT        }
1204            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1205            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1206          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1207          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1208          redo A;            $self->{next_char} == 0x000C or # FF
1209        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1210          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1211            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1212            !!!next-input-character;
1213            redo A;
1214          } elsif ($self->{next_char} == 0x003E) { # >
1215            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1216              !!!cp (35);
1217            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1218          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1219            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1220            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1221              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1222            }            #  !!! cp (36);
1223              #  !!! parse-error (type => 'end tag attribute');
1224              #} else {
1225                !!!cp (37);
1226              #}
1227          } else {          } else {
1228            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1229          }          }
1230          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1231          !!!next-input-character;          !!!next-input-character;
1232    
1233          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1234    
1235          redo A;          redo A;
1236        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1237                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1238          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1239            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1240            # start tag or end tag            # start tag or end tag
1241          ## Stay in this state          ## Stay in this state
1242          !!!next-input-character;          !!!next-input-character;
1243          redo A;          redo A;
1244        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1245          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1246          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1247              !!!cp (39);
1248            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1249          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1250            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1251            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1252              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1253            }            #  !!! cp (40);
1254              #  !!! parse-error (type => 'end tag attribute');
1255              #} else {
1256                !!!cp (41);
1257              #}
1258          } else {          } else {
1259            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1260          }          }
1261          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1262          # reconsume          # reconsume
1263    
1264          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1265    
1266          redo A;          redo A;
1267        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1268            !!!cp (42);
1269            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1270          !!!next-input-character;          !!!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  
1271          redo A;          redo A;
1272        } else {        } else {
1273          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1274            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1275            # start tag or end tag            # start tag or end tag
1276          ## Stay in the state          ## Stay in the state
1277          !!!next-input-character;          !!!next-input-character;
1278          redo A;          redo A;
1279        }        }
1280      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1281        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1282            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1283            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1284            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1285            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1286            !!!cp (45);
1287          ## Stay in the state          ## Stay in the state
1288          !!!next-input-character;          !!!next-input-character;
1289          redo A;          redo A;
1290        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1291          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1292              !!!cp (46);
1293            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1294          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1295            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1296            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1297                !!!cp (47);
1298              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1299              } else {
1300                !!!cp (48);
1301            }            }
1302          } else {          } else {
1303            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1304          }          }
1305          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1306          !!!next-input-character;          !!!next-input-character;
1307    
1308          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1309    
1310          redo A;          redo A;
1311        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1312                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1313          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1314                                value => ''};          $self->{current_attribute}
1315          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1316          !!!next-input-character;                 value => '',
1317          redo A;                 line => $self->{line}, column => $self->{column}};
1318        } elsif ($self->{next_input_character} == 0x002F) { # /          $self->{state} = ATTRIBUTE_NAME_STATE;
1319            !!!next-input-character;
1320            redo A;
1321          } elsif ($self->{next_char} == 0x002F) { # /
1322            !!!cp (50);
1323            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1324          !!!next-input-character;          !!!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  
1325          redo A;          redo A;
1326        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1327          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1328          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1329              !!!cp (52);
1330            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1331          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1332            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1333            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1334                !!!cp (53);
1335              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1336              } else {
1337                !!!cp (54);
1338            }            }
1339          } else {          } else {
1340            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1341          }          }
1342          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1343          # reconsume          # reconsume
1344    
1345          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1346    
1347          redo A;          redo A;
1348        } else {        } else {
1349          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1350                                value => ''};               0x0022 => 1, # "
1351          $self->{state} = 'attribute name';               0x0027 => 1, # '
1352                 0x003D => 1, # =
1353                }->{$self->{next_char}}) {
1354              !!!cp (55);
1355              !!!parse-error (type => 'bad attribute name');
1356            } else {
1357              !!!cp (56);
1358            }
1359            $self->{current_attribute}
1360                = {name => chr ($self->{next_char}),
1361                   value => '',
1362                   line => $self->{line}, column => $self->{column}};
1363            $self->{state} = ATTRIBUTE_NAME_STATE;
1364          !!!next-input-character;          !!!next-input-character;
1365          redo A;          redo A;
1366        }        }
1367      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1368        my $before_leave = sub {        my $before_leave = sub {
1369          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1370              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1371            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1372              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1373            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1374          } else {          } else {
1375              !!!cp (58);
1376            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1377              = $self->{current_attribute};              = $self->{current_attribute};
1378          }          }
1379        }; # $before_leave        }; # $before_leave
1380    
1381        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1382            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1383            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1384            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1385            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1386            !!!cp (59);
1387          $before_leave->();          $before_leave->();
1388          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1389          !!!next-input-character;          !!!next-input-character;
1390          redo A;          redo A;
1391        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1392            !!!cp (60);
1393          $before_leave->();          $before_leave->();
1394          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1395          !!!next-input-character;          !!!next-input-character;
1396          redo A;          redo A;
1397        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1398          $before_leave->();          $before_leave->();
1399          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1400              !!!cp (61);
1401            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1402          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1403            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1404              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1405            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1406              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1407            }            }
1408          } else {          } else {
1409            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1410          }          }
1411          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1412          !!!next-input-character;          !!!next-input-character;
1413    
1414          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1415    
1416          redo A;          redo A;
1417        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1418                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1419          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1420            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1421          ## Stay in the state          ## Stay in the state
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
1424        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1425            !!!cp (64);
1426          $before_leave->();          $before_leave->();
1427            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1428          !!!next-input-character;          !!!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  
1429          redo A;          redo A;
1430        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1431          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1432          $before_leave->();          $before_leave->();
1433          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1434              !!!cp (66);
1435            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1436          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1437            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1438            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1439                !!!cp (67);
1440              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1441              } else {
1442                ## NOTE: This state should never be reached.
1443                !!!cp (68);
1444            }            }
1445          } else {          } else {
1446            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1447          }          }
1448          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1449          # reconsume          # reconsume
1450    
1451          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1452    
1453          redo A;          redo A;
1454        } else {        } else {
1455          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1456                $self->{next_char} == 0x0027) { # '
1457              !!!cp (69);
1458              !!!parse-error (type => 'bad attribute name');
1459            } else {
1460              !!!cp (70);
1461            }
1462            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1463          ## Stay in the state          ## Stay in the state
1464          !!!next-input-character;          !!!next-input-character;
1465          redo A;          redo A;
1466        }        }
1467      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1468        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1469            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1470            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1471            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1472            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1473            !!!cp (71);
1474          ## Stay in the state          ## Stay in the state
1475          !!!next-input-character;          !!!next-input-character;
1476          redo A;          redo A;
1477        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1478          $self->{state} = 'before attribute value';          !!!cp (72);
1479            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1480          !!!next-input-character;          !!!next-input-character;
1481          redo A;          redo A;
1482        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1483          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1484              !!!cp (73);
1485            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1486          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1487            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1488            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1489                !!!cp (74);
1490              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1491              } else {
1492                ## NOTE: This state should never be reached.
1493                !!!cp (75);
1494            }            }
1495          } else {          } else {
1496            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1497          }          }
1498          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1499          !!!next-input-character;          !!!next-input-character;
1500    
1501          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1502    
1503          redo A;          redo A;
1504        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1505                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1506          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1507                                value => ''};          $self->{current_attribute}
1508          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1509                   value => '',
1510                   line => $self->{line}, column => $self->{column}};
1511            $self->{state} = ATTRIBUTE_NAME_STATE;
1512            !!!next-input-character;
1513            redo A;
1514          } elsif ($self->{next_char} == 0x002F) { # /
1515            !!!cp (77);
1516            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1517          !!!next-input-character;          !!!next-input-character;
1518          redo A;          redo A;
1519        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!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) {  
1520          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1521          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1522              !!!cp (79);
1523            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1524          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1525            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1526            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1527                !!!cp (80);
1528              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1529              } else {
1530                ## NOTE: This state should never be reached.
1531                !!!cp (81);
1532            }            }
1533          } else {          } else {
1534            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1535          }          }
1536          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1537          # reconsume          # reconsume
1538    
1539          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1540    
1541          redo A;          redo A;
1542        } else {        } else {
1543          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1544                                value => ''};              $self->{next_char} == 0x0027) { # '
1545          $self->{state} = 'attribute name';            !!!cp (78);
1546              !!!parse-error (type => 'bad attribute name');
1547            } else {
1548              !!!cp (82);
1549            }
1550            $self->{current_attribute}
1551                = {name => chr ($self->{next_char}),
1552                   value => '',
1553                   line => $self->{line}, column => $self->{column}};
1554            $self->{state} = ATTRIBUTE_NAME_STATE;
1555          !!!next-input-character;          !!!next-input-character;
1556          redo A;                  redo A;        
1557        }        }
1558      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1559        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1560            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1561            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1562            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1563            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1564            !!!cp (83);
1565          ## Stay in the state          ## Stay in the state
1566          !!!next-input-character;          !!!next-input-character;
1567          redo A;          redo A;
1568        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1569          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1570            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1571          !!!next-input-character;          !!!next-input-character;
1572          redo A;          redo A;
1573        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1574          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1575            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1576          ## reconsume          ## reconsume
1577          redo A;          redo A;
1578        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1579          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1580            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1581          !!!next-input-character;          !!!next-input-character;
1582          redo A;          redo A;
1583        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1584          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1585            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1586              !!!cp (87);
1587            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1588          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1589            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1590            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1591                !!!cp (88);
1592              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1593              } else {
1594                ## NOTE: This state should never be reached.
1595                !!!cp (89);
1596            }            }
1597          } else {          } else {
1598            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1599          }          }
1600          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1601          !!!next-input-character;          !!!next-input-character;
1602    
1603          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1604    
1605          redo A;          redo A;
1606        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1607          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1608          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1609              !!!cp (90);
1610            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1611          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1612            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1613            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1614                !!!cp (91);
1615              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1616              } else {
1617                ## NOTE: This state should never be reached.
1618                !!!cp (92);
1619            }            }
1620          } else {          } else {
1621            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1622          }          }
1623          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1624          ## reconsume          ## reconsume
1625    
1626          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1627    
1628          redo A;          redo A;
1629        } else {        } else {
1630          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1631          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1632              !!!parse-error (type => 'bad attribute value');
1633            } else {
1634              !!!cp (94);
1635            }
1636            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1637            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1638          !!!next-input-character;          !!!next-input-character;
1639          redo A;          redo A;
1640        }        }
1641      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1642        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1643          $self->{state} = 'before attribute name';          !!!cp (95);
1644            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1645          !!!next-input-character;          !!!next-input-character;
1646          redo A;          redo A;
1647        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1648          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1649          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1650            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1651          !!!next-input-character;          !!!next-input-character;
1652          redo A;          redo A;
1653        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1654          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1655          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1656              !!!cp (97);
1657            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1658          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1659            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1660            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1661                !!!cp (98);
1662              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1663              } else {
1664                ## NOTE: This state should never be reached.
1665                !!!cp (99);
1666            }            }
1667          } else {          } else {
1668            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1669          }          }
1670          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1671          ## reconsume          ## reconsume
1672    
1673          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1674    
1675          redo A;          redo A;
1676        } else {        } else {
1677          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1678            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1679          ## Stay in the state          ## Stay in the state
1680          !!!next-input-character;          !!!next-input-character;
1681          redo A;          redo A;
1682        }        }
1683      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1684        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1685          $self->{state} = 'before attribute name';          !!!cp (101);
1686            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1687          !!!next-input-character;          !!!next-input-character;
1688          redo A;          redo A;
1689        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1690          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1691          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1692            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1693          !!!next-input-character;          !!!next-input-character;
1694          redo A;          redo A;
1695        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1696          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1697          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1698              !!!cp (103);
1699            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1700          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1701            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1702            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1703                !!!cp (104);
1704              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1705              } else {
1706                ## NOTE: This state should never be reached.
1707                !!!cp (105);
1708            }            }
1709          } else {          } else {
1710            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1711          }          }
1712          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1713          ## reconsume          ## reconsume
1714    
1715          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1716    
1717          redo A;          redo A;
1718        } else {        } else {
1719          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1720            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1721          ## Stay in the state          ## Stay in the state
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        }        }
1725      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1726        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1727            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1728            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1729            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1730            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1731          $self->{state} = 'before attribute name';          !!!cp (107);
1732          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1733          redo A;          !!!next-input-character;
1734        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1735          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1736          $self->{state} = 'entity in attribute value';          !!!cp (108);
1737          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1738          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1739        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1740          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1741          } elsif ($self->{next_char} == 0x003E) { # >
1742            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1743              !!!cp (109);
1744            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1745          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1746            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1747            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1748                !!!cp (110);
1749              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1750              } else {
1751                ## NOTE: This state should never be reached.
1752                !!!cp (111);
1753            }            }
1754          } else {          } else {
1755            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1756          }          }
1757          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1758          !!!next-input-character;          !!!next-input-character;
1759    
1760          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1761    
1762          redo A;          redo A;
1763        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1764          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1765          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1766              !!!cp (112);
1767            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1768          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1769            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1770            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1771                !!!cp (113);
1772              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1773              } else {
1774                ## NOTE: This state should never be reached.
1775                !!!cp (114);
1776            }            }
1777          } else {          } else {
1778            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1779          }          }
1780          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1781          ## reconsume          ## reconsume
1782    
1783          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1784    
1785          redo A;          redo A;
1786        } else {        } else {
1787          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1788                 0x0022 => 1, # "
1789                 0x0027 => 1, # '
1790                 0x003D => 1, # =
1791                }->{$self->{next_char}}) {
1792              !!!cp (115);
1793              !!!parse-error (type => 'bad attribute value');
1794            } else {
1795              !!!cp (116);
1796            }
1797            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1798          ## Stay in the state          ## Stay in the state
1799          !!!next-input-character;          !!!next-input-character;
1800          redo A;          redo A;
1801        }        }
1802      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1803        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1804              (1,
1805               $self->{last_attribute_value_state}
1806                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1807               $self->{last_attribute_value_state}
1808                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1809               -1);
1810    
1811        unless (defined $token) {        unless (defined $token) {
1812            !!!cp (117);
1813          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1814        } else {        } else {
1815            !!!cp (118);
1816          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1817            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1818          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1819        }        }
1820    
1821        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1822        # next-input-character is already done        # next-input-character is already done
1823        redo A;        redo A;
1824      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1825          if ($self->{next_char} == 0x0009 or # HT
1826              $self->{next_char} == 0x000A or # LF
1827              $self->{next_char} == 0x000B or # VT
1828              $self->{next_char} == 0x000C or # FF
1829              $self->{next_char} == 0x0020) { # SP
1830            !!!cp (118);
1831            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1832            !!!next-input-character;
1833            redo A;
1834          } elsif ($self->{next_char} == 0x003E) { # >
1835            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1836              !!!cp (119);
1837              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1838            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1839              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1840              if ($self->{current_token}->{attributes}) {
1841                !!!cp (120);
1842                !!!parse-error (type => 'end tag attribute');
1843              } else {
1844                ## NOTE: This state should never be reached.
1845                !!!cp (121);
1846              }
1847            } else {
1848              die "$0: $self->{current_token}->{type}: Unknown token type";
1849            }
1850            $self->{state} = DATA_STATE;
1851            !!!next-input-character;
1852    
1853            !!!emit ($self->{current_token}); # start tag or end tag
1854    
1855            redo A;
1856          } elsif ($self->{next_char} == 0x002F) { # /
1857            !!!cp (122);
1858            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1859            !!!next-input-character;
1860            redo A;
1861          } elsif ($self->{next_char} == -1) {
1862            !!!parse-error (type => 'unclosed tag');
1863            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1864              !!!cp (122.3);
1865              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1866            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1867              if ($self->{current_token}->{attributes}) {
1868                !!!cp (122.1);
1869                !!!parse-error (type => 'end tag attribute');
1870              } else {
1871                ## NOTE: This state should never be reached.
1872                !!!cp (122.2);
1873              }
1874            } else {
1875              die "$0: $self->{current_token}->{type}: Unknown token type";
1876            }
1877            $self->{state} = DATA_STATE;
1878            ## Reconsume.
1879            !!!emit ($self->{current_token}); # start tag or end tag
1880            redo A;
1881          } else {
1882            !!!cp ('124.1');
1883            !!!parse-error (type => 'no space between attributes');
1884            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1885            ## reconsume
1886            redo A;
1887          }
1888        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1889          if ($self->{next_char} == 0x003E) { # >
1890            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1891              !!!cp ('124.2');
1892              !!!parse-error (type => 'nestc', token => $self->{current_token});
1893              ## TODO: Different type than slash in start tag
1894              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1895              if ($self->{current_token}->{attributes}) {
1896                !!!cp ('124.4');
1897                !!!parse-error (type => 'end tag attribute');
1898              } else {
1899                !!!cp ('124.5');
1900              }
1901              ## TODO: Test |<title></title/>|
1902            } else {
1903              !!!cp ('124.3');
1904              $self->{self_closing} = 1;
1905            }
1906    
1907            $self->{state} = DATA_STATE;
1908            !!!next-input-character;
1909    
1910            !!!emit ($self->{current_token}); # start tag or end tag
1911    
1912            redo A;
1913          } elsif ($self->{next_char} == -1) {
1914            !!!parse-error (type => 'unclosed tag');
1915            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1916              !!!cp (124.7);
1917              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1918            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1919              if ($self->{current_token}->{attributes}) {
1920                !!!cp (124.5);
1921                !!!parse-error (type => 'end tag attribute');
1922              } else {
1923                ## NOTE: This state should never be reached.
1924                !!!cp (124.6);
1925              }
1926            } else {
1927              die "$0: $self->{current_token}->{type}: Unknown token type";
1928            }
1929            $self->{state} = DATA_STATE;
1930            ## Reconsume.
1931            !!!emit ($self->{current_token}); # start tag or end tag
1932            redo A;
1933          } else {
1934            !!!cp ('124.4');
1935            !!!parse-error (type => 'nestc');
1936            ## TODO: This error type is wrong.
1937            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1938            ## Reconsume.
1939            redo A;
1940          }
1941        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1942        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1943                
1944        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1945          #my $token = {type => COMMENT_TOKEN, data => ''};
1946    
1947        BC: {        BC: {
1948          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1949            $self->{state} = 'data';            !!!cp (124);
1950              $self->{state} = DATA_STATE;
1951            !!!next-input-character;            !!!next-input-character;
1952    
1953            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1954    
1955            redo A;            redo A;
1956          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1957            $self->{state} = 'data';            !!!cp (125);
1958              $self->{state} = DATA_STATE;
1959            ## reconsume            ## reconsume
1960    
1961            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1962    
1963            redo A;            redo A;
1964          } else {          } else {
1965            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1966              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1967            !!!next-input-character;            !!!next-input-character;
1968            redo BC;            redo BC;
1969          }          }
1970        } # BC        } # BC
1971      } elsif ($self->{state} eq 'markup declaration open') {  
1972          die "$0: _get_next_token: unexpected case [BC]";
1973        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1974        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1975    
1976          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1977    
1978        my @next_char;        my @next_char;
1979        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1980                
1981        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1982          !!!next-input-character;          !!!next-input-character;
1983          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1984          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1985            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1986            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1987                                        line => $l, column => $c,
1988                                       };
1989              $self->{state} = COMMENT_START_STATE;
1990            !!!next-input-character;            !!!next-input-character;
1991            redo A;            redo A;
1992            } else {
1993              !!!cp (128);
1994          }          }
1995        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1996                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1997          !!!next-input-character;          !!!next-input-character;
1998          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1999          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
2000              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
2001            !!!next-input-character;            !!!next-input-character;
2002            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
2003            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
2004                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
2005              !!!next-input-character;              !!!next-input-character;
2006              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
2007              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2008                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2009                !!!next-input-character;                !!!next-input-character;
2010                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
2011                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
2012                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
2013                  !!!next-input-character;                  !!!next-input-character;
2014                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
2015                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
2016                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
2017                    !!!next-input-character;                    !!!next-input-character;
2018                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
2019                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
2020                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
2021                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
2022                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
2023                        $self->{state} = DOCTYPE_STATE;
2024                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2025                                                  quirks => 1,
2026                                                  line => $l, column => $c,
2027                                                 };
2028                      !!!next-input-character;                      !!!next-input-character;
2029                      redo A;                      redo A;
2030                      } else {
2031                        !!!cp (130);
2032                    }                    }
2033                    } else {
2034                      !!!cp (131);
2035                  }                  }
2036                  } else {
2037                    !!!cp (132);
2038                }                }
2039                } else {
2040                  !!!cp (133);
2041              }              }
2042              } else {
2043                !!!cp (134);
2044              }
2045            } else {
2046              !!!cp (135);
2047            }
2048          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2049                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2050                   $self->{next_char} == 0x005B) { # [
2051            !!!next-input-character;
2052            push @next_char, $self->{next_char};
2053            if ($self->{next_char} == 0x0043) { # C
2054              !!!next-input-character;
2055              push @next_char, $self->{next_char};
2056              if ($self->{next_char} == 0x0044) { # D
2057                !!!next-input-character;
2058                push @next_char, $self->{next_char};
2059                if ($self->{next_char} == 0x0041) { # A
2060                  !!!next-input-character;
2061                  push @next_char, $self->{next_char};
2062                  if ($self->{next_char} == 0x0054) { # T
2063                    !!!next-input-character;
2064                    push @next_char, $self->{next_char};
2065                    if ($self->{next_char} == 0x0041) { # A
2066                      !!!next-input-character;
2067                      push @next_char, $self->{next_char};
2068                      if ($self->{next_char} == 0x005B) { # [
2069                        !!!cp (135.1);
2070                        $self->{state} = CDATA_BLOCK_STATE;
2071                        !!!next-input-character;
2072                        redo A;
2073                      } else {
2074                        !!!cp (135.2);
2075                      }
2076                    } else {
2077                      !!!cp (135.3);
2078                    }
2079                  } else {
2080                    !!!cp (135.4);                
2081                  }
2082                } else {
2083                  !!!cp (135.5);
2084                }
2085              } else {
2086                !!!cp (135.6);
2087            }            }
2088            } else {
2089              !!!cp (135.7);
2090          }          }
2091          } else {
2092            !!!cp (136);
2093        }        }
2094    
2095        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
2096        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2097        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2098        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2099          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2100                                    line => $l, column => $c,
2101                                   };
2102        redo A;        redo A;
2103                
2104        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2105        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
2106      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2107        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2108          $self->{state} = 'comment dash';          !!!cp (137);
2109            $self->{state} = COMMENT_START_DASH_STATE;
2110            !!!next-input-character;
2111            redo A;
2112          } elsif ($self->{next_char} == 0x003E) { # >
2113            !!!cp (138);
2114            !!!parse-error (type => 'bogus comment');
2115            $self->{state} = DATA_STATE;
2116            !!!next-input-character;
2117    
2118            !!!emit ($self->{current_token}); # comment
2119    
2120            redo A;
2121          } elsif ($self->{next_char} == -1) {
2122            !!!cp (139);
2123            !!!parse-error (type => 'unclosed comment');
2124            $self->{state} = DATA_STATE;
2125            ## reconsume
2126    
2127            !!!emit ($self->{current_token}); # comment
2128    
2129            redo A;
2130          } else {
2131            !!!cp (140);
2132            $self->{current_token}->{data} # comment
2133                .= chr ($self->{next_char});
2134            $self->{state} = COMMENT_STATE;
2135            !!!next-input-character;
2136            redo A;
2137          }
2138        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2139          if ($self->{next_char} == 0x002D) { # -
2140            !!!cp (141);
2141            $self->{state} = COMMENT_END_STATE;
2142            !!!next-input-character;
2143            redo A;
2144          } elsif ($self->{next_char} == 0x003E) { # >
2145            !!!cp (142);
2146            !!!parse-error (type => 'bogus comment');
2147            $self->{state} = DATA_STATE;
2148            !!!next-input-character;
2149    
2150            !!!emit ($self->{current_token}); # comment
2151    
2152            redo A;
2153          } elsif ($self->{next_char} == -1) {
2154            !!!cp (143);
2155            !!!parse-error (type => 'unclosed comment');
2156            $self->{state} = DATA_STATE;
2157            ## reconsume
2158    
2159            !!!emit ($self->{current_token}); # comment
2160    
2161            redo A;
2162          } else {
2163            !!!cp (144);
2164            $self->{current_token}->{data} # comment
2165                .= '-' . chr ($self->{next_char});
2166            $self->{state} = COMMENT_STATE;
2167            !!!next-input-character;
2168            redo A;
2169          }
2170        } elsif ($self->{state} == COMMENT_STATE) {
2171          if ($self->{next_char} == 0x002D) { # -
2172            !!!cp (145);
2173            $self->{state} = COMMENT_END_DASH_STATE;
2174          !!!next-input-character;          !!!next-input-character;
2175          redo A;          redo A;
2176        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2177            !!!cp (146);
2178          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2179          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2180          ## reconsume          ## reconsume
2181    
2182          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2183    
2184          redo A;          redo A;
2185        } else {        } else {
2186          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2187            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2188          ## Stay in the state          ## Stay in the state
2189          !!!next-input-character;          !!!next-input-character;
2190          redo A;          redo A;
2191        }        }
2192      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2193        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2194          $self->{state} = 'comment end';          !!!cp (148);
2195            $self->{state} = COMMENT_END_STATE;
2196          !!!next-input-character;          !!!next-input-character;
2197          redo A;          redo A;
2198        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2199            !!!cp (149);
2200          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2201          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2202          ## reconsume          ## reconsume
2203    
2204          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2205    
2206          redo A;          redo A;
2207        } else {        } else {
2208          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2209          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2210            $self->{state} = COMMENT_STATE;
2211          !!!next-input-character;          !!!next-input-character;
2212          redo A;          redo A;
2213        }        }
2214      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2215        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2216          $self->{state} = 'data';          !!!cp (151);
2217            $self->{state} = DATA_STATE;
2218          !!!next-input-character;          !!!next-input-character;
2219    
2220          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2221    
2222          redo A;          redo A;
2223        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2224          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2225            !!!parse-error (type => 'dash in comment',
2226                            line => $self->{line_prev},
2227                            column => $self->{column_prev});
2228          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2229          ## Stay in the state          ## Stay in the state
2230          !!!next-input-character;          !!!next-input-character;
2231          redo A;          redo A;
2232        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2233            !!!cp (153);
2234          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2235          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2236          ## reconsume          ## reconsume
2237    
2238          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2239    
2240          redo A;          redo A;
2241        } else {        } else {
2242          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2243          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2244          $self->{state} = 'comment';                          line => $self->{line_prev},
2245                            column => $self->{column_prev});
2246            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2247            $self->{state} = COMMENT_STATE;
2248          !!!next-input-character;          !!!next-input-character;
2249          redo A;          redo A;
2250        }        }
2251      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2252        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2253            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2254            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2255            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2256            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2257          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2258            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2259          !!!next-input-character;          !!!next-input-character;
2260          redo A;          redo A;
2261        } else {        } else {
2262            !!!cp (156);
2263          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2264          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2265          ## reconsume          ## reconsume
2266          redo A;          redo A;
2267        }        }
2268      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2269        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2270            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2271            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2272            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2273            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2274            !!!cp (157);
2275          ## Stay in the state          ## Stay in the state
2276          !!!next-input-character;          !!!next-input-character;
2277          redo A;          redo A;
2278        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2279                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (158);
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
2280          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2281          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2282          !!!next-input-character;          !!!next-input-character;
2283    
2284          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2285    
2286          redo A;          redo A;
2287        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2288            !!!cp (159);
2289          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2290          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2291          ## reconsume          ## reconsume
2292    
2293          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2294    
2295          redo A;          redo A;
2296        } else {        } else {
2297          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
2298                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name} = chr $self->{next_char};
2299                            error => 1};          delete $self->{current_token}->{quirks};
2300  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2301          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2302          !!!next-input-character;          !!!next-input-character;
2303          redo A;          redo A;
2304        }        }
2305      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2306        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
2307            $self->{next_input_character} == 0x000A or # LF        if ($self->{next_char} == 0x0009 or # HT
2308            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000A or # LF
2309            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000B or # VT
2310            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000C or # FF
2311          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{next_char} == 0x0020) { # SP
2312          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2313            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2314          !!!next-input-character;          !!!next-input-character;
2315          redo A;          redo A;
2316        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2317          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
2318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2319          !!!next-input-character;          !!!next-input-character;
2320    
2321          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2322    
2323          redo A;          redo A;
2324        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2325                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
2326          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2327          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2328            ## reconsume
2329    
2330            $self->{current_token}->{quirks} = 1;
2331            !!!emit ($self->{current_token}); # DOCTYPE
2332    
2333            redo A;
2334          } else {
2335            !!!cp (164);
2336            $self->{current_token}->{name}
2337              .= chr ($self->{next_char}); # DOCTYPE
2338            ## Stay in the state
2339            !!!next-input-character;
2340            redo A;
2341          }
2342        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2343          if ($self->{next_char} == 0x0009 or # HT
2344              $self->{next_char} == 0x000A or # LF
2345              $self->{next_char} == 0x000B or # VT
2346              $self->{next_char} == 0x000C or # FF
2347              $self->{next_char} == 0x0020) { # SP
2348            !!!cp (165);
2349          ## Stay in the state          ## Stay in the state
2350          !!!next-input-character;          !!!next-input-character;
2351          redo A;          redo A;
2352        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2353            !!!cp (166);
2354            $self->{state} = DATA_STATE;
2355            !!!next-input-character;
2356    
2357            !!!emit ($self->{current_token}); # DOCTYPE
2358    
2359            redo A;
2360          } elsif ($self->{next_char} == -1) {
2361            !!!cp (167);
2362          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2363          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2364          ## reconsume          ## reconsume
2365    
2366          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2367          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2368    
2369          redo A;          redo A;
2370          } elsif ($self->{next_char} == 0x0050 or # P
2371                   $self->{next_char} == 0x0070) { # p
2372            !!!next-input-character;
2373            if ($self->{next_char} == 0x0055 or # U
2374                $self->{next_char} == 0x0075) { # u
2375              !!!next-input-character;
2376              if ($self->{next_char} == 0x0042 or # B
2377                  $self->{next_char} == 0x0062) { # b
2378                !!!next-input-character;
2379                if ($self->{next_char} == 0x004C or # L
2380                    $self->{next_char} == 0x006C) { # l
2381                  !!!next-input-character;
2382                  if ($self->{next_char} == 0x0049 or # I
2383                      $self->{next_char} == 0x0069) { # i
2384                    !!!next-input-character;
2385                    if ($self->{next_char} == 0x0043 or # C
2386                        $self->{next_char} == 0x0063) { # c
2387                      !!!cp (168);
2388                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2389                      !!!next-input-character;
2390                      redo A;
2391                    } else {
2392                      !!!cp (169);
2393                    }
2394                  } else {
2395                    !!!cp (170);
2396                  }
2397                } else {
2398                  !!!cp (171);
2399                }
2400              } else {
2401                !!!cp (172);
2402              }
2403            } else {
2404              !!!cp (173);
2405            }
2406    
2407            #
2408          } elsif ($self->{next_char} == 0x0053 or # S
2409                   $self->{next_char} == 0x0073) { # s
2410            !!!next-input-character;
2411            if ($self->{next_char} == 0x0059 or # Y
2412                $self->{next_char} == 0x0079) { # y
2413              !!!next-input-character;
2414              if ($self->{next_char} == 0x0053 or # S
2415                  $self->{next_char} == 0x0073) { # s
2416                !!!next-input-character;
2417                if ($self->{next_char} == 0x0054 or # T
2418                    $self->{next_char} == 0x0074) { # t
2419                  !!!next-input-character;
2420                  if ($self->{next_char} == 0x0045 or # E
2421                      $self->{next_char} == 0x0065) { # e
2422                    !!!next-input-character;
2423                    if ($self->{next_char} == 0x004D or # M
2424                        $self->{next_char} == 0x006D) { # m
2425                      !!!cp (174);
2426                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2427                      !!!next-input-character;
2428                      redo A;
2429                    } else {
2430                      !!!cp (175);
2431                    }
2432                  } else {
2433                    !!!cp (176);
2434                  }
2435                } else {
2436                  !!!cp (177);
2437                }
2438              } else {
2439                !!!cp (178);
2440              }
2441            } else {
2442              !!!cp (179);
2443            }
2444    
2445            #
2446        } else {        } else {
2447          $self->{current_token}->{name}          !!!cp (180);
2448            .= chr ($self->{next_input_character}); # DOCTYPE          !!!next-input-character;
2449          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          #
2450          }
2451    
2452          !!!parse-error (type => 'string after DOCTYPE name');
2453          $self->{current_token}->{quirks} = 1;
2454    
2455          $self->{state} = BOGUS_DOCTYPE_STATE;
2456          # next-input-character is already done
2457          redo A;
2458        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2459          if ({
2460                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2461                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2462              }->{$self->{next_char}}) {
2463            !!!cp (181);
2464          ## Stay in the state          ## Stay in the state
2465          !!!next-input-character;          !!!next-input-character;
2466          redo A;          redo A;
2467          } elsif ($self->{next_char} eq 0x0022) { # "
2468            !!!cp (182);
2469            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2470            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2471            !!!next-input-character;
2472            redo A;
2473          } elsif ($self->{next_char} eq 0x0027) { # '
2474            !!!cp (183);
2475            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2476            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2477            !!!next-input-character;
2478            redo A;
2479          } elsif ($self->{next_char} eq 0x003E) { # >
2480            !!!cp (184);
2481            !!!parse-error (type => 'no PUBLIC literal');
2482    
2483            $self->{state} = DATA_STATE;
2484            !!!next-input-character;
2485    
2486            $self->{current_token}->{quirks} = 1;
2487            !!!emit ($self->{current_token}); # DOCTYPE
2488    
2489            redo A;
2490          } elsif ($self->{next_char} == -1) {
2491            !!!cp (185);
2492            !!!parse-error (type => 'unclosed DOCTYPE');
2493    
2494            $self->{state} = DATA_STATE;
2495            ## reconsume
2496    
2497            $self->{current_token}->{quirks} = 1;
2498            !!!emit ($self->{current_token}); # DOCTYPE
2499    
2500            redo A;
2501          } else {
2502            !!!cp (186);
2503            !!!parse-error (type => 'string after PUBLIC');
2504            $self->{current_token}->{quirks} = 1;
2505    
2506            $self->{state} = BOGUS_DOCTYPE_STATE;
2507            !!!next-input-character;
2508            redo A;
2509          }
2510        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2511          if ($self->{next_char} == 0x0022) { # "
2512            !!!cp (187);
2513            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2514            !!!next-input-character;
2515            redo A;
2516          } elsif ($self->{next_char} == 0x003E) { # >
2517            !!!cp (188);
2518            !!!parse-error (type => 'unclosed PUBLIC literal');
2519    
2520            $self->{state} = DATA_STATE;
2521            !!!next-input-character;
2522    
2523            $self->{current_token}->{quirks} = 1;
2524            !!!emit ($self->{current_token}); # DOCTYPE
2525    
2526            redo A;
2527          } elsif ($self->{next_char} == -1) {
2528            !!!cp (189);
2529            !!!parse-error (type => 'unclosed PUBLIC literal');
2530    
2531            $self->{state} = DATA_STATE;
2532            ## reconsume
2533    
2534            $self->{current_token}->{quirks} = 1;
2535            !!!emit ($self->{current_token}); # DOCTYPE
2536    
2537            redo A;
2538          } else {
2539            !!!cp (190);
2540            $self->{current_token}->{public_identifier} # DOCTYPE
2541                .= chr $self->{next_char};
2542            ## Stay in the state
2543            !!!next-input-character;
2544            redo A;
2545          }
2546        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2547          if ($self->{next_char} == 0x0027) { # '
2548            !!!cp (191);
2549            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2550            !!!next-input-character;
2551            redo A;
2552          } elsif ($self->{next_char} == 0x003E) { # >
2553            !!!cp (192);
2554            !!!parse-error (type => 'unclosed PUBLIC literal');
2555    
2556            $self->{state} = DATA_STATE;
2557            !!!next-input-character;
2558    
2559            $self->{current_token}->{quirks} = 1;
2560            !!!emit ($self->{current_token}); # DOCTYPE
2561    
2562            redo A;
2563          } elsif ($self->{next_char} == -1) {
2564            !!!cp (193);
2565            !!!parse-error (type => 'unclosed PUBLIC literal');
2566    
2567            $self->{state} = DATA_STATE;
2568            ## reconsume
2569    
2570            $self->{current_token}->{quirks} = 1;
2571            !!!emit ($self->{current_token}); # DOCTYPE
2572    
2573            redo A;
2574          } else {
2575            !!!cp (194);
2576            $self->{current_token}->{public_identifier} # DOCTYPE
2577                .= chr $self->{next_char};
2578            ## Stay in the state
2579            !!!next-input-character;
2580            redo A;
2581          }
2582        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2583          if ({
2584                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2585                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2586              }->{$self->{next_char}}) {
2587            !!!cp (195);
2588            ## Stay in the state
2589            !!!next-input-character;
2590            redo A;
2591          } elsif ($self->{next_char} == 0x0022) { # "
2592            !!!cp (196);
2593            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2594            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2595            !!!next-input-character;
2596            redo A;
2597          } elsif ($self->{next_char} == 0x0027) { # '
2598            !!!cp (197);
2599            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2600            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2601            !!!next-input-character;
2602            redo A;
2603          } elsif ($self->{next_char} == 0x003E) { # >
2604            !!!cp (198);
2605            $self->{state} = DATA_STATE;
2606            !!!next-input-character;
2607    
2608            !!!emit ($self->{current_token}); # DOCTYPE
2609    
2610            redo A;
2611          } elsif ($self->{next_char} == -1) {
2612            !!!cp (199);
2613            !!!parse-error (type => 'unclosed DOCTYPE');
2614    
2615            $self->{state} = DATA_STATE;
2616            ## reconsume
2617    
2618            $self->{current_token}->{quirks} = 1;
2619            !!!emit ($self->{current_token}); # DOCTYPE
2620    
2621            redo A;
2622          } else {
2623            !!!cp (200);
2624            !!!parse-error (type => 'string after PUBLIC literal');
2625            $self->{current_token}->{quirks} = 1;
2626    
2627            $self->{state} = BOGUS_DOCTYPE_STATE;
2628            !!!next-input-character;
2629            redo A;
2630          }
2631        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2632          if ({
2633                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2634                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2635              }->{$self->{next_char}}) {
2636            !!!cp (201);
2637            ## Stay in the state
2638            !!!next-input-character;
2639            redo A;
2640          } elsif ($self->{next_char} == 0x0022) { # "
2641            !!!cp (202);
2642            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2643            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2644            !!!next-input-character;
2645            redo A;
2646          } elsif ($self->{next_char} == 0x0027) { # '
2647            !!!cp (203);
2648            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2649            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2650            !!!next-input-character;
2651            redo A;
2652          } elsif ($self->{next_char} == 0x003E) { # >
2653            !!!cp (204);
2654            !!!parse-error (type => 'no SYSTEM literal');
2655            $self->{state} = DATA_STATE;
2656            !!!next-input-character;
2657    
2658            $self->{current_token}->{quirks} = 1;
2659            !!!emit ($self->{current_token}); # DOCTYPE
2660    
2661            redo A;
2662          } elsif ($self->{next_char} == -1) {
2663            !!!cp (205);
2664            !!!parse-error (type => 'unclosed DOCTYPE');
2665    
2666            $self->{state} = DATA_STATE;
2667            ## reconsume
2668    
2669            $self->{current_token}->{quirks} = 1;
2670            !!!emit ($self->{current_token}); # DOCTYPE
2671    
2672            redo A;
2673          } else {
2674            !!!cp (206);
2675            !!!parse-error (type => 'string after SYSTEM');
2676            $self->{current_token}->{quirks} = 1;
2677    
2678            $self->{state} = BOGUS_DOCTYPE_STATE;
2679            !!!next-input-character;
2680            redo A;
2681        }        }
2682      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2683        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0022) { # "
2684            $self->{next_input_character} == 0x000A or # LF          !!!cp (207);
2685            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2686            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2687            $self->{next_input_character} == 0x0020) { # SP          redo A;
2688          } elsif ($self->{next_char} == 0x003E) { # >
2689            !!!cp (208);
2690            !!!parse-error (type => 'unclosed SYSTEM literal');
2691    
2692            $self->{state} = DATA_STATE;
2693            !!!next-input-character;
2694    
2695            $self->{current_token}->{quirks} = 1;
2696            !!!emit ($self->{current_token}); # DOCTYPE
2697    
2698            redo A;
2699          } elsif ($self->{next_char} == -1) {
2700            !!!cp (209);
2701            !!!parse-error (type => 'unclosed SYSTEM literal');
2702    
2703            $self->{state} = DATA_STATE;
2704            ## reconsume
2705    
2706            $self->{current_token}->{quirks} = 1;
2707            !!!emit ($self->{current_token}); # DOCTYPE
2708    
2709            redo A;
2710          } else {
2711            !!!cp (210);
2712            $self->{current_token}->{system_identifier} # DOCTYPE
2713                .= chr $self->{next_char};
2714          ## Stay in the state          ## Stay in the state
2715          !!!next-input-character;          !!!next-input-character;
2716          redo A;          redo A;
2717        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2718          $self->{state} = 'data';      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2719          if ($self->{next_char} == 0x0027) { # '
2720            !!!cp (211);
2721            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2722            !!!next-input-character;
2723            redo A;
2724          } elsif ($self->{next_char} == 0x003E) { # >
2725            !!!cp (212);
2726            !!!parse-error (type => 'unclosed SYSTEM literal');
2727    
2728            $self->{state} = DATA_STATE;
2729          !!!next-input-character;          !!!next-input-character;
2730    
2731            $self->{current_token}->{quirks} = 1;
2732            !!!emit ($self->{current_token}); # DOCTYPE
2733    
2734            redo A;
2735          } elsif ($self->{next_char} == -1) {
2736            !!!cp (213);
2737            !!!parse-error (type => 'unclosed SYSTEM literal');
2738    
2739            $self->{state} = DATA_STATE;
2740            ## reconsume
2741    
2742            $self->{current_token}->{quirks} = 1;
2743          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2744    
2745          redo A;          redo A;
2746        } elsif ($self->{next_input_character} == -1) {        } else {
2747            !!!cp (214);
2748            $self->{current_token}->{system_identifier} # DOCTYPE
2749                .= chr $self->{next_char};
2750            ## Stay in the state
2751            !!!next-input-character;
2752            redo A;
2753          }
2754        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2755          if ({
2756                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2757                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2758              }->{$self->{next_char}}) {
2759            !!!cp (215);
2760            ## Stay in the state
2761            !!!next-input-character;
2762            redo A;
2763          } elsif ($self->{next_char} == 0x003E) { # >
2764            !!!cp (216);
2765            $self->{state} = DATA_STATE;
2766            !!!next-input-character;
2767    
2768            !!!emit ($self->{current_token}); # DOCTYPE
2769    
2770            redo A;
2771          } elsif ($self->{next_char} == -1) {
2772            !!!cp (217);
2773          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2774          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2775          ## reconsume          ## reconsume
2776    
2777            $self->{current_token}->{quirks} = 1;
2778          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2779    
2780          redo A;          redo A;
2781        } else {        } else {
2782          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (218);
2783          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after SYSTEM literal');
2784          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2785    
2786            $self->{state} = BOGUS_DOCTYPE_STATE;
2787          !!!next-input-character;          !!!next-input-character;
2788          redo A;          redo A;
2789        }        }
2790      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2791        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2792          $self->{state} = 'data';          !!!cp (219);
2793            $self->{state} = DATA_STATE;
2794          !!!next-input-character;          !!!next-input-character;
2795    
2796          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2797    
2798          redo A;          redo A;
2799        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2800            !!!cp (220);
2801          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2802          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2803          ## reconsume          ## reconsume
2804    
2805          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2806    
2807          redo A;          redo A;
2808        } else {        } else {
2809            !!!cp (221);
2810          ## Stay in the state          ## Stay in the state
2811          !!!next-input-character;          !!!next-input-character;
2812          redo A;          redo A;
2813        }        }
2814        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2815          my $s = '';
2816          
2817          my ($l, $c) = ($self->{line}, $self->{column});
2818    
2819          CS: while ($self->{next_char} != -1) {
2820            if ($self->{next_char} == 0x005D) { # ]
2821              !!!next-input-character;
2822              if ($self->{next_char} == 0x005D) { # ]
2823                !!!next-input-character;
2824                MDC: {
2825                  if ($self->{next_char} == 0x003E) { # >
2826                    !!!cp (221.1);
2827                    !!!next-input-character;
2828                    last CS;
2829                  } elsif ($self->{next_char} == 0x005D) { # ]
2830                    !!!cp (221.2);
2831                    $s .= ']';
2832                    !!!next-input-character;
2833                    redo MDC;
2834                  } else {
2835                    !!!cp (221.3);
2836                    $s .= ']]';
2837                    #
2838                  }
2839                } # MDC
2840              } else {
2841                !!!cp (221.4);
2842                $s .= ']';
2843                #
2844              }
2845            } else {
2846              !!!cp (221.5);
2847              #
2848            }
2849            $s .= chr $self->{next_char};
2850            !!!next-input-character;
2851          } # CS
2852    
2853          $self->{state} = DATA_STATE;
2854          ## next-input-character done or EOF, which is reconsumed.
2855    
2856          if (length $s) {
2857            !!!cp (221.6);
2858            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2859                      line => $l, column => $c});
2860          } else {
2861            !!!cp (221.7);
2862          }
2863    
2864          redo A;
2865    
2866          ## ISSUE: "text tokens" in spec.
2867          ## TODO: Streaming support
2868      } else {      } else {
2869        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2870      }      }
# Line 1523  sub _get_next_token ($) { Line 2873  sub _get_next_token ($) {
2873    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2874  } # _get_next_token  } # _get_next_token
2875    
2876  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2877    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2878      
2879    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2880    
2881      if ({
2882           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2883           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2884           $additional => 1,
2885          }->{$self->{next_char}}) {
2886        !!!cp (1001);
2887        ## Don't consume
2888        ## No error
2889        return undef;
2890      } elsif ($self->{next_char} == 0x0023) { # #
2891      !!!next-input-character;      !!!next-input-character;
2892      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2893          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2894        my $num;        my $code;
2895        X: {        X: {
2896          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2897          !!!next-input-character;          !!!next-input-character;
2898          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2899              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2900            $num ||= 0;            !!!cp (1002);
2901            $num *= 0x10;            $code ||= 0;
2902            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2903              $code += $self->{next_char} - 0x0030;
2904            redo X;            redo X;
2905          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2906                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2907            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2908            $num ||= 0;            $code ||= 0;
2909            $num *= 0x10;            $code *= 0x10;
2910            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2911            redo X;            redo X;
2912          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2913                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2914            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2915            $num ||= 0;            $code ||= 0;
2916            $num *= 0x10;            $code *= 0x10;
2917            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2918            redo X;            redo X;
2919          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2920            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2921            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2922            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2923              $self->{next_char} = 0x0023; # #
2924            return undef;            return undef;
2925          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2926              !!!cp (1006);
2927            !!!next-input-character;            !!!next-input-character;
2928          } else {          } else {
2929            !!!parse-error (type => 'no refc');            !!!cp (1007);
2930          }            !!!parse-error (type => 'no refc', line => $l, column => $c);
   
         ## 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};  
2931          }          }
2932    
2933          return {type => 'character', data => chr $num};          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2934              !!!cp (1008);
2935              !!!parse-error (type => 'invalid character reference',
2936                              text => (sprintf 'U+%04X', $code),
2937                              line => $l, column => $c);
2938              $code = 0xFFFD;
2939            } elsif ($code > 0x10FFFF) {
2940              !!!cp (1009);
2941              !!!parse-error (type => 'invalid character reference',
2942                              text => (sprintf 'U-%08X', $code),
2943                              line => $l, column => $c);
2944              $code = 0xFFFD;
2945            } elsif ($code == 0x000D) {
2946              !!!cp (1010);
2947              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2948              $code = 0x000A;
2949            } elsif (0x80 <= $code and $code <= 0x9F) {
2950              !!!cp (1011);
2951              !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2952              $code = $c1_entity_char->{$code};
2953            }
2954    
2955            return {type => CHARACTER_TOKEN, data => chr $code,
2956                    has_reference => 1,
2957                    line => $l, column => $c,
2958                   };
2959        } # X        } # X
2960      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2961               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2962        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2963        !!!next-input-character;        !!!next-input-character;
2964                
2965        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2966                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2967            !!!cp (1012);
2968          $code *= 10;          $code *= 10;
2969          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2970                    
2971          !!!next-input-character;          !!!next-input-character;
2972        }        }
2973    
2974        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2975            !!!cp (1013);
2976          !!!next-input-character;          !!!next-input-character;
2977        } else {        } else {
2978          !!!parse-error (type => 'no refc');          !!!cp (1014);
2979            !!!parse-error (type => 'no refc', line => $l, column => $c);
2980        }        }
2981    
2982        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2983        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
2984          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => 'invalid character reference',
2985          ## ISSUE: Why this is not an error?                          text => (sprintf 'U+%04X', $code),
2986                            line => $l, column => $c);
2987            $code = 0xFFFD;
2988          } elsif ($code > 0x10FFFF) {
2989            !!!cp (1016);
2990            !!!parse-error (type => 'invalid character reference',
2991                            text => (sprintf 'U-%08X', $code),
2992                            line => $l, column => $c);
2993            $code = 0xFFFD;
2994          } elsif ($code == 0x000D) {
2995            !!!cp (1017);
2996            !!!parse-error (type => 'CR character reference',
2997                            line => $l, column => $c);
2998            $code = 0x000A;
2999        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3000          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!cp (1018);
3001            !!!parse-error (type => 'C1 character reference',
3002                            text => (sprintf 'U+%04X', $code),
3003                            line => $l, column => $c);
3004          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3005        }        }
3006                
3007        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3008                  line => $l, column => $c,
3009                 };
3010      } else {      } else {
3011        !!!parse-error (type => 'bare nero');        !!!cp (1019);
3012        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3013        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
3014          $self->{next_char} = 0x0023; # #
3015        return undef;        return undef;
3016      }      }
3017    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
3018              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
3019             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
3020              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
3021      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
3022      !!!next-input-character;      !!!next-input-character;
3023    
3024      my $value = $entity_name;      my $value = $entity_name;
3025      my $match;      my $match = 0;
3026        require Whatpm::_NamedEntityList;
3027        our $EntityChar;
3028    
3029      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3030             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3031             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
3032               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
3033              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
3034               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
3035              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
3036               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
3037        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
3038        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
3039          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
3040          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
3041              !!!cp (1020);
3042              $value = $EntityChar->{$entity_name};
3043              $match = 1;
3044              !!!next-input-character;
3045              last;
3046            } else {
3047              !!!cp (1021);
3048              $value = $EntityChar->{$entity_name};
3049              $match = -1;
3050              !!!next-input-character;
3051            }
3052        } else {        } else {
3053          $value .= chr $self->{next_input_character};          !!!cp (1022);
3054            $value .= chr $self->{next_char};
3055            $match *= 2;
3056            !!!next-input-character;
3057        }        }
       !!!next-input-character;  
3058      }      }
3059            
3060      if ($match) {      if ($match > 0) {
3061        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
3062          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3063                  line => $l, column => $c,
3064                 };
3065        } elsif ($match < 0) {
3066          !!!parse-error (type => 'no refc', line => $l, column => $c);
3067          if ($in_attr and $match < -1) {
3068            !!!cp (1024);
3069            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3070                    line => $l, column => $c,
3071                   };
3072        } else {        } else {
3073          !!!parse-error (type => 'refc');          !!!cp (1025);
3074            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3075                    line => $l, column => $c,
3076                   };
3077        }        }
   
       return {type => 'character', data => $value};  
3078      } else {      } else {
3079        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3080        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3081        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
3082        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
3083                  line => $l, column => $c,
3084                 };
3085      }      }
3086    } else {    } else {
3087        !!!cp (1027);
3088      ## no characters are consumed      ## no characters are consumed
3089      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3090      return undef;      return undef;
3091    }    }
3092  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1667  sub _initialize_tree_constructor ($) { Line 3097  sub _initialize_tree_constructor ($) {
3097    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3098    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3099    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3100    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3101      $self->{document}->set_user_data (manakai_source_line => 1);
3102      $self->{document}->set_user_data (manakai_source_column => 1);
3103  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3104    
3105  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1694  sub _construct_tree ($) { Line 3126  sub _construct_tree ($) {
3126        
3127    !!!next-token;    !!!next-token;
3128    
   $self->{insertion_mode} = 'before head';  
3129    undef $self->{form_element};    undef $self->{form_element};
3130    undef $self->{head_element};    undef $self->{head_element};
3131    $self->{open_elements} = [];    $self->{open_elements} = [];
3132    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3133    
3134      ## NOTE: The "initial" insertion mode.
3135    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3136    
3137      ## NOTE: The "before html" insertion mode.
3138    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3139      $self->{insertion_mode} = BEFORE_HEAD_IM;
3140    
3141      ## NOTE: The "before head" insertion mode and so on.
3142    $self->_tree_construction_main;    $self->_tree_construction_main;
3143  } # _construct_tree  } # _construct_tree
3144    
3145  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3146    my $self = shift;    my $self = shift;
3147    B: {  
3148        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3149          if ($token->{error}) {  
3150            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3151            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3152          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3153          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3154            ($token->{name});        ## language.
3155          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3156          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3157          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/;
3158          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3159          return;            defined $token->{public_identifier} or
3160        } elsif ({            defined $token->{system_identifier}) {
3161                  comment => 1,          !!!cp ('t1');
3162                  'start tag' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3163                  'end tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3164                  'end-of-file' => 1,          !!!cp ('t2');
3165                 }->{$token->{type}}) {          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3166          ## ISSUE: Spec currently left this case undefined.          !!!parse-error (type => 'not HTML5', token => $token);
3167          !!!parse-error (type => 'missing DOCTYPE');        } else {
3168          #$phase = 'root element';          !!!cp ('t3');
3169          ## reprocess        }
3170          #redo B;        
3171          return;        my $doctype = $self->{document}->create_document_type_definition
3172        } elsif ($token->{type} eq 'character') {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3173          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        ## NOTE: Default value for both |public_id| and |system_id| attributes
3174            $self->{document}->manakai_append_text ($1);        ## are empty strings, so that we don't set any value in missing cases.
3175            ## ISSUE: DOM3 Core does not allow Document > Text        $doctype->public_id ($token->{public_identifier})
3176            unless (length $token->{data}) {            if defined $token->{public_identifier};
3177              ## Stay in the phase        $doctype->system_id ($token->{system_identifier})
3178              !!!next-token;            if defined $token->{system_identifier};
3179              redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
3180          ## ISSUE: internalSubset = null??
3181          $self->{document}->append_child ($doctype);
3182          
3183          if ($token->{quirks} or $doctype_name ne 'HTML') {
3184            !!!cp ('t4');
3185            $self->{document}->manakai_compat_mode ('quirks');
3186          } elsif (defined $token->{public_identifier}) {
3187            my $pubid = $token->{public_identifier};
3188            $pubid =~ tr/a-z/A-z/;
3189            my $prefix = [
3190              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3191              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3192              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3193              "-//IETF//DTD HTML 2.0 LEVEL 1//",
3194              "-//IETF//DTD HTML 2.0 LEVEL 2//",
3195              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3196              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3197              "-//IETF//DTD HTML 2.0 STRICT//",
3198              "-//IETF//DTD HTML 2.0//",
3199              "-//IETF//DTD HTML 2.1E//",
3200              "-//IETF//DTD HTML 3.0//",
3201              "-//IETF//DTD HTML 3.2 FINAL//",
3202              "-//IETF//DTD HTML 3.2//",
3203              "-//IETF//DTD HTML 3//",
3204              "-//IETF//DTD HTML LEVEL 0//",
3205              "-//IETF//DTD HTML LEVEL 1//",
3206              "-//IETF//DTD HTML LEVEL 2//",
3207              "-//IETF//DTD HTML LEVEL 3//",
3208              "-//IETF//DTD HTML STRICT LEVEL 0//",
3209              "-//IETF//DTD HTML STRICT LEVEL 1//",
3210              "-//IETF//DTD HTML STRICT LEVEL 2//",
3211              "-//IETF//DTD HTML STRICT LEVEL 3//",
3212              "-//IETF//DTD HTML STRICT//",
3213              "-//IETF//DTD HTML//",
3214              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3215              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3216              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3217              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3218              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3219              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3220              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3221              "-//NETSCAPE COMM. CORP.//DTD HTML//",
3222              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3223              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3224              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3225              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3226              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3227              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3228              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3229              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3230              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3231              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3232              "-//W3C//DTD HTML 3 1995-03-24//",
3233              "-//W3C//DTD HTML 3.2 DRAFT//",
3234              "-//W3C//DTD HTML 3.2 FINAL//",
3235              "-//W3C//DTD HTML 3.2//",
3236              "-//W3C//DTD HTML 3.2S DRAFT//",
3237              "-//W3C//DTD HTML 4.0 FRAMESET//",
3238              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3239              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3240              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3241              "-//W3C//DTD W3 HTML//",
3242              "-//W3O//DTD W3 HTML 3.0//",
3243              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3244              "-//WEBTECHS//DTD MOZILLA HTML//",
3245            ]; # $prefix
3246            my $match;
3247            for (@$prefix) {
3248              if (substr ($prefix, 0, length $_) eq $_) {
3249                $match = 1;
3250                last;
3251              }
3252            }
3253            if ($match or
3254                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3255                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3256                $pubid eq "HTML") {
3257              !!!cp ('t5');
3258              $self->{document}->manakai_compat_mode ('quirks');
3259            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3260                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3261              if (defined $token->{system_identifier}) {
3262                !!!cp ('t6');
3263                $self->{document}->manakai_compat_mode ('quirks');
3264              } else {
3265                !!!cp ('t7');
3266                $self->{document}->manakai_compat_mode ('limited quirks');
3267            }            }
3268            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3269                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3270              !!!cp ('t8');
3271              $self->{document}->manakai_compat_mode ('limited quirks');
3272            } else {
3273              !!!cp ('t9');
3274          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3275        } else {        } else {
3276          die "$0: $token->{type}: Unknown token";          !!!cp ('t10');
3277        }        }
3278      } # B        if (defined $token->{system_identifier}) {
3279            my $sysid = $token->{system_identifier};
3280            $sysid =~ tr/A-Z/a-z/;
3281            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3282              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3283              ## marked as quirks.
3284              $self->{document}->manakai_compat_mode ('quirks');
3285              !!!cp ('t11');
3286            } else {
3287              !!!cp ('t12');
3288            }
3289          } else {
3290            !!!cp ('t13');
3291          }
3292          
3293          ## Go to the "before html" insertion mode.
3294          !!!next-token;
3295          return;
3296        } elsif ({
3297                  START_TAG_TOKEN, 1,
3298                  END_TAG_TOKEN, 1,
3299                  END_OF_FILE_TOKEN, 1,
3300                 }->{$token->{type}}) {
3301          !!!cp ('t14');
3302          !!!parse-error (type => 'no DOCTYPE', token => $token);
3303          $self->{document}->manakai_compat_mode ('quirks');
3304          ## Go to the "before html" insertion mode.
3305          ## reprocess
3306          !!!ack-later;
3307          return;
3308        } elsif ($token->{type} == CHARACTER_TOKEN) {
3309          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3310            ## Ignore the token
3311    
3312            unless (length $token->{data}) {
3313              !!!cp ('t15');
3314              ## Stay in the insertion mode.
3315              !!!next-token;
3316              redo INITIAL;
3317            } else {
3318              !!!cp ('t16');
3319            }
3320          } else {
3321            !!!cp ('t17');
3322          }
3323    
3324          !!!parse-error (type => 'no DOCTYPE', token => $token);
3325          $self->{document}->manakai_compat_mode ('quirks');
3326          ## Go to the "before html" insertion mode.
3327          ## reprocess
3328          return;
3329        } elsif ($token->{type} == COMMENT_TOKEN) {
3330          !!!cp ('t18');
3331          my $comment = $self->{document}->create_comment ($token->{data});
3332          $self->{document}->append_child ($comment);
3333          
3334          ## Stay in the insertion mode.
3335          !!!next-token;
3336          redo INITIAL;
3337        } else {
3338          die "$0: $token->{type}: Unknown token type";
3339        }
3340      } # INITIAL
3341    
3342      die "$0: _tree_construction_initial: This should be never reached";
3343  } # _tree_construction_initial  } # _tree_construction_initial
3344    
3345  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3346    my $self = shift;    my $self = shift;
3347    
3348      ## NOTE: "before html" insertion mode.
3349        
3350    B: {    B: {
3351        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3352          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3353            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3354          ## Ignore the token          ## Ignore the token
3355          ## Stay in the phase          ## Stay in the insertion mode.
3356          !!!next-token;          !!!next-token;
3357          redo B;          redo B;
3358        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3359            !!!cp ('t20');
3360          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3361          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3362          ## Stay in the phase          ## Stay in the insertion mode.
3363          !!!next-token;          !!!next-token;
3364          redo B;          redo B;
3365        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3366          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3367            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3368            ## ISSUE: DOM3 Core does not allow Document > Text  
3369            unless (length $token->{data}) {            unless (length $token->{data}) {
3370              ## Stay in the phase              !!!cp ('t21');
3371                ## Stay in the insertion mode.
3372              !!!next-token;              !!!next-token;
3373              redo B;              redo B;
3374              } else {
3375                !!!cp ('t22');
3376            }            }
3377            } else {
3378              !!!cp ('t23');
3379          }          }
3380    
3381            $self->{application_cache_selection}->(undef);
3382    
3383          #          #
3384          } elsif ($token->{type} == START_TAG_TOKEN) {
3385            if ($token->{tag_name} eq 'html') {
3386              my $root_element;
3387              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3388              $self->{document}->append_child ($root_element);
3389              push @{$self->{open_elements}},
3390                  [$root_element, $el_category->{html}];
3391    
3392              if ($token->{attributes}->{manifest}) {
3393                !!!cp ('t24');
3394                $self->{application_cache_selection}
3395                    ->($token->{attributes}->{manifest}->{value});
3396                ## ISSUE: Spec is unclear on relative references.
3397                ## According to Hixie (#whatwg 2008-03-19), it should be
3398                ## resolved against the base URI of the document in HTML
3399                ## or xml:base of the element in XHTML.
3400              } else {
3401                !!!cp ('t25');
3402                $self->{application_cache_selection}->(undef);
3403              }
3404    
3405              !!!nack ('t25c');
3406    
3407              !!!next-token;
3408              return; ## Go to the "before head" insertion mode.
3409            } else {
3410              !!!cp ('t25.1');
3411              #
3412            }
3413        } elsif ({        } elsif ({
3414                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3415                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3416                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3417          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3418          #          #
3419        } else {        } else {
3420          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3421        }        }
3422        my $root_element; !!!create-element ($root_element, 'html');  
3423        $self->{document}->append_child ($root_element);      my $root_element;
3424        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3425        #$phase = 'main';      $self->{document}->append_child ($root_element);
3426        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3427        #redo B;  
3428        return;      $self->{application_cache_selection}->(undef);
3429    
3430        ## NOTE: Reprocess the token.
3431        !!!ack-later;
3432        return; ## Go to the "before head" insertion mode.
3433    
3434        ## ISSUE: There is an issue in the spec
3435    } # B    } # B
3436    
3437      die "$0: _tree_construction_root_element: This should never be reached";
3438  } # _tree_construction_root_element  } # _tree_construction_root_element
3439    
3440  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1813  sub _reset_insertion_mode ($) { Line 3449  sub _reset_insertion_mode ($) {
3449            
3450      ## Step 3      ## Step 3
3451      S3: {      S3: {
3452        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3453        if (defined $self->{inner_html_node}) {          $last = 1;
3454          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3455              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3456              $node = $self->{inner_html_node};
3457            } else {
3458              die "_reset_insertion_mode: t27";
3459            }
3460          }
3461          
3462          ## Step 4..14
3463          my $new_mode;
3464          if ($node->[1] & FOREIGN_EL) {
3465            !!!cp ('t28.1');
3466            ## NOTE: Strictly spaking, the line below only applies to MathML and
3467            ## SVG elements.  Currently the HTML syntax supports only MathML and
3468            ## SVG elements as foreigners.
3469            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3470          } elsif ($node->[1] & TABLE_CELL_EL) {
3471            if ($last) {
3472              !!!cp ('t28.2');
3473            #            #
3474          } else {          } else {
3475            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3476              $new_mode = IN_CELL_IM;
3477          }          }
3478          } else {
3479            !!!cp ('t28.4');
3480            $new_mode = {
3481                          select => IN_SELECT_IM,
3482                          ## NOTE: |option| and |optgroup| do not set
3483                          ## insertion mode to "in select" by themselves.
3484                          tr => IN_ROW_IM,
3485                          tbody => IN_TABLE_BODY_IM,
3486                          thead => IN_TABLE_BODY_IM,
3487                          tfoot => IN_TABLE_BODY_IM,
3488                          caption => IN_CAPTION_IM,
3489                          colgroup => IN_COLUMN_GROUP_IM,
3490                          table => IN_TABLE_IM,
3491                          head => IN_BODY_IM, # not in head!
3492                          body => IN_BODY_IM,
3493                          frameset => IN_FRAMESET_IM,
3494                         }->{$node->[0]->manakai_local_name};
3495        }        }
       
       ## 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]};  
3496        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3497                
3498        ## Step 14        ## Step 15
3499        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3500          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3501            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3502              $self->{insertion_mode} = BEFORE_HEAD_IM;
3503          } else {          } else {
3504            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3505              !!!cp ('t30');
3506              $self->{insertion_mode} = AFTER_HEAD_IM;
3507          }          }
3508          return;          return;
3509          } else {
3510            !!!cp ('t31');
3511        }        }
3512                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3513        ## Step 16        ## Step 16
3514          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3515          
3516          ## Step 17
3517        $i--;        $i--;
3518        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3519                
3520        ## Step 17        ## Step 18
3521        redo S3;        redo S3;
3522      } # S3      } # S3
3523    
3524      die "$0: _reset_insertion_mode: This line should never be reached";
3525  } # _reset_insertion_mode  } # _reset_insertion_mode
3526    
3527  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3528    my $self = shift;    my $self = shift;
3529    
   my $phase = 'main';  
   
3530    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3531    
3532    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1884  sub _tree_construction_main ($) { Line 3543  sub _tree_construction_main ($) {
3543      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3544      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3545        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3546            !!!cp ('t32');
3547          return;          return;
3548        }        }
3549      }      }
# Line 1898  sub _tree_construction_main ($) { Line 3558  sub _tree_construction_main ($) {
3558    
3559        ## Step 6        ## Step 6
3560        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3561            !!!cp ('t33_1');
3562          #          #
3563        } else {        } else {
3564          my $in_open_elements;          my $in_open_elements;
3565          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3566            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3567                !!!cp ('t33');
3568              $in_open_elements = 1;              $in_open_elements = 1;
3569              last OE;              last OE;
3570            }            }
3571          }          }
3572          if ($in_open_elements) {          if ($in_open_elements) {
3573              !!!cp ('t34');
3574            #            #
3575          } else {          } else {
3576              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3577              !!!cp ('t35');
3578            redo S4;            redo S4;
3579          }          }
3580        }        }
# Line 1932  sub _tree_construction_main ($) { Line 3597  sub _tree_construction_main ($) {
3597    
3598        ## Step 11        ## Step 11
3599        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3600            !!!cp ('t36');
3601          ## Step 7'          ## Step 7'
3602          $i++;          $i++;
3603          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3604                    
3605          redo S7;          redo S7;
3606        }        }
3607    
3608          !!!cp ('t37');
3609      } # S7      } # S7
3610    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3611    
3612    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3613      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3614        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3615            !!!cp ('t38');
3616          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3617          return;          return;
3618        }        }
3619      }      }
3620    
3621        !!!cp ('t39');
3622    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3623    
3624    my $style_start_tag = sub {    my $insert;
3625      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
3626      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3627      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3628       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3629        ->append_child ($style_el);      ## Step 1
3630      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3631        my $el;
3632        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3633    
3634        ## Step 2
3635        $insert->($el);
3636    
3637        ## Step 3
3638        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3639      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3640                  
3641        ## Step 4
3642      my $text = '';      my $text = '';
3643        !!!nack ('t40.1');
3644      !!!next-token;      !!!next-token;
3645      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3646          !!!cp ('t40');
3647        $text .= $token->{data};        $text .= $token->{data};
3648        !!!next-token;        !!!next-token;
3649      } # stop if non-character token or tokenizer stops tokenising      }
3650    
3651        ## Step 5
3652      if (length $text) {      if (length $text) {
3653        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3654          my $text = $self->{document}->create_text_node ($text);
3655          $el->append_child ($text);
3656      }      }
3657        
3658      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3659                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3660      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3661        ## Step 7
3662        if ($token->{type} == END_TAG_TOKEN and
3663            $token->{tag_name} eq $start_tag_name) {
3664          !!!cp ('t42');
3665        ## Ignore the token        ## Ignore the token
3666      } else {      } else {
3667        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3668        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3669            !!!cp ('t43');
3670            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3671          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3672            !!!cp ('t44');
3673            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3674          } else {
3675            die "$0: $content_model_flag in parse_rcdata";
3676          }
3677      }      }
3678      !!!next-token;      !!!next-token;
3679    }; # $style_start_tag    }; # $parse_rcdata
3680    
3681    my $script_start_tag = sub {    my $script_start_tag = sub () {
3682      my $script_el;      my $script_el;
3683      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3684      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3685    
3686      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3687      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3688            
3689      my $text = '';      my $text = '';
3690        !!!nack ('t45.1');
3691      !!!next-token;      !!!next-token;
3692      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3693          !!!cp ('t45');
3694        $text .= $token->{data};        $text .= $token->{data};
3695        !!!next-token;        !!!next-token;
3696      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3697      if (length $text) {      if (length $text) {
3698          !!!cp ('t46');
3699        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3700      }      }
3701                                
3702      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3703    
3704      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3705          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3706          !!!cp ('t47');
3707        ## Ignore the token        ## Ignore the token
3708      } else {      } else {
3709        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3710          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3711        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3712        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3713      }      }
3714            
3715      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3716          !!!cp ('t49');
3717        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3718      } else {      } else {
3719          !!!cp ('t50');
3720        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3721        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3722          
3723        (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})        $insert->($script_el);
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
3724                
3725        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3726                
# Line 2026  sub _tree_construction_main ($) { Line 3730  sub _tree_construction_main ($) {
3730      !!!next-token;      !!!next-token;
3731    }; # $script_start_tag    }; # $script_start_tag
3732    
3733      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3734      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3735      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3736    
3737    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3738      my $tag_name = shift;      my $end_tag_token = shift;
3739        my $tag_name = $end_tag_token->{tag_name};
3740    
3741        ## NOTE: The adoption agency algorithm (AAA).
3742    
3743      FET: {      FET: {
3744        ## Step 1        ## Step 1
3745        my $formatting_element;        my $formatting_element;
3746        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3747        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3748          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3749              !!!cp ('t52');
3750              last AFE;
3751            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3752                         eq $tag_name) {
3753              !!!cp ('t51');
3754            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3755            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3756            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3757          }          }
3758        } # AFE        } # AFE
3759        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3760          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3761            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3762          ## Ignore the token          ## Ignore the token
3763          !!!next-token;          !!!next-token;
3764          return;          return;
# Line 2055  sub _tree_construction_main ($) { Line 3770  sub _tree_construction_main ($) {
3770          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3771          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3772            if ($in_scope) {            if ($in_scope) {
3773                !!!cp ('t54');
3774              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3775              last INSCOPE;              last INSCOPE;
3776            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3777              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3778                !!!parse-error (type => 'unmatched end tag',
3779                                text => $token->{tag_name},
3780                                token => $end_tag_token);
3781              ## Ignore the token              ## Ignore the token
3782              !!!next-token;              !!!next-token;
3783              return;              return;
3784            }            }
3785          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3786                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3787            $in_scope = 0;            $in_scope = 0;
3788          }          }
3789        } # INSCOPE        } # INSCOPE
3790        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3791          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3792            !!!parse-error (type => 'unmatched end tag',
3793                            text => $token->{tag_name},
3794                            token => $end_tag_token);
3795          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3796          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3797          return;          return;
3798        }        }
3799        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3800          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3801            !!!parse-error (type => 'not closed',
3802                            text => $self->{open_elements}->[-1]->[0]
3803                                ->manakai_local_name,
3804                            token => $end_tag_token);
3805        }        }
3806                
3807        ## Step 2        ## Step 2
# Line 2085  sub _tree_construction_main ($) { Line 3809  sub _tree_construction_main ($) {
3809        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3810        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3811          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3812          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3813              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3814              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3815               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3816              !!!cp ('t59');
3817            $furthest_block = $node;            $furthest_block = $node;
3818            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3819          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3820              !!!cp ('t60');
3821            last OE;            last OE;
3822          }          }
3823        } # OE        } # OE
3824                
3825        ## Step 3        ## Step 3
3826        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3827            !!!cp ('t61');
3828          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3829          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3830          !!!next-token;          !!!next-token;
# Line 2110  sub _tree_construction_main ($) { Line 3837  sub _tree_construction_main ($) {
3837        ## Step 5        ## Step 5
3838        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3839        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3840            !!!cp ('t62');
3841          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3842        }        }
3843                
# Line 2132  sub _tree_construction_main ($) { Line 3860  sub _tree_construction_main ($) {
3860          S7S2: {          S7S2: {
3861            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3862              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3863                  !!!cp ('t63');
3864                $node_i_in_active = $_;                $node_i_in_active = $_;
3865                last S7S2;                last S7S2;
3866              }              }
# Line 2145  sub _tree_construction_main ($) { Line 3874  sub _tree_construction_main ($) {
3874                    
3875          ## Step 4          ## Step 4
3876          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3877              !!!cp ('t64');
3878            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3879          }          }
3880                    
3881          ## Step 5          ## Step 5
3882          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3883              !!!cp ('t65');
3884            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3885            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3886            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2167  sub _tree_construction_main ($) { Line 3898  sub _tree_construction_main ($) {
3898        } # S7          } # S7  
3899                
3900        ## Step 8        ## Step 8
3901        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3902            my $foster_parent_element;
3903            my $next_sibling;
3904            OE: for (reverse 0..$#{$self->{open_elements}}) {
3905              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3906                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3907                                 if (defined $parent and $parent->node_type == 1) {
3908                                   !!!cp ('t65.1');
3909                                   $foster_parent_element = $parent;
3910                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3911                                 } else {
3912                                   !!!cp ('t65.2');
3913                                   $foster_parent_element
3914                                     = $self->{open_elements}->[$_ - 1]->[0];
3915                                 }
3916                                 last OE;
3917                               }
3918                             } # OE
3919                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3920                               unless defined $foster_parent_element;
3921            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3922            $open_tables->[-1]->[1] = 1; # tainted
3923          } else {
3924            !!!cp ('t65.3');
3925            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3926          }
3927                
3928        ## Step 9        ## Step 9
3929        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2184  sub _tree_construction_main ($) { Line 3940  sub _tree_construction_main ($) {
3940        my $i;        my $i;
3941        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3942          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3943              !!!cp ('t66');
3944            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3945            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3946          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3947              !!!cp ('t67');
3948            $i = $_;            $i = $_;
3949          }          }
3950        } # AFE        } # AFE
# Line 2196  sub _tree_construction_main ($) { Line 3954  sub _tree_construction_main ($) {
3954        undef $i;        undef $i;
3955        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3956          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3957              !!!cp ('t68');
3958            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3959            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3960          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3961              !!!cp ('t69');
3962            $i = $_;            $i = $_;
3963          }          }
3964        } # OE        } # OE
# Line 2209  sub _tree_construction_main ($) { Line 3969  sub _tree_construction_main ($) {
3969      } # FET      } # FET
3970    }; # $formatting_end_tag    }; # $formatting_end_tag
3971    
3972    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3973      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3974    }; # $insert_to_current    }; # $insert_to_current
3975    
3976    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3977                         my $child = shift;      my $child = shift;
3978                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3979                              table => 1, tbody => 1, tfoot => 1,        # MUST
3980                              thead => 1, tr => 1,        my $foster_parent_element;
3981                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3982                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3983                           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') {  
3984                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3985                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3986                                   !!!cp ('t70');
3987                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3988                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3989                               } else {                               } else {
3990                                   !!!cp ('t71');
3991                                 $foster_parent_element                                 $foster_parent_element
3992                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3993                               }                               }
# Line 2239  sub _tree_construction_main ($) { Line 3998  sub _tree_construction_main ($) {
3998                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3999                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4000                             ($child, $next_sibling);                             ($child, $next_sibling);
4001                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4002                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4003                         }        !!!cp ('t72');
4004          $self->{open_elements}->[-1]->[0]->append_child ($child);
4005        }
4006    }; # $insert_to_foster    }; # $insert_to_foster
4007    
4008    my $in_body = sub {    B: while (1) {
4009      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4010      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4011        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4012          $script_start_tag->();        ## Ignore the token
4013          return;        ## Stay in the phase
4014        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4015          $style_start_tag->();        next B;
4016          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4017        } elsif ({               $token->{tag_name} eq 'html') {
4018                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4019                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4020          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4021          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4022          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4023          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4024          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4025            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4026          } else {        } else {
4027            $insert->($el);          !!!cp ('t81');
4028          }        }
4029            
4030          !!!next-token;        !!!cp ('t82');
4031          return;        !!!parse-error (type => 'not first start tag', token => $token);
4032        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4033          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4034          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4035          my $title_el;            !!!cp ('t84');
4036          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4037          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4038            ->append_child ($title_el);               $token->{attributes}->{$attr_name}->{value});
         $self->{content_model_flag} = 'RCDATA';  
         delete $self->{escape}; # MUST  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
4039          }          }
4040                    }
4041          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4042                    !!!next-token;
4043          next B;
4044        } elsif ($token->{type} == COMMENT_TOKEN) {
4045          my $comment = $self->{document}->create_comment ($token->{data});
4046          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4047            !!!cp ('t85');
4048            $self->{document}->append_child ($comment);
4049          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4050            !!!cp ('t86');
4051            $self->{open_elements}->[0]->[0]->append_child ($comment);
4052          } else {
4053            !!!cp ('t87');
4054            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4055          }
4056          !!!next-token;
4057          next B;
4058        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4059          if ($token->{type} == CHARACTER_TOKEN) {
4060            !!!cp ('t87.1');
4061            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4062          !!!next-token;          !!!next-token;
4063          return;          next B;
4064        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4065          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4066            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4067            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4068              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4069                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4070              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4071              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4072              $formatting_end_tag->($token->{tag_name});            #
4073                        } elsif ({
4074              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4075                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4076                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4077                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4078                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4079              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4080              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4081                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4082                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4083                  last OE;            !!!cp ('t87.2');
4084                }            !!!parse-error (type => 'not closed',
4085              } # OE                            text => $self->{open_elements}->[-1]->[0]
4086              last AFE;                                ->manakai_local_name,
4087            } elsif ($node->[0] eq '#marker') {                            token => $token);
4088              last AFE;  
4089              pop @{$self->{open_elements}}
4090                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4091    
4092              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4093              ## Reprocess.
4094              next B;
4095            } else {
4096              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4097              my $tag_name = $token->{tag_name};
4098              if ($nsuri eq $SVG_NS) {
4099                $tag_name = {
4100                   altglyph => 'altGlyph',
4101                   altglyphdef => 'altGlyphDef',
4102                   altglyphitem => 'altGlyphItem',
4103                   animatecolor => 'animateColor',
4104                   animatemotion => 'animateMotion',
4105                   animatetransform => 'animateTransform',
4106                   clippath => 'clipPath',
4107                   feblend => 'feBlend',
4108                   fecolormatrix => 'feColorMatrix',
4109                   fecomponenttransfer => 'feComponentTransfer',
4110                   fecomposite => 'feComposite',
4111                   feconvolvematrix => 'feConvolveMatrix',
4112                   fediffuselighting => 'feDiffuseLighting',
4113                   fedisplacementmap => 'feDisplacementMap',
4114                   fedistantlight => 'feDistantLight',
4115                   feflood => 'feFlood',
4116                   fefunca => 'feFuncA',
4117                   fefuncb => 'feFuncB',
4118                   fefuncg => 'feFuncG',
4119                   fefuncr => 'feFuncR',
4120                   fegaussianblur => 'feGaussianBlur',
4121                   feimage => 'feImage',
4122                   femerge => 'feMerge',
4123                   femergenode => 'feMergeNode',
4124                   femorphology => 'feMorphology',
4125                   feoffset => 'feOffset',
4126                   fepointlight => 'fePointLight',
4127                   fespecularlighting => 'feSpecularLighting',
4128                   fespotlight => 'feSpotLight',
4129                   fetile => 'feTile',
4130                   feturbulence => 'feTurbulence',
4131                   foreignobject => 'foreignObject',
4132                   glyphref => 'glyphRef',
4133                   lineargradient => 'linearGradient',
4134                   radialgradient => 'radialGradient',
4135                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4136                   textpath => 'textPath',  
4137                }->{$tag_name} || $tag_name;
4138            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4139    
4140          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            ## "adjust SVG attributes" (SVG only) - done in insert-element-f
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
4141    
4142          !!!next-token;            ## "adjust foreign attributes" - done in insert-element-f
         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', ''];  
4143    
4144          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4145          return;  
4146        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4147                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4148          $reconstruct_active_formatting_elements->($insert_to_current);              !!!ack ('t87.3');
           
         !!!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';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         if ($token->{tag_name} eq 'textarea') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
4149            } else {            } else {
4150              !!!parse-error (type => 'in CDATA:#'.$token->{type});              !!!cp ('t87.4');
4151            }            }
4152            ## ISSUE: And ignore?  
4153              !!!next-token;
4154              next B;
4155          }          }
4156          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4157          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4158        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4159          $reconstruct_active_formatting_elements->($insert_to_current);          #
4160                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4161          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4162                    !!!parse-error (type => 'not closed',
4163          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4164          !!!next-token;                              ->manakai_local_name,
4165          return;                          token => $token);
4166        } elsif ({  
4167                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4168                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4169                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4170                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4171                 }->{$token->{tag_name}}) {          ## Reprocess.
4172          !!!parse-error (type => 'in body:'.$token->{tag_name});          next B;
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4173        } else {        } else {
4174          $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;  
4175        }        }
4176      } elsif ($token->{type} eq 'end tag') {      }
4177        if ($token->{tag_name} eq 'body') {  
4178          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4179            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4180            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4181              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4182            }              !!!cp ('t88.2');
4183            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4184            !!!next-token;            } else {
4185            return;              !!!cp ('t88.1');
4186          } else {              ## Ignore the token.
4187            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
4188            ## Ignore the token              next B;
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4189            }            }
4190          } # INSCOPE            unless (length $token->{data}) {
4191                        !!!cp ('t88');
4192          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4193            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              next B;
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         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 => 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;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4194            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4195          }          }
4196    
4197          undef $self->{form_element};          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4198          !!!next-token;            !!!cp ('t89');
4199          return;            ## As if <head>
4200        } elsif ({            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4201                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4202                 }->{$token->{tag_name}}) {            push @{$self->{open_elements}},
4203          ## has an element in scope                [$self->{head_element}, $el_category->{head}];
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
4204    
4205          ## Step 2            ## Reprocess in the "in head" insertion mode...
4206          S2: {            pop @{$self->{open_elements}};
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4207    
4208              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4209              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4210            } else {            !!!cp ('t90');
4211              ## Step 3            ## As if </noscript>
4212              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4213                  #not $phrasing_category->{$node->[1]} and            !!!parse-error (type => 'in noscript:#text', token => $token);
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4214                        
4215            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4216            redo S2;            ## As if </head>
4217          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
4218    
4219    B: {            ## Reprocess in the "after head" insertion mode...
4220      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4221        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4222          !!!parse-error (type => 'in html:#DOCTYPE');            pop @{$self->{open_elements}};
         ## 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});  
           }  
         }  
         !!!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]);  
         }  
4223    
4224          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4225          last B;          } else {
4226              !!!cp ('t92');
4227            }
4228    
4229          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4230        } else {          ## As if <body>
4231          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4232            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4233              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4234                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4235                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4236                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4237                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4238                }              !!!cp ('t93');
4239              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4240              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4241              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4242              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4243              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4244              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4245              ## reprocess              !!!nack ('t93.1');
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
4246              !!!next-token;              !!!next-token;
4247              redo B;              next B;
4248            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4249              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4250              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4251              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4252              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4253              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4254              if ($token->{tag_name} eq 'head') {              !!!next-token;
4255                !!!next-token;              next B;
             #} 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;  
             }  
4256            } else {            } else {
4257              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4258            }              !!!parse-error (type => 'in head:head',
4259          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4260            if ($token->{type} eq 'character') {              ## Ignore the token
4261              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              !!!nack ('t95.1');
               $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);  
4262              !!!next-token;              !!!next-token;
4263              redo B;              next B;
4264            } elsif ($token->{type} eq 'start tag') {            }
4265              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4266                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4267                my $title_el;            ## As if <head>
4268                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4269                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4270                  ->append_child ($title_el);            push @{$self->{open_elements}},
4271                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4272                delete $self->{escape}; # MUST  
4273              $self->{insertion_mode} = IN_HEAD_IM;
4274                my $text = '';            ## Reprocess in the "in head" insertion mode...
4275                !!!next-token;          } else {
4276                while ($token->{type} eq 'character') {            !!!cp ('t97');
4277                  $text .= $token->{data};          }
4278                  !!!next-token;  
4279                }              if ($token->{tag_name} eq 'base') {
4280                if (length $text) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4281                  $title_el->manakai_append_text ($text);                  !!!cp ('t98');
4282                }                  ## As if </noscript>
4283                                  pop @{$self->{open_elements}};
4284                $self->{content_model_flag} = 'PCDATA';                  !!!parse-error (type => 'in noscript', text => 'base',
4285                                    token => $token);
4286                                
4287                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4288                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4289                } else {                } else {
4290                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4291                }                }
               !!!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);  
4292    
4293                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4294                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4295              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4296                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4297                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4298                !!!next-token;                  push @{$self->{open_elements}},
4299                redo B;                      [$self->{head_element}, $el_category->{head}];
             } 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}};  
4300                } else {                } else {
4301                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4302                }                }
4303                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4304                !!!next-token;                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4305                redo B;                pop @{$self->{open_elements}} # <head>
4306              } elsif ($token->{tag_name} eq 'html') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4307                #                !!!nack ('t101.1');
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
4308                !!!next-token;                !!!next-token;
4309                redo B;                next B;
4310              }              } elsif ($token->{tag_name} eq 'link') {
4311            } else {                ## NOTE: There is a "as if in head" code clone.
4312              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4313            }                  !!!cp ('t102');
4314                    !!!parse-error (type => 'after head',
4315            if ($self->{open_elements}->[-1]->[1] eq 'head') {                                  text => $token->{tag_name}, token => $token);
4316              ## As if </head>                  push @{$self->{open_elements}},
4317              pop @{$self->{open_elements}};                      [$self->{head_element}, $el_category->{head}];
4318            }                } else {
4319            $self->{insertion_mode} = 'after head';                  !!!cp ('t103');
           ## reprocess  
           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;  
4320                }                }
4321              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4322                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4323              #                pop @{$self->{open_elements}} # <head>
4324            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4325              my $comment = $self->{document}->create_comment ($token->{data});                !!!ack ('t103.1');
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
4326                !!!next-token;                !!!next-token;
4327                redo B;                next B;
4328              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4329                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4330                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4331                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4332                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4333                $self->{insertion_mode} = 'in head';                                  text => $token->{tag_name}, token => $token);
4334                ## reprocess                  push @{$self->{open_elements}},
4335                redo B;                      [$self->{head_element}, $el_category->{head}];
4336              } else {                } else {
4337                #                  !!!cp ('t105');
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!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;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## 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;  
4338                }                }
4339              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4340                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
4341    
4342              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
4343              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) {
4344              ## into the current node" while characters might not be                    !!!cp ('t106');
4345              ## result in a new Text node.                    ## NOTE: Whether the encoding is supported or not is handled
4346              $reconstruct_active_formatting_elements->($insert_to_foster);                    ## in the {change_encoding} callback.
4347                                  $self->{change_encoding}
4348              if ({                        ->($self, $token->{attributes}->{charset}->{value},
4349                   table => 1, tbody => 1, tfoot => 1,                           $token);
4350                   thead => 1, tr => 1,                    
4351                  }->{$self->{open_elements}->[-1]->[1]}) {                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4352                # MUST                        ->set_user_data (manakai_has_reference =>
4353                my $foster_parent_element;                                             $token->{attributes}->{charset}
4354                my $next_sibling;                                                 ->{has_reference});
4355                my $prev_sibling;                  } elsif ($token->{attributes}->{content}) {
4356                OE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($token->{attributes}->{content}->{value}
4357                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4358                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*=
4359                    if (defined $parent and $parent->node_type == 1) {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4360                      $foster_parent_element = $parent;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4361                      $next_sibling = $self->{open_elements}->[$_]->[0];                      !!!cp ('t107');
4362                      $prev_sibling = $next_sibling->previous_sibling;                      ## NOTE: Whether the encoding is supported or not is handled
4363                        ## in the {change_encoding} callback.
4364                        $self->{change_encoding}
4365                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4366                               $token);
4367                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4368                            ->set_user_data (manakai_has_reference =>
4369                                                 $token->{attributes}->{content}
4370                                                       ->{has_reference});
4371                    } else {                    } else {
4372                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4373                    }                    }
                   last OE;  
4374                  }                  }
               } # 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});  
4375                } else {                } else {
4376                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4377                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4378                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4379                }                        ->set_user_data (manakai_has_reference =>
4380              } else {                                             $token->{attributes}->{charset}
4381                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4382              }                  }
4383                                if ($token->{attributes}->{content}) {
4384              !!!next-token;                    !!!cp ('t110');
4385              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4386            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4387              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4388              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4389              !!!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}};  
4390                }                }
4391    
4392                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4393                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4394                  !!!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}};  
4395                !!!next-token;                !!!next-token;
4396                redo B;                next B;
4397              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4398                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4399                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4400                       }->{$token->{tag_name}}) {                  ## As if </noscript>
               ## 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]);  
4401                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4402                    !!!parse-error (type => 'in noscript', text => 'title',
4403                                    token => $token);
4404                  
4405                    $self->{insertion_mode} = IN_HEAD_IM;
4406                    ## Reprocess in the "in head" insertion mode...
4407                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4408                    !!!cp ('t112');
4409                    !!!parse-error (type => 'after head',
4410                                    text => $token->{tag_name}, token => $token);
4411                    push @{$self->{open_elements}},
4412                        [$self->{head_element}, $el_category->{head}];
4413                  } else {
4414                    !!!cp ('t113');
4415                }                }
4416    
4417                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4418                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4419                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4420                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4421                redo B;                pop @{$self->{open_elements}} # <head>
4422              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4423                ## NOTE: There are code clones for this "table in table"                next B;
4424                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4425                         $token->{tag_name} eq 'noframes') {
4426                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4427                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4428                my $i;                ## NOTE: There is a "as if in head" code clone.
4429                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4430                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4431                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4432                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4433                    last INSCOPE;                  push @{$self->{open_elements}},
4434                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4435                            table => 1, html => 1,                } else {
4436                           }->{$node->[1]}) {                  !!!cp ('t115');
4437                    last INSCOPE;                }
4438                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4439                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4440                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4441                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4442                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4443                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4444                    !!!cp ('t116');
4445                    ## NOTE: and scripting is disalbed
4446                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4447                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4448                    !!!nack ('t116.1');
4449                  !!!next-token;                  !!!next-token;
4450                  redo B;                  next B;
4451                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4452                    !!!cp ('t117');
4453                    !!!parse-error (type => 'in noscript', text => 'noscript',
4454                                    token => $token);
4455                    ## Ignore the token
4456                    !!!nack ('t117.1');
4457                    !!!next-token;
4458                    next B;
4459                  } else {
4460                    !!!cp ('t118');
4461                    #
4462                }                }
4463                } elsif ($token->{tag_name} eq 'script') {
4464                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4465                    !!!cp ('t119');
4466                    ## As if </noscript>
4467                    pop @{$self->{open_elements}};
4468                    !!!parse-error (type => 'in noscript', text => 'script',
4469                                    token => $token);
4470                                
4471                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4472                if ({                  ## Reprocess in the "in head" insertion mode...
4473                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4474                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4475                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4476                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4477                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4478                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4479                  $token = {type => 'end tag',                } else {
4480                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
                 redo B;  
4481                }                }
4482    
4483                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4484                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4485                  pop @{$self->{open_elements}} # <head>
4486                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4487                  next B;
4488                } elsif ($token->{tag_name} eq 'body' or
4489                         $token->{tag_name} eq 'frameset') {
4490                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4491                    !!!cp ('t122');
4492                    ## As if </noscript>
4493                    pop @{$self->{open_elements}};
4494                    !!!parse-error (type => 'in noscript',
4495                                    text => $token->{tag_name}, token => $token);
4496                    
4497                    ## Reprocess in the "in head" insertion mode...
4498                    ## As if </head>
4499                    pop @{$self->{open_elements}};
4500                    
4501                    ## Reprocess in the "after head" insertion mode...
4502                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4503                    !!!cp ('t124');
4504                    pop @{$self->{open_elements}};
4505                    
4506                    ## Reprocess in the "after head" insertion mode...
4507                  } else {
4508                    !!!cp ('t125');
4509                }                }
4510    
4511                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4512                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4513                  if ($token->{tag_name} eq 'body') {
4514                    !!!cp ('t126');
4515                    $self->{insertion_mode} = IN_BODY_IM;
4516                  } elsif ($token->{tag_name} eq 'frameset') {
4517                    !!!cp ('t127');
4518                    $self->{insertion_mode} = IN_FRAMESET_IM;
4519                  } else {
4520                    die "$0: tag name: $self->{tag_name}";
4521                  }
4522                  !!!nack ('t127.1');
4523                  !!!next-token;
4524                  next B;
4525                } else {
4526                  !!!cp ('t128');
4527                  #
4528                }
4529    
4530                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4531                  !!!cp ('t129');
4532                  ## As if </noscript>
4533                  pop @{$self->{open_elements}};
4534                  !!!parse-error (type => 'in noscript:/',
4535                                  text => $token->{tag_name}, token => $token);
4536                  
4537                  ## Reprocess in the "in head" insertion mode...
4538                  ## As if </head>
4539                  pop @{$self->{open_elements}};
4540    
4541                ## reprocess                ## Reprocess in the "after head" insertion mode...
4542                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4543                  !!!cp ('t130');
4544                  ## As if </head>
4545                  pop @{$self->{open_elements}};
4546    
4547                  ## Reprocess in the "after head" insertion mode...
4548              } else {              } else {
4549                #                !!!cp ('t131');
4550              }              }
4551            } elsif ($token->{type} eq 'end tag') {  
4552              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4553                ## have a table element in table scope              ## As if <body>
4554                my $i;              !!!insert-element ('body',, $token);
4555                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4556                  my $node = $self->{open_elements}->[$_];              ## reprocess
4557                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4558                    $i = $_;              next B;
4559                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4560                  } elsif ({              if ($token->{tag_name} eq 'head') {
4561                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4562                           }->{$node->[1]}) {                  !!!cp ('t132');
4563                    last INSCOPE;                  ## As if <head>
4564                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4565                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4566                unless (defined $i) {                  push @{$self->{open_elements}},
4567                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4568    
4569                    ## Reprocess in the "in head" insertion mode...
4570                    pop @{$self->{open_elements}};
4571                    $self->{insertion_mode} = AFTER_HEAD_IM;
4572                    !!!next-token;
4573                    next B;
4574                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4575                    !!!cp ('t133');
4576                    ## As if </noscript>
4577                    pop @{$self->{open_elements}};
4578                    !!!parse-error (type => 'in noscript:/',
4579                                    text => 'head', token => $token);
4580                    
4581                    ## Reprocess in the "in head" insertion mode...
4582                    pop @{$self->{open_elements}};
4583                    $self->{insertion_mode} = AFTER_HEAD_IM;
4584                    !!!next-token;
4585                    next B;
4586                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4587                    !!!cp ('t134');
4588                    pop @{$self->{open_elements}};
4589                    $self->{insertion_mode} = AFTER_HEAD_IM;
4590                    !!!next-token;
4591                    next B;
4592                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4593                    !!!cp ('t134.1');
4594                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4595                                    token => $token);
4596                  ## Ignore the token                  ## Ignore the token
4597                  !!!next-token;                  !!!next-token;
4598                  redo B;                  next B;
4599                  } else {
4600                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4601                }                }
4602                              } elsif ($token->{tag_name} eq 'noscript') {
4603                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4604                if ({                  !!!cp ('t136');
4605                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4606                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4607                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4608                  !!!back-token;                  next B;
4609                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4610                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4611                  redo B;                  !!!cp ('t137');
4612                    !!!parse-error (type => 'unmatched end tag',
4613                                    text => 'noscript', token => $token);
4614                    ## Ignore the token ## ISSUE: An issue in the spec.
4615                    !!!next-token;
4616                    next B;
4617                  } else {
4618                    !!!cp ('t138');
4619                    #
4620                }                }
4621                } elsif ({
4622                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4623                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4624                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4625                      $self->{insertion_mode} == IN_HEAD_IM or
4626                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4627                    !!!cp ('t140');
4628                    !!!parse-error (type => 'unmatched end tag',
4629                                    text => $token->{tag_name}, token => $token);
4630                    ## Ignore the token
4631                    !!!next-token;
4632                    next B;
4633                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4634                    !!!cp ('t140.1');
4635                    !!!parse-error (type => 'unmatched end tag',
4636                                    text => $token->{tag_name}, token => $token);
4637                    ## Ignore the token
4638                    !!!next-token;
4639                    next B;
4640                  } else {
4641                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4642                }                }
4643                } elsif ($token->{tag_name} eq 'p') {
4644                  !!!cp ('t142');
4645                  !!!parse-error (type => 'unmatched end tag',
4646                                  text => $token->{tag_name}, token => $token);
4647                  ## Ignore the token
4648                  !!!next-token;
4649                  next B;
4650                } elsif ($token->{tag_name} eq 'br') {
4651                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4652                    !!!cp ('t142.2');
4653                    ## (before head) as if <head>, (in head) as if </head>
4654                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4655                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4656                    $self->{insertion_mode} = AFTER_HEAD_IM;
4657      
4658                    ## Reprocess in the "after head" insertion mode...
4659                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4660                    !!!cp ('t143.2');
4661                    ## As if </head>
4662                    pop @{$self->{open_elements}};
4663                    $self->{insertion_mode} = AFTER_HEAD_IM;
4664      
4665                    ## Reprocess in the "after head" insertion mode...
4666                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4667                    !!!cp ('t143.3');
4668                    ## ISSUE: Two parse errors for <head><noscript></br>
4669                    !!!parse-error (type => 'unmatched end tag',
4670                                    text => 'br', token => $token);
4671                    ## As if </noscript>
4672                    pop @{$self->{open_elements}};
4673                    $self->{insertion_mode} = IN_HEAD_IM;
4674    
4675                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
4676                    ## As if </head>
4677                    pop @{$self->{open_elements}};
4678                    $self->{insertion_mode} = AFTER_HEAD_IM;
4679    
4680                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
4681                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4682                    !!!cp ('t143.4');
4683                    #
4684                  } else {
4685                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4686                  }
4687    
4688                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4689                  !!!parse-error (type => 'unmatched end tag',
4690                                  text => 'br', token => $token);
4691                  ## Ignore the token
4692                !!!next-token;                !!!next-token;
4693                redo B;                next B;
4694              } elsif ({              } else {
4695                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
4696                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
4697                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4698                ## Ignore the token                ## Ignore the token
4699                !!!next-token;                !!!next-token;
4700                redo B;                next B;
4701                }
4702    
4703                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4704                  !!!cp ('t146');
4705                  ## As if </noscript>
4706                  pop @{$self->{open_elements}};
4707                  !!!parse-error (type => 'in noscript:/',
4708                                  text => $token->{tag_name}, token => $token);
4709                  
4710                  ## Reprocess in the "in head" insertion mode...
4711                  ## As if </head>
4712                  pop @{$self->{open_elements}};
4713    
4714                  ## Reprocess in the "after head" insertion mode...
4715                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4716                  !!!cp ('t147');
4717                  ## As if </head>
4718                  pop @{$self->{open_elements}};
4719    
4720                  ## Reprocess in the "after head" insertion mode...
4721                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4722    ## ISSUE: This case cannot be reached?
4723                  !!!cp ('t148');
4724                  !!!parse-error (type => 'unmatched end tag',
4725                                  text => $token->{tag_name}, token => $token);
4726                  ## Ignore the token ## ISSUE: An issue in the spec.
4727                  !!!next-token;
4728                  next B;
4729              } else {              } else {
4730                #                !!!cp ('t149');
4731              }              }
           } else {  
             #  
           }  
4732    
4733            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4734            $in_body->($insert_to_foster);              ## As if <body>
4735            redo B;              !!!insert-element ('body',, $token);
4736          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4737            if ($token->{type} eq 'character') {              ## reprocess
4738              ## NOTE: This is a code clone of "character in body".              next B;
4739          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4740            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4741              !!!cp ('t149.1');
4742    
4743              ## NOTE: As if <head>
4744              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4745              $self->{open_elements}->[-1]->[0]->append_child
4746                  ($self->{head_element});
4747              #push @{$self->{open_elements}},
4748              #    [$self->{head_element}, $el_category->{head}];
4749              #$self->{insertion_mode} = IN_HEAD_IM;
4750              ## NOTE: Reprocess.
4751    
4752              ## NOTE: As if </head>
4753              #pop @{$self->{open_elements}};
4754              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4755              ## NOTE: Reprocess.
4756              
4757              #
4758            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4759              !!!cp ('t149.2');
4760    
4761              ## NOTE: As if </head>
4762              pop @{$self->{open_elements}};
4763              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4764              ## NOTE: Reprocess.
4765    
4766              #
4767            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4768              !!!cp ('t149.3');
4769    
4770              !!!parse-error (type => 'in noscript:#eof', token => $token);
4771    
4772              ## As if </noscript>
4773              pop @{$self->{open_elements}};
4774              #$self->{insertion_mode} = IN_HEAD_IM;
4775              ## NOTE: Reprocess.
4776    
4777              ## NOTE: As if </head>
4778              pop @{$self->{open_elements}};
4779              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4780              ## NOTE: Reprocess.
4781    
4782              #
4783            } else {
4784              !!!cp ('t149.4');
4785              #
4786            }
4787    
4788            ## NOTE: As if <body>
4789            !!!insert-element ('body',, $token);
4790            $self->{insertion_mode} = IN_BODY_IM;
4791            ## NOTE: Reprocess.
4792            next B;
4793          } else {
4794            die "$0: $token->{type}: Unknown token type";
4795          }
4796    
4797              ## ISSUE: An issue in the spec.
4798        } elsif ($self->{insertion_mode} & BODY_IMS) {
4799              if ($token->{type} == CHARACTER_TOKEN) {
4800                !!!cp ('t150');
4801                ## NOTE: There is a code clone of "character in body".
4802              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4803                            
4804              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4805    
4806              !!!next-token;              !!!next-token;
4807              redo B;              next B;
4808            } 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') {  
4809              if ({              if ({
4810                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4811                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4812                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4813                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4814                    ## have an element in table scope
4815                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4816                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4817                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4818                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4819                  my $node = $self->{open_elements}->[$_];  
4820                  if ($node->[1] eq 'caption') {                      ## Close the cell
4821                    $i = $_;                      !!!back-token; # <x>
4822                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4823                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4824                            table => 1, html => 1,                                line => $token->{line},
4825                           }->{$node->[1]}) {                                column => $token->{column}};
4826                    last INSCOPE;                      next B;
4827                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4828                        !!!cp ('t152');
4829                        ## ISSUE: This case can never be reached, maybe.
4830                        last;
4831                      }
4832                  }                  }
4833                } # INSCOPE  
4834                unless (defined $i) {                  !!!cp ('t153');
4835                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4836                        text => $token->{tag_name}, token => $token);
4837                  ## Ignore the token                  ## Ignore the token
4838                    !!!nack ('t153.1');
4839                  !!!next-token;                  !!!next-token;
4840                  redo B;                  next B;
4841                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4842                                  !!!parse-error (type => 'not closed', text => 'caption',
4843                ## generate implied end tags                                  token => $token);
4844                if ({                  
4845                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
4846                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
4847                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
4848                  !!!back-token; # <?>                  INSCOPE: {
4849                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
4850                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
4851                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
4852                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
4853                  redo B;                        $i = $_;
4854                }                        last INSCOPE;
4855                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4856                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
4857                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
4858                }                      }
4859                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4860    
4861                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4862                      !!!parse-error (type => 'start tag not allowed',
4863                                      text => $token->{tag_name}, token => $token);
4864                      ## Ignore the token
4865                      !!!nack ('t157.1');
4866                      !!!next-token;
4867                      next B;
4868                    } # INSCOPE
4869                    
4870                    ## generate implied end tags
4871                    while ($self->{open_elements}->[-1]->[1]
4872                               & END_TAG_OPTIONAL_EL) {
4873                      !!!cp ('t158');
4874                      pop @{$self->{open_elements}};
4875                    }
4876    
4877                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4878                redo B;                    !!!cp ('t159');
4879                      !!!parse-error (type => 'not closed',
4880                                      text => $self->{open_elements}->[-1]->[0]
4881                                          ->manakai_local_name,
4882                                      token => $token);
4883                    } else {
4884                      !!!cp ('t160');
4885                    }
4886                    
4887                    splice @{$self->{open_elements}}, $i;
4888                    
4889                    $clear_up_to_marker->();
4890                    
4891                    $self->{insertion_mode} = IN_TABLE_IM;
4892                    
4893                    ## reprocess
4894                    !!!ack-later;
4895                    next B;
4896                  } else {
4897                    !!!cp ('t161');
4898                    #
4899                  }
4900              } else {              } else {
4901                  !!!cp ('t162');
4902                #                #
4903              }              }
4904            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4905              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4906                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4907                my $i;                  ## have an element in table scope
4908                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4909                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4910                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4911                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4912                    last INSCOPE;                      !!!cp ('t163');
4913                  } elsif ({                      $i = $_;
4914                            table => 1, html => 1,                      last INSCOPE;
4915                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4916                    last INSCOPE;                      !!!cp ('t164');
4917                        last INSCOPE;
4918                      }
4919                    } # INSCOPE
4920                      unless (defined $i) {
4921                        !!!cp ('t165');
4922                        !!!parse-error (type => 'unmatched end tag',
4923                                        text => $token->{tag_name},
4924                                        token => $token);
4925                        ## Ignore the token
4926                        !!!next-token;
4927                        next B;
4928                      }
4929                    
4930                    ## generate implied end tags
4931                    while ($self->{open_elements}->[-1]->[1]
4932                               & END_TAG_OPTIONAL_EL) {
4933                      !!!cp ('t166');
4934                      pop @{$self->{open_elements}};
4935                  }                  }
4936                } # INSCOPE  
4937                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4938                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4939                      !!!cp ('t167');
4940                      !!!parse-error (type => 'not closed',
4941                                      text => $self->{open_elements}->[-1]->[0]
4942                                          ->manakai_local_name,
4943                                      token => $token);
4944                    } else {
4945                      !!!cp ('t168');
4946                    }
4947                    
4948                    splice @{$self->{open_elements}}, $i;
4949                    
4950                    $clear_up_to_marker->();
4951                    
4952                    $self->{insertion_mode} = IN_ROW_IM;
4953                    
4954                    !!!next-token;
4955                    next B;
4956                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4957                    !!!cp ('t169');
4958                    !!!parse-error (type => 'unmatched end tag',
4959                                    text => $token->{tag_name}, token => $token);
4960                  ## Ignore the token                  ## Ignore the token
4961                  !!!next-token;                  !!!next-token;
4962                  redo B;                  next B;
4963                }                } else {
4964                                  !!!cp ('t170');
4965                ## 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;  
4966                }                }
4967                } elsif ($token->{tag_name} eq 'caption') {
4968                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4969                    ## have a table element in table scope
4970                    my $i;
4971                    INSCOPE: {
4972                      for (reverse 0..$#{$self->{open_elements}}) {
4973                        my $node = $self->{open_elements}->[$_];
4974                        if ($node->[1] & CAPTION_EL) {
4975                          !!!cp ('t171');
4976                          $i = $_;
4977                          last INSCOPE;
4978                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4979                          !!!cp ('t172');
4980                          last;
4981                        }
4982                      }
4983    
4984                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4985                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4986                                      text => $token->{tag_name}, token => $token);
4987                      ## Ignore the token
4988                      !!!next-token;
4989                      next B;
4990                    } # INSCOPE
4991                    
4992                    ## generate implied end tags
4993                    while ($self->{open_elements}->[-1]->[1]
4994                               & END_TAG_OPTIONAL_EL) {
4995                      !!!cp ('t174');
4996                      pop @{$self->{open_elements}};
4997                    }
4998                    
4999                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5000                      !!!cp ('t175');
5001                      !!!parse-error (type => 'not closed',
5002                                      text => $self->{open_elements}->[-1]->[0]
5003                                          ->manakai_local_name,
5004                                      token => $token);
5005                    } else {
5006                      !!!cp ('t176');
5007                    }
5008                    
5009                    splice @{$self->{open_elements}}, $i;
5010                    
5011                    $clear_up_to_marker->();
5012                    
5013                    $self->{insertion_mode} = IN_TABLE_IM;
5014                    
5015                    !!!next-token;
5016                    next B;
5017                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5018                    !!!cp ('t177');
5019                    !!!parse-error (type => 'unmatched end tag',
5020                                    text => $token->{tag_name}, token => $token);
5021                    ## Ignore the token
5022                    !!!next-token;
5023                    next B;
5024                  } else {
5025                    !!!cp ('t178');
5026                    #
5027                }                }
5028                } elsif ({
5029                          table => 1, tbody => 1, tfoot => 1,
5030                          thead => 1, tr => 1,
5031                         }->{$token->{tag_name}} and
5032                         $self->{insertion_mode} == IN_CELL_IM) {
5033                  ## have an element in table scope
5034                  my $i;
5035                  my $tn;
5036                  INSCOPE: {
5037                    for (reverse 0..$#{$self->{open_elements}}) {
5038                      my $node = $self->{open_elements}->[$_];
5039                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5040                        !!!cp ('t179');
5041                        $i = $_;
5042    
5043                        ## Close the cell
5044                        !!!back-token; # </x>
5045                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5046                                  line => $token->{line},
5047                                  column => $token->{column}};
5048                        next B;
5049                      } elsif ($node->[1] & TABLE_CELL_EL) {
5050                        !!!cp ('t180');
5051                        $tn = $node->[0]->manakai_local_name;
5052                        ## NOTE: There is exactly one |td| or |th| element
5053                        ## in scope in the stack of open elements by definition.
5054                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5055                        ## ISSUE: Can this be reached?
5056                        !!!cp ('t181');
5057                        last;
5058                      }
5059                    }
5060    
5061                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5062                    !!!parse-error (type => 'unmatched end tag',
5063                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5064                    ## Ignore the token
5065                $self->{insertion_mode} = 'in table';                  !!!next-token;
5066                    next B;
5067                !!!next-token;                } # INSCOPE
5068                redo B;              } elsif ($token->{tag_name} eq 'table' and
5069              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5070                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5071                                  token => $token);
5072    
5073                ## As if </caption>                ## As if </caption>
5074                ## have a table element in table scope                ## have a table element in table scope
5075                my $i;                my $i;
5076                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5077                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5078                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5079                      !!!cp ('t184');
5080                    $i = $_;                    $i = $_;
5081                    last INSCOPE;                    last INSCOPE;
5082                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5083                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5084                    last INSCOPE;                    last INSCOPE;
5085                  }                  }
5086                } # INSCOPE                } # INSCOPE
5087                unless (defined $i) {                unless (defined $i) {
5088                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5089                    !!!parse-error (type => 'unmatched end tag',
5090                                    text => 'caption', token => $token);
5091                  ## Ignore the token                  ## Ignore the token
5092                  !!!next-token;                  !!!next-token;
5093                  redo B;                  next B;
5094                }                }
5095                                
5096                ## generate implied end tags                ## generate implied end tags
5097                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5098                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5099                     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;  
5100                }                }
5101    
5102                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5103                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5104                    !!!parse-error (type => 'not closed',
5105                                    text => $self->{open_elements}->[-1]->[0]
5106                                        ->manakai_local_name,
5107                                    token => $token);
5108                  } else {
5109                    !!!cp ('t189');
5110                }                }
5111    
5112                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5113    
5114                $clear_up_to_marker->();                $clear_up_to_marker->();
5115    
5116                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5117    
5118                ## reprocess                ## reprocess
5119                redo B;                next B;
5120              } elsif ({              } elsif ({
5121                        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,  
5122                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5123                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5124                ## Ignore the token                  !!!cp ('t190');
5125                redo B;                  !!!parse-error (type => 'unmatched end tag',
5126              } 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');  
5127                  ## Ignore the token                  ## Ignore the token
5128                  !!!next-token;                  !!!next-token;
5129                  redo B;                  next B;
5130                } else {                } else {
5131                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5132                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5133                }                }
5134              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5135                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5136                          thead => 1, tr => 1,
5137                         }->{$token->{tag_name}} and
5138                         $self->{insertion_mode} == IN_CAPTION_IM) {
5139                  !!!cp ('t192');
5140                  !!!parse-error (type => 'unmatched end tag',
5141                                  text => $token->{tag_name}, token => $token);
5142                ## Ignore the token                ## Ignore the token
5143                !!!next-token;                !!!next-token;
5144                redo B;                next B;
5145              } else {              } else {
5146                #                !!!cp ('t193');
5147                  #
5148              }              }
5149            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5150              #          for my $entry (@{$self->{open_elements}}) {
5151              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5152                !!!cp ('t75');
5153                !!!parse-error (type => 'in body:#eof', token => $token);
5154                last;
5155            }            }
5156            }
5157    
5158            ## As if </colgroup>          ## Stop parsing.
5159            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5160              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5161              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5162          }
5163    
5164          $insert = $insert_to_current;
5165          #
5166        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5167          if ($token->{type} == CHARACTER_TOKEN) {
5168            if (not $open_tables->[-1]->[1] and # tainted
5169                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5170              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5171                  
5172              unless (length $token->{data}) {
5173                !!!cp ('t194');
5174              !!!next-token;              !!!next-token;
5175              redo B;              next B;
5176            } else {            } else {
5177              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5178            }            }
5179          } 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;  
               }  
             }  
5180    
5181              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5182    
5183              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5184              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5185              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5186              ## result in a new Text node.              ## result in a new Text node.
5187              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5188                
5189              if ({              if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5190                # MUST                # MUST
5191                my $foster_parent_element;                my $foster_parent_element;
5192                my $next_sibling;                my $next_sibling;
5193                my $prev_sibling;                my $prev_sibling;
5194                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5195                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5196                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5197                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5198                        !!!cp ('t196');
5199                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5200                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5201                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5202                    } else {                    } else {
5203                        !!!cp ('t197');
5204                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5205                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5206                    }                    }
# Line 3816  sub _tree_construction_main ($) { Line 5212  sub _tree_construction_main ($) {
5212                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5213                if (defined $prev_sibling and                if (defined $prev_sibling and
5214                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5215                    !!!cp ('t198');
5216                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5217                } else {                } else {
5218                    !!!cp ('t199');
5219                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5220                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5221                     $next_sibling);                     $next_sibling);
5222                }                }
5223              } else {            $open_tables->[-1]->[1] = 1; # tainted
5224                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5225              !!!cp ('t200');
5226              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5227            }
5228                
5229            !!!next-token;
5230            next B;
5231          } elsif ($token->{type} == START_TAG_TOKEN) {
5232            if ({
5233                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5234                 th => 1, td => 1,
5235                }->{$token->{tag_name}}) {
5236              if ($self->{insertion_mode} == IN_TABLE_IM) {
5237                ## Clear back to table context
5238                while (not ($self->{open_elements}->[-1]->[1]
5239                                & TABLE_SCOPING_EL)) {
5240                  !!!cp ('t201');
5241                  pop @{$self->{open_elements}};
5242              }              }
5243                            
5244              !!!next-token;              !!!insert-element ('tbody',, $token);
5245              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5246            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5247              ## Copied from 'in table'            }
5248              my $comment = $self->{document}->create_comment ($token->{data});            
5249              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5250              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5251              redo B;                !!!cp ('t202');
5252            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5253              if ({              }
5254                   tr => 1,                  
5255                   th => 1, td => 1,              ## Clear back to table body context
5256                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5257                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5258                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5259                  ## ISSUE: Can this case be reached?
5260                  pop @{$self->{open_elements}};
5261                }
5262                    
5263                    $self->{insertion_mode} = IN_ROW_IM;
5264                    if ($token->{tag_name} eq 'tr') {
5265                      !!!cp ('t204');
5266                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5267                      !!!nack ('t204');
5268                      !!!next-token;
5269                      next B;
5270                    } else {
5271                      !!!cp ('t205');
5272                      !!!insert-element ('tr',, $token);
5273                      ## reprocess in the "in row" insertion mode
5274                    }
5275                  } else {
5276                    !!!cp ('t206');
5277                }                }
5278    
5279                ## Clear back to table body context                ## Clear back to table row context
5280                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5281                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5282                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5283                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5284                }                }
5285                                
5286                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5287                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5288                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5289                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5290                } else {                
5291                  !!!insert-element ('tr');                !!!nack ('t207.1');
5292                  ## reprocess                !!!next-token;
5293                }                next B;
               redo B;  
5294              } elsif ({              } elsif ({
5295                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5296                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5297                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5298                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5299                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5300                my $i;                  ## As if </tr>
5301                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5302                  my $node = $self->{open_elements}->[$_];                  my $i;
5303                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5304                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5305                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5306                    $i = $_;                      !!!cp ('t208');
5307                    last INSCOPE;                      $i = $_;
5308                  } elsif ({                      last INSCOPE;
5309                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5310                           }->{$node->[1]}) {                      !!!cp ('t209');
5311                    last INSCOPE;                      last INSCOPE;
5312                      }
5313                    } # INSCOPE
5314                    unless (defined $i) {
5315                      !!!cp ('t210');
5316    ## TODO: This type is wrong.
5317                      !!!parse-error (type => 'unmacthed end tag',
5318                                      text => $token->{tag_name}, token => $token);
5319                      ## Ignore the token
5320                      !!!nack ('t210.1');
5321                      !!!next-token;
5322                      next B;
5323                    }
5324                    
5325                    ## Clear back to table row context
5326                    while (not ($self->{open_elements}->[-1]->[1]
5327                                    & TABLE_ROW_SCOPING_EL)) {
5328                      !!!cp ('t211');
5329                      ## ISSUE: Can this case be reached?
5330                      pop @{$self->{open_elements}};
5331                    }
5332                    
5333                    pop @{$self->{open_elements}}; # tr
5334                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5335                    if ($token->{tag_name} eq 'tr') {
5336                      !!!cp ('t212');
5337                      ## reprocess
5338                      !!!ack-later;
5339                      next B;
5340                    } else {
5341                      !!!cp ('t213');
5342                      ## reprocess in the "in table body" insertion mode...
5343                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5344                }                }
5345    
5346                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5347                while (not {                  ## have an element in table scope
5348                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5349                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5350                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5351                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5352                        !!!cp ('t214');
5353                        $i = $_;
5354                        last INSCOPE;
5355                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5356                        !!!cp ('t215');
5357                        last INSCOPE;
5358                      }
5359                    } # INSCOPE
5360                    unless (defined $i) {
5361                      !!!cp ('t216');
5362    ## TODO: This erorr type is wrong.
5363                      !!!parse-error (type => 'unmatched end tag',
5364                                      text => $token->{tag_name}, token => $token);
5365                      ## Ignore the token
5366                      !!!nack ('t216.1');
5367                      !!!next-token;
5368                      next B;
5369                    }
5370    
5371                    ## Clear back to table body context
5372                    while (not ($self->{open_elements}->[-1]->[1]
5373                                    & TABLE_ROWS_SCOPING_EL)) {
5374                      !!!cp ('t217');
5375                      ## ISSUE: Can this state be reached?
5376                      pop @{$self->{open_elements}};
5377                    }
5378                    
5379                    ## As if <{current node}>
5380                    ## have an element in table scope
5381                    ## true by definition
5382                    
5383                    ## Clear back to table body context
5384                    ## nop by definition
5385                    
5386                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5387                    $self->{insertion_mode} = IN_TABLE_IM;
5388                    ## reprocess in "in table" insertion mode...
5389                  } else {
5390                    !!!cp ('t218');
5391                }                }
5392    
5393                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5394                ## have an element in table scope                  ## Clear back to table context
5395                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5396                                    & TABLE_SCOPING_EL)) {
5397                ## Clear back to table body context                    !!!cp ('t219');
5398                ## nop by definition                    ## ISSUE: Can this state be reached?
5399                      pop @{$self->{open_elements}};
5400                pop @{$self->{open_elements}};                  }
5401                $self->{insertion_mode} = 'in table';                  
5402                ## reprocess                  !!!insert-element ('colgroup',, $token);
5403                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5404                    ## reprocess
5405                    !!!ack-later;
5406                    next B;
5407                  } elsif ({
5408                            caption => 1,
5409                            colgroup => 1,
5410                            tbody => 1, tfoot => 1, thead => 1,
5411                           }->{$token->{tag_name}}) {
5412                    ## Clear back to table context
5413                    while (not ($self->{open_elements}->[-1]->[1]
5414                                    & TABLE_SCOPING_EL)) {
5415                      !!!cp ('t220');
5416                      ## ISSUE: Can this state be reached?
5417                      pop @{$self->{open_elements}};
5418                    }
5419                    
5420                    push @$active_formatting_elements, ['#marker', '']
5421                        if $token->{tag_name} eq 'caption';
5422                    
5423                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5424                    $self->{insertion_mode} = {
5425                                               caption => IN_CAPTION_IM,
5426                                               colgroup => IN_COLUMN_GROUP_IM,
5427                                               tbody => IN_TABLE_BODY_IM,
5428                                               tfoot => IN_TABLE_BODY_IM,
5429                                               thead => IN_TABLE_BODY_IM,
5430                                              }->{$token->{tag_name}};
5431                    !!!next-token;
5432                    !!!nack ('t220.1');
5433                    next B;
5434                  } else {
5435                    die "$0: in table: <>: $token->{tag_name}";
5436                  }
5437              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5438                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5439                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5440                                      ->manakai_local_name,
5441                                  token => $token);
5442    
5443                ## As if </table>                ## As if </table>
5444                ## have a table element in table scope                ## have a table element in table scope
5445                my $i;                my $i;
5446                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5447                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5448                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5449                      !!!cp ('t221');
5450                    $i = $_;                    $i = $_;
5451                    last INSCOPE;                    last INSCOPE;
5452                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5453                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5454                    last INSCOPE;                    last INSCOPE;
5455                  }                  }
5456                } # INSCOPE                } # INSCOPE
5457                unless (defined $i) {                unless (defined $i) {
5458                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5459    ## TODO: The following is wrong, maybe.
5460                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5461                                    token => $token);
5462                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5463                    !!!nack ('t223.1');
5464                  !!!next-token;                  !!!next-token;
5465                  redo B;                  next B;
5466                }                }
5467                                
5468    ## TODO: Followings are removed from the latest spec.
5469                ## generate implied end tags                ## generate implied end tags
5470                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5471                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5472                     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;  
5473                }                }
5474    
5475                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5476                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5477                    ## NOTE: |<table><tr><table>|
5478                    !!!parse-error (type => 'not closed',
5479                                    text => $self->{open_elements}->[-1]->[0]
5480                                        ->manakai_local_name,
5481                                    token => $token);
5482                  } else {
5483                    !!!cp ('t226');
5484                }                }
5485    
5486                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5487                  pop @{$open_tables};
5488    
5489                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5490    
5491                ## reprocess            ## reprocess
5492                redo B;            !!!ack-later;
5493              } else {            next B;
5494                #          } elsif ($token->{tag_name} eq 'style') {
5495              }            if (not $open_tables->[-1]->[1]) { # tainted
5496            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5497              if ({              ## NOTE: This is a "as if in head" code clone.
5498                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5499                  }->{$token->{tag_name}}) {              next B;
5500                ## have an element in table scope            } else {
5501                my $i;              !!!cp ('t227.7');
5502                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5503                  my $node = $self->{open_elements}->[$_];            }
5504                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5505                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5506                    last INSCOPE;              !!!cp ('t227.6');
5507                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5508                            table => 1, html => 1,              $script_start_tag->();
5509                           }->{$node->[1]}) {              next B;
5510                    last INSCOPE;            } else {
5511                  }              !!!cp ('t227.5');
5512                } # INSCOPE              #
5513                unless (defined $i) {            }
5514                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5515                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5516                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5517                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5518                }                if ($type eq 'hidden') {
5519                    !!!cp ('t227.3');
5520                    !!!parse-error (type => 'in table',
5521                                    text => $token->{tag_name}, token => $token);
5522    
5523                ## Clear back to table body context                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
5524    
5525                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;  
               }  
5526    
               ## 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]);  
5527                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
5528    
5529                ## Clear back to table body context                  !!!next-token;
5530                ## nop by definition                  !!!ack ('t227.2.1');
5531                    next B;
5532                pop @{$self->{open_elements}};                } else {
5533                $self->{insertion_mode} = 'in table';                  !!!cp ('t227.2');
5534                ## reprocess                  #
5535                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;  
5536              } else {              } else {
5537                  !!!cp ('t227.1');
5538                #                #
5539              }              }
5540            } else {            } else {
5541                !!!cp ('t227.4');
5542              #              #
5543            }            }
5544                      } else {
5545            ## As if in table            !!!cp ('t227');
5546            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5547            $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;  
               }  
             }  
5548    
5549              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table', text => $token->{tag_name},
5550                            token => $token);
5551    
5552              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5553              ## ISSUE: Spec says that "whenever a node would be inserted          #
5554              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5555              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5556              $reconstruct_active_formatting_elements->($insert_to_foster);                  $self->{insertion_mode} == IN_ROW_IM) {
               
             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>  
5557                ## have an element in table scope                ## have an element in table scope
5558                my $i;                my $i;
5559                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5560                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5561                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5562                      !!!cp ('t228');
5563                    $i = $_;                    $i = $_;
5564                    last INSCOPE;                    last INSCOPE;
5565                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5566                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5567                    last INSCOPE;                    last INSCOPE;
5568                  }                  }
5569                } # INSCOPE                } # INSCOPE
5570                unless (defined $i) {                unless (defined $i) {
5571                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5572                    !!!parse-error (type => 'unmatched end tag',
5573                                    text => $token->{tag_name}, token => $token);
5574                  ## Ignore the token                  ## Ignore the token
5575                    !!!nack ('t230.1');
5576                  !!!next-token;                  !!!next-token;
5577                  redo B;                  next B;
5578                  } else {
5579                    !!!cp ('t232');
5580                }                }
5581    
5582                ## Clear back to table row context                ## Clear back to table row context
5583                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5584                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5585                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5586                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5587                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5588                }                }
5589    
5590                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5591                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5592                ## reprocess                !!!next-token;
5593                redo B;                !!!nack ('t231.1');
5594                  next B;
5595              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5596                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5597                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5598                    ## have an element in table scope
5599                ## As if </table>                  my $i;
5600                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5601                my $i;                    my $node = $self->{open_elements}->[$_];
5602                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5603                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5604                  if ($node->[1] eq 'table') {                      $i = $_;
5605                    $i = $_;                      last INSCOPE;
5606                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5607                  } elsif ({                      !!!cp ('t234');
5608                            table => 1, html => 1,                      last INSCOPE;
5609                           }->{$node->[1]}) {                    }
5610                    last INSCOPE;                  } # INSCOPE
5611                    unless (defined $i) {
5612                      !!!cp ('t235');
5613    ## TODO: The following is wrong.
5614                      !!!parse-error (type => 'unmatched end tag',
5615                                      text => $token->{type}, token => $token);
5616                      ## Ignore the token
5617                      !!!nack ('t236.1');
5618                      !!!next-token;
5619                      next B;
5620                  }                  }
5621                } # INSCOPE                  
5622                unless (defined $i) {                  ## Clear back to table row context
5623                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5624                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5625                  !!!next-token;                    !!!cp ('t236');
5626                  redo B;  ## ISSUE: Can this state be reached?
5627                }                    pop @{$self->{open_elements}};
                 
               ## 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5628                  }                  }
5629                } # INSCOPE                  
5630                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5631                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5632                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5633                  !!!next-token;                }
5634                  redo B;  
5635                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5636                    ## have an element in table scope
5637                ## Clear back to table row context                  my $i;
5638                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5639                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5640                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5641                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5642                        $i = $_;
5643                        last INSCOPE;
5644                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5645                        !!!cp ('t238');
5646                        last INSCOPE;
5647                      }
5648                    } # INSCOPE
5649                    unless (defined $i) {
5650                      !!!cp ('t239');
5651                      !!!parse-error (type => 'unmatched end tag',
5652                                      text => $token->{tag_name}, token => $token);
5653                      ## Ignore the token
5654                      !!!nack ('t239.1');
5655                      !!!next-token;
5656                      next B;
5657                    }
5658                    
5659                    ## Clear back to table body context
5660                    while (not ($self->{open_elements}->[-1]->[1]
5661                                    & TABLE_ROWS_SCOPING_EL)) {
5662                      !!!cp ('t240');
5663                      pop @{$self->{open_elements}};
5664                    }
5665                    
5666                    ## As if <{current node}>
5667                    ## have an element in table scope
5668                    ## true by definition
5669                    
5670                    ## Clear back to table body context
5671                    ## nop by definition
5672                    
5673                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5674                    $self->{insertion_mode} = IN_TABLE_IM;
5675                    ## reprocess in the "in table" insertion mode...
5676                }                }
5677    
5678                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5679                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5680                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5681                redo B;                ## is synced with it.
5682              } elsif ($token->{tag_name} eq 'table') {  
5683                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5684                my $i;                my $i;
5685                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5686                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5687                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5688                      !!!cp ('t241');
5689                    $i = $_;                    $i = $_;
5690                    last INSCOPE;                    last INSCOPE;
5691                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5692                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5693                    last INSCOPE;                    last INSCOPE;
5694                  }                  }
5695                } # INSCOPE                } # INSCOPE
5696                unless (defined $i) {                unless (defined $i) {
5697                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5698                    !!!parse-error (type => 'unmatched end tag',
5699                                    text => $token->{tag_name}, token => $token);
5700                  ## Ignore the token                  ## Ignore the token
5701                    !!!nack ('t243.1');
5702                  !!!next-token;                  !!!next-token;
5703                  redo B;                  next B;
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
5704                }                }
5705                    
5706                pop @{$self->{open_elements}}; # tr                splice @{$self->{open_elements}}, $i;
5707                $self->{insertion_mode} = 'in table body';                pop @{$open_tables};
5708                ## reprocess                
5709                redo B;                $self->_reset_insertion_mode;
5710                  
5711                  !!!next-token;
5712                  next B;
5713              } elsif ({              } elsif ({
5714                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5715                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5716                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5717                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5718                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5719                  my $node = $self->{open_elements}->[$_];                  my $i;
5720                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5721                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5722                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5723                  } elsif ({                      !!!cp ('t247');
5724                            table => 1, html => 1,                      $i = $_;
5725                           }->{$node->[1]}) {                      last INSCOPE;
5726                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5727                        !!!cp ('t248');
5728                        last INSCOPE;
5729                      }
5730                    } # INSCOPE
5731                      unless (defined $i) {
5732                        !!!cp ('t249');
5733                        !!!parse-error (type => 'unmatched end tag',
5734                                        text => $token->{tag_name}, token => $token);
5735                        ## Ignore the token
5736                        !!!nack ('t249.1');
5737                        !!!next-token;
5738                        next B;
5739                      }
5740                    
5741                    ## As if </tr>
5742                    ## have an element in table scope
5743                    my $i;
5744                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5745                      my $node = $self->{open_elements}->[$_];
5746                      if ($node->[1] & TABLE_ROW_EL) {
5747                        !!!cp ('t250');
5748                        $i = $_;
5749                        last INSCOPE;
5750                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5751                        !!!cp ('t251');
5752                        last INSCOPE;
5753                      }
5754                    } # INSCOPE
5755                      unless (defined $i) {
5756                        !!!cp ('t252');
5757                        !!!parse-error (type => 'unmatched end tag',
5758                                        text => 'tr', token => $token);
5759                        ## Ignore the token
5760                        !!!nack ('t252.1');
5761                        !!!next-token;
5762                        next B;
5763                      }
5764                    
5765                    ## Clear back to table row context
5766                    while (not ($self->{open_elements}->[-1]->[1]
5767                                    & TABLE_ROW_SCOPING_EL)) {
5768                      !!!cp ('t253');
5769    ## ISSUE: Can this case be reached?
5770                      pop @{$self->{open_elements}};
5771                  }                  }
5772                } # INSCOPE                  
5773                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5774                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5775                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5776                }                }
5777    
               ## As if </tr>  
5778                ## have an element in table scope                ## have an element in table scope
5779                my $i;                my $i;
5780                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5781                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5782                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5783                      !!!cp ('t254');
5784                    $i = $_;                    $i = $_;
5785                    last INSCOPE;                    last INSCOPE;
5786                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5787                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5788                    last INSCOPE;                    last INSCOPE;
5789                  }                  }
5790                } # INSCOPE                } # INSCOPE
5791                unless (defined $i) {                unless (defined $i) {
5792                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5793                    !!!parse-error (type => 'unmatched end tag',
5794                                    text => $token->{tag_name}, token => $token);
5795                  ## Ignore the token                  ## Ignore the token
5796                    !!!nack ('t256.1');
5797                  !!!next-token;                  !!!next-token;
5798                  redo B;                  next B;
5799                }                }
5800    
5801                ## Clear back to table row context                ## Clear back to table body context
5802                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5803                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5804                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5805                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5806                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5807                }                }
5808    
5809                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5810                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5811                ## reprocess                !!!nack ('t257.1');
5812                redo B;                !!!next-token;
5813                  next B;
5814              } elsif ({              } elsif ({
5815                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5816                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5817                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5818                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5819                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5820                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5821                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
5822                !!!next-token;                            text => $token->{tag_name}, token => $token);
5823                redo B;            ## Ignore the token
5824              } else {            !!!nack ('t258.1');
5825                #             !!!next-token;
5826              }            next B;
5827            } else {          } else {
5828              #            !!!cp ('t259');
5829            }            !!!parse-error (type => 'in table:/',
5830                              text => $token->{tag_name}, token => $token);
5831    
5832            ## As if in table            $insert = $insert_to_foster;
5833            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5834            $in_body->($insert_to_foster);          }
5835            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5836          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5837            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5838              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5839              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5840                          #
5841              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5842              !!!cp ('t259.2');
5843              #
5844            }
5845    
5846              !!!next-token;          ## Stop parsing
5847              redo B;          last B;
5848            } elsif ($token->{type} eq 'comment') {        } else {
5849              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
5850              my $comment = $self->{document}->create_comment ($token->{data});        }
5851              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5852              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
5853              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5854            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5855              if ({                unless (length $token->{data}) {
5856                   caption => 1, col => 1, colgroup => 1,                  !!!cp ('t260');
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
5857                  !!!next-token;                  !!!next-token;
5858                  redo B;                  next B;
5859                }                }
5860                }
5861                ## Close the cell              
5862                !!!back-token; # <?>              !!!cp ('t261');
5863                $token = {type => 'end tag', tag_name => $tn};              #
5864                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5865              } else {              if ($token->{tag_name} eq 'col') {
5866                  !!!cp ('t262');
5867                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5868                  pop @{$self->{open_elements}};
5869                  !!!ack ('t262.1');
5870                  !!!next-token;
5871                  next B;
5872                } else {
5873                  !!!cp ('t263');
5874                #                #
5875              }              }
5876            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5877              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5878                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5879                my $i;                  !!!cp ('t264');
5880                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
5881                  my $node = $self->{open_elements}->[$_];                                  text => 'colgroup', 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:'.$token->{tag_name});  
5882                  ## Ignore the token                  ## Ignore the token
5883                  !!!next-token;                  !!!next-token;
5884                  redo B;                  next B;
5885                }                } else {
5886                                  !!!cp ('t265');
5887                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5888                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5889                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5890                     td => ($token->{tag_name} eq 'th'),                  next B;            
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5891                }                }
5892                } elsif ($token->{tag_name} eq 'col') {
5893                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5894                  !!!parse-error (type => 'unmatched end tag',
5895                $clear_up_to_marker->();                                text => 'col', token => $token);
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5896                ## Ignore the token                ## Ignore the token
5897                !!!next-token;                !!!next-token;
5898                redo B;                next B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
5899              } else {              } else {
5900                #                !!!cp ('t267');
5901                  #
5902              }              }
5903          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5904            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5905                @{$self->{open_elements}} == 1) { # redundant, maybe
5906              !!!cp ('t270.2');
5907              ## Stop parsing.
5908              last B;
5909            } else {
5910              ## NOTE: As if </colgroup>.
5911              !!!cp ('t270.1');
5912              pop @{$self->{open_elements}}; # colgroup
5913              $self->{insertion_mode} = IN_TABLE_IM;
5914              ## Reprocess.
5915              next B;
5916            }
5917          } else {
5918            die "$0: $token->{type}: Unknown token type";
5919          }
5920    
5921              ## As if </colgroup>
5922              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5923                !!!cp ('t269');
5924    ## TODO: Wrong error type?
5925                !!!parse-error (type => 'unmatched end tag',
5926                                text => 'colgroup', token => $token);
5927                ## Ignore the token
5928                !!!nack ('t269.1');
5929                !!!next-token;
5930                next B;
5931            } else {            } else {
5932              #              !!!cp ('t270');
5933                pop @{$self->{open_elements}}; # colgroup
5934                $self->{insertion_mode} = IN_TABLE_IM;
5935                !!!ack-later;
5936                ## reprocess
5937                next B;
5938              }
5939        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5940          if ($token->{type} == CHARACTER_TOKEN) {
5941            !!!cp ('t271');
5942            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5943            !!!next-token;
5944            next B;
5945          } elsif ($token->{type} == START_TAG_TOKEN) {
5946            if ($token->{tag_name} eq 'option') {
5947              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5948                !!!cp ('t272');
5949                ## As if </option>
5950                pop @{$self->{open_elements}};
5951              } else {
5952                !!!cp ('t273');
5953            }            }
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($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 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
5954    
5955                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5956                !!!next-token;            !!!nack ('t273.1');
5957                redo B;            !!!next-token;
5958              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5959                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5960                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5961                  pop @{$self->{open_elements}};              !!!cp ('t274');
5962                }              ## As if </option>
5963                pop @{$self->{open_elements}};
5964              } else {
5965                !!!cp ('t275');
5966              }
5967    
5968                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5969                  ## As if </optgroup>              !!!cp ('t276');
5970                  pop @{$self->{open_elements}};              ## As if </optgroup>
5971                }              pop @{$self->{open_elements}};
5972              } else {
5973                !!!cp ('t277');
5974              }
5975    
5976                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5977                !!!next-token;            !!!nack ('t277.1');
5978                redo B;            !!!next-token;
5979              } elsif ($token->{tag_name} eq 'select') {            next B;
5980                !!!parse-error (type => 'not closed:select');          } elsif ({
5981                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
5982                ## have an element in table scope                   }->{$token->{tag_name}} or
5983                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5984                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
5985                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
5986                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
5987                    $i = $_;                     tr => 1, td => 1, th => 1,
5988                    last INSCOPE;                    }->{$token->{tag_name}})) {
5989                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
5990                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
5991                           }->{$node->[1]}) {                            token => $token);
5992                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5993                  }            ## as if there were </select> (otherwise).
5994                } # INSCOPE            ## have an element in table scope
5995                unless (defined $i) {            my $i;
5996                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5997                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
5998                  !!!next-token;              if ($node->[1] & SELECT_EL) {
5999                  redo B;                !!!cp ('t278');
6000                }                $i = $_;
6001                  last INSCOPE;
6002                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6003                  !!!cp ('t279');
6004                  last INSCOPE;
6005                }
6006              } # INSCOPE
6007              unless (defined $i) {
6008                !!!cp ('t280');
6009                !!!parse-error (type => 'unmatched end tag',
6010                                text => 'select', token => $token);
6011                ## Ignore the token
6012                !!!nack ('t280.1');
6013                !!!next-token;
6014                next B;
6015              }
6016                                
6017                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6018              splice @{$self->{open_elements}}, $i;
6019    
6020                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6021    
6022                !!!next-token;            if ($token->{tag_name} eq 'select') {
6023                redo B;              !!!nack ('t281.2');
6024              } else {              !!!next-token;
6025                #              next B;
6026              } else {
6027                !!!cp ('t281.1');
6028                !!!ack-later;
6029                ## Reprocess the token.
6030                next B;
6031              }
6032            } else {
6033              !!!cp ('t282');
6034              !!!parse-error (type => 'in select',
6035                              text => $token->{tag_name}, token => $token);
6036              ## Ignore the token
6037              !!!nack ('t282.1');
6038              !!!next-token;
6039              next B;
6040            }
6041          } elsif ($token->{type} == END_TAG_TOKEN) {
6042            if ($token->{tag_name} eq 'optgroup') {
6043              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6044                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6045                !!!cp ('t283');
6046                ## As if </option>
6047                splice @{$self->{open_elements}}, -2;
6048              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6049                !!!cp ('t284');
6050                pop @{$self->{open_elements}};
6051              } else {
6052                !!!cp ('t285');
6053                !!!parse-error (type => 'unmatched end tag',
6054                                text => $token->{tag_name}, token => $token);
6055                ## Ignore the token
6056              }
6057              !!!nack ('t285.1');
6058              !!!next-token;
6059              next B;
6060            } elsif ($token->{tag_name} eq 'option') {
6061              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6062                !!!cp ('t286');
6063                pop @{$self->{open_elements}};
6064              } else {
6065                !!!cp ('t287');
6066                !!!parse-error (type => 'unmatched end tag',
6067                                text => $token->{tag_name}, token => $token);
6068                ## Ignore the token
6069              }
6070              !!!nack ('t287.1');
6071              !!!next-token;
6072              next B;
6073            } elsif ($token->{tag_name} eq 'select') {
6074              ## have an element in table scope
6075              my $i;
6076              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6077                my $node = $self->{open_elements}->[$_];
6078                if ($node->[1] & SELECT_EL) {
6079                  !!!cp ('t288');
6080                  $i = $_;
6081                  last INSCOPE;
6082                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6083                  !!!cp ('t289');
6084                  last INSCOPE;
6085              }              }
6086            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6087              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6088                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6089                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6090                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6091                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6092                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6093                  pop @{$self->{open_elements}};              !!!next-token;
6094                } else {              next B;
6095                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            }
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## 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;  
               }  
6096                                
6097                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6098              splice @{$self->{open_elements}}, $i;
6099    
6100                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6101    
6102                !!!next-token;            !!!nack ('t291.1');
6103                redo B;            !!!next-token;
6104              } elsif ({            next B;
6105                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6106                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6107                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6108                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6109                                   }->{$token->{tag_name}}) {
6110                ## have an element in table scope  ## TODO: The following is wrong?
6111                my $i;            !!!parse-error (type => 'unmatched end tag',
6112                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                            text => $token->{tag_name}, token => $token);
                 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) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
6113                                
6114                ## As if </select>            ## have an element in table scope
6115                ## have an element in table scope            my $i;
6116                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6117                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6118                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6119                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6120                    $i = $_;                $i = $_;
6121                    last INSCOPE;                last INSCOPE;
6122                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6123                            table => 1, html => 1,                !!!cp ('t293');
6124                           }->{$node->[1]}) {                last INSCOPE;
6125                    last INSCOPE;              }
6126                  }            } # INSCOPE
6127                } # INSCOPE            unless (defined $i) {
6128                unless (defined $i) {              !!!cp ('t294');
6129                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6130                  ## Ignore the </select> token              !!!nack ('t294.1');
6131                  !!!next-token; ## TODO: ok?              !!!next-token;
6132                  redo B;              next B;
6133                }            }
6134                                
6135                splice @{$self->{open_elements}}, $i;            ## As if </select>
6136              ## have an element in table scope
6137                $self->_reset_insertion_mode;            undef $i;
6138              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6139                ## reprocess              my $node = $self->{open_elements}->[$_];
6140                redo B;              if ($node->[1] & SELECT_EL) {
6141              } else {                !!!cp ('t295');
6142                #                $i = $_;
6143                  last INSCOPE;
6144                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6145    ## ISSUE: Can this state be reached?
6146                  !!!cp ('t296');
6147                  last INSCOPE;
6148              }              }
6149            } else {            } # INSCOPE
6150              #            unless (defined $i) {
6151                !!!cp ('t297');
6152    ## TODO: The following error type is correct?
6153                !!!parse-error (type => 'unmatched end tag',
6154                                text => 'select', token => $token);
6155                ## Ignore the </select> token
6156                !!!nack ('t297.1');
6157                !!!next-token; ## TODO: ok?
6158                next B;
6159            }            }
6160                  
6161              !!!cp ('t298');
6162              splice @{$self->{open_elements}}, $i;
6163    
6164              $self->_reset_insertion_mode;
6165    
6166            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6167              ## reprocess
6168              next B;
6169            } else {
6170              !!!cp ('t299');
6171              !!!parse-error (type => 'in select:/',
6172                              text => $token->{tag_name}, token => $token);
6173            ## Ignore the token            ## Ignore the token
6174              !!!nack ('t299.3');
6175            !!!next-token;            !!!next-token;
6176            redo B;            next B;
6177          } elsif ($self->{insertion_mode} eq 'after body') {          }
6178            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6179              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6180                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6181                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6182                            !!!parse-error (type => 'in body:#eof', token => $token);
6183                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6184              !!!cp ('t299.2');
6185            }
6186    
6187                unless (length $token->{data}) {          ## Stop parsing.
6188                  !!!next-token;          last B;
6189                  redo B;        } else {
6190                }          die "$0: $token->{type}: Unknown token type";
6191              }        }
6192                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6193              #        if ($token->{type} == CHARACTER_TOKEN) {
6194              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6195            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6196              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6197              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6198                  
6199              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6200              
6201              unless (length $token->{data}) {
6202                !!!cp ('t300');
6203              !!!next-token;              !!!next-token;
6204              redo B;              next B;
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
6205            }            }
6206            }
6207            
6208            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6209              !!!cp ('t301');
6210              !!!parse-error (type => 'after html:#text', token => $token);
6211    
6212            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6213            ## reprocess          } else {
6214            redo B;            !!!cp ('t302');
6215          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6216            if ($token->{type} eq 'character') {          
6217              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6218                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6219    
6220                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6221                  !!!next-token;          ## reprocess
6222                  redo B;          next B;
6223                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6224              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6225              !!!cp ('t303');
6226              !!!parse-error (type => 'after html',
6227                              text => $token->{tag_name}, token => $token);
6228              
6229              ## Reprocess in the "after body" insertion mode.
6230            } else {
6231              !!!cp ('t304');
6232            }
6233    
6234              #          ## "after body" insertion mode
6235            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6236              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6237              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6238            $self->{insertion_mode} = IN_BODY_IM;
6239            !!!ack-later;
6240            ## reprocess
6241            next B;
6242          } elsif ($token->{type} == END_TAG_TOKEN) {
6243            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6244              !!!cp ('t305');
6245              !!!parse-error (type => 'after html:/',
6246                              text => $token->{tag_name}, token => $token);
6247              
6248              $self->{insertion_mode} = AFTER_BODY_IM;
6249              ## Reprocess in the "after body" insertion mode.
6250            } else {
6251              !!!cp ('t306');
6252            }
6253    
6254            ## "after body" insertion mode
6255            if ($token->{tag_name} eq 'html') {
6256              if (defined $self->{inner_html_node}) {
6257                !!!cp ('t307');
6258                !!!parse-error (type => 'unmatched end tag',
6259                                text => 'html', token => $token);
6260                ## Ignore the token
6261              !!!next-token;              !!!next-token;
6262              redo B;              next B;
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
6263            } else {            } else {
6264              #              !!!cp ('t308');
6265                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6266                !!!next-token;
6267                next B;
6268            }            }
6269            } else {
6270              !!!cp ('t309');
6271              !!!parse-error (type => 'after body:/',
6272                              text => $token->{tag_name}, token => $token);
6273    
6274              $self->{insertion_mode} = IN_BODY_IM;
6275              ## reprocess
6276              next B;
6277            }
6278          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6279            !!!cp ('t309.2');
6280            ## Stop parsing
6281            last B;
6282          } else {
6283            die "$0: $token->{type}: Unknown token type";
6284          }
6285        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6286          if ($token->{type} == CHARACTER_TOKEN) {
6287            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6288              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6289                        
6290            if (defined $token->{tag_name}) {            unless (length $token->{data}) {
6291              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t310');
6292                !!!next-token;
6293                next B;
6294              }
6295            }
6296            
6297            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6298              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6299                !!!cp ('t311');
6300                !!!parse-error (type => 'in frameset:#text', token => $token);
6301              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6302                !!!cp ('t312');
6303                !!!parse-error (type => 'after frameset:#text', token => $token);
6304              } else { # "after after frameset"
6305                !!!cp ('t313');
6306                !!!parse-error (type => 'after html:#text', token => $token);
6307              }
6308              
6309              ## Ignore the token.
6310              if (length $token->{data}) {
6311                !!!cp ('t314');
6312                ## reprocess the rest of characters
6313            } else {            } else {
6314              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
6315                !!!next-token;
6316              }
6317              next B;
6318            }
6319            
6320            die qq[$0: Character "$token->{data}"];
6321          } elsif ($token->{type} == START_TAG_TOKEN) {
6322            if ($token->{tag_name} eq 'frameset' and
6323                $self->{insertion_mode} == IN_FRAMESET_IM) {
6324              !!!cp ('t318');
6325              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6326              !!!nack ('t318.1');
6327              !!!next-token;
6328              next B;
6329            } elsif ($token->{tag_name} eq 'frame' and
6330                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6331              !!!cp ('t319');
6332              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6333              pop @{$self->{open_elements}};
6334              !!!ack ('t319.1');
6335              !!!next-token;
6336              next B;
6337            } elsif ($token->{tag_name} eq 'noframes') {
6338              !!!cp ('t320');
6339              ## NOTE: As if in head.
6340              $parse_rcdata->(CDATA_CONTENT_MODEL);
6341              next B;
6342    
6343              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6344              ## has no parse error.
6345            } else {
6346              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6347                !!!cp ('t321');
6348                !!!parse-error (type => 'in frameset',
6349                                text => $token->{tag_name}, token => $token);
6350              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6351                !!!cp ('t322');
6352                !!!parse-error (type => 'after frameset',
6353                                text => $token->{tag_name}, token => $token);
6354              } else { # "after after frameset"
6355                !!!cp ('t322.2');
6356                !!!parse-error (type => 'after after frameset',
6357                                text => $token->{tag_name}, token => $token);
6358            }            }
6359            ## Ignore the token            ## Ignore the token
6360              !!!nack ('t322.1');
6361            !!!next-token;            !!!next-token;
6362            redo B;            next B;
6363          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6364            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6365              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'frameset' and
6366                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{insertion_mode} == IN_FRAMESET_IM) {
6367              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6368                  @{$self->{open_elements}} == 1) {
6369                !!!cp ('t325');
6370                !!!parse-error (type => 'unmatched end tag',
6371                                text => $token->{tag_name}, token => $token);
6372                ## Ignore the token
6373                !!!next-token;
6374              } else {
6375                !!!cp ('t326');
6376                pop @{$self->{open_elements}};
6377                !!!next-token;
6378              }
6379    
6380                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
6381                  !!!next-token;                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6382                  redo B;              !!!cp ('t327');
6383                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6384              } else {
6385                !!!cp ('t328');
6386              }
6387              next B;
6388            } elsif ($token->{tag_name} eq 'html' and
6389                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6390              !!!cp ('t329');
6391              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6392              !!!next-token;
6393              next B;
6394            } else {
6395              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6396                !!!cp ('t330');
6397                !!!parse-error (type => 'in frameset:/',
6398                                text => $token->{tag_name}, token => $token);
6399              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6400                !!!cp ('t330.1');
6401                !!!parse-error (type => 'after frameset:/',
6402                                text => $token->{tag_name}, token => $token);
6403              } else { # "after after html"
6404                !!!cp ('t331');
6405                !!!parse-error (type => 'after after frameset:/',
6406                                text => $token->{tag_name}, token => $token);
6407              }
6408              ## Ignore the token
6409              !!!next-token;
6410              next B;
6411            }
6412          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6413            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6414                    @{$self->{open_elements}} == 1) { # redundant, maybe
6415              !!!cp ('t331.1');
6416              !!!parse-error (type => 'in body:#eof', token => $token);
6417            } else {
6418              !!!cp ('t331.2');
6419            }
6420            
6421            ## Stop parsing
6422            last B;
6423          } else {
6424            die "$0: $token->{type}: Unknown token type";
6425          }
6426    
6427          ## ISSUE: An issue in spec here
6428        } else {
6429          die "$0: $self->{insertion_mode}: Unknown insertion mode";
6430        }
6431    
6432        ## "in body" insertion mode
6433        if ($token->{type} == START_TAG_TOKEN) {
6434          if ($token->{tag_name} eq 'script') {
6435            !!!cp ('t332');
6436            ## NOTE: This is an "as if in head" code clone
6437            $script_start_tag->();
6438            next B;
6439          } elsif ($token->{tag_name} eq 'style') {
6440            !!!cp ('t333');
6441            ## NOTE: This is an "as if in head" code clone
6442            $parse_rcdata->(CDATA_CONTENT_MODEL);
6443            next B;
6444          } elsif ({
6445                    base => 1, link => 1,
6446                   }->{$token->{tag_name}}) {
6447            !!!cp ('t334');
6448            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6449            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6450            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6451            !!!ack ('t334.1');
6452            !!!next-token;
6453            next B;
6454          } elsif ($token->{tag_name} eq 'meta') {
6455            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6456            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6457            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6458    
6459            unless ($self->{confident}) {
6460              if ($token->{attributes}->{charset}) {
6461                !!!cp ('t335');
6462                ## NOTE: Whether the encoding is supported or not is handled
6463                ## in the {change_encoding} callback.
6464                $self->{change_encoding}
6465                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6466                
6467                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6468                    ->set_user_data (manakai_has_reference =>
6469                                         $token->{attributes}->{charset}
6470                                             ->{has_reference});
6471              } elsif ($token->{attributes}->{content}) {
6472                if ($token->{attributes}->{content}->{value}
6473                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6474                        [\x09-\x0D\x20]*=
6475                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6476                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6477                  !!!cp ('t336');
6478                  ## NOTE: Whether the encoding is supported or not is handled
6479                  ## in the {change_encoding} callback.
6480                  $self->{change_encoding}
6481                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6482                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6483                      ->set_user_data (manakai_has_reference =>
6484                                           $token->{attributes}->{content}
6485                                                 ->{has_reference});
6486              }              }
6487              }
6488            } else {
6489              if ($token->{attributes}->{charset}) {
6490                !!!cp ('t337');
6491                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6492                    ->set_user_data (manakai_has_reference =>
6493                                         $token->{attributes}->{charset}
6494                                             ->{has_reference});
6495              }
6496              if ($token->{attributes}->{content}) {
6497                !!!cp ('t338');
6498                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6499                    ->set_user_data (manakai_has_reference =>
6500                                         $token->{attributes}->{content}
6501                                             ->{has_reference});
6502              }
6503            }
6504    
6505              #          !!!ack ('t338.1');
6506            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6507              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6508              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6509              !!!next-token;          !!!cp ('t341');
6510              redo B;          ## NOTE: This is an "as if in head" code clone
6511            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6512              if ($token->{tag_name} eq 'noframes') {          next B;
6513                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6514                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6515              } else {                
6516                #          if (@{$self->{open_elements}} == 1 or
6517                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6518              !!!cp ('t342');
6519              ## Ignore the token
6520            } else {
6521              my $body_el = $self->{open_elements}->[1]->[0];
6522              for my $attr_name (keys %{$token->{attributes}}) {
6523                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6524                  !!!cp ('t343');
6525                  $body_el->set_attribute_ns
6526                    (undef, [undef, $attr_name],
6527                     $token->{attributes}->{$attr_name}->{value});
6528              }              }
6529            } elsif ($token->{type} eq 'end tag') {            }
6530              if ($token->{tag_name} eq 'html') {          }
6531                $phase = 'trailing end';          !!!nack ('t343.1');
6532            !!!next-token;
6533            next B;
6534          } elsif ({
6535                    address => 1, blockquote => 1, center => 1, dir => 1,
6536                    div => 1, dl => 1, fieldset => 1,
6537                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6538                    menu => 1, ol => 1, p => 1, ul => 1,
6539                    pre => 1, listing => 1,
6540                    form => 1,
6541                    table => 1,
6542                    hr => 1,
6543                   }->{$token->{tag_name}}) {
6544            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6545              !!!cp ('t350');
6546              !!!parse-error (type => 'in form:form', token => $token);
6547              ## Ignore the token
6548              !!!nack ('t350.1');
6549              !!!next-token;
6550              next B;
6551            }
6552    
6553            ## has a p element in scope
6554            INSCOPE: for (reverse @{$self->{open_elements}}) {
6555              if ($_->[1] & P_EL) {
6556                !!!cp ('t344');
6557                !!!back-token; # <form>
6558                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6559                          line => $token->{line}, column => $token->{column}};
6560                next B;
6561              } elsif ($_->[1] & SCOPING_EL) {
6562                !!!cp ('t345');
6563                last INSCOPE;
6564              }
6565            } # INSCOPE
6566              
6567            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6568            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6569              !!!nack ('t346.1');
6570              !!!next-token;
6571              if ($token->{type} == CHARACTER_TOKEN) {
6572                $token->{data} =~ s/^\x0A//;
6573                unless (length $token->{data}) {
6574                  !!!cp ('t346');
6575                !!!next-token;                !!!next-token;
               redo B;  
6576              } else {              } else {
6577                #                !!!cp ('t349');
6578              }              }
6579            } else {            } else {
6580              #              !!!cp ('t348');
6581              }
6582            } elsif ($token->{tag_name} eq 'form') {
6583              !!!cp ('t347.1');
6584              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6585    
6586              !!!nack ('t347.2');
6587              !!!next-token;
6588            } elsif ($token->{tag_name} eq 'table') {
6589              !!!cp ('t382');
6590              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6591              
6592              $self->{insertion_mode} = IN_TABLE_IM;
6593    
6594              !!!nack ('t382.1');
6595              !!!next-token;
6596            } elsif ($token->{tag_name} eq 'hr') {
6597              !!!cp ('t386');
6598              pop @{$self->{open_elements}};
6599            
6600              !!!nack ('t386.1');
6601              !!!next-token;
6602            } else {
6603              !!!nack ('t347.1');
6604              !!!next-token;
6605            }
6606            next B;
6607          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6608            ## has a p element in scope
6609            INSCOPE: for (reverse @{$self->{open_elements}}) {
6610              if ($_->[1] & P_EL) {
6611                !!!cp ('t353');
6612                !!!back-token; # <x>
6613                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6614                          line => $token->{line}, column => $token->{column}};
6615                next B;
6616              } elsif ($_->[1] & SCOPING_EL) {
6617                !!!cp ('t354');
6618                last INSCOPE;
6619            }            }
6620            } # INSCOPE
6621                        
6622            if (defined $token->{tag_name}) {          ## Step 1
6623              !!!parse-error (type => 'after frameset:'.$token->{tag_name});          my $i = -1;
6624            my $node = $self->{open_elements}->[$i];
6625            my $li_or_dtdd = {li => {li => 1},
6626                              dt => {dt => 1, dd => 1},
6627                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6628            LI: {
6629              ## Step 2
6630              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6631                if ($i != -1) {
6632                  !!!cp ('t355');
6633                  !!!parse-error (type => 'not closed',
6634                                  text => $self->{open_elements}->[-1]->[0]
6635                                      ->manakai_local_name,
6636                                  token => $token);
6637                } else {
6638                  !!!cp ('t356');
6639                }
6640                splice @{$self->{open_elements}}, $i;
6641                last LI;
6642            } else {            } else {
6643              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6644              }
6645              
6646              ## Step 3
6647              if (not ($node->[1] & FORMATTING_EL) and
6648                  #not $phrasing_category->{$node->[1]} and
6649                  ($node->[1] & SPECIAL_EL or
6650                   $node->[1] & SCOPING_EL) and
6651                  not ($node->[1] & ADDRESS_EL) and
6652                  not ($node->[1] & DIV_EL)) {
6653                !!!cp ('t358');
6654                last LI;
6655              }
6656              
6657              !!!cp ('t359');
6658              ## Step 4
6659              $i--;
6660              $node = $self->{open_elements}->[$i];
6661              redo LI;
6662            } # LI
6663              
6664            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6665            !!!nack ('t359.1');
6666            !!!next-token;
6667            next B;
6668          } elsif ($token->{tag_name} eq 'plaintext') {
6669            ## has a p element in scope
6670            INSCOPE: for (reverse @{$self->{open_elements}}) {
6671              if ($_->[1] & P_EL) {
6672                !!!cp ('t367');
6673                !!!back-token; # <plaintext>
6674                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6675                          line => $token->{line}, column => $token->{column}};
6676                next B;
6677              } elsif ($_->[1] & SCOPING_EL) {
6678                !!!cp ('t368');
6679                last INSCOPE;
6680            }            }
6681            } # INSCOPE
6682              
6683            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6684              
6685            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6686              
6687            !!!nack ('t368.1');
6688            !!!next-token;
6689            next B;
6690          } elsif ($token->{tag_name} eq 'a') {
6691            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6692              my $node = $active_formatting_elements->[$i];
6693              if ($node->[1] & A_EL) {
6694                !!!cp ('t371');
6695                !!!parse-error (type => 'in a:a', token => $token);
6696                
6697                !!!back-token; # <a>
6698                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6699                          line => $token->{line}, column => $token->{column}};
6700                $formatting_end_tag->($token);
6701                
6702                AFE2: for (reverse 0..$#$active_formatting_elements) {
6703                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6704                    !!!cp ('t372');
6705                    splice @$active_formatting_elements, $_, 1;
6706                    last AFE2;
6707                  }
6708                } # AFE2
6709                OE: for (reverse 0..$#{$self->{open_elements}}) {
6710                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6711                    !!!cp ('t373');
6712                    splice @{$self->{open_elements}}, $_, 1;
6713                    last OE;
6714                  }
6715                } # OE
6716                last AFE;
6717              } elsif ($node->[0] eq '#marker') {
6718                !!!cp ('t374');
6719                last AFE;
6720              }
6721            } # AFE
6722              
6723            $reconstruct_active_formatting_elements->($insert_to_current);
6724    
6725            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6726            push @$active_formatting_elements, $self->{open_elements}->[-1];
6727    
6728            !!!nack ('t374.1');
6729            !!!next-token;
6730            next B;
6731          } elsif ($token->{tag_name} eq 'nobr') {
6732            $reconstruct_active_formatting_elements->($insert_to_current);
6733    
6734            ## has a |nobr| element in scope
6735            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6736              my $node = $self->{open_elements}->[$_];
6737              if ($node->[1] & NOBR_EL) {
6738                !!!cp ('t376');
6739                !!!parse-error (type => 'in nobr:nobr', token => $token);
6740                !!!back-token; # <nobr>
6741                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6742                          line => $token->{line}, column => $token->{column}};
6743                next B;
6744              } elsif ($node->[1] & SCOPING_EL) {
6745                !!!cp ('t377');
6746                last INSCOPE;
6747              }
6748            } # INSCOPE
6749            
6750            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6751            push @$active_formatting_elements, $self->{open_elements}->[-1];
6752            
6753            !!!nack ('t377.1');
6754            !!!next-token;
6755            next B;
6756          } elsif ($token->{tag_name} eq 'button') {
6757            ## has a button element in scope
6758            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6759              my $node = $self->{open_elements}->[$_];
6760              if ($node->[1] & BUTTON_EL) {
6761                !!!cp ('t378');
6762                !!!parse-error (type => 'in button:button', token => $token);
6763                !!!back-token; # <button>
6764                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6765                          line => $token->{line}, column => $token->{column}};
6766                next B;
6767              } elsif ($node->[1] & SCOPING_EL) {
6768                !!!cp ('t379');
6769                last INSCOPE;
6770              }
6771            } # INSCOPE
6772              
6773            $reconstruct_active_formatting_elements->($insert_to_current);
6774              
6775            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6776    
6777            ## TODO: associate with $self->{form_element} if defined
6778    
6779            push @$active_formatting_elements, ['#marker', ''];
6780    
6781            !!!nack ('t379.1');
6782            !!!next-token;
6783            next B;
6784          } elsif ({
6785                    xmp => 1,
6786                    iframe => 1,
6787                    noembed => 1,
6788                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6789                    noscript => 0, ## TODO: 1 if scripting is enabled
6790                   }->{$token->{tag_name}}) {
6791            if ($token->{tag_name} eq 'xmp') {
6792              !!!cp ('t381');
6793              $reconstruct_active_formatting_elements->($insert_to_current);
6794            } else {
6795              !!!cp ('t399');
6796            }
6797            ## NOTE: There is an "as if in body" code clone.
6798            $parse_rcdata->(CDATA_CONTENT_MODEL);
6799            next B;
6800          } elsif ($token->{tag_name} eq 'isindex') {
6801            !!!parse-error (type => 'isindex', token => $token);
6802            
6803            if (defined $self->{form_element}) {
6804              !!!cp ('t389');
6805            ## Ignore the token            ## Ignore the token
6806              !!!nack ('t389'); ## NOTE: Not acknowledged.
6807            !!!next-token;            !!!next-token;
6808            redo B;            next B;
6809            } else {
6810              !!!ack ('t391.1');
6811    
6812            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
6813              my $form_attrs;
6814              $form_attrs->{action} = $at->{action} if $at->{action};
6815              my $prompt_attr = $at->{prompt};
6816              $at->{name} = {name => 'name', value => 'isindex'};
6817              delete $at->{action};
6818              delete $at->{prompt};
6819              my @tokens = (
6820                            {type => START_TAG_TOKEN, tag_name => 'form',
6821                             attributes => $form_attrs,
6822                             line => $token->{line}, column => $token->{column}},
6823                            {type => START_TAG_TOKEN, tag_name => 'hr',
6824                             line => $token->{line}, column => $token->{column}},
6825                            {type => START_TAG_TOKEN, tag_name => 'p',
6826                             line => $token->{line}, column => $token->{column}},
6827                            {type => START_TAG_TOKEN, tag_name => 'label',
6828                             line => $token->{line}, column => $token->{column}},
6829                           );
6830              if ($prompt_attr) {
6831                !!!cp ('t390');
6832                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6833                               #line => $token->{line}, column => $token->{column},
6834                              };
6835              } else {
6836                !!!cp ('t391');
6837                push @tokens, {type => CHARACTER_TOKEN,
6838                               data => 'This is a searchable index. Insert your search keywords here: ',
6839                               #line => $token->{line}, column => $token->{column},
6840                              }; # SHOULD
6841                ## TODO: make this configurable
6842              }
6843              push @tokens,
6844                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6845                             line => $token->{line}, column => $token->{column}},
6846                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6847                            {type => END_TAG_TOKEN, tag_name => 'label',
6848                             line => $token->{line}, column => $token->{column}},
6849                            {type => END_TAG_TOKEN, tag_name => 'p',
6850                             line => $token->{line}, column => $token->{column}},
6851                            {type => START_TAG_TOKEN, tag_name => 'hr',
6852                             line => $token->{line}, column => $token->{column}},
6853                            {type => END_TAG_TOKEN, tag_name => 'form',
6854                             line => $token->{line}, column => $token->{column}};
6855              !!!back-token (@tokens);
6856              !!!next-token;
6857              next B;
6858            }
6859          } elsif ($token->{tag_name} eq 'textarea') {
6860            my $tag_name = $token->{tag_name};
6861            my $el;
6862            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6863            
6864            ## TODO: $self->{form_element} if defined
6865            $self->{content_model} = RCDATA_CONTENT_MODEL;
6866            delete $self->{escape}; # MUST
6867            
6868            $insert->($el);
6869            
6870            my $text = '';
6871            !!!nack ('t392.1');
6872            !!!next-token;
6873            if ($token->{type} == CHARACTER_TOKEN) {
6874              $token->{data} =~ s/^\x0A//;
6875              unless (length $token->{data}) {
6876                !!!cp ('t392');
6877                !!!next-token;
6878              } else {
6879                !!!cp ('t393');
6880              }
6881          } else {          } else {
6882            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
6883            }
6884            while ($token->{type} == CHARACTER_TOKEN) {
6885              !!!cp ('t395');
6886              $text .= $token->{data};
6887              !!!next-token;
6888            }
6889            if (length $text) {
6890              !!!cp ('t396');
6891              $el->manakai_append_text ($text);
6892            }
6893            
6894            $self->{content_model} = PCDATA_CONTENT_MODEL;
6895            
6896            if ($token->{type} == END_TAG_TOKEN and
6897                $token->{tag_name} eq $tag_name) {
6898              !!!cp ('t397');
6899              ## Ignore the token
6900            } else {
6901              !!!cp ('t398');
6902              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6903          }          }
       }  
     } 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  
6904          !!!next-token;          !!!next-token;
6905          redo B;          next B;
6906        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
6907          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
6908          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
6909            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6910              my $node = $self->{open_elements}->[$_];
6911              if ($node->[1] & RUBY_EL) {
6912                !!!cp ('t398.1');
6913                ## generate implied end tags
6914                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6915                  !!!cp ('t398.2');
6916                  pop @{$self->{open_elements}};
6917                }
6918                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6919                  !!!cp ('t398.3');
6920                  !!!parse-error (type => 'not closed',
6921                                  text => $self->{open_elements}->[-1]->[0]
6922                                      ->manakai_local_name,
6923                                  token => $token);
6924                  pop @{$self->{open_elements}}
6925                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6926                }
6927                last INSCOPE;
6928              } elsif ($node->[1] & SCOPING_EL) {
6929                !!!cp ('t398.4');
6930                last INSCOPE;
6931              }
6932            } # INSCOPE
6933    
6934            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6935    
6936            !!!nack ('t398.5');
6937          !!!next-token;          !!!next-token;
6938          redo B;          redo B;
6939        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
6940          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
6941            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
6942            ## As if in the main phase.  
6943            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6944            ## just before the phase has been changed to the trailing  
6945            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6946            $reconstruct_active_formatting_elements->($insert_to_current)  
6947              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
6948            
6949            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6950            
6951            if ($self->{self_closing}) {
6952              pop @{$self->{open_elements}};
6953              !!!ack ('t398.1');
6954            } else {
6955              !!!cp ('t398.2');
6956              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6957              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6958              ## mode, "in body" (not "in foreign content") secondary insertion
6959              ## mode, maybe.
6960            }
6961    
6962            !!!next-token;
6963            next B;
6964          } elsif ({
6965                    caption => 1, col => 1, colgroup => 1, frame => 1,
6966                    frameset => 1, head => 1, option => 1, optgroup => 1,
6967                    tbody => 1, td => 1, tfoot => 1, th => 1,
6968                    thead => 1, tr => 1,
6969                   }->{$token->{tag_name}}) {
6970            !!!cp ('t401');
6971            !!!parse-error (type => 'in body',
6972                            text => $token->{tag_name}, token => $token);
6973            ## Ignore the token
6974            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6975            !!!next-token;
6976            next B;
6977            
6978            ## ISSUE: An issue on HTML5 new elements in the spec.
6979          } else {
6980            if ($token->{tag_name} eq 'image') {
6981              !!!cp ('t384');
6982              !!!parse-error (type => 'image', token => $token);
6983              $token->{tag_name} = 'img';
6984            } else {
6985              !!!cp ('t385');
6986            }
6987    
6988            ## NOTE: There is an "as if <br>" code clone.
6989            $reconstruct_active_formatting_elements->($insert_to_current);
6990            
6991            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6992    
6993            if ({
6994                 applet => 1, marquee => 1, object => 1,
6995                }->{$token->{tag_name}}) {
6996              !!!cp ('t380');
6997              push @$active_formatting_elements, ['#marker', ''];
6998              !!!nack ('t380.1');
6999            } elsif ({
7000                      b => 1, big => 1, em => 1, font => 1, i => 1,
7001                      s => 1, small => 1, strile => 1,
7002                      strong => 1, tt => 1, u => 1,
7003                     }->{$token->{tag_name}}) {
7004              !!!cp ('t375');
7005              push @$active_formatting_elements, $self->{open_elements}->[-1];
7006              !!!nack ('t375.1');
7007            } elsif ($token->{tag_name} eq 'input') {
7008              !!!cp ('t388');
7009              ## TODO: associate with $self->{form_element} if defined
7010              pop @{$self->{open_elements}};
7011              !!!ack ('t388.2');
7012            } elsif ({
7013                      area => 1, basefont => 1, bgsound => 1, br => 1,
7014                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7015                      #image => 1,
7016                     }->{$token->{tag_name}}) {
7017              !!!cp ('t388.1');
7018              pop @{$self->{open_elements}};
7019              !!!ack ('t388.3');
7020            } elsif ($token->{tag_name} eq 'select') {
7021              ## TODO: associate with $self->{form_element} if defined
7022            
7023              if ($self->{insertion_mode} & TABLE_IMS or
7024                  $self->{insertion_mode} & BODY_TABLE_IMS or
7025                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7026                !!!cp ('t400.1');
7027                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7028              } else {
7029                !!!cp ('t400.2');
7030                $self->{insertion_mode} = IN_SELECT_IM;
7031              }
7032              !!!nack ('t400.3');
7033            } else {
7034              !!!nack ('t402');
7035            }
7036            
7037            !!!next-token;
7038            next B;
7039          }
7040        } elsif ($token->{type} == END_TAG_TOKEN) {
7041          if ($token->{tag_name} eq 'body') {
7042            ## has a |body| element in scope
7043            my $i;
7044            INSCOPE: {
7045              for (reverse @{$self->{open_elements}}) {
7046                if ($_->[1] & BODY_EL) {
7047                  !!!cp ('t405');
7048                  $i = $_;
7049                  last INSCOPE;
7050                } elsif ($_->[1] & SCOPING_EL) {
7051                  !!!cp ('t405.1');
7052                  last;
7053                }
7054              }
7055    
7056              !!!parse-error (type => 'start tag not allowed',
7057                              text => $token->{tag_name}, token => $token);
7058              ## NOTE: Ignore the token.
7059              !!!next-token;
7060              next B;
7061            } # INSCOPE
7062    
7063            for (@{$self->{open_elements}}) {
7064              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7065                !!!cp ('t403');
7066                !!!parse-error (type => 'not closed',
7067                                text => $_->[0]->manakai_local_name,
7068                                token => $token);
7069                last;
7070              } else {
7071                !!!cp ('t404');
7072              }
7073            }
7074    
7075            $self->{insertion_mode} = AFTER_BODY_IM;
7076            !!!next-token;
7077            next B;
7078          } elsif ($token->{tag_name} eq 'html') {
7079            ## TODO: Update this code.  It seems that the code below is not
7080            ## up-to-date, though it has same effect as speced.
7081            if (@{$self->{open_elements}} > 1 and
7082                $self->{open_elements}->[1]->[1] & BODY_EL) {
7083              ## ISSUE: There is an issue in the spec.
7084              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7085                !!!cp ('t406');
7086                !!!parse-error (type => 'not closed',
7087                                text => $self->{open_elements}->[1]->[0]
7088                                    ->manakai_local_name,
7089                                token => $token);
7090              } else {
7091                !!!cp ('t407');
7092              }
7093              $self->{insertion_mode} = AFTER_BODY_IM;
7094              ## reprocess
7095              next B;
7096            } else {
7097              !!!cp ('t408');
7098              !!!parse-error (type => 'unmatched end tag',
7099                              text => $token->{tag_name}, token => $token);
7100              ## Ignore the token
7101              !!!next-token;
7102              next B;
7103            }
7104          } elsif ({
7105                    address => 1, blockquote => 1, center => 1, dir => 1,
7106                    div => 1, dl => 1, fieldset => 1, listing => 1,
7107                    menu => 1, ol => 1, pre => 1, ul => 1,
7108                    dd => 1, dt => 1, li => 1,
7109                    applet => 1, button => 1, marquee => 1, object => 1,
7110                   }->{$token->{tag_name}}) {
7111            ## has an element in scope
7112            my $i;
7113            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7114              my $node = $self->{open_elements}->[$_];
7115              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7116                !!!cp ('t410');
7117                $i = $_;
7118                last INSCOPE;
7119              } elsif ($node->[1] & SCOPING_EL) {
7120                !!!cp ('t411');
7121                last INSCOPE;
7122              }
7123            } # INSCOPE
7124    
7125            unless (defined $i) { # has an element in scope
7126              !!!cp ('t413');
7127              !!!parse-error (type => 'unmatched end tag',
7128                              text => $token->{tag_name}, token => $token);
7129              ## NOTE: Ignore the token.
7130            } else {
7131              ## Step 1. generate implied end tags
7132              while ({
7133                      ## END_TAG_OPTIONAL_EL
7134                      dd => ($token->{tag_name} ne 'dd'),
7135                      dt => ($token->{tag_name} ne 'dt'),
7136                      li => ($token->{tag_name} ne 'li'),
7137                      p => 1,
7138                      rt => 1,
7139                      rp => 1,
7140                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7141                !!!cp ('t409');
7142                pop @{$self->{open_elements}};
7143              }
7144    
7145              ## Step 2.
7146              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7147                      ne $token->{tag_name}) {
7148                !!!cp ('t412');
7149                !!!parse-error (type => 'not closed',
7150                                text => $self->{open_elements}->[-1]->[0]
7151                                    ->manakai_local_name,
7152                                token => $token);
7153              } else {
7154                !!!cp ('t414');
7155              }
7156    
7157              ## Step 3.
7158              splice @{$self->{open_elements}}, $i;
7159    
7160              ## Step 4.
7161              $clear_up_to_marker->()
7162                  if {
7163                    applet => 1, button => 1, marquee => 1, object => 1,
7164                  }->{$token->{tag_name}};
7165            }
7166            !!!next-token;
7167            next B;
7168          } elsif ($token->{tag_name} eq 'form') {
7169            undef $self->{form_element};
7170    
7171            ## has an element in scope
7172            my $i;
7173            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7174              my $node = $self->{open_elements}->[$_];
7175              if ($node->[1] & FORM_EL) {
7176                !!!cp ('t418');
7177                $i = $_;
7178                last INSCOPE;
7179              } elsif ($node->[1] & SCOPING_EL) {
7180                !!!cp ('t419');
7181                last INSCOPE;
7182              }
7183            } # INSCOPE
7184    
7185            unless (defined $i) { # has an element in scope
7186              !!!cp ('t421');
7187              !!!parse-error (type => 'unmatched end tag',
7188                              text => $token->{tag_name}, token => $token);
7189              ## NOTE: Ignore the token.
7190            } else {
7191              ## Step 1. generate implied end tags
7192              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7193                !!!cp ('t417');
7194                pop @{$self->{open_elements}};
7195              }
7196                        
7197            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7198              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7199                      ne $token->{tag_name}) {
7200                !!!cp ('t417.1');
7201                !!!parse-error (type => 'not closed',
7202                                text => $self->{open_elements}->[-1]->[0]
7203                                    ->manakai_local_name,
7204                                token => $token);
7205              } else {
7206                !!!cp ('t420');
7207              }  
7208                        
7209            unless (length $token->{data}) {            ## Step 3.
7210              !!!next-token;            splice @{$self->{open_elements}}, $i;
7211              redo B;          }
7212    
7213            !!!next-token;
7214            next B;
7215          } elsif ({
7216                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7217                   }->{$token->{tag_name}}) {
7218            ## has an element in scope
7219            my $i;
7220            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7221              my $node = $self->{open_elements}->[$_];
7222              if ($node->[1] & HEADING_EL) {
7223                !!!cp ('t423');
7224                $i = $_;
7225                last INSCOPE;
7226              } elsif ($node->[1] & SCOPING_EL) {
7227                !!!cp ('t424');
7228                last INSCOPE;
7229              }
7230            } # INSCOPE
7231    
7232            unless (defined $i) { # has an element in scope
7233              !!!cp ('t425.1');
7234              !!!parse-error (type => 'unmatched end tag',
7235                              text => $token->{tag_name}, token => $token);
7236              ## NOTE: Ignore the token.
7237            } else {
7238              ## Step 1. generate implied end tags
7239              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7240                !!!cp ('t422');
7241                pop @{$self->{open_elements}};
7242            }            }
7243              
7244              ## Step 2.
7245              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7246                      ne $token->{tag_name}) {
7247                !!!cp ('t425');
7248                !!!parse-error (type => 'unmatched end tag',
7249                                text => $token->{tag_name}, token => $token);
7250              } else {
7251                !!!cp ('t426');
7252              }
7253    
7254              ## Step 3.
7255              splice @{$self->{open_elements}}, $i;
7256          }          }
7257            
7258            !!!next-token;
7259            next B;
7260          } elsif ($token->{tag_name} eq 'p') {
7261            ## has an element in scope
7262            my $i;
7263            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7264              my $node = $self->{open_elements}->[$_];
7265              if ($node->[1] & P_EL) {
7266                !!!cp ('t410.1');
7267                $i = $_;
7268                last INSCOPE;
7269              } elsif ($node->[1] & SCOPING_EL) {
7270                !!!cp ('t411.1');
7271                last INSCOPE;
7272              }
7273            } # INSCOPE
7274    
7275          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7276          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7277          ## reprocess                    ne $token->{tag_name}) {
7278          redo B;              !!!cp ('t412.1');
7279        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7280                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7281          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7282          $phase = 'main';                              token => $token);
7283          ## reprocess            } else {
7284          redo B;              !!!cp ('t414.1');
7285        } elsif ($token->{type} eq 'end-of-file') {            }
7286          ## Stop parsing  
7287          last B;            splice @{$self->{open_elements}}, $i;
7288            } else {
7289              !!!cp ('t413.1');
7290              !!!parse-error (type => 'unmatched end tag',
7291                              text => $token->{tag_name}, token => $token);
7292    
7293              !!!cp ('t415.1');
7294              ## As if <p>, then reprocess the current token
7295              my $el;
7296              !!!create-element ($el, $HTML_NS, 'p',, $token);
7297              $insert->($el);
7298              ## NOTE: Not inserted into |$self->{open_elements}|.
7299            }
7300    
7301            !!!next-token;
7302            next B;
7303          } elsif ({
7304                    a => 1,
7305                    b => 1, big => 1, em => 1, font => 1, i => 1,
7306                    nobr => 1, s => 1, small => 1, strile => 1,
7307                    strong => 1, tt => 1, u => 1,
7308                   }->{$token->{tag_name}}) {
7309            !!!cp ('t427');
7310            $formatting_end_tag->($token);
7311            next B;
7312          } elsif ($token->{tag_name} eq 'br') {
7313            !!!cp ('t428');
7314            !!!parse-error (type => 'unmatched end tag',
7315                            text => 'br', token => $token);
7316    
7317            ## As if <br>
7318            $reconstruct_active_formatting_elements->($insert_to_current);
7319            
7320            my $el;
7321            !!!create-element ($el, $HTML_NS, 'br',, $token);
7322            $insert->($el);
7323            
7324            ## Ignore the token.
7325            !!!next-token;
7326            next B;
7327          } elsif ({
7328                    caption => 1, col => 1, colgroup => 1, frame => 1,
7329                    frameset => 1, head => 1, option => 1, optgroup => 1,
7330                    tbody => 1, td => 1, tfoot => 1, th => 1,
7331                    thead => 1, tr => 1,
7332                    area => 1, basefont => 1, bgsound => 1,
7333                    embed => 1, hr => 1, iframe => 1, image => 1,
7334                    img => 1, input => 1, isindex => 1, noembed => 1,
7335                    noframes => 1, param => 1, select => 1, spacer => 1,
7336                    table => 1, textarea => 1, wbr => 1,
7337                    noscript => 0, ## TODO: if scripting is enabled
7338                   }->{$token->{tag_name}}) {
7339            !!!cp ('t429');
7340            !!!parse-error (type => 'unmatched end tag',
7341                            text => $token->{tag_name}, token => $token);
7342            ## Ignore the token
7343            !!!next-token;
7344            next B;
7345            
7346            ## ISSUE: Issue on HTML5 new elements in spec
7347            
7348        } else {        } else {
7349          die "$0: $token->{type}: Unknown token";          ## Step 1
7350            my $node_i = -1;
7351            my $node = $self->{open_elements}->[$node_i];
7352    
7353            ## Step 2
7354            S2: {
7355              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7356                ## Step 1
7357                ## generate implied end tags
7358                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7359                  !!!cp ('t430');
7360                  ## NOTE: |<ruby><rt></ruby>|.
7361                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7362                  ## which seems wrong.
7363                  pop @{$self->{open_elements}};
7364                  $node_i++;
7365                }
7366            
7367                ## Step 2
7368                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7369                        ne $token->{tag_name}) {
7370                  !!!cp ('t431');
7371                  ## NOTE: <x><y></x>
7372                  !!!parse-error (type => 'not closed',
7373                                  text => $self->{open_elements}->[-1]->[0]
7374                                      ->manakai_local_name,
7375                                  token => $token);
7376                } else {
7377                  !!!cp ('t432');
7378                }
7379                
7380                ## Step 3
7381                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7382    
7383                !!!next-token;
7384                last S2;
7385              } else {
7386                ## Step 3
7387                if (not ($node->[1] & FORMATTING_EL) and
7388                    #not $phrasing_category->{$node->[1]} and
7389                    ($node->[1] & SPECIAL_EL or
7390                     $node->[1] & SCOPING_EL)) {
7391                  !!!cp ('t433');
7392                  !!!parse-error (type => 'unmatched end tag',
7393                                  text => $token->{tag_name}, token => $token);
7394                  ## Ignore the token
7395                  !!!next-token;
7396                  last S2;
7397                }
7398    
7399                !!!cp ('t434');
7400              }
7401              
7402              ## Step 4
7403              $node_i--;
7404              $node = $self->{open_elements}->[$node_i];
7405              
7406              ## Step 5;
7407              redo S2;
7408            } # S2
7409            next B;
7410        }        }
7411      }      }
7412        next B;
7413      } continue { # B
7414        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7415          ## NOTE: The code below is executed in cases where it does not have
7416          ## to be, but it it is harmless even in those cases.
7417          ## has an element in scope
7418          INSCOPE: {
7419            for (reverse 0..$#{$self->{open_elements}}) {
7420              my $node = $self->{open_elements}->[$_];
7421              if ($node->[1] & FOREIGN_EL) {
7422                last INSCOPE;
7423              } elsif ($node->[1] & SCOPING_EL) {
7424                last;
7425              }
7426            }
7427            
7428            ## NOTE: No foreign element in scope.
7429            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7430          } # INSCOPE
7431        }
7432    } # B    } # B
7433    
7434    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4913  sub set_inner_html ($$$) { Line 7442  sub set_inner_html ($$$) {
7442    my $s = \$_[0];    my $s = \$_[0];
7443    my $onerror = $_[1];    my $onerror = $_[1];
7444    
7445      ## ISSUE: Should {confident} be true?
7446    
7447    my $nt = $node->node_type;    my $nt = $node->node_type;
7448    if ($nt == 9) {    if ($nt == 9) {
7449      # MUST      # MUST
# Line 4935  sub set_inner_html ($$$) { Line 7466  sub set_inner_html ($$$) {
7466      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7467    
7468      ## Step 1 # MUST      ## Step 1 # MUST
7469      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7470      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7471        $doc->manakai_is_html (1);
7472      my $p = $class->new;      my $p = $class->new;
7473      $p->{document} = $doc;      $p->{document} = $doc;
7474    
7475      ## Step 9 # MUST      ## Step 8 # MUST
7476      my $i = 0;      my $i = 0;
7477      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7478      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7479      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7480        my $self = shift;        my $self = shift;
7481        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7482        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7483        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7484    
7485        if ($self->{next_input_character} == 0x000A) { # LF        $self->{next_char} = -1 and return if $i >= length $$s;
7486          $line++;        $self->{next_char} = ord substr $$s, $i++, 1;
7487          $column = 0;  
7488        } elsif ($self->{next_input_character} == 0x000D) { # CR        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7489          if ($i >= length $$s) {        $p->{column}++;
7490            #  
7491          if ($self->{next_char} == 0x000A) { # LF
7492            $p->{line}++;
7493            $p->{column} = 0;
7494            !!!cp ('i1');
7495          } elsif ($self->{next_char} == 0x000D) { # CR
7496            $i++ if substr ($$s, $i, 1) eq "\x0A";
7497            $self->{next_char} = 0x000A; # LF # MUST
7498            $p->{line}++;
7499            $p->{column} = 0;
7500            !!!cp ('i2');
7501          } elsif ($self->{next_char} > 0x10FFFF) {
7502            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7503            !!!cp ('i3');
7504          } elsif ($self->{next_char} == 0x0000) { # NULL
7505            !!!cp ('i4');
7506            !!!parse-error (type => 'NULL');
7507            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7508          } elsif ($self->{next_char} <= 0x0008 or
7509                   (0x000E <= $self->{next_char} and
7510                    $self->{next_char} <= 0x001F) or
7511                   (0x007F <= $self->{next_char} and
7512                    $self->{next_char} <= 0x009F) or
7513                   (0xD800 <= $self->{next_char} and
7514                    $self->{next_char} <= 0xDFFF) or
7515                   (0xFDD0 <= $self->{next_char} and
7516                    $self->{next_char} <= 0xFDDF) or
7517                   {
7518                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7519                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7520                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7521                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7522                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7523                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7524                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7525                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7526                    0x10FFFE => 1, 0x10FFFF => 1,
7527                   }->{$self->{next_char}}) {
7528            !!!cp ('i4.1');
7529            if ($self->{next_char} < 0x10000) {
7530              !!!parse-error (type => 'control char',
7531                              text => (sprintf 'U+%04X', $self->{next_char}));
7532          } else {          } else {
7533            my $next_char = ord substr $$s, $i++, 1;            !!!parse-error (type => 'control char',
7534            if ($next_char == 0x000A) { # LF                            text => (sprintf 'U-%08X', $self->{next_char}));
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
7535          }          }
         $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  
         $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
7536        }        }
7537      };      };
7538        $p->{prev_char} = [-1, -1, -1];
7539        $p->{next_char} = -1;
7540            
7541      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7542        my (%opt) = @_;        my (%opt) = @_;
7543        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7544          my $column = $opt{column};
7545          if (defined $opt{token} and defined $opt{token}->{line}) {
7546            $line = $opt{token}->{line};
7547            $column = $opt{token}->{column};
7548          }
7549          warn "Parse error ($opt{type}) at line $line column $column\n";
7550      };      };
7551      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7552        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7553      };      };
7554            
7555      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7556      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7557    
7558      ## Step 2      ## Step 2
7559      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7560      $p->{content_model_flag} = {      $p->{content_model} = {
7561        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7562        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7563        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7564        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7565        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7566        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7567        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7568        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7569        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7570        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7571      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7572         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7573            unless defined $p->{content_model};
7574            ## ISSUE: What is "the name of the element"? local name?
7575    
7576      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7577          ## TODO: Foreign element OK?
7578    
7579      ## Step 4      ## Step 3
7580      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7581        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7582    
7583      ## Step 5 # MUST      ## Step 4 # MUST
7584      $doc->append_child ($root);      $doc->append_child ($root);
7585    
7586      ## Step 6 # MUST      ## Step 5 # MUST
7587      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7588    
7589      undef $p->{head_element};      undef $p->{head_element};
7590    
7591      ## Step 7 # MUST      ## Step 6 # MUST
7592      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7593    
7594      ## Step 8 # MUST      ## Step 7 # MUST
7595      my $anode = $node;      my $anode = $node;
7596      AN: while (defined $anode) {      AN: while (defined $anode) {
7597        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7598          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7599          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7600            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7601                !!!cp ('i5');
7602              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7603              last AN;              last AN;
7604            }            }
# Line 5033  sub set_inner_html ($$$) { Line 7607  sub set_inner_html ($$$) {
7607        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7608      } # AN      } # AN
7609            
7610      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7611      {      {
7612        my $self = $p;        my $self = $p;
7613        !!!next-token;        !!!next-token;
7614      }      }
7615      $p->_tree_construction_main;      $p->_tree_construction_main;
7616    
7617      ## Step 11 # MUST      ## Step 10 # MUST
7618      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7619      for (@cn) {      for (@cn) {
7620        $node->remove_child ($_);        $node->remove_child ($_);
7621      }      }
7622      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7623    
7624      ## Step 12 # MUST      ## Step 11 # MUST
7625      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7626      for (@cn) {      for (@cn) {
7627          $this_doc->adopt_node ($_);
7628        $node->append_child ($_);        $node->append_child ($_);
7629      }      }
7630      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7631    
7632      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7633    
7634        delete $p->{parse_error}; # delete loop
7635    } else {    } else {
7636      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";
7637    }    }
# Line 5063  sub set_inner_html ($$$) { Line 7639  sub set_inner_html ($$$) {
7639    
7640  } # tree construction stage  } # tree construction stage
7641    
7642  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7643    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  
7644    
7645  1;  1;
7646  # $Date$  # $Date$

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.158

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24