/[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.15 by wakaba, Sat Jun 23 06:48:24 2007 UTC revision 1.183 by wakaba, Mon Sep 15 08:09:39 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.  ## NOTE: This module don't check all HTML5 parse errors; character
7    ## encoding related parse errors are expected to be handled by relevant
8    ## modules.
9    ## Parse errors for control characters that are not allowed in HTML5
10    ## documents, for surrogate code points, and for noncharacter code
11    ## points, as well as U+FFFD substitions for characters whose code points
12    ## is higher than U+10FFFF may be detected by combining the parser with
13    ## the checker implemented by Whatpm::Charset::UnicodeChecker (for its
14    ## usage example, see |t/HTML-tree.t| in the Whatpm package or the
15    ## WebHACC::Language::HTML module in the WebHACC package).
16    
17    ## ISSUE:
18    ## var doc = implementation.createDocument (null, null, null);
19    ## doc.write ('');
20    ## alert (doc.compatMode);
21    
22    require IO::Handle;
23    
24    my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
25    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
26    my $SVG_NS = q<http://www.w3.org/2000/svg>;
27    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
28    my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
29    my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
30    
31    sub A_EL () { 0b1 }
32    sub ADDRESS_EL () { 0b10 }
33    sub BODY_EL () { 0b100 }
34    sub BUTTON_EL () { 0b1000 }
35    sub CAPTION_EL () { 0b10000 }
36    sub DD_EL () { 0b100000 }
37    sub DIV_EL () { 0b1000000 }
38    sub DT_EL () { 0b10000000 }
39    sub FORM_EL () { 0b100000000 }
40    sub FORMATTING_EL () { 0b1000000000 }
41    sub FRAMESET_EL () { 0b10000000000 }
42    sub HEADING_EL () { 0b100000000000 }
43    sub HTML_EL () { 0b1000000000000 }
44    sub LI_EL () { 0b10000000000000 }
45    sub NOBR_EL () { 0b100000000000000 }
46    sub OPTION_EL () { 0b1000000000000000 }
47    sub OPTGROUP_EL () { 0b10000000000000000 }
48    sub P_EL () { 0b100000000000000000 }
49    sub SELECT_EL () { 0b1000000000000000000 }
50    sub TABLE_EL () { 0b10000000000000000000 }
51    sub TABLE_CELL_EL () { 0b100000000000000000000 }
52    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
53    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
54    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
55    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
56    sub FOREIGN_EL () { 0b10000000000000000000000000 }
57    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
58    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
59    sub RUBY_EL () { 0b10000000000000000000000000000 }
60    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
61    
62    sub TABLE_ROWS_EL () {
63      TABLE_EL |
64      TABLE_ROW_EL |
65      TABLE_ROW_GROUP_EL
66    }
67    
68    ## NOTE: Used in "generate implied end tags" algorithm.
69    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
70    ## is used in "generate implied end tags" implementation (search for the
71    ## function mae).
72    sub END_TAG_OPTIONAL_EL () {
73      DD_EL |
74      DT_EL |
75      LI_EL |
76      P_EL |
77      RUBY_COMPONENT_EL
78    }
79    
80    ## NOTE: Used in </body> and EOF algorithms.
81    sub ALL_END_TAG_OPTIONAL_EL () {
82      DD_EL |
83      DT_EL |
84      LI_EL |
85      P_EL |
86    
87      BODY_EL |
88      HTML_EL |
89      TABLE_CELL_EL |
90      TABLE_ROW_EL |
91      TABLE_ROW_GROUP_EL
92    }
93    
94    sub SCOPING_EL () {
95      BUTTON_EL |
96      CAPTION_EL |
97      HTML_EL |
98      TABLE_EL |
99      TABLE_CELL_EL |
100      MISC_SCOPING_EL
101    }
102    
103    sub TABLE_SCOPING_EL () {
104      HTML_EL |
105      TABLE_EL
106    }
107    
108    sub TABLE_ROWS_SCOPING_EL () {
109      HTML_EL |
110      TABLE_ROW_GROUP_EL
111    }
112    
113    sub TABLE_ROW_SCOPING_EL () {
114      HTML_EL |
115      TABLE_ROW_EL
116    }
117    
118    sub SPECIAL_EL () {
119      ADDRESS_EL |
120      BODY_EL |
121      DIV_EL |
122    
123      DD_EL |
124      DT_EL |
125      LI_EL |
126      P_EL |
127    
128      FORM_EL |
129      FRAMESET_EL |
130      HEADING_EL |
131      OPTION_EL |
132      OPTGROUP_EL |
133      SELECT_EL |
134      TABLE_ROW_EL |
135      TABLE_ROW_GROUP_EL |
136      MISC_SPECIAL_EL
137    }
138    
139    my $el_category = {
140      a => A_EL | FORMATTING_EL,
141      address => ADDRESS_EL,
142      applet => MISC_SCOPING_EL,
143      area => MISC_SPECIAL_EL,
144      b => FORMATTING_EL,
145      base => MISC_SPECIAL_EL,
146      basefont => MISC_SPECIAL_EL,
147      bgsound => MISC_SPECIAL_EL,
148      big => FORMATTING_EL,
149      blockquote => MISC_SPECIAL_EL,
150      body => BODY_EL,
151      br => MISC_SPECIAL_EL,
152      button => BUTTON_EL,
153      caption => CAPTION_EL,
154      center => MISC_SPECIAL_EL,
155      col => MISC_SPECIAL_EL,
156      colgroup => MISC_SPECIAL_EL,
157      dd => DD_EL,
158      dir => MISC_SPECIAL_EL,
159      div => DIV_EL,
160      dl => MISC_SPECIAL_EL,
161      dt => DT_EL,
162      em => FORMATTING_EL,
163      embed => MISC_SPECIAL_EL,
164      fieldset => MISC_SPECIAL_EL,
165      font => FORMATTING_EL,
166      form => FORM_EL,
167      frame => MISC_SPECIAL_EL,
168      frameset => FRAMESET_EL,
169      h1 => HEADING_EL,
170      h2 => HEADING_EL,
171      h3 => HEADING_EL,
172      h4 => HEADING_EL,
173      h5 => HEADING_EL,
174      h6 => HEADING_EL,
175      head => MISC_SPECIAL_EL,
176      hr => MISC_SPECIAL_EL,
177      html => HTML_EL,
178      i => FORMATTING_EL,
179      iframe => MISC_SPECIAL_EL,
180      img => MISC_SPECIAL_EL,
181      input => MISC_SPECIAL_EL,
182      isindex => MISC_SPECIAL_EL,
183      li => LI_EL,
184      link => MISC_SPECIAL_EL,
185      listing => MISC_SPECIAL_EL,
186      marquee => MISC_SCOPING_EL,
187      menu => MISC_SPECIAL_EL,
188      meta => MISC_SPECIAL_EL,
189      nobr => NOBR_EL | FORMATTING_EL,
190      noembed => MISC_SPECIAL_EL,
191      noframes => MISC_SPECIAL_EL,
192      noscript => MISC_SPECIAL_EL,
193      object => MISC_SCOPING_EL,
194      ol => MISC_SPECIAL_EL,
195      optgroup => OPTGROUP_EL,
196      option => OPTION_EL,
197      p => P_EL,
198      param => MISC_SPECIAL_EL,
199      plaintext => MISC_SPECIAL_EL,
200      pre => MISC_SPECIAL_EL,
201      rp => RUBY_COMPONENT_EL,
202      rt => RUBY_COMPONENT_EL,
203      ruby => RUBY_EL,
204      s => FORMATTING_EL,
205      script => MISC_SPECIAL_EL,
206      select => SELECT_EL,
207      small => FORMATTING_EL,
208      spacer => MISC_SPECIAL_EL,
209      strike => FORMATTING_EL,
210      strong => FORMATTING_EL,
211      style => MISC_SPECIAL_EL,
212      table => TABLE_EL,
213      tbody => TABLE_ROW_GROUP_EL,
214      td => TABLE_CELL_EL,
215      textarea => MISC_SPECIAL_EL,
216      tfoot => TABLE_ROW_GROUP_EL,
217      th => TABLE_CELL_EL,
218      thead => TABLE_ROW_GROUP_EL,
219      title => MISC_SPECIAL_EL,
220      tr => TABLE_ROW_EL,
221      tt => FORMATTING_EL,
222      u => FORMATTING_EL,
223      ul => MISC_SPECIAL_EL,
224      wbr => MISC_SPECIAL_EL,
225    };
226    
227    my $el_category_f = {
228      $MML_NS => {
229        'annotation-xml' => MML_AXML_EL,
230        mi => FOREIGN_FLOW_CONTENT_EL,
231        mo => FOREIGN_FLOW_CONTENT_EL,
232        mn => FOREIGN_FLOW_CONTENT_EL,
233        ms => FOREIGN_FLOW_CONTENT_EL,
234        mtext => FOREIGN_FLOW_CONTENT_EL,
235      },
236      $SVG_NS => {
237        foreignObject => FOREIGN_FLOW_CONTENT_EL,
238        desc => FOREIGN_FLOW_CONTENT_EL,
239        title => FOREIGN_FLOW_CONTENT_EL,
240      },
241      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
242    };
243    
244  my $permitted_slash_tag_name = {  my $svg_attr_name = {
245    base => 1,    attributename => 'attributeName',
246    link => 1,    attributetype => 'attributeType',
247    meta => 1,    basefrequency => 'baseFrequency',
248    hr => 1,    baseprofile => 'baseProfile',
249    br => 1,    calcmode => 'calcMode',
250    img=> 1,    clippathunits => 'clipPathUnits',
251    embed => 1,    contentscripttype => 'contentScriptType',
252    param => 1,    contentstyletype => 'contentStyleType',
253    area => 1,    diffuseconstant => 'diffuseConstant',
254    col => 1,    edgemode => 'edgeMode',
255    input => 1,    externalresourcesrequired => 'externalResourcesRequired',
256      filterres => 'filterRes',
257      filterunits => 'filterUnits',
258      glyphref => 'glyphRef',
259      gradienttransform => 'gradientTransform',
260      gradientunits => 'gradientUnits',
261      kernelmatrix => 'kernelMatrix',
262      kernelunitlength => 'kernelUnitLength',
263      keypoints => 'keyPoints',
264      keysplines => 'keySplines',
265      keytimes => 'keyTimes',
266      lengthadjust => 'lengthAdjust',
267      limitingconeangle => 'limitingConeAngle',
268      markerheight => 'markerHeight',
269      markerunits => 'markerUnits',
270      markerwidth => 'markerWidth',
271      maskcontentunits => 'maskContentUnits',
272      maskunits => 'maskUnits',
273      numoctaves => 'numOctaves',
274      pathlength => 'pathLength',
275      patterncontentunits => 'patternContentUnits',
276      patterntransform => 'patternTransform',
277      patternunits => 'patternUnits',
278      pointsatx => 'pointsAtX',
279      pointsaty => 'pointsAtY',
280      pointsatz => 'pointsAtZ',
281      preservealpha => 'preserveAlpha',
282      preserveaspectratio => 'preserveAspectRatio',
283      primitiveunits => 'primitiveUnits',
284      refx => 'refX',
285      refy => 'refY',
286      repeatcount => 'repeatCount',
287      repeatdur => 'repeatDur',
288      requiredextensions => 'requiredExtensions',
289      requiredfeatures => 'requiredFeatures',
290      specularconstant => 'specularConstant',
291      specularexponent => 'specularExponent',
292      spreadmethod => 'spreadMethod',
293      startoffset => 'startOffset',
294      stddeviation => 'stdDeviation',
295      stitchtiles => 'stitchTiles',
296      surfacescale => 'surfaceScale',
297      systemlanguage => 'systemLanguage',
298      tablevalues => 'tableValues',
299      targetx => 'targetX',
300      targety => 'targetY',
301      textlength => 'textLength',
302      viewbox => 'viewBox',
303      viewtarget => 'viewTarget',
304      xchannelselector => 'xChannelSelector',
305      ychannelselector => 'yChannelSelector',
306      zoomandpan => 'zoomAndPan',
307  };  };
308    
309  my $entity_char = {  my $foreign_attr_xname = {
310    AElig => "\x{00C6}",    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
311    Aacute => "\x{00C1}",    'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
312    Acirc => "\x{00C2}",    'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
313    Agrave => "\x{00C0}",    'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
314    Alpha => "\x{0391}",    'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
315    Aring => "\x{00C5}",    'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
316    Atilde => "\x{00C3}",    'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
317    Auml => "\x{00C4}",    'xml:base' => [$XML_NS, ['xml', 'base']],
318    Beta => "\x{0392}",    'xml:lang' => [$XML_NS, ['xml', 'lang']],
319    Ccedil => "\x{00C7}",    'xml:space' => [$XML_NS, ['xml', 'space']],
320    Chi => "\x{03A7}",    'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
321    Dagger => "\x{2021}",    'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
322    Delta => "\x{0394}",  };
323    ETH => "\x{00D0}",  
324    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  
325    
326  my $c1_entity_char = {  my $c1_entity_char = {
327    0x80 => 0x20AC,    0x80 => 0x20AC,
# Line 315  my $c1_entity_char = { Line 358  my $c1_entity_char = {
358    0x9F => 0x0178,    0x9F => 0x0178,
359  }; # $c1_entity_char  }; # $c1_entity_char
360    
361  my $special_category = {  sub parse_byte_string ($$$$;$) {
362    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
363    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
364    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
365    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
366    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
367    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
368    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$$) {
369    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
370    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $self = ref $_[0] ? shift : shift->new;
371    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $charset_name = shift;
372  };    my $byte_stream = $_[0];
373  my $scoping_category = {  
374    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    my $onerror = $_[2] || sub {
375    table => 1, td => 1, th => 1,      my (%opt) = @_;
376  };      warn "Parse error ($opt{type})\n";
377  my $formatting_category = {    };
378    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,    $self->{parse_error} = $onerror; # updated later by parse_char_string
379    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
380  };    my $get_wrapper = $_[3] || sub ($) {
381  # $phrasing_category: all other elements      return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
382      };
383    
384      ## HTML5 encoding sniffing algorithm
385      require Message::Charset::Info;
386      my $charset;
387      my $buffer;
388      my ($char_stream, $e_status);
389    
390      SNIFFING: {
391        ## NOTE: By setting |allow_fallback| option true when the
392        ## |get_decode_handle| method is invoked, we ignore what the HTML5
393        ## spec requires, i.e. unsupported encoding should be ignored.
394          ## TODO: We should not do this unless the parser is invoked
395          ## in the conformance checking mode, in which this behavior
396          ## would be useful.
397    
398        ## Step 1
399        if (defined $charset_name) {
400          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
401              ## TODO: Is this ok?  Transfer protocol's parameter should be
402              ## interpreted in its semantics?
403    
404          ## ISSUE: Unsupported encoding is not ignored according to the spec.
405          ($char_stream, $e_status) = $charset->get_decode_handle
406              ($byte_stream, allow_error_reporting => 1,
407               allow_fallback => 1);
408          if ($char_stream) {
409            $self->{confident} = 1;
410            last SNIFFING;
411          } else {
412            ## TODO: unsupported error
413          }
414        }
415    
416        ## Step 2
417        my $byte_buffer = '';
418        for (1..1024) {
419          my $char = $byte_stream->getc;
420          last unless defined $char;
421          $byte_buffer .= $char;
422        } ## TODO: timeout
423    
424        ## Step 3
425        if ($byte_buffer =~ /^\xFE\xFF/) {
426          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
427          ($char_stream, $e_status) = $charset->get_decode_handle
428              ($byte_stream, allow_error_reporting => 1,
429               allow_fallback => 1, byte_buffer => \$byte_buffer);
430          $self->{confident} = 1;
431          last SNIFFING;
432        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
433          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
434          ($char_stream, $e_status) = $charset->get_decode_handle
435              ($byte_stream, allow_error_reporting => 1,
436               allow_fallback => 1, byte_buffer => \$byte_buffer);
437          $self->{confident} = 1;
438          last SNIFFING;
439        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
440          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($byte_stream, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          $self->{confident} = 1;
445          last SNIFFING;
446        }
447    
448        ## Step 4
449        ## TODO: <meta charset>
450    
451        ## Step 5
452        ## TODO: from history
453    
454        ## Step 6
455        require Whatpm::Charset::UniversalCharDet;
456        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
457            ($byte_buffer);
458        if (defined $charset_name) {
459          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
460    
461          ## ISSUE: Unsupported encoding is not ignored according to the spec.
462          require Whatpm::Charset::DecodeHandle;
463          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
464              ($byte_stream);
465          ($char_stream, $e_status) = $charset->get_decode_handle
466              ($buffer, allow_error_reporting => 1,
467               allow_fallback => 1, byte_buffer => \$byte_buffer);
468          if ($char_stream) {
469            $buffer->{buffer} = $byte_buffer;
470            !!!parse-error (type => 'sniffing:chardet',
471                            text => $charset_name,
472                            level => $self->{level}->{info},
473                            layer => 'encode',
474                            line => 1, column => 1);
475            $self->{confident} = 0;
476            last SNIFFING;
477          }
478        }
479    
480        ## Step 7: default
481        ## TODO: Make this configurable.
482        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
483            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
484            ## detectable in the step 6.
485        require Whatpm::Charset::DecodeHandle;
486        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
487            ($byte_stream);
488        ($char_stream, $e_status)
489            = $charset->get_decode_handle ($buffer,
490                                           allow_error_reporting => 1,
491                                           allow_fallback => 1,
492                                           byte_buffer => \$byte_buffer);
493        $buffer->{buffer} = $byte_buffer;
494        !!!parse-error (type => 'sniffing:default',
495                        text => 'windows-1252',
496                        level => $self->{level}->{info},
497                        line => 1, column => 1,
498                        layer => 'encode');
499        $self->{confident} = 0;
500      } # SNIFFING
501    
502      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
503        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
504        !!!parse-error (type => 'chardecode:fallback',
505                        #text => $self->{input_encoding},
506                        level => $self->{level}->{uncertain},
507                        line => 1, column => 1,
508                        layer => 'encode');
509      } elsif (not ($e_status &
510                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
511        $self->{input_encoding} = $charset->get_iana_name;
512        !!!parse-error (type => 'chardecode:no error',
513                        text => $self->{input_encoding},
514                        level => $self->{level}->{uncertain},
515                        line => 1, column => 1,
516                        layer => 'encode');
517      } else {
518        $self->{input_encoding} = $charset->get_iana_name;
519      }
520    
521      $self->{change_encoding} = sub {
522        my $self = shift;
523        $charset_name = shift;
524        my $token = shift;
525    
526        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
527        ($char_stream, $e_status) = $charset->get_decode_handle
528            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
529             byte_buffer => \ $buffer->{buffer});
530        
531        if ($char_stream) { # if supported
532          ## "Change the encoding" algorithm:
533    
534          ## Step 1    
535          if ($charset->{category} &
536              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
537            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
538            ($char_stream, $e_status) = $charset->get_decode_handle
539                ($byte_stream,
540                 byte_buffer => \ $buffer->{buffer});
541          }
542          $charset_name = $charset->get_iana_name;
543          
544          ## Step 2
545          if (defined $self->{input_encoding} and
546              $self->{input_encoding} eq $charset_name) {
547            !!!parse-error (type => 'charset label:matching',
548                            text => $charset_name,
549                            level => $self->{level}->{info});
550            $self->{confident} = 1;
551            return;
552          }
553    
554  sub parse_string ($$$;$) {        !!!parse-error (type => 'charset label detected',
555    my $self = shift->new;                        text => $self->{input_encoding},
556    my $s = \$_[0];                        value => $charset_name,
557                          level => $self->{level}->{warn},
558                          token => $token);
559          
560          ## Step 3
561          # if (can) {
562            ## change the encoding on the fly.
563            #$self->{confident} = 1;
564            #return;
565          # }
566          
567          ## Step 4
568          throw Whatpm::HTML::RestartParser ();
569        }
570      }; # $self->{change_encoding}
571    
572      my $char_onerror = sub {
573        my (undef, $type, %opt) = @_;
574        !!!parse-error (layer => 'encode',
575                        line => $self->{line}, column => $self->{column} + 1,
576                        %opt, type => $type);
577        if ($opt{octets}) {
578          ${$opt{octets}} = "\x{FFFD}"; # relacement character
579        }
580      };
581    
582      my $wrapped_char_stream = $get_wrapper->($char_stream);
583      $wrapped_char_stream->onerror ($char_onerror);
584    
585      my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
586      my $return;
587      try {
588        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
589      } catch Whatpm::HTML::RestartParser with {
590        ## NOTE: Invoked after {change_encoding}.
591    
592        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
593          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
594          !!!parse-error (type => 'chardecode:fallback',
595                          level => $self->{level}->{uncertain},
596                          #text => $self->{input_encoding},
597                          line => 1, column => 1,
598                          layer => 'encode');
599        } elsif (not ($e_status &
600                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
601          $self->{input_encoding} = $charset->get_iana_name;
602          !!!parse-error (type => 'chardecode:no error',
603                          text => $self->{input_encoding},
604                          level => $self->{level}->{uncertain},
605                          line => 1, column => 1,
606                          layer => 'encode');
607        } else {
608          $self->{input_encoding} = $charset->get_iana_name;
609        }
610        $self->{confident} = 1;
611    
612        $wrapped_char_stream = $get_wrapper->($char_stream);
613        $wrapped_char_stream->onerror ($char_onerror);
614    
615        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
616      };
617      return $return;
618    } # parse_byte_stream
619    
620    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
621    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
622    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
623    ## because the core part of our HTML parser expects a string of character,
624    ## not a string of bytes or code units or anything which might contain a BOM.
625    ## Therefore, any parser interface that accepts a string of bytes,
626    ## such as |parse_byte_string| in this module, must ensure that it does
627    ## strip the BOM and never strip any ZWNBSP.
628    
629    sub parse_char_string ($$$;$$) {
630      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
631      my $self = shift;
632      my $s = ref $_[0] ? $_[0] : \($_[0]);
633      require Whatpm::Charset::DecodeHandle;
634      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
635      return $self->parse_char_stream ($input, @_[1..$#_]);
636    } # parse_char_string
637    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
638    
639    sub parse_char_stream ($$$;$$) {
640      my $self = ref $_[0] ? shift : shift->new;
641      my $input = $_[0];
642    $self->{document} = $_[1];    $self->{document} = $_[1];
643      @{$self->{document}->child_nodes} = ();
644    
645    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
646    
647    my $i = 0;    $self->{confident} = 1 unless exists $self->{confident};
648    my $line = 1;    $self->{document}->input_encoding ($self->{input_encoding})
649    my $column = 0;        if defined $self->{input_encoding};
650    $self->{set_next_input_character} = sub {  ## TODO: |{input_encoding}| is needless?
651    
652      $self->{line_prev} = $self->{line} = 1;
653      $self->{column_prev} = -1;
654      $self->{column} = 0;
655      $self->{set_nc} = sub {
656      my $self = shift;      my $self = shift;
657    
658      pop @{$self->{prev_input_character}};      my $char = '';
659      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      if (defined $self->{next_nc}) {
660          $char = $self->{next_nc};
661          delete $self->{next_nc};
662          $self->{nc} = ord $char;
663        } else {
664          $self->{char_buffer} = '';
665          $self->{char_buffer_pos} = 0;
666    
667      $self->{next_input_character} = -1 and return if $i >= length $$s;        my $count = $input->manakai_read_until
668      $self->{next_input_character} = ord substr $$s, $i++, 1;           ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
669      $column++;        if ($count) {
670            $self->{line_prev} = $self->{line};
671            $self->{column_prev} = $self->{column};
672            $self->{column}++;
673            $self->{nc}
674                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
675            return;
676          }
677    
678          if ($input->read ($char, 1)) {
679            $self->{nc} = ord $char;
680          } else {
681            $self->{nc} = -1;
682            return;
683          }
684        }
685    
686        ($self->{line_prev}, $self->{column_prev})
687            = ($self->{line}, $self->{column});
688        $self->{column}++;
689            
690      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{nc} == 0x000A) { # LF
691        $line++;        !!!cp ('j1');
692        $column = 0;        $self->{line}++;
693      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
694        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{nc} == 0x000D) { # CR
695        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
696        $line++;  ## TODO: support for abort/streaming
697        $column = 0;        my $next = '';
698      } elsif ($self->{next_input_character} > 0x10FFFF) {        if ($input->read ($next, 1) and $next ne "\x0A") {
699        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_nc} = $next;
700      } elsif ($self->{next_input_character} == 0x0000) { # NULL        }
701          $self->{nc} = 0x000A; # LF # MUST
702          $self->{line}++;
703          $self->{column} = 0;
704        } elsif ($self->{nc} == 0x0000) { # NULL
705          !!!cp ('j4');
706        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
707        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
708      }      }
709    };    };
710    $self->{prev_input_character} = [-1, -1, -1];  
711    $self->{next_input_character} = -1;    $self->{read_until} = sub {
712        #my ($scalar, $specials_range, $offset) = @_;
713        return 0 if defined $self->{next_nc};
714    
715        my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
716        my $offset = $_[2] || 0;
717    
718        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
719          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
720          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
721            substr ($_[0], $offset)
722                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
723            my $count = $+[0] - $-[0];
724            if ($count) {
725              $self->{column} += $count;
726              $self->{char_buffer_pos} += $count;
727              $self->{line_prev} = $self->{line};
728              $self->{column_prev} = $self->{column} - 1;
729              $self->{prev_char} = [-1, -1, -1];
730              $self->{nc} = -1;
731            }
732            return $count;
733          } else {
734            return 0;
735          }
736        } else {
737          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
738          if ($count) {
739            $self->{column} += $count;
740            $self->{line_prev} = $self->{line};
741            $self->{column_prev} = $self->{column} - 1;
742            $self->{prev_char} = [-1, -1, -1];
743            $self->{nc} = -1;
744          }
745          return $count;
746        }
747      }; # $self->{read_until}
748    
749    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
750      my (%opt) = @_;      my (%opt) = @_;
751      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
752        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
753        warn "Parse error ($opt{type}) at line $line column $column\n";
754    };    };
755    $self->{parse_error} = sub {    $self->{parse_error} = sub {
756      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
757    };    };
758    
759      my $char_onerror = sub {
760        my (undef, $type, %opt) = @_;
761        !!!parse-error (layer => 'encode',
762                        line => $self->{line}, column => $self->{column} + 1,
763                        %opt, type => $type);
764      }; # $char_onerror
765    
766      if ($_[3]) {
767        $input = $_[3]->($input);
768        $input->onerror ($char_onerror);
769      } else {
770        $input->onerror ($char_onerror) unless defined $input->onerror;
771      }
772    
773    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
774    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
775    $self->_construct_tree;    $self->_construct_tree;
776    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
777    
778      delete $self->{parse_error}; # remove loop
779    
780    return $self->{document};    return $self->{document};
781  } # parse_string  } # parse_char_stream
782    
783  sub new ($) {  sub new ($) {
784    my $class = shift;    my $class = shift;
785    my $self = bless {}, $class;    my $self = bless {
786    $self->{set_next_input_character} = sub {      level => {must => 'm',
787      $self->{next_input_character} = -1;                should => 's',
788                  warn => 'w',
789                  info => 'i',
790                  uncertain => 'u'},
791      }, $class;
792      $self->{set_nc} = sub {
793        $self->{nc} = -1;
794    };    };
795    $self->{parse_error} = sub {    $self->{parse_error} = sub {
796      #      #
797    };    };
798      $self->{change_encoding} = sub {
799        # if ($_[0] is a supported encoding) {
800        #   run "change the encoding" algorithm;
801        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
802        # }
803      };
804      $self->{application_cache_selection} = sub {
805        #
806      };
807    return $self;    return $self;
808  } # new  } # new
809    
810    sub CM_ENTITY () { 0b001 } # & markup in data
811    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
812    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
813    
814    sub PLAINTEXT_CONTENT_MODEL () { 0 }
815    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
816    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
817    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
818    
819    sub DATA_STATE () { 0 }
820    #sub ENTITY_DATA_STATE () { 1 }
821    sub TAG_OPEN_STATE () { 2 }
822    sub CLOSE_TAG_OPEN_STATE () { 3 }
823    sub TAG_NAME_STATE () { 4 }
824    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
825    sub ATTRIBUTE_NAME_STATE () { 6 }
826    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
827    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
828    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
829    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
830    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
831    #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
832    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
833    sub COMMENT_START_STATE () { 14 }
834    sub COMMENT_START_DASH_STATE () { 15 }
835    sub COMMENT_STATE () { 16 }
836    sub COMMENT_END_STATE () { 17 }
837    sub COMMENT_END_DASH_STATE () { 18 }
838    sub BOGUS_COMMENT_STATE () { 19 }
839    sub DOCTYPE_STATE () { 20 }
840    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
841    sub DOCTYPE_NAME_STATE () { 22 }
842    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
843    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
844    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
845    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
846    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
847    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
848    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
849    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
850    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
851    sub BOGUS_DOCTYPE_STATE () { 32 }
852    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
853    sub SELF_CLOSING_START_TAG_STATE () { 34 }
854    sub CDATA_SECTION_STATE () { 35 }
855    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
856    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
857    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
858    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
859    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
860    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
861    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
862    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
863    ## NOTE: "Entity data state", "entity in attribute value state", and
864    ## "consume a character reference" algorithm are jointly implemented
865    ## using the following six states:
866    sub ENTITY_STATE () { 44 }
867    sub ENTITY_HASH_STATE () { 45 }
868    sub NCR_NUM_STATE () { 46 }
869    sub HEXREF_X_STATE () { 47 }
870    sub HEXREF_HEX_STATE () { 48 }
871    sub ENTITY_NAME_STATE () { 49 }
872    
873    sub DOCTYPE_TOKEN () { 1 }
874    sub COMMENT_TOKEN () { 2 }
875    sub START_TAG_TOKEN () { 3 }
876    sub END_TAG_TOKEN () { 4 }
877    sub END_OF_FILE_TOKEN () { 5 }
878    sub CHARACTER_TOKEN () { 6 }
879    
880    sub AFTER_HTML_IMS () { 0b100 }
881    sub HEAD_IMS ()       { 0b1000 }
882    sub BODY_IMS ()       { 0b10000 }
883    sub BODY_TABLE_IMS () { 0b100000 }
884    sub TABLE_IMS ()      { 0b1000000 }
885    sub ROW_IMS ()        { 0b10000000 }
886    sub BODY_AFTER_IMS () { 0b100000000 }
887    sub FRAME_IMS ()      { 0b1000000000 }
888    sub SELECT_IMS ()     { 0b10000000000 }
889    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
890        ## NOTE: "in foreign content" insertion mode is special; it is combined
891        ## with the secondary insertion mode.  In this parser, they are stored
892        ## together in the bit-or'ed form.
893    
894    ## NOTE: "initial" and "before html" insertion modes have no constants.
895    
896    ## NOTE: "after after body" insertion mode.
897    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
898    
899    ## NOTE: "after after frameset" insertion mode.
900    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
901    
902    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
903    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
904    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
905    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
906    sub IN_BODY_IM () { BODY_IMS }
907    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
908    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
909    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
910    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
911    sub IN_TABLE_IM () { TABLE_IMS }
912    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
913    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
914    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
915    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
916    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
917    sub IN_COLUMN_GROUP_IM () { 0b10 }
918    
919  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
920    
921  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
922    my $self = shift;    my $self = shift;
923    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
924    $self->{content_model_flag} = 'PCDATA'; # be    #$self->{s_kwd}; # state keyword - initialized when used
925    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    #$self->{entity__value}; # initialized when used
926    undef $self->{current_attribute};    #$self->{entity__match}; # initialized when used
927    undef $self->{last_emitted_start_tag_name};    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
928    undef $self->{last_attribute_value_state};    undef $self->{ct}; # current token
929    $self->{char} = [];    undef $self->{ca}; # current attribute
930    # $self->{next_input_character}    undef $self->{last_stag_name}; # last emitted start tag name
931      #$self->{prev_state}; # initialized when used
932      delete $self->{self_closing};
933      $self->{char_buffer} = '';
934      $self->{char_buffer_pos} = 0;
935      $self->{prev_char} = [-1, -1, -1];
936      $self->{nc} = -1; # next input character
937      #$self->{next_nc}
938    !!!next-input-character;    !!!next-input-character;
939    $self->{token} = [];    $self->{token} = [];
940      # $self->{escape}
941  } # _initialize_tokenizer  } # _initialize_tokenizer
942    
943  ## A token has:  ## A token has:
944  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
945  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
946  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
947      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
948  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{pubid} (DOCTYPE_TOKEN)
949  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{sysid} (DOCTYPE_TOKEN)
950  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
951    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
952  ## Macros  ##        ->{name}
953  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
954  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
955  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
956    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
957    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
958    ##     while the token is pushed back to the stack.
959    
960  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
961    
# Line 441  sub _initialize_tokenizer ($) { Line 965  sub _initialize_tokenizer ($) {
965  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
966  ## and removed from the list.  ## and removed from the list.
967    
968    ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
969    ## (This requirement was dropped from HTML5 spec, unfortunately.)
970    
971  sub _get_next_token ($) {  sub _get_next_token ($) {
972    my $self = shift;    my $self = shift;
973    
974      if ($self->{self_closing}) {
975        !!!parse-error (type => 'nestc', token => $self->{ct});
976        ## NOTE: The |self_closing| flag is only set by start tag token.
977        ## In addition, when a start tag token is emitted, it is always set to
978        ## |ct|.
979        delete $self->{self_closing};
980      }
981    
982    if (@{$self->{token}}) {    if (@{$self->{token}}) {
983        $self->{self_closing} = $self->{token}->[0]->{self_closing};
984      return shift @{$self->{token}};      return shift @{$self->{token}};
985    }    }
986    
987    A: {    A: {
988      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
989        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{nc} == 0x0026) { # &
990          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
991              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
992            $self->{state} = 'entity data';            !!!cp (1);
993              ## NOTE: In the spec, the tokenizer is switched to the
994              ## "entity data state".  In this implementation, the tokenizer
995              ## is switched to the |ENTITY_STATE|, which is an implementation
996              ## of the "consume a character reference" algorithm.
997              $self->{entity_add} = -1;
998              $self->{prev_state} = DATA_STATE;
999              $self->{state} = ENTITY_STATE;
1000            !!!next-input-character;            !!!next-input-character;
1001            redo A;            redo A;
1002          } else {          } else {
1003              !!!cp (2);
1004            #            #
1005          }          }
1006        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1007          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
1008            unless ($self->{escape}) {            unless ($self->{escape}) {
1009              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
1010                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
1011                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
1012                  !!!cp (3);
1013                $self->{escape} = 1;                $self->{escape} = 1;
1014                } else {
1015                  !!!cp (4);
1016              }              }
1017              } else {
1018                !!!cp (5);
1019            }            }
1020          }          }
1021                    
1022          #          #
1023        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{nc} == 0x003C) { # <
1024          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1025              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
1026               not $self->{escape})) {               not $self->{escape})) {
1027            $self->{state} = 'tag open';            !!!cp (6);
1028              $self->{state} = TAG_OPEN_STATE;
1029            !!!next-input-character;            !!!next-input-character;
1030            redo A;            redo A;
1031          } else {          } else {
1032              !!!cp (7);
1033            #            #
1034          }          }
1035        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1036          if ($self->{escape} and          if ($self->{escape} and
1037              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1038               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
1039            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
1040                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
1041              delete $self->{escape};              delete $self->{escape};
1042              } else {
1043                !!!cp (9);
1044            }            }
1045            } else {
1046              !!!cp (10);
1047          }          }
1048                    
1049          #          #
1050        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1051          !!!emit ({type => 'end-of-file'});          !!!cp (11);
1052            !!!emit ({type => END_OF_FILE_TOKEN,
1053                      line => $self->{line}, column => $self->{column}});
1054          last A; ## TODO: ok?          last A; ## TODO: ok?
1055          } else {
1056            !!!cp (12);
1057        }        }
1058        # Anything else        # Anything else
1059        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
1060                     data => chr $self->{next_input_character}};                     data => chr $self->{nc},
1061                       line => $self->{line}, column => $self->{column},
1062                      };
1063          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1064    
1065        ## Stay in the data state        ## Stay in the data state
1066        !!!next-input-character;        !!!next-input-character;
1067    
1068        !!!emit ($token);        !!!emit ($token);
1069    
1070        redo A;        redo A;
1071      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1072        ## (cannot happen in CDATA state)        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1073                  if ($self->{nc} == 0x002F) { # /
1074        my $token = $self->_tokenize_attempt_to_consume_an_entity;            !!!cp (15);
   
       $self->{state} = 'data';  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!emit ({type => 'character', data => '&'});  
       } else {  
         !!!emit ($token);  
       }  
   
       redo A;  
     } elsif ($self->{state} eq 'tag open') {  
       if ($self->{content_model_flag} eq 'RCDATA' or  
           $self->{content_model_flag} eq 'CDATA') {  
         if ($self->{next_input_character} == 0x002F) { # /  
1075            !!!next-input-character;            !!!next-input-character;
1076            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1077            redo A;            redo A;
1078          } else {          } else {
1079              !!!cp (16);
1080            ## reconsume            ## reconsume
1081            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1082    
1083            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1084                        line => $self->{line_prev},
1085                        column => $self->{column_prev},
1086                       });
1087    
1088            redo A;            redo A;
1089          }          }
1090        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1091          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{nc} == 0x0021) { # !
1092            $self->{state} = 'markup declaration open';            !!!cp (17);
1093              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1094            !!!next-input-character;            !!!next-input-character;
1095            redo A;            redo A;
1096          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1097            $self->{state} = 'close tag open';            !!!cp (18);
1098              $self->{state} = CLOSE_TAG_OPEN_STATE;
1099            !!!next-input-character;            !!!next-input-character;
1100            redo A;            redo A;
1101          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{nc} and
1102                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1103            $self->{current_token}            !!!cp (19);
1104              = {type => 'start tag',            $self->{ct}
1105                 tag_name => chr ($self->{next_input_character} + 0x0020)};              = {type => START_TAG_TOKEN,
1106            $self->{state} = 'tag name';                 tag_name => chr ($self->{nc} + 0x0020),
1107                   line => $self->{line_prev},
1108                   column => $self->{column_prev}};
1109              $self->{state} = TAG_NAME_STATE;
1110            !!!next-input-character;            !!!next-input-character;
1111            redo A;            redo A;
1112          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{nc} and
1113                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1114            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1115                              tag_name => chr ($self->{next_input_character})};            $self->{ct} = {type => START_TAG_TOKEN,
1116            $self->{state} = 'tag name';                                      tag_name => chr ($self->{nc}),
1117                                        line => $self->{line_prev},
1118                                        column => $self->{column_prev}};
1119              $self->{state} = TAG_NAME_STATE;
1120            !!!next-input-character;            !!!next-input-character;
1121            redo A;            redo A;
1122          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1123            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1124            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1125                              line => $self->{line_prev},
1126                              column => $self->{column_prev});
1127              $self->{state} = DATA_STATE;
1128            !!!next-input-character;            !!!next-input-character;
1129    
1130            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1131                        line => $self->{line_prev},
1132                        column => $self->{column_prev},
1133                       });
1134    
1135            redo A;            redo A;
1136          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1137            !!!parse-error (type => 'pio');            !!!cp (22);
1138            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1139            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1140                              column => $self->{column_prev});
1141              $self->{state} = BOGUS_COMMENT_STATE;
1142              $self->{ct} = {type => COMMENT_TOKEN, data => '',
1143                                        line => $self->{line_prev},
1144                                        column => $self->{column_prev},
1145                                       };
1146              ## $self->{nc} is intentionally left as is
1147            redo A;            redo A;
1148          } else {          } else {
1149            !!!parse-error (type => 'bare stago');            !!!cp (23);
1150            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1151                              line => $self->{line_prev},
1152                              column => $self->{column_prev});
1153              $self->{state} = DATA_STATE;
1154            ## reconsume            ## reconsume
1155    
1156            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1157                        line => $self->{line_prev},
1158                        column => $self->{column_prev},
1159                       });
1160    
1161            redo A;            redo A;
1162          }          }
1163        } else {        } else {
1164          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1165        }        }
1166      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1167        if ($self->{content_model_flag} eq 'RCDATA' or        ## NOTE: The "close tag open state" in the spec is implemented as
1168            $self->{content_model_flag} eq 'CDATA') {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1169          my @next_char;  
1170          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1171            push @next_char, $self->{next_input_character};        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1172            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);          if (defined $self->{last_stag_name}) {
1173            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1174            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {            $self->{s_kwd} = '';
1175              !!!next-input-character;            ## Reconsume.
             next TAGNAME;  
           } else {  
             !!!parse-error (type => 'unmatched end tag');  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = 'data';  
   
             !!!emit ({type => 'character', data => '</'});  
   
             redo A;  
           }  
         }  
         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 => '</'});  
   
1176            redo A;            redo A;
1177          } else {          } else {
1178            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1179            !!!back-next-input-character (@next_char);            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1180            # and consume...            !!!cp (28);
1181              $self->{state} = DATA_STATE;
1182              ## Reconsume.
1183              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1184                        line => $l, column => $c,
1185                       });
1186              redo A;
1187          }          }
1188        }        }
1189          
1190        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{nc} and
1191            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1192          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1193                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{ct}
1194          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1195          !!!next-input-character;                 tag_name => chr ($self->{nc} + 0x0020),
1196          redo A;                 line => $l, column => $c};
1197        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1198                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1199          $self->{current_token} = {type => 'end tag',          redo A;
1200                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{nc} and
1201          $self->{state} = 'tag name';                 $self->{nc} <= 0x007A) { # a..z
1202          !!!next-input-character;          !!!cp (30);
1203          redo A;          $self->{ct} = {type => END_TAG_TOKEN,
1204        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{nc}),
1205          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1206          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1207            !!!next-input-character;
1208            redo A;
1209          } elsif ($self->{nc} == 0x003E) { # >
1210            !!!cp (31);
1211            !!!parse-error (type => 'empty end tag',
1212                            line => $self->{line_prev}, ## "<" in "</>"
1213                            column => $self->{column_prev} - 1);
1214            $self->{state} = DATA_STATE;
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1218            !!!cp (32);
1219          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1220          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1221          # reconsume          # reconsume
1222    
1223          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1224                      line => $l, column => $c,
1225                     });
1226    
1227          redo A;          redo A;
1228        } else {        } else {
1229            !!!cp (33);
1230          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1231          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1232          ## $self->{next_input_character} is intentionally left as is          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1233          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1234        }                                    column => $self->{column_prev} - 1,
1235      } elsif ($self->{state} eq 'tag name') {                                   };
1236        if ($self->{next_input_character} == 0x0009 or # HT          ## NOTE: $self->{nc} is intentionally left as is.
1237            $self->{next_input_character} == 0x000A or # LF          ## Although the "anything else" case of the spec not explicitly
1238            $self->{next_input_character} == 0x000B or # VT          ## states that the next input character is to be reconsumed,
1239            $self->{next_input_character} == 0x000C or # FF          ## it will be included to the |data| of the comment token
1240            $self->{next_input_character} == 0x0020) { # SP          ## generated from the bogus end tag, as defined in the
1241          $self->{state} = 'before attribute name';          ## "bogus comment state" entry.
1242          !!!next-input-character;          redo A;
1243          redo A;        }
1244        } elsif ($self->{next_input_character} == 0x003E) { # >      } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1245          if ($self->{current_token}->{type} eq 'start tag') {        my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1246            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};        if (length $ch) {
1247          } elsif ($self->{current_token}->{type} eq 'end tag') {          my $CH = $ch;
1248            $self->{content_model_flag} = 'PCDATA'; # MUST          $ch =~ tr/a-z/A-Z/;
1249            if ($self->{current_token}->{attributes}) {          my $nch = chr $self->{nc};
1250              !!!parse-error (type => 'end tag attribute');          if ($nch eq $ch or $nch eq $CH) {
1251            }            !!!cp (24);
1252              ## Stay in the state.
1253              $self->{s_kwd} .= $nch;
1254              !!!next-input-character;
1255              redo A;
1256          } else {          } else {
1257            die "$0: $self->{current_token}->{type}: Unknown token type";            !!!cp (25);
1258              $self->{state} = DATA_STATE;
1259              ## Reconsume.
1260              !!!emit ({type => CHARACTER_TOKEN,
1261                        data => '</' . $self->{s_kwd},
1262                        line => $self->{line_prev},
1263                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1264                       });
1265              redo A;
1266          }          }
1267          $self->{state} = 'data';        } else { # after "<{tag-name}"
1268          !!!next-input-character;          unless ({
1269                     0x0009 => 1, # HT
1270          !!!emit ($self->{current_token}); # start tag or end tag                   0x000A => 1, # LF
1271          undef $self->{current_token};                   0x000B => 1, # VT
1272                     0x000C => 1, # FF
1273          redo A;                   0x0020 => 1, # SP
1274        } elsif (0x0041 <= $self->{next_input_character} and                   0x003E => 1, # >
1275                 $self->{next_input_character} <= 0x005A) { # A..Z                   0x002F => 1, # /
1276          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);                   -1 => 1, # EOF
1277                    }->{$self->{nc}}) {
1278              !!!cp (26);
1279              ## Reconsume.
1280              $self->{state} = DATA_STATE;
1281              !!!emit ({type => CHARACTER_TOKEN,
1282                        data => '</' . $self->{s_kwd},
1283                        line => $self->{line_prev},
1284                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1285                       });
1286              redo A;
1287            } else {
1288              !!!cp (27);
1289              $self->{ct}
1290                  = {type => END_TAG_TOKEN,
1291                     tag_name => $self->{last_stag_name},
1292                     line => $self->{line_prev},
1293                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1294              $self->{state} = TAG_NAME_STATE;
1295              ## Reconsume.
1296              redo A;
1297            }
1298          }
1299        } elsif ($self->{state} == TAG_NAME_STATE) {
1300          if ($self->{nc} == 0x0009 or # HT
1301              $self->{nc} == 0x000A or # LF
1302              $self->{nc} == 0x000B or # VT
1303              $self->{nc} == 0x000C or # FF
1304              $self->{nc} == 0x0020) { # SP
1305            !!!cp (34);
1306            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1307            !!!next-input-character;
1308            redo A;
1309          } elsif ($self->{nc} == 0x003E) { # >
1310            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1311              !!!cp (35);
1312              $self->{last_stag_name} = $self->{ct}->{tag_name};
1313            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1314              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1315              #if ($self->{ct}->{attributes}) {
1316              #  ## NOTE: This should never be reached.
1317              #  !!! cp (36);
1318              #  !!! parse-error (type => 'end tag attribute');
1319              #} else {
1320                !!!cp (37);
1321              #}
1322            } else {
1323              die "$0: $self->{ct}->{type}: Unknown token type";
1324            }
1325            $self->{state} = DATA_STATE;
1326            !!!next-input-character;
1327    
1328            !!!emit ($self->{ct}); # start tag or end tag
1329    
1330            redo A;
1331          } elsif (0x0041 <= $self->{nc} and
1332                   $self->{nc} <= 0x005A) { # A..Z
1333            !!!cp (38);
1334            $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1335            # start tag or end tag            # start tag or end tag
1336          ## Stay in this state          ## Stay in this state
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
1339        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1340          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1341          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1342            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (39);
1343          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1344            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1345            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1346              !!!parse-error (type => 'end tag attribute');            #if ($self->{ct}->{attributes}) {
1347            }            #  ## NOTE: This state should never be reached.
1348              #  !!! cp (40);
1349              #  !!! parse-error (type => 'end tag attribute');
1350              #} else {
1351                !!!cp (41);
1352              #}
1353          } else {          } else {
1354            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1355          }          }
1356          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1357          # reconsume          # reconsume
1358    
1359          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1360    
1361          redo A;          redo A;
1362        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1363            !!!cp (42);
1364            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1365          !!!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  
1366          redo A;          redo A;
1367        } else {        } else {
1368          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1369            $self->{ct}->{tag_name} .= chr $self->{nc};
1370            # start tag or end tag            # start tag or end tag
1371          ## Stay in the state          ## Stay in the state
1372          !!!next-input-character;          !!!next-input-character;
1373          redo A;          redo A;
1374        }        }
1375      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1376        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1377            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1378            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1379            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1380            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1381            !!!cp (45);
1382          ## Stay in the state          ## Stay in the state
1383          !!!next-input-character;          !!!next-input-character;
1384          redo A;          redo A;
1385        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1386          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1387            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (46);
1388          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1389            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1390            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1391              if ($self->{ct}->{attributes}) {
1392                !!!cp (47);
1393              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1394              } else {
1395                !!!cp (48);
1396            }            }
1397          } else {          } else {
1398            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1399          }          }
1400          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1401          !!!next-input-character;          !!!next-input-character;
1402    
1403          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1404    
1405          redo A;          redo A;
1406        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1407                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1408          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1409                                value => ''};          $self->{ca}
1410          $self->{state} = 'attribute name';              = {name => chr ($self->{nc} + 0x0020),
1411                   value => '',
1412                   line => $self->{line}, column => $self->{column}};
1413            $self->{state} = ATTRIBUTE_NAME_STATE;
1414          !!!next-input-character;          !!!next-input-character;
1415          redo A;          redo A;
1416        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1417            !!!cp (50);
1418            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1419          !!!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  
1420          redo A;          redo A;
1421        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1422          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1423          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1424            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (52);
1425          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1426            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1427            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1428              if ($self->{ct}->{attributes}) {
1429                !!!cp (53);
1430              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1431              } else {
1432                !!!cp (54);
1433            }            }
1434          } else {          } else {
1435            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1436          }          }
1437          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1438          # reconsume          # reconsume
1439    
1440          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1441    
1442          redo A;          redo A;
1443        } else {        } else {
1444          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1445                                value => ''};               0x0022 => 1, # "
1446          $self->{state} = 'attribute name';               0x0027 => 1, # '
1447                 0x003D => 1, # =
1448                }->{$self->{nc}}) {
1449              !!!cp (55);
1450              !!!parse-error (type => 'bad attribute name');
1451            } else {
1452              !!!cp (56);
1453            }
1454            $self->{ca}
1455                = {name => chr ($self->{nc}),
1456                   value => '',
1457                   line => $self->{line}, column => $self->{column}};
1458            $self->{state} = ATTRIBUTE_NAME_STATE;
1459          !!!next-input-character;          !!!next-input-character;
1460          redo A;          redo A;
1461        }        }
1462      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1463        my $before_leave = sub {        my $before_leave = sub {
1464          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1465              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1466            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1467            ## Discard $self->{current_attribute} # MUST            !!!parse-error (type => 'duplicate attribute', text => $self->{ca}->{name}, line => $self->{ca}->{line}, column => $self->{ca}->{column});
1468          } else {            ## Discard $self->{ca} # MUST
1469            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}          } else {
1470              = $self->{current_attribute};            !!!cp (58);
1471              $self->{ct}->{attributes}->{$self->{ca}->{name}}
1472                = $self->{ca};
1473          }          }
1474        }; # $before_leave        }; # $before_leave
1475    
1476        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1477            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1478            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1479            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1480            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1481            !!!cp (59);
1482          $before_leave->();          $before_leave->();
1483          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1484          !!!next-input-character;          !!!next-input-character;
1485          redo A;          redo A;
1486        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1487            !!!cp (60);
1488          $before_leave->();          $before_leave->();
1489          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1490          !!!next-input-character;          !!!next-input-character;
1491          redo A;          redo A;
1492        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1493          $before_leave->();          $before_leave->();
1494          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1495            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (61);
1496          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1497            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1498            if ($self->{current_token}->{attributes}) {            !!!cp (62);
1499              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1500              if ($self->{ct}->{attributes}) {
1501              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1502            }            }
1503          } else {          } else {
1504            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1505          }          }
1506          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1507          !!!next-input-character;          !!!next-input-character;
1508    
1509          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1510    
1511          redo A;          redo A;
1512        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1513                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1514          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1515            $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1516          ## Stay in the state          ## Stay in the state
1517          !!!next-input-character;          !!!next-input-character;
1518          redo A;          redo A;
1519        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1520            !!!cp (64);
1521          $before_leave->();          $before_leave->();
1522            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1523          !!!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  
1524          redo A;          redo A;
1525        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1526          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1527          $before_leave->();          $before_leave->();
1528          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1529            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (66);
1530          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1531            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1532            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1533              if ($self->{ct}->{attributes}) {
1534                !!!cp (67);
1535              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1536              } else {
1537                ## NOTE: This state should never be reached.
1538                !!!cp (68);
1539            }            }
1540          } else {          } else {
1541            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1542          }          }
1543          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1544          # reconsume          # reconsume
1545    
1546          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1547    
1548          redo A;          redo A;
1549        } else {        } else {
1550          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x0022 or # "
1551                $self->{nc} == 0x0027) { # '
1552              !!!cp (69);
1553              !!!parse-error (type => 'bad attribute name');
1554            } else {
1555              !!!cp (70);
1556            }
1557            $self->{ca}->{name} .= chr ($self->{nc});
1558          ## Stay in the state          ## Stay in the state
1559          !!!next-input-character;          !!!next-input-character;
1560          redo A;          redo A;
1561        }        }
1562      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1563        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1564            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1565            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1566            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1567            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1568            !!!cp (71);
1569          ## Stay in the state          ## Stay in the state
1570          !!!next-input-character;          !!!next-input-character;
1571          redo A;          redo A;
1572        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1573          $self->{state} = 'before attribute value';          !!!cp (72);
1574            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1575          !!!next-input-character;          !!!next-input-character;
1576          redo A;          redo A;
1577        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1578          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1579            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (73);
1580          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1581            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1582            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1583              if ($self->{ct}->{attributes}) {
1584                !!!cp (74);
1585              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1586              } else {
1587                ## NOTE: This state should never be reached.
1588                !!!cp (75);
1589            }            }
1590          } else {          } else {
1591            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1592          }          }
1593          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1594          !!!next-input-character;          !!!next-input-character;
1595    
1596          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1597    
1598          redo A;          redo A;
1599        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1600                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1601          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1602                                value => ''};          $self->{ca}
1603          $self->{state} = 'attribute name';              = {name => chr ($self->{nc} + 0x0020),
1604                   value => '',
1605                   line => $self->{line}, column => $self->{column}};
1606            $self->{state} = ATTRIBUTE_NAME_STATE;
1607          !!!next-input-character;          !!!next-input-character;
1608          redo A;          redo A;
1609        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1610            !!!cp (77);
1611            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1612          !!!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  
1613          redo A;          redo A;
1614        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1615          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1616          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1617            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (79);
1618          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1619            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1620            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1621              if ($self->{ct}->{attributes}) {
1622                !!!cp (80);
1623              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1624              } else {
1625                ## NOTE: This state should never be reached.
1626                !!!cp (81);
1627            }            }
1628          } else {          } else {
1629            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1630          }          }
1631          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1632          # reconsume          # reconsume
1633    
1634          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1635    
1636          redo A;          redo A;
1637        } else {        } else {
1638          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{nc} == 0x0022 or # "
1639                                value => ''};              $self->{nc} == 0x0027) { # '
1640          $self->{state} = 'attribute name';            !!!cp (78);
1641              !!!parse-error (type => 'bad attribute name');
1642            } else {
1643              !!!cp (82);
1644            }
1645            $self->{ca}
1646                = {name => chr ($self->{nc}),
1647                   value => '',
1648                   line => $self->{line}, column => $self->{column}};
1649            $self->{state} = ATTRIBUTE_NAME_STATE;
1650          !!!next-input-character;          !!!next-input-character;
1651          redo A;                  redo A;        
1652        }        }
1653      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1654        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1655            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1656            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1657            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1658            $self->{next_input_character} == 0x0020) { # SP                  $self->{nc} == 0x0020) { # SP      
1659            !!!cp (83);
1660          ## Stay in the state          ## Stay in the state
1661          !!!next-input-character;          !!!next-input-character;
1662          redo A;          redo A;
1663        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1664          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1665            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1666          !!!next-input-character;          !!!next-input-character;
1667          redo A;          redo A;
1668        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1669          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1670            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1671          ## reconsume          ## reconsume
1672          redo A;          redo A;
1673        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1674          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1675            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1679          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1680            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1681          } elsif ($self->{current_token}->{type} eq 'end tag') {            !!!cp (87);
1682            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{last_stag_name} = $self->{ct}->{tag_name};
1683            if ($self->{current_token}->{attributes}) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1684              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1685              if ($self->{ct}->{attributes}) {
1686                !!!cp (88);
1687              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1688              } else {
1689                ## NOTE: This state should never be reached.
1690                !!!cp (89);
1691            }            }
1692          } else {          } else {
1693            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1694          }          }
1695          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1696          !!!next-input-character;          !!!next-input-character;
1697    
1698          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1699    
1700          redo A;          redo A;
1701        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1702          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1703          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1704            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (90);
1705          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1706            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1707            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1708              if ($self->{ct}->{attributes}) {
1709                !!!cp (91);
1710              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1711              } else {
1712                ## NOTE: This state should never be reached.
1713                !!!cp (92);
1714            }            }
1715          } else {          } else {
1716            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1717          }          }
1718          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1719          ## reconsume          ## reconsume
1720    
1721          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1722    
1723          redo A;          redo A;
1724        } else {        } else {
1725          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x003D) { # =
1726          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1727              !!!parse-error (type => 'bad attribute value');
1728            } else {
1729              !!!cp (94);
1730            }
1731            $self->{ca}->{value} .= chr ($self->{nc});
1732            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1733          !!!next-input-character;          !!!next-input-character;
1734          redo A;          redo A;
1735        }        }
1736      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1737        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1738          $self->{state} = 'before attribute name';          !!!cp (95);
1739            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1743          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1744          $self->{state} = 'entity in attribute value';          ## NOTE: In the spec, the tokenizer is switched to the
1745            ## "entity in attribute value state".  In this implementation, the
1746            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1747            ## implementation of the "consume a character reference" algorithm.
1748            $self->{prev_state} = $self->{state};
1749            $self->{entity_add} = 0x0022; # "
1750            $self->{state} = ENTITY_STATE;
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1754          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1755          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1756            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (97);
1757          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1758            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1759            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1760              if ($self->{ct}->{attributes}) {
1761                !!!cp (98);
1762              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1763              } else {
1764                ## NOTE: This state should never be reached.
1765                !!!cp (99);
1766            }            }
1767          } else {          } else {
1768            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1769          }          }
1770          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1771          ## reconsume          ## reconsume
1772    
1773          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1774    
1775          redo A;          redo A;
1776        } else {        } else {
1777          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1778            $self->{ca}->{value} .= chr ($self->{nc});
1779            $self->{read_until}->($self->{ca}->{value},
1780                                  q["&],
1781                                  length $self->{ca}->{value});
1782    
1783          ## Stay in the state          ## Stay in the state
1784          !!!next-input-character;          !!!next-input-character;
1785          redo A;          redo A;
1786        }        }
1787      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1788        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1789          $self->{state} = 'before attribute name';          !!!cp (101);
1790            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1791            !!!next-input-character;
1792            redo A;
1793          } elsif ($self->{nc} == 0x0026) { # &
1794            !!!cp (102);
1795            ## NOTE: In the spec, the tokenizer is switched to the
1796            ## "entity in attribute value state".  In this implementation, the
1797            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1798            ## implementation of the "consume a character reference" algorithm.
1799            $self->{entity_add} = 0x0027; # '
1800            $self->{prev_state} = $self->{state};
1801            $self->{state} = ENTITY_STATE;
1802          !!!next-input-character;          !!!next-input-character;
1803          redo A;          redo A;
1804        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == -1) {
         $self->{last_attribute_value_state} = 'attribute value (single-quoted)';  
         $self->{state} = 'entity in attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1805          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1806          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1807            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (103);
1808          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1809            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1810            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1811              if ($self->{ct}->{attributes}) {
1812                !!!cp (104);
1813              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1814              } else {
1815                ## NOTE: This state should never be reached.
1816                !!!cp (105);
1817            }            }
1818          } else {          } else {
1819            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1820          }          }
1821          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1822          ## reconsume          ## reconsume
1823    
1824          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1825    
1826          redo A;          redo A;
1827        } else {        } else {
1828          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1829            $self->{ca}->{value} .= chr ($self->{nc});
1830            $self->{read_until}->($self->{ca}->{value},
1831                                  q['&],
1832                                  length $self->{ca}->{value});
1833    
1834          ## Stay in the state          ## Stay in the state
1835          !!!next-input-character;          !!!next-input-character;
1836          redo A;          redo A;
1837        }        }
1838      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1839        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1840            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1841            $self->{next_input_character} == 0x000B or # HT            $self->{nc} == 0x000B or # HT
1842            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1843            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1844          $self->{state} = 'before attribute name';          !!!cp (107);
1845          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1846          redo A;          !!!next-input-character;
1847        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1848          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{nc} == 0x0026) { # &
1849          $self->{state} = 'entity in attribute value';          !!!cp (108);
1850          !!!next-input-character;          ## NOTE: In the spec, the tokenizer is switched to the
1851          redo A;          ## "entity in attribute value state".  In this implementation, the
1852        } elsif ($self->{next_input_character} == 0x003E) { # >          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1853          if ($self->{current_token}->{type} eq 'start tag') {          ## implementation of the "consume a character reference" algorithm.
1854            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          $self->{entity_add} = -1;
1855          } elsif ($self->{current_token}->{type} eq 'end tag') {          $self->{prev_state} = $self->{state};
1856            $self->{content_model_flag} = 'PCDATA'; # MUST          $self->{state} = ENTITY_STATE;
1857            if ($self->{current_token}->{attributes}) {          !!!next-input-character;
1858            redo A;
1859          } elsif ($self->{nc} == 0x003E) { # >
1860            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1861              !!!cp (109);
1862              $self->{last_stag_name} = $self->{ct}->{tag_name};
1863            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1864              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1865              if ($self->{ct}->{attributes}) {
1866                !!!cp (110);
1867              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1868              } else {
1869                ## NOTE: This state should never be reached.
1870                !!!cp (111);
1871            }            }
1872          } else {          } else {
1873            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1874          }          }
1875          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1876          !!!next-input-character;          !!!next-input-character;
1877    
1878          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1879    
1880          redo A;          redo A;
1881        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1882          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1883          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1884            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (112);
1885          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1886            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1887            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1888              if ($self->{ct}->{attributes}) {
1889                !!!cp (113);
1890              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1891              } else {
1892                ## NOTE: This state should never be reached.
1893                !!!cp (114);
1894            }            }
1895          } else {          } else {
1896            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1897          }          }
1898          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1899          ## reconsume          ## reconsume
1900    
1901          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1902    
1903          redo A;          redo A;
1904        } else {        } else {
1905          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1906                 0x0022 => 1, # "
1907                 0x0027 => 1, # '
1908                 0x003D => 1, # =
1909                }->{$self->{nc}}) {
1910              !!!cp (115);
1911              !!!parse-error (type => 'bad attribute value');
1912            } else {
1913              !!!cp (116);
1914            }
1915            $self->{ca}->{value} .= chr ($self->{nc});
1916            $self->{read_until}->($self->{ca}->{value},
1917                                  q["'=& >],
1918                                  length $self->{ca}->{value});
1919    
1920          ## Stay in the state          ## Stay in the state
1921          !!!next-input-character;          !!!next-input-character;
1922          redo A;          redo A;
1923        }        }
1924      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1925        my $token = $self->_tokenize_attempt_to_consume_an_entity;        if ($self->{nc} == 0x0009 or # HT
1926              $self->{nc} == 0x000A or # LF
1927              $self->{nc} == 0x000B or # VT
1928              $self->{nc} == 0x000C or # FF
1929              $self->{nc} == 0x0020) { # SP
1930            !!!cp (118);
1931            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1932            !!!next-input-character;
1933            redo A;
1934          } elsif ($self->{nc} == 0x003E) { # >
1935            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1936              !!!cp (119);
1937              $self->{last_stag_name} = $self->{ct}->{tag_name};
1938            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1939              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1940              if ($self->{ct}->{attributes}) {
1941                !!!cp (120);
1942                !!!parse-error (type => 'end tag attribute');
1943              } else {
1944                ## NOTE: This state should never be reached.
1945                !!!cp (121);
1946              }
1947            } else {
1948              die "$0: $self->{ct}->{type}: Unknown token type";
1949            }
1950            $self->{state} = DATA_STATE;
1951            !!!next-input-character;
1952    
1953        unless (defined $token) {          !!!emit ($self->{ct}); # start tag or end tag
1954          $self->{current_attribute}->{value} .= '&';  
1955            redo A;
1956          } elsif ($self->{nc} == 0x002F) { # /
1957            !!!cp (122);
1958            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1959            !!!next-input-character;
1960            redo A;
1961          } elsif ($self->{nc} == -1) {
1962            !!!parse-error (type => 'unclosed tag');
1963            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1964              !!!cp (122.3);
1965              $self->{last_stag_name} = $self->{ct}->{tag_name};
1966            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1967              if ($self->{ct}->{attributes}) {
1968                !!!cp (122.1);
1969                !!!parse-error (type => 'end tag attribute');
1970              } else {
1971                ## NOTE: This state should never be reached.
1972                !!!cp (122.2);
1973              }
1974            } else {
1975              die "$0: $self->{ct}->{type}: Unknown token type";
1976            }
1977            $self->{state} = DATA_STATE;
1978            ## Reconsume.
1979            !!!emit ($self->{ct}); # start tag or end tag
1980            redo A;
1981        } else {        } else {
1982          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
1983          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
1984            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1985            ## reconsume
1986            redo A;
1987        }        }
1988        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1989          if ($self->{nc} == 0x003E) { # >
1990            if ($self->{ct}->{type} == END_TAG_TOKEN) {
1991              !!!cp ('124.2');
1992              !!!parse-error (type => 'nestc', token => $self->{ct});
1993              ## TODO: Different type than slash in start tag
1994              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1995              if ($self->{ct}->{attributes}) {
1996                !!!cp ('124.4');
1997                !!!parse-error (type => 'end tag attribute');
1998              } else {
1999                !!!cp ('124.5');
2000              }
2001              ## TODO: Test |<title></title/>|
2002            } else {
2003              !!!cp ('124.3');
2004              $self->{self_closing} = 1;
2005            }
2006    
2007        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
2008        # next-input-character is already done          !!!next-input-character;
       redo A;  
     } elsif ($self->{state} eq 'bogus comment') {  
       ## (only happen if PCDATA state)  
         
       my $token = {type => 'comment', data => ''};  
   
       BC: {  
         if ($self->{next_input_character} == 0x003E) { # >  
           $self->{state} = 'data';  
           !!!next-input-character;  
   
           !!!emit ($token);  
   
           redo A;  
         } elsif ($self->{next_input_character} == -1) {  
           $self->{state} = 'data';  
           ## reconsume  
2009    
2010            !!!emit ($token);          !!!emit ($self->{ct}); # start tag or end tag
2011    
2012            redo A;          redo A;
2013          } elsif ($self->{nc} == -1) {
2014            !!!parse-error (type => 'unclosed tag');
2015            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2016              !!!cp (124.7);
2017              $self->{last_stag_name} = $self->{ct}->{tag_name};
2018            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2019              if ($self->{ct}->{attributes}) {
2020                !!!cp (124.5);
2021                !!!parse-error (type => 'end tag attribute');
2022              } else {
2023                ## NOTE: This state should never be reached.
2024                !!!cp (124.6);
2025              }
2026          } else {          } else {
2027            $token->{data} .= chr ($self->{next_input_character});            die "$0: $self->{ct}->{type}: Unknown token type";
           !!!next-input-character;  
           redo BC;  
2028          }          }
2029        } # BC          $self->{state} = DATA_STATE;
2030      } elsif ($self->{state} eq 'markup declaration open') {          ## Reconsume.
2031            !!!emit ($self->{ct}); # start tag or end tag
2032            redo A;
2033          } else {
2034            !!!cp ('124.4');
2035            !!!parse-error (type => 'nestc');
2036            ## TODO: This error type is wrong.
2037            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2038            ## Reconsume.
2039            redo A;
2040          }
2041        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2042        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2043    
2044        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2045        push @next_char, $self->{next_input_character};        ## consumes characters one-by-one basis.
2046                
2047        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x003E) { # >
2048            !!!cp (124);
2049            $self->{state} = DATA_STATE;
2050          !!!next-input-character;          !!!next-input-character;
2051          push @next_char, $self->{next_input_character};  
2052          if ($self->{next_input_character} == 0x002D) { # -          !!!emit ($self->{ct}); # comment
2053            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2054            $self->{state} = 'comment';        } elsif ($self->{nc} == -1) {
2055            !!!next-input-character;          !!!cp (125);
2056            redo A;          $self->{state} = DATA_STATE;
2057          }          ## reconsume
2058        } elsif ($self->{next_input_character} == 0x0044 or # D  
2059                 $self->{next_input_character} == 0x0064) { # d          !!!emit ($self->{ct}); # comment
2060            redo A;
2061          } else {
2062            !!!cp (126);
2063            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2064            $self->{read_until}->($self->{ct}->{data},
2065                                  q[>],
2066                                  length $self->{ct}->{data});
2067    
2068            ## Stay in the state.
2069          !!!next-input-character;          !!!next-input-character;
2070          push @next_char, $self->{next_input_character};          redo A;
2071          if ($self->{next_input_character} == 0x004F or # O        }
2072              $self->{next_input_character} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2073            !!!next-input-character;        ## (only happen if PCDATA state)
2074            push @next_char, $self->{next_input_character};        
2075            if ($self->{next_input_character} == 0x0043 or # C        if ($self->{nc} == 0x002D) { # -
2076                $self->{next_input_character} == 0x0063) { # c          !!!cp (133);
2077              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2078              push @next_char, $self->{next_input_character};          !!!next-input-character;
2079              if ($self->{next_input_character} == 0x0054 or # T          redo A;
2080                  $self->{next_input_character} == 0x0074) { # t        } elsif ($self->{nc} == 0x0044 or # D
2081                !!!next-input-character;                 $self->{nc} == 0x0064) { # d
2082                push @next_char, $self->{next_input_character};          ## ASCII case-insensitive.
2083                if ($self->{next_input_character} == 0x0059 or # Y          !!!cp (130);
2084                    $self->{next_input_character} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2085                  !!!next-input-character;          $self->{s_kwd} = chr $self->{nc};
2086                  push @next_char, $self->{next_input_character};          !!!next-input-character;
2087                  if ($self->{next_input_character} == 0x0050 or # P          redo A;
2088                      $self->{next_input_character} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2089                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2090                    push @next_char, $self->{next_input_character};                 $self->{nc} == 0x005B) { # [
2091                    if ($self->{next_input_character} == 0x0045 or # E          !!!cp (135.4);                
2092                        $self->{next_input_character} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2093                      ## ISSUE: What a stupid code this is!          $self->{s_kwd} = '[';
2094                      $self->{state} = 'DOCTYPE';          !!!next-input-character;
2095                      !!!next-input-character;          redo A;
2096                      redo A;        } else {
2097                    }          !!!cp (136);
                 }  
               }  
             }  
           }  
         }  
2098        }        }
2099    
2100        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment',
2101        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2102        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2103        $self->{state} = 'bogus comment';        ## Reconsume.
2104          $self->{state} = BOGUS_COMMENT_STATE;
2105          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2106                                    line => $self->{line_prev},
2107                                    column => $self->{column_prev} - 1,
2108                                   };
2109        redo A;        redo A;
2110              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2111        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2112        ## 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?          !!!cp (127);
2113      } elsif ($self->{state} eq 'comment') {          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2114        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2115          $self->{state} = 'comment dash';                                    column => $self->{column_prev} - 2,
2116                                     };
2117            $self->{state} = COMMENT_START_STATE;
2118            !!!next-input-character;
2119            redo A;
2120          } else {
2121            !!!cp (128);
2122            !!!parse-error (type => 'bogus comment',
2123                            line => $self->{line_prev},
2124                            column => $self->{column_prev} - 2);
2125            $self->{state} = BOGUS_COMMENT_STATE;
2126            ## Reconsume.
2127            $self->{ct} = {type => COMMENT_TOKEN,
2128                                      data => '-',
2129                                      line => $self->{line_prev},
2130                                      column => $self->{column_prev} - 2,
2131                                     };
2132            redo A;
2133          }
2134        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2135          ## ASCII case-insensitive.
2136          if ($self->{nc} == [
2137                undef,
2138                0x004F, # O
2139                0x0043, # C
2140                0x0054, # T
2141                0x0059, # Y
2142                0x0050, # P
2143              ]->[length $self->{s_kwd}] or
2144              $self->{nc} == [
2145                undef,
2146                0x006F, # o
2147                0x0063, # c
2148                0x0074, # t
2149                0x0079, # y
2150                0x0070, # p
2151              ]->[length $self->{s_kwd}]) {
2152            !!!cp (131);
2153            ## Stay in the state.
2154            $self->{s_kwd} .= chr $self->{nc};
2155            !!!next-input-character;
2156            redo A;
2157          } elsif ((length $self->{s_kwd}) == 6 and
2158                   ($self->{nc} == 0x0045 or # E
2159                    $self->{nc} == 0x0065)) { # e
2160            !!!cp (129);
2161            $self->{state} = DOCTYPE_STATE;
2162            $self->{ct} = {type => DOCTYPE_TOKEN,
2163                                      quirks => 1,
2164                                      line => $self->{line_prev},
2165                                      column => $self->{column_prev} - 7,
2166                                     };
2167            !!!next-input-character;
2168            redo A;
2169          } else {
2170            !!!cp (132);        
2171            !!!parse-error (type => 'bogus comment',
2172                            line => $self->{line_prev},
2173                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2174            $self->{state} = BOGUS_COMMENT_STATE;
2175            ## Reconsume.
2176            $self->{ct} = {type => COMMENT_TOKEN,
2177                                      data => $self->{s_kwd},
2178                                      line => $self->{line_prev},
2179                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2180                                     };
2181            redo A;
2182          }
2183        } elsif ($self->{state} == MD_CDATA_STATE) {
2184          if ($self->{nc} == {
2185                '[' => 0x0043, # C
2186                '[C' => 0x0044, # D
2187                '[CD' => 0x0041, # A
2188                '[CDA' => 0x0054, # T
2189                '[CDAT' => 0x0041, # A
2190              }->{$self->{s_kwd}}) {
2191            !!!cp (135.1);
2192            ## Stay in the state.
2193            $self->{s_kwd} .= chr $self->{nc};
2194            !!!next-input-character;
2195            redo A;
2196          } elsif ($self->{s_kwd} eq '[CDATA' and
2197                   $self->{nc} == 0x005B) { # [
2198            !!!cp (135.2);
2199            $self->{ct} = {type => CHARACTER_TOKEN,
2200                                      data => '',
2201                                      line => $self->{line_prev},
2202                                      column => $self->{column_prev} - 7};
2203            $self->{state} = CDATA_SECTION_STATE;
2204            !!!next-input-character;
2205            redo A;
2206          } else {
2207            !!!cp (135.3);
2208            !!!parse-error (type => 'bogus comment',
2209                            line => $self->{line_prev},
2210                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2211            $self->{state} = BOGUS_COMMENT_STATE;
2212            ## Reconsume.
2213            $self->{ct} = {type => COMMENT_TOKEN,
2214                                      data => $self->{s_kwd},
2215                                      line => $self->{line_prev},
2216                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2217                                     };
2218            redo A;
2219          }
2220        } elsif ($self->{state} == COMMENT_START_STATE) {
2221          if ($self->{nc} == 0x002D) { # -
2222            !!!cp (137);
2223            $self->{state} = COMMENT_START_DASH_STATE;
2224            !!!next-input-character;
2225            redo A;
2226          } elsif ($self->{nc} == 0x003E) { # >
2227            !!!cp (138);
2228            !!!parse-error (type => 'bogus comment');
2229            $self->{state} = DATA_STATE;
2230            !!!next-input-character;
2231    
2232            !!!emit ($self->{ct}); # comment
2233    
2234            redo A;
2235          } elsif ($self->{nc} == -1) {
2236            !!!cp (139);
2237            !!!parse-error (type => 'unclosed comment');
2238            $self->{state} = DATA_STATE;
2239            ## reconsume
2240    
2241            !!!emit ($self->{ct}); # comment
2242    
2243            redo A;
2244          } else {
2245            !!!cp (140);
2246            $self->{ct}->{data} # comment
2247                .= chr ($self->{nc});
2248            $self->{state} = COMMENT_STATE;
2249            !!!next-input-character;
2250            redo A;
2251          }
2252        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2253          if ($self->{nc} == 0x002D) { # -
2254            !!!cp (141);
2255            $self->{state} = COMMENT_END_STATE;
2256            !!!next-input-character;
2257            redo A;
2258          } elsif ($self->{nc} == 0x003E) { # >
2259            !!!cp (142);
2260            !!!parse-error (type => 'bogus comment');
2261            $self->{state} = DATA_STATE;
2262          !!!next-input-character;          !!!next-input-character;
2263    
2264            !!!emit ($self->{ct}); # comment
2265    
2266          redo A;          redo A;
2267        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2268            !!!cp (143);
2269          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2270          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2271          ## reconsume          ## reconsume
2272    
2273          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
         undef $self->{current_token};  
2274    
2275          redo A;          redo A;
2276        } else {        } else {
2277          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (144);
2278            $self->{ct}->{data} # comment
2279                .= '-' . chr ($self->{nc});
2280            $self->{state} = COMMENT_STATE;
2281            !!!next-input-character;
2282            redo A;
2283          }
2284        } elsif ($self->{state} == COMMENT_STATE) {
2285          if ($self->{nc} == 0x002D) { # -
2286            !!!cp (145);
2287            $self->{state} = COMMENT_END_DASH_STATE;
2288            !!!next-input-character;
2289            redo A;
2290          } elsif ($self->{nc} == -1) {
2291            !!!cp (146);
2292            !!!parse-error (type => 'unclosed comment');
2293            $self->{state} = DATA_STATE;
2294            ## reconsume
2295    
2296            !!!emit ($self->{ct}); # comment
2297    
2298            redo A;
2299          } else {
2300            !!!cp (147);
2301            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2302            $self->{read_until}->($self->{ct}->{data},
2303                                  q[-],
2304                                  length $self->{ct}->{data});
2305    
2306          ## Stay in the state          ## Stay in the state
2307          !!!next-input-character;          !!!next-input-character;
2308          redo A;          redo A;
2309        }        }
2310      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2311        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2312          $self->{state} = 'comment end';          !!!cp (148);
2313            $self->{state} = COMMENT_END_STATE;
2314          !!!next-input-character;          !!!next-input-character;
2315          redo A;          redo A;
2316        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2317            !!!cp (149);
2318          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2319          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2320          ## reconsume          ## reconsume
2321    
2322          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
         undef $self->{current_token};  
2323    
2324          redo A;          redo A;
2325        } else {        } else {
2326          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2327          $self->{state} = 'comment';          $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2328            $self->{state} = COMMENT_STATE;
2329          !!!next-input-character;          !!!next-input-character;
2330          redo A;          redo A;
2331        }        }
2332      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2333        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2334          $self->{state} = 'data';          !!!cp (151);
2335            $self->{state} = DATA_STATE;
2336          !!!next-input-character;          !!!next-input-character;
2337    
2338          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
         undef $self->{current_token};  
2339    
2340          redo A;          redo A;
2341        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2342          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2343          $self->{current_token}->{data} .= '-'; # comment          !!!parse-error (type => 'dash in comment',
2344                            line => $self->{line_prev},
2345                            column => $self->{column_prev});
2346            $self->{ct}->{data} .= '-'; # comment
2347          ## Stay in the state          ## Stay in the state
2348          !!!next-input-character;          !!!next-input-character;
2349          redo A;          redo A;
2350        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2351            !!!cp (153);
2352          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2353          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2354          ## reconsume          ## reconsume
2355    
2356          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
         undef $self->{current_token};  
2357    
2358          redo A;          redo A;
2359        } else {        } else {
2360          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2361          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2362          $self->{state} = 'comment';                          line => $self->{line_prev},
2363                            column => $self->{column_prev});
2364            $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2365            $self->{state} = COMMENT_STATE;
2366          !!!next-input-character;          !!!next-input-character;
2367          redo A;          redo A;
2368        }        }
2369      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2370        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2371            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2372            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2373            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2374            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2375          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2376            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2377          !!!next-input-character;          !!!next-input-character;
2378          redo A;          redo A;
2379        } else {        } else {
2380            !!!cp (156);
2381          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2382          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2383          ## reconsume          ## reconsume
2384          redo A;          redo A;
2385        }        }
2386      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2387        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2388            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2389            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2390            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2391            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2392            !!!cp (157);
2393          ## Stay in the state          ## Stay in the state
2394          !!!next-input-character;          !!!next-input-character;
2395          redo A;          redo A;
2396        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{nc} == 0x003E) { # >
2397                 $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) { # >  
2398          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2399          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2400          !!!next-input-character;          !!!next-input-character;
2401    
2402          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2403    
2404          redo A;          redo A;
2405        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2406            !!!cp (159);
2407          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2408          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2409          ## reconsume          ## reconsume
2410    
2411          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2412    
2413          redo A;          redo A;
2414        } else {        } else {
2415          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
2416                            name => chr ($self->{next_input_character}),          $self->{ct}->{name} = chr $self->{nc};
2417                            error => 1};          delete $self->{ct}->{quirks};
2418  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2419          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2420            !!!next-input-character;
2421            redo A;
2422          }
2423        } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2424    ## ISSUE: Redundant "First," in the spec.
2425          if ($self->{nc} == 0x0009 or # HT
2426              $self->{nc} == 0x000A or # LF
2427              $self->{nc} == 0x000B or # VT
2428              $self->{nc} == 0x000C or # FF
2429              $self->{nc} == 0x0020) { # SP
2430            !!!cp (161);
2431            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2432            !!!next-input-character;
2433            redo A;
2434          } elsif ($self->{nc} == 0x003E) { # >
2435            !!!cp (162);
2436            $self->{state} = DATA_STATE;
2437            !!!next-input-character;
2438    
2439            !!!emit ($self->{ct}); # DOCTYPE
2440    
2441            redo A;
2442          } elsif ($self->{nc} == -1) {
2443            !!!cp (163);
2444            !!!parse-error (type => 'unclosed DOCTYPE');
2445            $self->{state} = DATA_STATE;
2446            ## reconsume
2447    
2448            $self->{ct}->{quirks} = 1;
2449            !!!emit ($self->{ct}); # DOCTYPE
2450    
2451            redo A;
2452          } else {
2453            !!!cp (164);
2454            $self->{ct}->{name}
2455              .= chr ($self->{nc}); # DOCTYPE
2456            ## Stay in the state
2457            !!!next-input-character;
2458            redo A;
2459          }
2460        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2461          if ($self->{nc} == 0x0009 or # HT
2462              $self->{nc} == 0x000A or # LF
2463              $self->{nc} == 0x000B or # VT
2464              $self->{nc} == 0x000C or # FF
2465              $self->{nc} == 0x0020) { # SP
2466            !!!cp (165);
2467            ## Stay in the state
2468            !!!next-input-character;
2469            redo A;
2470          } elsif ($self->{nc} == 0x003E) { # >
2471            !!!cp (166);
2472            $self->{state} = DATA_STATE;
2473            !!!next-input-character;
2474    
2475            !!!emit ($self->{ct}); # DOCTYPE
2476    
2477            redo A;
2478          } elsif ($self->{nc} == -1) {
2479            !!!cp (167);
2480            !!!parse-error (type => 'unclosed DOCTYPE');
2481            $self->{state} = DATA_STATE;
2482            ## reconsume
2483    
2484            $self->{ct}->{quirks} = 1;
2485            !!!emit ($self->{ct}); # DOCTYPE
2486    
2487            redo A;
2488          } elsif ($self->{nc} == 0x0050 or # P
2489                   $self->{nc} == 0x0070) { # p
2490            $self->{state} = PUBLIC_STATE;
2491            $self->{s_kwd} = chr $self->{nc};
2492            !!!next-input-character;
2493            redo A;
2494          } elsif ($self->{nc} == 0x0053 or # S
2495                   $self->{nc} == 0x0073) { # s
2496            $self->{state} = SYSTEM_STATE;
2497            $self->{s_kwd} = chr $self->{nc};
2498            !!!next-input-character;
2499            redo A;
2500          } else {
2501            !!!cp (180);
2502            !!!parse-error (type => 'string after DOCTYPE name');
2503            $self->{ct}->{quirks} = 1;
2504    
2505            $self->{state} = BOGUS_DOCTYPE_STATE;
2506            !!!next-input-character;
2507            redo A;
2508          }
2509        } elsif ($self->{state} == PUBLIC_STATE) {
2510          ## ASCII case-insensitive
2511          if ($self->{nc} == [
2512                undef,
2513                0x0055, # U
2514                0x0042, # B
2515                0x004C, # L
2516                0x0049, # I
2517              ]->[length $self->{s_kwd}] or
2518              $self->{nc} == [
2519                undef,
2520                0x0075, # u
2521                0x0062, # b
2522                0x006C, # l
2523                0x0069, # i
2524              ]->[length $self->{s_kwd}]) {
2525            !!!cp (175);
2526            ## Stay in the state.
2527            $self->{s_kwd} .= chr $self->{nc};
2528            !!!next-input-character;
2529            redo A;
2530          } elsif ((length $self->{s_kwd}) == 5 and
2531                   ($self->{nc} == 0x0043 or # C
2532                    $self->{nc} == 0x0063)) { # c
2533            !!!cp (168);
2534            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2535            !!!next-input-character;
2536            redo A;
2537          } else {
2538            !!!cp (169);
2539            !!!parse-error (type => 'string after DOCTYPE name',
2540                            line => $self->{line_prev},
2541                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2542            $self->{ct}->{quirks} = 1;
2543    
2544            $self->{state} = BOGUS_DOCTYPE_STATE;
2545            ## Reconsume.
2546            redo A;
2547          }
2548        } elsif ($self->{state} == SYSTEM_STATE) {
2549          ## ASCII case-insensitive
2550          if ($self->{nc} == [
2551                undef,
2552                0x0059, # Y
2553                0x0053, # S
2554                0x0054, # T
2555                0x0045, # E
2556              ]->[length $self->{s_kwd}] or
2557              $self->{nc} == [
2558                undef,
2559                0x0079, # y
2560                0x0073, # s
2561                0x0074, # t
2562                0x0065, # e
2563              ]->[length $self->{s_kwd}]) {
2564            !!!cp (170);
2565            ## Stay in the state.
2566            $self->{s_kwd} .= chr $self->{nc};
2567            !!!next-input-character;
2568            redo A;
2569          } elsif ((length $self->{s_kwd}) == 5 and
2570                   ($self->{nc} == 0x004D or # M
2571                    $self->{nc} == 0x006D)) { # m
2572            !!!cp (171);
2573            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2574            !!!next-input-character;
2575            redo A;
2576          } else {
2577            !!!cp (172);
2578            !!!parse-error (type => 'string after DOCTYPE name',
2579                            line => $self->{line_prev},
2580                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2581            $self->{ct}->{quirks} = 1;
2582    
2583            $self->{state} = BOGUS_DOCTYPE_STATE;
2584            ## Reconsume.
2585            redo A;
2586          }
2587        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2588          if ({
2589                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2590                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2591              }->{$self->{nc}}) {
2592            !!!cp (181);
2593            ## Stay in the state
2594            !!!next-input-character;
2595            redo A;
2596          } elsif ($self->{nc} eq 0x0022) { # "
2597            !!!cp (182);
2598            $self->{ct}->{pubid} = ''; # DOCTYPE
2599            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2600            !!!next-input-character;
2601            redo A;
2602          } elsif ($self->{nc} eq 0x0027) { # '
2603            !!!cp (183);
2604            $self->{ct}->{pubid} = ''; # DOCTYPE
2605            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2606            !!!next-input-character;
2607            redo A;
2608          } elsif ($self->{nc} eq 0x003E) { # >
2609            !!!cp (184);
2610            !!!parse-error (type => 'no PUBLIC literal');
2611    
2612            $self->{state} = DATA_STATE;
2613            !!!next-input-character;
2614    
2615            $self->{ct}->{quirks} = 1;
2616            !!!emit ($self->{ct}); # DOCTYPE
2617    
2618            redo A;
2619          } elsif ($self->{nc} == -1) {
2620            !!!cp (185);
2621            !!!parse-error (type => 'unclosed DOCTYPE');
2622    
2623            $self->{state} = DATA_STATE;
2624            ## reconsume
2625    
2626            $self->{ct}->{quirks} = 1;
2627            !!!emit ($self->{ct}); # DOCTYPE
2628    
2629            redo A;
2630          } else {
2631            !!!cp (186);
2632            !!!parse-error (type => 'string after PUBLIC');
2633            $self->{ct}->{quirks} = 1;
2634    
2635            $self->{state} = BOGUS_DOCTYPE_STATE;
2636          !!!next-input-character;          !!!next-input-character;
2637          redo A;          redo A;
2638        }        }
2639      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2640        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0022) { # "
2641            $self->{next_input_character} == 0x000A or # LF          !!!cp (187);
2642            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
         $self->{state} = 'after DOCTYPE name';  
2643          !!!next-input-character;          !!!next-input-character;
2644          redo A;          redo A;
2645        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2646          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (188);
2647          $self->{state} = 'data';          !!!parse-error (type => 'unclosed PUBLIC literal');
2648    
2649            $self->{state} = DATA_STATE;
2650          !!!next-input-character;          !!!next-input-character;
2651    
2652          !!!emit ($self->{current_token}); # DOCTYPE          $self->{ct}->{quirks} = 1;
2653          undef $self->{current_token};          !!!emit ($self->{ct}); # DOCTYPE
2654    
2655            redo A;
2656          } elsif ($self->{nc} == -1) {
2657            !!!cp (189);
2658            !!!parse-error (type => 'unclosed PUBLIC literal');
2659    
2660            $self->{state} = DATA_STATE;
2661            ## reconsume
2662    
2663            $self->{ct}->{quirks} = 1;
2664            !!!emit ($self->{ct}); # DOCTYPE
2665    
2666          redo A;          redo A;
2667        } elsif (0x0061 <= $self->{next_input_character} and        } else {
2668                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (190);
2669          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2670          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');              .= chr $self->{nc};
2671            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2672                                  length $self->{ct}->{pubid});
2673    
2674          ## Stay in the state          ## Stay in the state
2675          !!!next-input-character;          !!!next-input-character;
2676          redo A;          redo A;
2677        } elsif ($self->{next_input_character} == -1) {        }
2678        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2679          if ($self->{nc} == 0x0027) { # '
2680            !!!cp (191);
2681            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2682            !!!next-input-character;
2683            redo A;
2684          } elsif ($self->{nc} == 0x003E) { # >
2685            !!!cp (192);
2686            !!!parse-error (type => 'unclosed PUBLIC literal');
2687    
2688            $self->{state} = DATA_STATE;
2689            !!!next-input-character;
2690    
2691            $self->{ct}->{quirks} = 1;
2692            !!!emit ($self->{ct}); # DOCTYPE
2693    
2694            redo A;
2695          } elsif ($self->{nc} == -1) {
2696            !!!cp (193);
2697            !!!parse-error (type => 'unclosed PUBLIC literal');
2698    
2699            $self->{state} = DATA_STATE;
2700            ## reconsume
2701    
2702            $self->{ct}->{quirks} = 1;
2703            !!!emit ($self->{ct}); # DOCTYPE
2704    
2705            redo A;
2706          } else {
2707            !!!cp (194);
2708            $self->{ct}->{pubid} # DOCTYPE
2709                .= chr $self->{nc};
2710            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2711                                  length $self->{ct}->{pubid});
2712    
2713            ## Stay in the state
2714            !!!next-input-character;
2715            redo A;
2716          }
2717        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2718          if ({
2719                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2720                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2721              }->{$self->{nc}}) {
2722            !!!cp (195);
2723            ## Stay in the state
2724            !!!next-input-character;
2725            redo A;
2726          } elsif ($self->{nc} == 0x0022) { # "
2727            !!!cp (196);
2728            $self->{ct}->{sysid} = ''; # DOCTYPE
2729            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2730            !!!next-input-character;
2731            redo A;
2732          } elsif ($self->{nc} == 0x0027) { # '
2733            !!!cp (197);
2734            $self->{ct}->{sysid} = ''; # DOCTYPE
2735            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2736            !!!next-input-character;
2737            redo A;
2738          } elsif ($self->{nc} == 0x003E) { # >
2739            !!!cp (198);
2740            $self->{state} = DATA_STATE;
2741            !!!next-input-character;
2742    
2743            !!!emit ($self->{ct}); # DOCTYPE
2744    
2745            redo A;
2746          } elsif ($self->{nc} == -1) {
2747            !!!cp (199);
2748          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2749          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
2750          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2751          ## reconsume          ## reconsume
2752    
2753          !!!emit ($self->{current_token});          $self->{ct}->{quirks} = 1;
2754          undef $self->{current_token};          !!!emit ($self->{ct}); # DOCTYPE
2755    
2756          redo A;          redo A;
2757        } else {        } else {
2758          $self->{current_token}->{name}          !!!cp (200);
2759            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2760          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{ct}->{quirks} = 1;
2761    
2762            $self->{state} = BOGUS_DOCTYPE_STATE;
2763            !!!next-input-character;
2764            redo A;
2765          }
2766        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2767          if ({
2768                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2769                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2770              }->{$self->{nc}}) {
2771            !!!cp (201);
2772          ## Stay in the state          ## Stay in the state
2773          !!!next-input-character;          !!!next-input-character;
2774          redo A;          redo A;
2775          } elsif ($self->{nc} == 0x0022) { # "
2776            !!!cp (202);
2777            $self->{ct}->{sysid} = ''; # DOCTYPE
2778            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2779            !!!next-input-character;
2780            redo A;
2781          } elsif ($self->{nc} == 0x0027) { # '
2782            !!!cp (203);
2783            $self->{ct}->{sysid} = ''; # DOCTYPE
2784            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2785            !!!next-input-character;
2786            redo A;
2787          } elsif ($self->{nc} == 0x003E) { # >
2788            !!!cp (204);
2789            !!!parse-error (type => 'no SYSTEM literal');
2790            $self->{state} = DATA_STATE;
2791            !!!next-input-character;
2792    
2793            $self->{ct}->{quirks} = 1;
2794            !!!emit ($self->{ct}); # DOCTYPE
2795    
2796            redo A;
2797          } elsif ($self->{nc} == -1) {
2798            !!!cp (205);
2799            !!!parse-error (type => 'unclosed DOCTYPE');
2800    
2801            $self->{state} = DATA_STATE;
2802            ## reconsume
2803    
2804            $self->{ct}->{quirks} = 1;
2805            !!!emit ($self->{ct}); # DOCTYPE
2806    
2807            redo A;
2808          } else {
2809            !!!cp (206);
2810            !!!parse-error (type => 'string after SYSTEM');
2811            $self->{ct}->{quirks} = 1;
2812    
2813            $self->{state} = BOGUS_DOCTYPE_STATE;
2814            !!!next-input-character;
2815            redo A;
2816        }        }
2817      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2818        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0022) { # "
2819            $self->{next_input_character} == 0x000A or # LF          !!!cp (207);
2820            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2821            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2822            $self->{next_input_character} == 0x0020) { # SP          redo A;
2823          } elsif ($self->{nc} == 0x003E) { # >
2824            !!!cp (208);
2825            !!!parse-error (type => 'unclosed SYSTEM literal');
2826    
2827            $self->{state} = DATA_STATE;
2828            !!!next-input-character;
2829    
2830            $self->{ct}->{quirks} = 1;
2831            !!!emit ($self->{ct}); # DOCTYPE
2832    
2833            redo A;
2834          } elsif ($self->{nc} == -1) {
2835            !!!cp (209);
2836            !!!parse-error (type => 'unclosed SYSTEM literal');
2837    
2838            $self->{state} = DATA_STATE;
2839            ## reconsume
2840    
2841            $self->{ct}->{quirks} = 1;
2842            !!!emit ($self->{ct}); # DOCTYPE
2843    
2844            redo A;
2845          } else {
2846            !!!cp (210);
2847            $self->{ct}->{sysid} # DOCTYPE
2848                .= chr $self->{nc};
2849            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2850                                  length $self->{ct}->{sysid});
2851    
2852          ## Stay in the state          ## Stay in the state
2853          !!!next-input-character;          !!!next-input-character;
2854          redo A;          redo A;
2855        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2856          $self->{state} = 'data';      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2857          if ($self->{nc} == 0x0027) { # '
2858            !!!cp (211);
2859            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2860            !!!next-input-character;
2861            redo A;
2862          } elsif ($self->{nc} == 0x003E) { # >
2863            !!!cp (212);
2864            !!!parse-error (type => 'unclosed SYSTEM literal');
2865    
2866            $self->{state} = DATA_STATE;
2867          !!!next-input-character;          !!!next-input-character;
2868    
2869          !!!emit ($self->{current_token}); # DOCTYPE          $self->{ct}->{quirks} = 1;
2870          undef $self->{current_token};          !!!emit ($self->{ct}); # DOCTYPE
2871    
2872            redo A;
2873          } elsif ($self->{nc} == -1) {
2874            !!!cp (213);
2875            !!!parse-error (type => 'unclosed SYSTEM literal');
2876    
2877            $self->{state} = DATA_STATE;
2878            ## reconsume
2879    
2880            $self->{ct}->{quirks} = 1;
2881            !!!emit ($self->{ct}); # DOCTYPE
2882    
2883          redo A;          redo A;
2884        } elsif ($self->{next_input_character} == -1) {        } else {
2885            !!!cp (214);
2886            $self->{ct}->{sysid} # DOCTYPE
2887                .= chr $self->{nc};
2888            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2889                                  length $self->{ct}->{sysid});
2890    
2891            ## Stay in the state
2892            !!!next-input-character;
2893            redo A;
2894          }
2895        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2896          if ({
2897                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2898                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2899              }->{$self->{nc}}) {
2900            !!!cp (215);
2901            ## Stay in the state
2902            !!!next-input-character;
2903            redo A;
2904          } elsif ($self->{nc} == 0x003E) { # >
2905            !!!cp (216);
2906            $self->{state} = DATA_STATE;
2907            !!!next-input-character;
2908    
2909            !!!emit ($self->{ct}); # DOCTYPE
2910    
2911            redo A;
2912          } elsif ($self->{nc} == -1) {
2913            !!!cp (217);
2914          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2915          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2916          ## reconsume          ## reconsume
2917    
2918          !!!emit ($self->{current_token}); # DOCTYPE          $self->{ct}->{quirks} = 1;
2919          undef $self->{current_token};          !!!emit ($self->{ct}); # DOCTYPE
2920    
2921          redo A;          redo A;
2922        } else {        } else {
2923          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (218);
2924          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after SYSTEM literal');
2925          $self->{state} = 'bogus DOCTYPE';          #$self->{ct}->{quirks} = 1;
2926    
2927            $self->{state} = BOGUS_DOCTYPE_STATE;
2928          !!!next-input-character;          !!!next-input-character;
2929          redo A;          redo A;
2930        }        }
2931      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2932        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2933          $self->{state} = 'data';          !!!cp (219);
2934            $self->{state} = DATA_STATE;
2935          !!!next-input-character;          !!!next-input-character;
2936    
2937          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
         undef $self->{current_token};  
2938    
2939          redo A;          redo A;
2940        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2941            !!!cp (220);
2942          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2943          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2944          ## reconsume          ## reconsume
2945    
2946          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
         undef $self->{current_token};  
2947    
2948          redo A;          redo A;
2949        } else {        } else {
2950            !!!cp (221);
2951            my $s = '';
2952            $self->{read_until}->($s, q[>], 0);
2953    
2954          ## Stay in the state          ## Stay in the state
2955          !!!next-input-character;          !!!next-input-character;
2956          redo A;          redo A;
2957        }        }
2958      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2959        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2960      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2961    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2962          
2963          if ($self->{nc} == 0x005D) { # ]
2964            !!!cp (221.1);
2965            $self->{state} = CDATA_SECTION_MSE1_STATE;
2966            !!!next-input-character;
2967            redo A;
2968          } elsif ($self->{nc} == -1) {
2969            $self->{state} = DATA_STATE;
2970            !!!next-input-character;
2971            if (length $self->{ct}->{data}) { # character
2972              !!!cp (221.2);
2973              !!!emit ($self->{ct}); # character
2974            } else {
2975              !!!cp (221.3);
2976              ## No token to emit. $self->{ct} is discarded.
2977            }        
2978            redo A;
2979          } else {
2980            !!!cp (221.4);
2981            $self->{ct}->{data} .= chr $self->{nc};
2982            $self->{read_until}->($self->{ct}->{data},
2983                                  q<]>,
2984                                  length $self->{ct}->{data});
2985    
2986    die "$0: _get_next_token: unexpected case";          ## Stay in the state.
2987  } # _get_next_token          !!!next-input-character;
2988            redo A;
2989          }
2990    
2991  sub _tokenize_attempt_to_consume_an_entity ($) {        ## ISSUE: "text tokens" in spec.
2992    my $self = shift;      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2993            if ($self->{nc} == 0x005D) { # ]
2994    if ($self->{next_input_character} == 0x0023) { # #          !!!cp (221.5);
2995      !!!next-input-character;          $self->{state} = CDATA_SECTION_MSE2_STATE;
2996      if ($self->{next_input_character} == 0x0078 or # x          !!!next-input-character;
2997          $self->{next_input_character} == 0x0058) { # X          redo A;
2998        my $num;        } else {
2999        X: {          !!!cp (221.6);
3000          my $x_char = $self->{next_input_character};          $self->{ct}->{data} .= ']';
3001          !!!next-input-character;          $self->{state} = CDATA_SECTION_STATE;
3002          if (0x0030 <= $self->{next_input_character} and          ## Reconsume.
3003              $self->{next_input_character} <= 0x0039) { # 0..9          redo A;
3004            $num ||= 0;        }
3005            $num *= 0x10;      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3006            $num += $self->{next_input_character} - 0x0030;        if ($self->{nc} == 0x003E) { # >
3007            redo X;          $self->{state} = DATA_STATE;
3008          } elsif (0x0061 <= $self->{next_input_character} and          !!!next-input-character;
3009                   $self->{next_input_character} <= 0x0066) { # a..f          if (length $self->{ct}->{data}) { # character
3010            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (221.7);
3011            $num ||= 0;            !!!emit ($self->{ct}); # character
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           ## ISSUE: the spec says U+0058, which is apparently incorrect  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $num) { # no hexadecimal digit  
           !!!parse-error (type => 'bare hcro');  
           $self->{next_input_character} = 0x0023; # #  
           !!!back-next-input-character ($x_char);  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
3012          } else {          } else {
3013            !!!parse-error (type => 'no refc');            !!!cp (221.8);
3014              ## No token to emit. $self->{ct} is discarded.
3015          }          }
3016            redo A;
3017          ## TODO: check the definition for |a valid Unicode character|.        } elsif ($self->{nc} == 0x005D) { # ]
3018          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>          !!!cp (221.9); # character
3019          if ($num > 1114111 or $num == 0) {          $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3020            $num = 0xFFFD; # REPLACEMENT CHARACTER          ## Stay in the state.
3021            ## ISSUE: Why this is not an error?          !!!next-input-character;
3022          } elsif (0x80 <= $num and $num <= 0x9F) {          redo A;
3023            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);        } else {
3024            $num = $c1_entity_char->{$num};          !!!cp (221.11);
3025          }          $self->{ct}->{data} .= ']]'; # character
3026            $self->{state} = CDATA_SECTION_STATE;
3027          return {type => 'character', data => chr $num};          ## Reconsume.
3028        } # X          redo A;
3029      } elsif (0x0030 <= $self->{next_input_character} and        }
3030               $self->{next_input_character} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_STATE) {
3031        my $code = $self->{next_input_character} - 0x0030;        if ({
3032        !!!next-input-character;          0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3033                  0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3034        while (0x0030 <= $self->{next_input_character} and          $self->{entity_add} => 1,
3035                  $self->{next_input_character} <= 0x0039) { # 0..9        }->{$self->{nc}}) {
3036          $code *= 10;          !!!cp (1001);
3037          $code += $self->{next_input_character} - 0x0030;          ## Don't consume
3038                    ## No error
3039            ## Return nothing.
3040            #
3041          } elsif ($self->{nc} == 0x0023) { # #
3042            !!!cp (999);
3043            $self->{state} = ENTITY_HASH_STATE;
3044            $self->{s_kwd} = '#';
3045            !!!next-input-character;
3046            redo A;
3047          } elsif ((0x0041 <= $self->{nc} and
3048                    $self->{nc} <= 0x005A) or # A..Z
3049                   (0x0061 <= $self->{nc} and
3050                    $self->{nc} <= 0x007A)) { # a..z
3051            !!!cp (998);
3052            require Whatpm::_NamedEntityList;
3053            $self->{state} = ENTITY_NAME_STATE;
3054            $self->{s_kwd} = chr $self->{nc};
3055            $self->{entity__value} = $self->{s_kwd};
3056            $self->{entity__match} = 0;
3057          !!!next-input-character;          !!!next-input-character;
3058            redo A;
3059          } else {
3060            !!!cp (1027);
3061            !!!parse-error (type => 'bare ero');
3062            ## Return nothing.
3063            #
3064        }        }
3065    
3066        if ($self->{next_input_character} == 0x003B) { # ;        ## NOTE: No character is consumed by the "consume a character
3067          ## reference" algorithm.  In other word, there is an "&" character
3068          ## that does not introduce a character reference, which would be
3069          ## appended to the parent element or the attribute value in later
3070          ## process of the tokenizer.
3071    
3072          if ($self->{prev_state} == DATA_STATE) {
3073            !!!cp (997);
3074            $self->{state} = $self->{prev_state};
3075            ## Reconsume.
3076            !!!emit ({type => CHARACTER_TOKEN, data => '&',
3077                      line => $self->{line_prev},
3078                      column => $self->{column_prev},
3079                     });
3080            redo A;
3081          } else {
3082            !!!cp (996);
3083            $self->{ca}->{value} .= '&';
3084            $self->{state} = $self->{prev_state};
3085            ## Reconsume.
3086            redo A;
3087          }
3088        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3089          if ($self->{nc} == 0x0078 or # x
3090              $self->{nc} == 0x0058) { # X
3091            !!!cp (995);
3092            $self->{state} = HEXREF_X_STATE;
3093            $self->{s_kwd} .= chr $self->{nc};
3094            !!!next-input-character;
3095            redo A;
3096          } elsif (0x0030 <= $self->{nc} and
3097                   $self->{nc} <= 0x0039) { # 0..9
3098            !!!cp (994);
3099            $self->{state} = NCR_NUM_STATE;
3100            $self->{s_kwd} = $self->{nc} - 0x0030;
3101            !!!next-input-character;
3102            redo A;
3103          } else {
3104            !!!parse-error (type => 'bare nero',
3105                            line => $self->{line_prev},
3106                            column => $self->{column_prev} - 1);
3107    
3108            ## NOTE: According to the spec algorithm, nothing is returned,
3109            ## and then "&#" is appended to the parent element or the attribute
3110            ## value in the later processing.
3111    
3112            if ($self->{prev_state} == DATA_STATE) {
3113              !!!cp (1019);
3114              $self->{state} = $self->{prev_state};
3115              ## Reconsume.
3116              !!!emit ({type => CHARACTER_TOKEN,
3117                        data => '&#',
3118                        line => $self->{line_prev},
3119                        column => $self->{column_prev} - 1,
3120                       });
3121              redo A;
3122            } else {
3123              !!!cp (993);
3124              $self->{ca}->{value} .= '&#';
3125              $self->{state} = $self->{prev_state};
3126              ## Reconsume.
3127              redo A;
3128            }
3129          }
3130        } elsif ($self->{state} == NCR_NUM_STATE) {
3131          if (0x0030 <= $self->{nc} and
3132              $self->{nc} <= 0x0039) { # 0..9
3133            !!!cp (1012);
3134            $self->{s_kwd} *= 10;
3135            $self->{s_kwd} += $self->{nc} - 0x0030;
3136            
3137            ## Stay in the state.
3138          !!!next-input-character;          !!!next-input-character;
3139            redo A;
3140          } elsif ($self->{nc} == 0x003B) { # ;
3141            !!!cp (1013);
3142            !!!next-input-character;
3143            #
3144        } else {        } else {
3145            !!!cp (1014);
3146          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3147            ## Reconsume.
3148            #
3149        }        }
3150    
3151        ## TODO: check the definition for |a valid Unicode character|.        my $code = $self->{s_kwd};
3152        if ($code > 1114111 or $code == 0) {        my $l = $self->{line_prev};
3153          $code = 0xFFFD; # REPLACEMENT CHARACTER        my $c = $self->{column_prev};
3154          ## ISSUE: Why this is not an error?        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3155            !!!cp (1015);
3156            !!!parse-error (type => 'invalid character reference',
3157                            text => (sprintf 'U+%04X', $code),
3158                            line => $l, column => $c);
3159            $code = 0xFFFD;
3160          } elsif ($code > 0x10FFFF) {
3161            !!!cp (1016);
3162            !!!parse-error (type => 'invalid character reference',
3163                            text => (sprintf 'U-%08X', $code),
3164                            line => $l, column => $c);
3165            $code = 0xFFFD;
3166          } elsif ($code == 0x000D) {
3167            !!!cp (1017);
3168            !!!parse-error (type => 'CR character reference',
3169                            line => $l, column => $c);
3170            $code = 0x000A;
3171        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3172          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!cp (1018);
3173            !!!parse-error (type => 'C1 character reference',
3174                            text => (sprintf 'U+%04X', $code),
3175                            line => $l, column => $c);
3176          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3177        }        }
3178          
3179        return {type => 'character', data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3180      } else {          !!!cp (992);
3181        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3182        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3183        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3184        return undef;                    line => $l, column => $c,
3185      }                   });
3186    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
3187              $self->{next_input_character} <= 0x005A) or        } else {
3188             (0x0061 <= $self->{next_input_character} and          !!!cp (991);
3189              $self->{next_input_character} <= 0x007A)) {          $self->{ca}->{value} .= chr $code;
3190      my $entity_name = chr $self->{next_input_character};          $self->{ca}->{has_reference} = 1;
3191      !!!next-input-character;          $self->{state} = $self->{prev_state};
3192            ## Reconsume.
3193      my $value = $entity_name;          redo A;
3194      my $match;        }
3195        } elsif ($self->{state} == HEXREF_X_STATE) {
3196      while (length $entity_name < 10 and        if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3197             ## NOTE: Some number greater than the maximum length of entity name            (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3198             ((0x0041 <= $self->{next_input_character} and            (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3199               $self->{next_input_character} <= 0x005A) or          # 0..9, A..F, a..f
3200              (0x0061 <= $self->{next_input_character} and          !!!cp (990);
3201               $self->{next_input_character} <= 0x007A) or          $self->{state} = HEXREF_HEX_STATE;
3202              (0x0030 <= $self->{next_input_character} and          $self->{s_kwd} = 0;
3203               $self->{next_input_character} <= 0x0039))) {          ## Reconsume.
3204        $entity_name .= chr $self->{next_input_character};          redo A;
       if (defined $entity_char->{$entity_name}) {  
         $value = $entity_char->{$entity_name};  
         $match = 1;  
3205        } else {        } else {
3206          $value .= chr $self->{next_input_character};          !!!parse-error (type => 'bare hcro',
3207                            line => $self->{line_prev},
3208                            column => $self->{column_prev} - 2);
3209    
3210            ## NOTE: According to the spec algorithm, nothing is returned,
3211            ## and then "&#" followed by "X" or "x" is appended to the parent
3212            ## element or the attribute value in the later processing.
3213    
3214            if ($self->{prev_state} == DATA_STATE) {
3215              !!!cp (1005);
3216              $self->{state} = $self->{prev_state};
3217              ## Reconsume.
3218              !!!emit ({type => CHARACTER_TOKEN,
3219                        data => '&' . $self->{s_kwd},
3220                        line => $self->{line_prev},
3221                        column => $self->{column_prev} - length $self->{s_kwd},
3222                       });
3223              redo A;
3224            } else {
3225              !!!cp (989);
3226              $self->{ca}->{value} .= '&' . $self->{s_kwd};
3227              $self->{state} = $self->{prev_state};
3228              ## Reconsume.
3229              redo A;
3230            }
3231        }        }
3232        !!!next-input-character;      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3233      }        if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3234                # 0..9
3235      if ($match) {          !!!cp (1002);
3236        if ($self->{next_input_character} == 0x003B) { # ;          $self->{s_kwd} *= 0x10;
3237            $self->{s_kwd} += $self->{nc} - 0x0030;
3238            ## Stay in the state.
3239            !!!next-input-character;
3240            redo A;
3241          } elsif (0x0061 <= $self->{nc} and
3242                   $self->{nc} <= 0x0066) { # a..f
3243            !!!cp (1003);
3244            $self->{s_kwd} *= 0x10;
3245            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3246            ## Stay in the state.
3247            !!!next-input-character;
3248            redo A;
3249          } elsif (0x0041 <= $self->{nc} and
3250                   $self->{nc} <= 0x0046) { # A..F
3251            !!!cp (1004);
3252            $self->{s_kwd} *= 0x10;
3253            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3254            ## Stay in the state.
3255            !!!next-input-character;
3256            redo A;
3257          } elsif ($self->{nc} == 0x003B) { # ;
3258            !!!cp (1006);
3259          !!!next-input-character;          !!!next-input-character;
3260            #
3261        } else {        } else {
3262          !!!parse-error (type => 'refc');          !!!cp (1007);
3263            !!!parse-error (type => 'no refc',
3264                            line => $self->{line},
3265                            column => $self->{column});
3266            ## Reconsume.
3267            #
3268        }        }
3269    
3270        return {type => 'character', data => $value};        my $code = $self->{s_kwd};
3271          my $l = $self->{line_prev};
3272          my $c = $self->{column_prev};
3273          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3274            !!!cp (1008);
3275            !!!parse-error (type => 'invalid character reference',
3276                            text => (sprintf 'U+%04X', $code),
3277                            line => $l, column => $c);
3278            $code = 0xFFFD;
3279          } elsif ($code > 0x10FFFF) {
3280            !!!cp (1009);
3281            !!!parse-error (type => 'invalid character reference',
3282                            text => (sprintf 'U-%08X', $code),
3283                            line => $l, column => $c);
3284            $code = 0xFFFD;
3285          } elsif ($code == 0x000D) {
3286            !!!cp (1010);
3287            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3288            $code = 0x000A;
3289          } elsif (0x80 <= $code and $code <= 0x9F) {
3290            !!!cp (1011);
3291            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3292            $code = $c1_entity_char->{$code};
3293          }
3294    
3295          if ($self->{prev_state} == DATA_STATE) {
3296            !!!cp (988);
3297            $self->{state} = $self->{prev_state};
3298            ## Reconsume.
3299            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3300                      line => $l, column => $c,
3301                     });
3302            redo A;
3303          } else {
3304            !!!cp (987);
3305            $self->{ca}->{value} .= chr $code;
3306            $self->{ca}->{has_reference} = 1;
3307            $self->{state} = $self->{prev_state};
3308            ## Reconsume.
3309            redo A;
3310          }
3311        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3312          if (length $self->{s_kwd} < 30 and
3313              ## NOTE: Some number greater than the maximum length of entity name
3314              ((0x0041 <= $self->{nc} and # a
3315                $self->{nc} <= 0x005A) or # x
3316               (0x0061 <= $self->{nc} and # a
3317                $self->{nc} <= 0x007A) or # z
3318               (0x0030 <= $self->{nc} and # 0
3319                $self->{nc} <= 0x0039) or # 9
3320               $self->{nc} == 0x003B)) { # ;
3321            our $EntityChar;
3322            $self->{s_kwd} .= chr $self->{nc};
3323            if (defined $EntityChar->{$self->{s_kwd}}) {
3324              if ($self->{nc} == 0x003B) { # ;
3325                !!!cp (1020);
3326                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3327                $self->{entity__match} = 1;
3328                !!!next-input-character;
3329                #
3330              } else {
3331                !!!cp (1021);
3332                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3333                $self->{entity__match} = -1;
3334                ## Stay in the state.
3335                !!!next-input-character;
3336                redo A;
3337              }
3338            } else {
3339              !!!cp (1022);
3340              $self->{entity__value} .= chr $self->{nc};
3341              $self->{entity__match} *= 2;
3342              ## Stay in the state.
3343              !!!next-input-character;
3344              redo A;
3345            }
3346          }
3347    
3348          my $data;
3349          my $has_ref;
3350          if ($self->{entity__match} > 0) {
3351            !!!cp (1023);
3352            $data = $self->{entity__value};
3353            $has_ref = 1;
3354            #
3355          } elsif ($self->{entity__match} < 0) {
3356            !!!parse-error (type => 'no refc');
3357            if ($self->{prev_state} != DATA_STATE and # in attribute
3358                $self->{entity__match} < -1) {
3359              !!!cp (1024);
3360              $data = '&' . $self->{s_kwd};
3361              #
3362            } else {
3363              !!!cp (1025);
3364              $data = $self->{entity__value};
3365              $has_ref = 1;
3366              #
3367            }
3368          } else {
3369            !!!cp (1026);
3370            !!!parse-error (type => 'bare ero',
3371                            line => $self->{line_prev},
3372                            column => $self->{column_prev} - length $self->{s_kwd});
3373            $data = '&' . $self->{s_kwd};
3374            #
3375          }
3376      
3377          ## NOTE: In these cases, when a character reference is found,
3378          ## it is consumed and a character token is returned, or, otherwise,
3379          ## nothing is consumed and returned, according to the spec algorithm.
3380          ## In this implementation, anything that has been examined by the
3381          ## tokenizer is appended to the parent element or the attribute value
3382          ## as string, either literal string when no character reference or
3383          ## entity-replaced string otherwise, in this stage, since any characters
3384          ## that would not be consumed are appended in the data state or in an
3385          ## appropriate attribute value state anyway.
3386    
3387          if ($self->{prev_state} == DATA_STATE) {
3388            !!!cp (986);
3389            $self->{state} = $self->{prev_state};
3390            ## Reconsume.
3391            !!!emit ({type => CHARACTER_TOKEN,
3392                      data => $data,
3393                      line => $self->{line_prev},
3394                      column => $self->{column_prev} + 1 - length $self->{s_kwd},
3395                     });
3396            redo A;
3397          } else {
3398            !!!cp (985);
3399            $self->{ca}->{value} .= $data;
3400            $self->{ca}->{has_reference} = 1 if $has_ref;
3401            $self->{state} = $self->{prev_state};
3402            ## Reconsume.
3403            redo A;
3404          }
3405      } else {      } else {
3406        !!!parse-error (type => 'bare ero');        die "$0: $self->{state}: Unknown state";
       ## NOTE: No characters are consumed in the spec.  
       !!!back-token ({type => 'character', data => $value});  
       return undef;  
3407      }      }
3408    } else {    } # A  
3409      ## no characters are consumed  
3410      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3411      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3412    
3413  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3414    my $self = shift;    my $self = shift;
# Line 1658  sub _initialize_tree_constructor ($) { Line 3416  sub _initialize_tree_constructor ($) {
3416    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3417    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3418    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3419    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3420      $self->{document}->set_user_data (manakai_source_line => 1);
3421      $self->{document}->set_user_data (manakai_source_column => 1);
3422  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3423    
3424  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1685  sub _construct_tree ($) { Line 3445  sub _construct_tree ($) {
3445        
3446    !!!next-token;    !!!next-token;
3447    
   $self->{insertion_mode} = 'before head';  
3448    undef $self->{form_element};    undef $self->{form_element};
3449    undef $self->{head_element};    undef $self->{head_element};
3450    $self->{open_elements} = [];    $self->{open_elements} = [];
3451    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3452    
3453      ## NOTE: The "initial" insertion mode.
3454    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3455    
3456      ## NOTE: The "before html" insertion mode.
3457    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3458      $self->{insertion_mode} = BEFORE_HEAD_IM;
3459    
3460      ## NOTE: The "before head" insertion mode and so on.
3461    $self->_tree_construction_main;    $self->_tree_construction_main;
3462  } # _construct_tree  } # _construct_tree
3463    
3464  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3465    my $self = shift;    my $self = shift;
3466    B: {  
3467        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3468          if ($token->{error}) {  
3469            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3470            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3471          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3472          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3473            ($token->{name});        ## language.
3474          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3475          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3476          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3477          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3478          return;            defined $token->{sysid}) {
3479        } elsif ({          !!!cp ('t1');
3480                  comment => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3481                  'start tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3482                  'end tag' => 1,          !!!cp ('t2');
3483                  'end-of-file' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3484                 }->{$token->{type}}) {        } elsif (defined $token->{pubid}) {
3485          ## ISSUE: Spec currently left this case undefined.          if ($token->{pubid} eq 'XSLT-compat') {
3486          !!!parse-error (type => 'missing DOCTYPE');            !!!cp ('t1.2');
3487          #$phase = 'root element';            !!!parse-error (type => 'XSLT-compat', token => $token,
3488          ## reprocess                            level => $self->{level}->{should});
3489          #redo B;          } else {
3490          return;            !!!parse-error (type => 'not HTML5', token => $token);
3491        } elsif ($token->{type} eq 'character') {          }
3492          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } else {
3493            $self->{document}->manakai_append_text ($1);          !!!cp ('t3');
3494            ## ISSUE: DOM3 Core does not allow Document > Text          #
3495            unless (length $token->{data}) {        }
3496              ## Stay in the phase        
3497              !!!next-token;        my $doctype = $self->{document}->create_document_type_definition
3498              redo B;          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3499          ## NOTE: Default value for both |public_id| and |system_id| attributes
3500          ## are empty strings, so that we don't set any value in missing cases.
3501          $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3502          $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
3503          ## NOTE: Other DocumentType attributes are null or empty lists.
3504          ## ISSUE: internalSubset = null??
3505          $self->{document}->append_child ($doctype);
3506          
3507          if ($token->{quirks} or $doctype_name ne 'HTML') {
3508            !!!cp ('t4');
3509            $self->{document}->manakai_compat_mode ('quirks');
3510          } elsif (defined $token->{pubid}) {
3511            my $pubid = $token->{pubid};
3512            $pubid =~ tr/a-z/A-z/;
3513            my $prefix = [
3514              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3515              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3516              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3517              "-//IETF//DTD HTML 2.0 LEVEL 1//",
3518              "-//IETF//DTD HTML 2.0 LEVEL 2//",
3519              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3520              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3521              "-//IETF//DTD HTML 2.0 STRICT//",
3522              "-//IETF//DTD HTML 2.0//",
3523              "-//IETF//DTD HTML 2.1E//",
3524              "-//IETF//DTD HTML 3.0//",
3525              "-//IETF//DTD HTML 3.2 FINAL//",
3526              "-//IETF//DTD HTML 3.2//",
3527              "-//IETF//DTD HTML 3//",
3528              "-//IETF//DTD HTML LEVEL 0//",
3529              "-//IETF//DTD HTML LEVEL 1//",
3530              "-//IETF//DTD HTML LEVEL 2//",
3531              "-//IETF//DTD HTML LEVEL 3//",
3532              "-//IETF//DTD HTML STRICT LEVEL 0//",
3533              "-//IETF//DTD HTML STRICT LEVEL 1//",
3534              "-//IETF//DTD HTML STRICT LEVEL 2//",
3535              "-//IETF//DTD HTML STRICT LEVEL 3//",
3536              "-//IETF//DTD HTML STRICT//",
3537              "-//IETF//DTD HTML//",
3538              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3539              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3540              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3541              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3542              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3543              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3544              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3545              "-//NETSCAPE COMM. CORP.//DTD HTML//",
3546              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3547              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3548              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3549              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3550              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3551              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3552              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3553              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3554              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3555              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3556              "-//W3C//DTD HTML 3 1995-03-24//",
3557              "-//W3C//DTD HTML 3.2 DRAFT//",
3558              "-//W3C//DTD HTML 3.2 FINAL//",
3559              "-//W3C//DTD HTML 3.2//",
3560              "-//W3C//DTD HTML 3.2S DRAFT//",
3561              "-//W3C//DTD HTML 4.0 FRAMESET//",
3562              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3563              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3564              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3565              "-//W3C//DTD W3 HTML//",
3566              "-//W3O//DTD W3 HTML 3.0//",
3567              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3568              "-//WEBTECHS//DTD MOZILLA HTML//",
3569            ]; # $prefix
3570            my $match;
3571            for (@$prefix) {
3572              if (substr ($prefix, 0, length $_) eq $_) {
3573                $match = 1;
3574                last;
3575              }
3576            }
3577            if ($match or
3578                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3579                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3580                $pubid eq "HTML") {
3581              !!!cp ('t5');
3582              $self->{document}->manakai_compat_mode ('quirks');
3583            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3584                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3585              if (defined $token->{sysid}) {
3586                !!!cp ('t6');
3587                $self->{document}->manakai_compat_mode ('quirks');
3588              } else {
3589                !!!cp ('t7');
3590                $self->{document}->manakai_compat_mode ('limited quirks');
3591            }            }
3592            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3593                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3594              !!!cp ('t8');
3595              $self->{document}->manakai_compat_mode ('limited quirks');
3596            } else {
3597              !!!cp ('t9');
3598            }
3599          } else {
3600            !!!cp ('t10');
3601          }
3602          if (defined $token->{sysid}) {
3603            my $sysid = $token->{sysid};
3604            $sysid =~ tr/A-Z/a-z/;
3605            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3606              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3607              ## marked as quirks.
3608              $self->{document}->manakai_compat_mode ('quirks');
3609              !!!cp ('t11');
3610            } else {
3611              !!!cp ('t12');
3612            }
3613          } else {
3614            !!!cp ('t13');
3615          }
3616          
3617          ## Go to the "before html" insertion mode.
3618          !!!next-token;
3619          return;
3620        } elsif ({
3621                  START_TAG_TOKEN, 1,
3622                  END_TAG_TOKEN, 1,
3623                  END_OF_FILE_TOKEN, 1,
3624                 }->{$token->{type}}) {
3625          !!!cp ('t14');
3626          !!!parse-error (type => 'no DOCTYPE', token => $token);
3627          $self->{document}->manakai_compat_mode ('quirks');
3628          ## Go to the "before html" insertion mode.
3629          ## reprocess
3630          !!!ack-later;
3631          return;
3632        } elsif ($token->{type} == CHARACTER_TOKEN) {
3633          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3634            ## Ignore the token
3635    
3636            unless (length $token->{data}) {
3637              !!!cp ('t15');
3638              ## Stay in the insertion mode.
3639              !!!next-token;
3640              redo INITIAL;
3641            } else {
3642              !!!cp ('t16');
3643          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3644        } else {        } else {
3645          die "$0: $token->{type}: Unknown token";          !!!cp ('t17');
3646        }        }
3647      } # B  
3648          !!!parse-error (type => 'no DOCTYPE', token => $token);
3649          $self->{document}->manakai_compat_mode ('quirks');
3650          ## Go to the "before html" insertion mode.
3651          ## reprocess
3652          return;
3653        } elsif ($token->{type} == COMMENT_TOKEN) {
3654          !!!cp ('t18');
3655          my $comment = $self->{document}->create_comment ($token->{data});
3656          $self->{document}->append_child ($comment);
3657          
3658          ## Stay in the insertion mode.
3659          !!!next-token;
3660          redo INITIAL;
3661        } else {
3662          die "$0: $token->{type}: Unknown token type";
3663        }
3664      } # INITIAL
3665    
3666      die "$0: _tree_construction_initial: This should be never reached";
3667  } # _tree_construction_initial  } # _tree_construction_initial
3668    
3669  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3670    my $self = shift;    my $self = shift;
3671    
3672      ## NOTE: "before html" insertion mode.
3673        
3674    B: {    B: {
3675        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3676          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3677            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3678          ## Ignore the token          ## Ignore the token
3679          ## Stay in the phase          ## Stay in the insertion mode.
3680          !!!next-token;          !!!next-token;
3681          redo B;          redo B;
3682        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3683            !!!cp ('t20');
3684          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3685          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3686          ## Stay in the phase          ## Stay in the insertion mode.
3687          !!!next-token;          !!!next-token;
3688          redo B;          redo B;
3689        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3690          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3691            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3692            ## ISSUE: DOM3 Core does not allow Document > Text  
3693            unless (length $token->{data}) {            unless (length $token->{data}) {
3694              ## Stay in the phase              !!!cp ('t21');
3695                ## Stay in the insertion mode.
3696              !!!next-token;              !!!next-token;
3697              redo B;              redo B;
3698              } else {
3699                !!!cp ('t22');
3700            }            }
3701            } else {
3702              !!!cp ('t23');
3703          }          }
3704    
3705            $self->{application_cache_selection}->(undef);
3706    
3707          #          #
3708          } elsif ($token->{type} == START_TAG_TOKEN) {
3709            if ($token->{tag_name} eq 'html') {
3710              my $root_element;
3711              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3712              $self->{document}->append_child ($root_element);
3713              push @{$self->{open_elements}},
3714                  [$root_element, $el_category->{html}];
3715    
3716              if ($token->{attributes}->{manifest}) {
3717                !!!cp ('t24');
3718                $self->{application_cache_selection}
3719                    ->($token->{attributes}->{manifest}->{value});
3720                ## ISSUE: Spec is unclear on relative references.
3721                ## According to Hixie (#whatwg 2008-03-19), it should be
3722                ## resolved against the base URI of the document in HTML
3723                ## or xml:base of the element in XHTML.
3724              } else {
3725                !!!cp ('t25');
3726                $self->{application_cache_selection}->(undef);
3727              }
3728    
3729              !!!nack ('t25c');
3730    
3731              !!!next-token;
3732              return; ## Go to the "before head" insertion mode.
3733            } else {
3734              !!!cp ('t25.1');
3735              #
3736            }
3737        } elsif ({        } elsif ({
3738                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3739                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3740                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3741          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3742          #          #
3743        } else {        } else {
3744          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3745        }        }
3746        my $root_element; !!!create-element ($root_element, 'html');  
3747        $self->{document}->append_child ($root_element);      my $root_element;
3748        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3749        #$phase = 'main';      $self->{document}->append_child ($root_element);
3750        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3751        #redo B;  
3752        return;      $self->{application_cache_selection}->(undef);
3753    
3754        ## NOTE: Reprocess the token.
3755        !!!ack-later;
3756        return; ## Go to the "before head" insertion mode.
3757    
3758        ## ISSUE: There is an issue in the spec
3759    } # B    } # B
3760    
3761      die "$0: _tree_construction_root_element: This should never be reached";
3762  } # _tree_construction_root_element  } # _tree_construction_root_element
3763    
3764  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1804  sub _reset_insertion_mode ($) { Line 3773  sub _reset_insertion_mode ($) {
3773            
3774      ## Step 3      ## Step 3
3775      S3: {      S3: {
3776        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3777        if (defined $self->{inner_html_node}) {          $last = 1;
3778          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3779              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3780              $node = $self->{inner_html_node};
3781            } else {
3782              die "_reset_insertion_mode: t27";
3783            }
3784          }
3785          
3786          ## Step 4..14
3787          my $new_mode;
3788          if ($node->[1] & FOREIGN_EL) {
3789            !!!cp ('t28.1');
3790            ## NOTE: Strictly spaking, the line below only applies to MathML and
3791            ## SVG elements.  Currently the HTML syntax supports only MathML and
3792            ## SVG elements as foreigners.
3793            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3794          } elsif ($node->[1] & TABLE_CELL_EL) {
3795            if ($last) {
3796              !!!cp ('t28.2');
3797            #            #
3798          } else {          } else {
3799            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3800              $new_mode = IN_CELL_IM;
3801          }          }
3802          } else {
3803            !!!cp ('t28.4');
3804            $new_mode = {
3805                          select => IN_SELECT_IM,
3806                          ## NOTE: |option| and |optgroup| do not set
3807                          ## insertion mode to "in select" by themselves.
3808                          tr => IN_ROW_IM,
3809                          tbody => IN_TABLE_BODY_IM,
3810                          thead => IN_TABLE_BODY_IM,
3811                          tfoot => IN_TABLE_BODY_IM,
3812                          caption => IN_CAPTION_IM,
3813                          colgroup => IN_COLUMN_GROUP_IM,
3814                          table => IN_TABLE_IM,
3815                          head => IN_BODY_IM, # not in head!
3816                          body => IN_BODY_IM,
3817                          frameset => IN_FRAMESET_IM,
3818                         }->{$node->[0]->manakai_local_name};
3819        }        }
       
       ## 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]};  
3820        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3821                
3822        ## Step 14        ## Step 15
3823        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3824          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3825            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3826              $self->{insertion_mode} = BEFORE_HEAD_IM;
3827          } else {          } else {
3828            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3829              !!!cp ('t30');
3830              $self->{insertion_mode} = AFTER_HEAD_IM;
3831          }          }
3832          return;          return;
3833          } else {
3834            !!!cp ('t31');
3835        }        }
3836                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3837        ## Step 16        ## Step 16
3838          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3839          
3840          ## Step 17
3841        $i--;        $i--;
3842        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3843                
3844        ## Step 17        ## Step 18
3845        redo S3;        redo S3;
3846      } # S3      } # S3
3847    
3848      die "$0: _reset_insertion_mode: This line should never be reached";
3849  } # _reset_insertion_mode  } # _reset_insertion_mode
3850    
3851  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3852    my $self = shift;    my $self = shift;
3853    
   my $phase = 'main';  
   
3854    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3855    
3856    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1875  sub _tree_construction_main ($) { Line 3867  sub _tree_construction_main ($) {
3867      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3868      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3869        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3870            !!!cp ('t32');
3871          return;          return;
3872        }        }
3873      }      }
# Line 1889  sub _tree_construction_main ($) { Line 3882  sub _tree_construction_main ($) {
3882    
3883        ## Step 6        ## Step 6
3884        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3885            !!!cp ('t33_1');
3886          #          #
3887        } else {        } else {
3888          my $in_open_elements;          my $in_open_elements;
3889          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3890            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3891                !!!cp ('t33');
3892              $in_open_elements = 1;              $in_open_elements = 1;
3893              last OE;              last OE;
3894            }            }
3895          }          }
3896          if ($in_open_elements) {          if ($in_open_elements) {
3897              !!!cp ('t34');
3898            #            #
3899          } else {          } else {
3900              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3901              !!!cp ('t35');
3902            redo S4;            redo S4;
3903          }          }
3904        }        }
# Line 1923  sub _tree_construction_main ($) { Line 3921  sub _tree_construction_main ($) {
3921    
3922        ## Step 11        ## Step 11
3923        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3924            !!!cp ('t36');
3925          ## Step 7'          ## Step 7'
3926          $i++;          $i++;
3927          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3928                    
3929          redo S7;          redo S7;
3930        }        }
3931    
3932          !!!cp ('t37');
3933      } # S7      } # S7
3934    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3935    
3936    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3937      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3938        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3939            !!!cp ('t38');
3940          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3941          return;          return;
3942        }        }
3943      }      }
3944    
3945        !!!cp ('t39');
3946    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3947    
3948    my $style_start_tag = sub {    my $insert;
3949      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
3950      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3951      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3952       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3953        ->append_child ($style_el);      ## Step 1
3954      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3955        my $el;
3956        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3957    
3958        ## Step 2
3959        $insert->($el);
3960    
3961        ## Step 3
3962        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3963      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3964                  
3965        ## Step 4
3966      my $text = '';      my $text = '';
3967        !!!nack ('t40.1');
3968      !!!next-token;      !!!next-token;
3969      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3970          !!!cp ('t40');
3971        $text .= $token->{data};        $text .= $token->{data};
3972        !!!next-token;        !!!next-token;
3973      } # stop if non-character token or tokenizer stops tokenising      }
3974    
3975        ## Step 5
3976      if (length $text) {      if (length $text) {
3977        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3978          my $text = $self->{document}->create_text_node ($text);
3979          $el->append_child ($text);
3980      }      }
3981        
3982      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3983                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3984      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3985        ## Step 7
3986        if ($token->{type} == END_TAG_TOKEN and
3987            $token->{tag_name} eq $start_tag_name) {
3988          !!!cp ('t42');
3989        ## Ignore the token        ## Ignore the token
3990      } else {      } else {
3991        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3992        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3993            !!!cp ('t43');
3994            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3995          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3996            !!!cp ('t44');
3997            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3998          } else {
3999            die "$0: $content_model_flag in parse_rcdata";
4000          }
4001      }      }
4002      !!!next-token;      !!!next-token;
4003    }; # $style_start_tag    }; # $parse_rcdata
4004    
4005    my $script_start_tag = sub {    my $script_start_tag = sub () {
4006      my $script_el;      my $script_el;
4007      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4008      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4009    
4010      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
4011      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4012            
4013      my $text = '';      my $text = '';
4014        !!!nack ('t45.1');
4015      !!!next-token;      !!!next-token;
4016      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
4017          !!!cp ('t45');
4018        $text .= $token->{data};        $text .= $token->{data};
4019        !!!next-token;        !!!next-token;
4020      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4021      if (length $text) {      if (length $text) {
4022          !!!cp ('t46');
4023        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4024      }      }
4025                                
4026      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
4027    
4028      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
4029          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4030          !!!cp ('t47');
4031        ## Ignore the token        ## Ignore the token
4032      } else {      } else {
4033        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4034          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4035        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4036        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4037      }      }
4038            
4039      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4040          !!!cp ('t49');
4041        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4042      } else {      } else {
4043          !!!cp ('t50');
4044        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4045        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4046          
4047        (($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);  
4048                
4049        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
4050                
# Line 2017  sub _tree_construction_main ($) { Line 4054  sub _tree_construction_main ($) {
4054      !!!next-token;      !!!next-token;
4055    }; # $script_start_tag    }; # $script_start_tag
4056    
4057      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4058      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4059      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4060    
4061    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4062      my $tag_name = shift;      my $end_tag_token = shift;
4063        my $tag_name = $end_tag_token->{tag_name};
4064    
4065        ## NOTE: The adoption agency algorithm (AAA).
4066    
4067      FET: {      FET: {
4068        ## Step 1        ## Step 1
4069        my $formatting_element;        my $formatting_element;
4070        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4071        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4072          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4073              !!!cp ('t52');
4074              last AFE;
4075            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4076                         eq $tag_name) {
4077              !!!cp ('t51');
4078            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4079            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4080            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4081          }          }
4082        } # AFE        } # AFE
4083        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4084          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4085            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4086          ## Ignore the token          ## Ignore the token
4087          !!!next-token;          !!!next-token;
4088          return;          return;
# Line 2046  sub _tree_construction_main ($) { Line 4094  sub _tree_construction_main ($) {
4094          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4095          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4096            if ($in_scope) {            if ($in_scope) {
4097                !!!cp ('t54');
4098              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4099              last INSCOPE;              last INSCOPE;
4100            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4101              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4102                !!!parse-error (type => 'unmatched end tag',
4103                                text => $token->{tag_name},
4104                                token => $end_tag_token);
4105              ## Ignore the token              ## Ignore the token
4106              !!!next-token;              !!!next-token;
4107              return;              return;
4108            }            }
4109          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4110                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4111            $in_scope = 0;            $in_scope = 0;
4112          }          }
4113        } # INSCOPE        } # INSCOPE
4114        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4115          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4116            !!!parse-error (type => 'unmatched end tag',
4117                            text => $token->{tag_name},
4118                            token => $end_tag_token);
4119          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4120          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4121          return;          return;
4122        }        }
4123        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4124          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4125            !!!parse-error (type => 'not closed',
4126                            text => $self->{open_elements}->[-1]->[0]
4127                                ->manakai_local_name,
4128                            token => $end_tag_token);
4129        }        }
4130                
4131        ## Step 2        ## Step 2
# Line 2076  sub _tree_construction_main ($) { Line 4133  sub _tree_construction_main ($) {
4133        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4134        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4135          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4136          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4137              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4138              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4139               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4140              !!!cp ('t59');
4141            $furthest_block = $node;            $furthest_block = $node;
4142            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4143          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4144              !!!cp ('t60');
4145            last OE;            last OE;
4146          }          }
4147        } # OE        } # OE
4148                
4149        ## Step 3        ## Step 3
4150        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4151            !!!cp ('t61');
4152          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4153          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4154          !!!next-token;          !!!next-token;
# Line 2101  sub _tree_construction_main ($) { Line 4161  sub _tree_construction_main ($) {
4161        ## Step 5        ## Step 5
4162        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4163        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4164            !!!cp ('t62');
4165          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4166        }        }
4167                
# Line 2123  sub _tree_construction_main ($) { Line 4184  sub _tree_construction_main ($) {
4184          S7S2: {          S7S2: {
4185            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4186              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4187                  !!!cp ('t63');
4188                $node_i_in_active = $_;                $node_i_in_active = $_;
4189                last S7S2;                last S7S2;
4190              }              }
# Line 2136  sub _tree_construction_main ($) { Line 4198  sub _tree_construction_main ($) {
4198                    
4199          ## Step 4          ## Step 4
4200          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4201              !!!cp ('t64');
4202            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4203          }          }
4204                    
4205          ## Step 5          ## Step 5
4206          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4207              !!!cp ('t65');
4208            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4209            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4210            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2158  sub _tree_construction_main ($) { Line 4222  sub _tree_construction_main ($) {
4222        } # S7          } # S7  
4223                
4224        ## Step 8        ## Step 8
4225        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4226            my $foster_parent_element;
4227            my $next_sibling;
4228            OE: for (reverse 0..$#{$self->{open_elements}}) {
4229              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4230                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4231                                 if (defined $parent and $parent->node_type == 1) {
4232                                   !!!cp ('t65.1');
4233                                   $foster_parent_element = $parent;
4234                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4235                                 } else {
4236                                   !!!cp ('t65.2');
4237                                   $foster_parent_element
4238                                     = $self->{open_elements}->[$_ - 1]->[0];
4239                                 }
4240                                 last OE;
4241                               }
4242                             } # OE
4243                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4244                               unless defined $foster_parent_element;
4245            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4246            $open_tables->[-1]->[1] = 1; # tainted
4247          } else {
4248            !!!cp ('t65.3');
4249            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4250          }
4251                
4252        ## Step 9        ## Step 9
4253        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2175  sub _tree_construction_main ($) { Line 4264  sub _tree_construction_main ($) {
4264        my $i;        my $i;
4265        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4266          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4267              !!!cp ('t66');
4268            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4269            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4270          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4271              !!!cp ('t67');
4272            $i = $_;            $i = $_;
4273          }          }
4274        } # AFE        } # AFE
# Line 2187  sub _tree_construction_main ($) { Line 4278  sub _tree_construction_main ($) {
4278        undef $i;        undef $i;
4279        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4280          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4281              !!!cp ('t68');
4282            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4283            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4284          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4285              !!!cp ('t69');
4286            $i = $_;            $i = $_;
4287          }          }
4288        } # OE        } # OE
# Line 2200  sub _tree_construction_main ($) { Line 4293  sub _tree_construction_main ($) {
4293      } # FET      } # FET
4294    }; # $formatting_end_tag    }; # $formatting_end_tag
4295    
4296    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4297      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4298    }; # $insert_to_current    }; # $insert_to_current
4299    
4300    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4301                         my $child = shift;      my $child = shift;
4302                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4303                              table => 1, tbody => 1, tfoot => 1,        # MUST
4304                              thead => 1, tr => 1,        my $foster_parent_element;
4305                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4306                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4307                           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') {  
4308                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4309                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4310                                   !!!cp ('t70');
4311                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4312                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4313                               } else {                               } else {
4314                                   !!!cp ('t71');
4315                                 $foster_parent_element                                 $foster_parent_element
4316                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4317                               }                               }
# Line 2230  sub _tree_construction_main ($) { Line 4322  sub _tree_construction_main ($) {
4322                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4323                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4324                             ($child, $next_sibling);                             ($child, $next_sibling);
4325                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4326                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4327                         }        !!!cp ('t72');
4328          $self->{open_elements}->[-1]->[0]->append_child ($child);
4329        }
4330    }; # $insert_to_foster    }; # $insert_to_foster
4331    
4332    my $in_body = sub {    B: while (1) {
4333      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4334      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4335        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4336          $script_start_tag->();        ## Ignore the token
4337          return;        ## Stay in the phase
4338        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4339          $style_start_tag->();        next B;
4340          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4341        } elsif ({               $token->{tag_name} eq 'html') {
4342                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4343                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4344          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4345          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4346          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4347          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4348          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4349            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4350          } else {        } else {
4351            $insert->($el);          !!!cp ('t81');
4352          }        }
4353            
4354          !!!next-token;        !!!cp ('t82');
4355          return;        !!!parse-error (type => 'not first start tag', token => $token);
4356        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4357          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4358          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4359          my $title_el;            !!!cp ('t84');
4360          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4361          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4362            ->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;  
4363          }          }
4364                    }
4365          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4366                    !!!next-token;
4367          next B;
4368        } elsif ($token->{type} == COMMENT_TOKEN) {
4369          my $comment = $self->{document}->create_comment ($token->{data});
4370          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4371            !!!cp ('t85');
4372            $self->{document}->append_child ($comment);
4373          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4374            !!!cp ('t86');
4375            $self->{open_elements}->[0]->[0]->append_child ($comment);
4376          } else {
4377            !!!cp ('t87');
4378            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4379          }
4380          !!!next-token;
4381          next B;
4382        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4383          if ($token->{type} == CHARACTER_TOKEN) {
4384            !!!cp ('t87.1');
4385            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4386          !!!next-token;          !!!next-token;
4387          return;          next B;
4388        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4389          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4390            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4391            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4392              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4393                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4394              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4395              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4396              $formatting_end_tag->($token->{tag_name});            #
4397                        } elsif ({
4398              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4399                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4400                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4401                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4402                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4403              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4404              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4405                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4406                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4407                  last OE;            !!!cp ('t87.2');
4408                }            !!!parse-error (type => 'not closed',
4409              } # OE                            text => $self->{open_elements}->[-1]->[0]
4410              last AFE;                                ->manakai_local_name,
4411            } elsif ($node->[0] eq '#marker') {                            token => $token);
4412              last AFE;  
4413              pop @{$self->{open_elements}}
4414                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4415    
4416              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4417              ## Reprocess.
4418              next B;
4419            } else {
4420              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4421              my $tag_name = $token->{tag_name};
4422              if ($nsuri eq $SVG_NS) {
4423                $tag_name = {
4424                   altglyph => 'altGlyph',
4425                   altglyphdef => 'altGlyphDef',
4426                   altglyphitem => 'altGlyphItem',
4427                   animatecolor => 'animateColor',
4428                   animatemotion => 'animateMotion',
4429                   animatetransform => 'animateTransform',
4430                   clippath => 'clipPath',
4431                   feblend => 'feBlend',
4432                   fecolormatrix => 'feColorMatrix',
4433                   fecomponenttransfer => 'feComponentTransfer',
4434                   fecomposite => 'feComposite',
4435                   feconvolvematrix => 'feConvolveMatrix',
4436                   fediffuselighting => 'feDiffuseLighting',
4437                   fedisplacementmap => 'feDisplacementMap',
4438                   fedistantlight => 'feDistantLight',
4439                   feflood => 'feFlood',
4440                   fefunca => 'feFuncA',
4441                   fefuncb => 'feFuncB',
4442                   fefuncg => 'feFuncG',
4443                   fefuncr => 'feFuncR',
4444                   fegaussianblur => 'feGaussianBlur',
4445                   feimage => 'feImage',
4446                   femerge => 'feMerge',
4447                   femergenode => 'feMergeNode',
4448                   femorphology => 'feMorphology',
4449                   feoffset => 'feOffset',
4450                   fepointlight => 'fePointLight',
4451                   fespecularlighting => 'feSpecularLighting',
4452                   fespotlight => 'feSpotLight',
4453                   fetile => 'feTile',
4454                   feturbulence => 'feTurbulence',
4455                   foreignobject => 'foreignObject',
4456                   glyphref => 'glyphRef',
4457                   lineargradient => 'linearGradient',
4458                   radialgradient => 'radialGradient',
4459                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4460                   textpath => 'textPath',  
4461                }->{$tag_name} || $tag_name;
4462            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4463    
4464          !!!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];  
4465    
4466          !!!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', ''];  
4467    
4468          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4469          return;  
4470        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4471                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4472          $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});  
4473            } else {            } else {
4474              !!!parse-error (type => 'in CDATA:#'.$token->{type});              !!!cp ('t87.4');
4475            }            }
4476            ## ISSUE: And ignore?  
4477              !!!next-token;
4478              next B;
4479          }          }
4480          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4481          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4482        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4483          $reconstruct_active_formatting_elements->($insert_to_current);          #
4484                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4485          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4486                    !!!parse-error (type => 'not closed',
4487          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4488          !!!next-token;                              ->manakai_local_name,
4489          return;                          token => $token);
4490        } elsif ({  
4491                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4492                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4493                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4494                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4495                 }->{$token->{tag_name}}) {          ## Reprocess.
4496          !!!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.  
4497        } else {        } else {
4498          $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;  
4499        }        }
4500      } elsif ($token->{type} eq 'end tag') {      }
4501        if ($token->{tag_name} eq 'body') {  
4502          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4503            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4504            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4505              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4506            }              !!!cp ('t88.2');
4507            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4508            !!!next-token;              #
4509            return;            } else {
4510          } else {              !!!cp ('t88.1');
4511            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              ## Ignore the token.
4512            ## Ignore the token              #
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 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;  
4513            }            }
4514          } # INSCOPE            unless (length $token->{data}) {
4515                        !!!cp ('t88');
4516          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4517            !!!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;  
4518            }            }
4519          } # INSCOPE  ## TODO: set $token->{column} appropriately
           
         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]);  
4520          }          }
4521    
4522          undef $self->{form_element};          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4523          !!!next-token;            !!!cp ('t89');
4524          return;            ## As if <head>
4525        } elsif ({            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4526                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4527                 }->{$token->{tag_name}}) {            push @{$self->{open_elements}},
4528          ## 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];  
4529    
4530          ## Step 2            ## Reprocess in the "in head" insertion mode...
4531          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;  
4532    
4533              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4534              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4535            } else {            !!!cp ('t90');
4536              ## Step 3            ## As if </noscript>
4537              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4538                  #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];  
4539                        
4540            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4541            redo S2;            ## As if </head>
4542          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
4543    
4544    B: {            ## Reprocess in the "after head" insertion mode...
4545      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4546        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4547          !!!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]);  
         }  
4548    
4549          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4550          last B;          } else {
4551              !!!cp ('t92');
4552            }
4553    
4554          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4555        } else {          ## As if <body>
4556          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4557            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4558              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4559                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4560                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4561                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4562                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4563                }              !!!cp ('t93');
4564              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4565              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4566              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4567              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4568              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4569              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4570              ## 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);  
4571              !!!next-token;              !!!next-token;
4572              redo B;              next B;
4573            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4574              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4575              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4576              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4577              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4578              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4579              if ($token->{tag_name} eq 'head') {              !!!next-token;
4580                !!!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;  
             }  
4581            } else {            } else {
4582              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4583            }              !!!parse-error (type => 'in head:head',
4584          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4585            if ($token->{type} eq 'character') {              ## Ignore the token
4586              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);  
4587              !!!next-token;              !!!next-token;
4588              redo B;              next B;
4589            } elsif ($token->{type} eq 'start tag') {            }
4590              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4591                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4592                my $title_el;            ## As if <head>
4593                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4594                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4595                  ->append_child ($title_el);            push @{$self->{open_elements}},
4596                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4597                delete $self->{escape}; # MUST  
4598              $self->{insertion_mode} = IN_HEAD_IM;
4599                my $text = '';            ## Reprocess in the "in head" insertion mode...
4600                !!!next-token;          } else {
4601                while ($token->{type} eq 'character') {            !!!cp ('t97');
4602                  $text .= $token->{data};          }
4603                  !!!next-token;  
4604                }              if ($token->{tag_name} eq 'base') {
4605                if (length $text) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4606                  $title_el->manakai_append_text ($text);                  !!!cp ('t98');
4607                }                  ## As if </noscript>
4608                                  pop @{$self->{open_elements}};
4609                $self->{content_model_flag} = 'PCDATA';                  !!!parse-error (type => 'in noscript', text => 'base',
4610                                    token => $token);
4611                                
4612                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4613                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4614                } else {                } else {
4615                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4616                }                }
               !!!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);  
4617    
4618                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4619                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4620              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4621                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4622                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4623                !!!next-token;                  push @{$self->{open_elements}},
4624                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}};  
4625                } else {                } else {
4626                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4627                }                }
4628                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4629                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4630                  pop @{$self->{open_elements}} # <head>
4631                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4632                  !!!nack ('t101.1');
4633                !!!next-token;                !!!next-token;
4634                redo B;                next B;
4635              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'link') {
4636                #                ## NOTE: There is a "as if in head" code clone.
4637              } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4638                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t102');
4639                ## Ignore the token                  !!!parse-error (type => 'after head',
4640                !!!next-token;                                  text => $token->{tag_name}, token => $token);
4641                redo B;                  push @{$self->{open_elements}},
4642              }                      [$self->{head_element}, $el_category->{head}];
4643            } else {                } else {
4644              #                  !!!cp ('t103');
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## 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;  
4645                }                }
4646              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4647                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4648              #                pop @{$self->{open_elements}} # <head>
4649            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4650              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';  
4651                !!!next-token;                !!!next-token;
4652                redo B;                next B;
4653              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4654                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4655                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4656                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4657                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4658                $self->{insertion_mode} = 'in head';                                  text => $token->{tag_name}, token => $token);
4659                ## reprocess                  push @{$self->{open_elements}},
4660                redo B;                      [$self->{head_element}, $el_category->{head}];
4661              } else {                } else {
4662                #                  !!!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;  
4663                }                }
4664              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4665                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4666    
4667              !!!parse-error (type => 'in table:#character');                unless ($self->{confident}) {
4668                    if ($token->{attributes}->{charset}) {
4669              ## As if in body, but insert into foster parent element                    !!!cp ('t106');
4670              ## ISSUE: Spec says that "whenever a node would be inserted                    ## NOTE: Whether the encoding is supported or not is handled
4671              ## into the current node" while characters might not be                    ## in the {change_encoding} callback.
4672              ## result in a new Text node.                    $self->{change_encoding}
4673              $reconstruct_active_formatting_elements->($insert_to_foster);                        ->($self, $token->{attributes}->{charset}->{value},
4674                                         $token);
4675              if ({                    
4676                   table => 1, tbody => 1, tfoot => 1,                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4677                   thead => 1, tr => 1,                        ->set_user_data (manakai_has_reference =>
4678                  }->{$self->{open_elements}->[-1]->[1]}) {                                             $token->{attributes}->{charset}
4679                # MUST                                                 ->{has_reference});
4680                my $foster_parent_element;                  } elsif ($token->{attributes}->{content}) {
4681                my $next_sibling;                    if ($token->{attributes}->{content}->{value}
4682                my $prev_sibling;                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4683                OE: for (reverse 0..$#{$self->{open_elements}}) {                            [\x09-\x0D\x20]*=
4684                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4685                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4686                    if (defined $parent and $parent->node_type == 1) {                      !!!cp ('t107');
4687                      $foster_parent_element = $parent;                      ## NOTE: Whether the encoding is supported or not is handled
4688                      $next_sibling = $self->{open_elements}->[$_]->[0];                      ## in the {change_encoding} callback.
4689                      $prev_sibling = $next_sibling->previous_sibling;                      $self->{change_encoding}
4690                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4691                               $token);
4692                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4693                            ->set_user_data (manakai_has_reference =>
4694                                                 $token->{attributes}->{content}
4695                                                       ->{has_reference});
4696                    } else {                    } else {
4697                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4698                    }                    }
                   last OE;  
4699                  }                  }
               } # 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});  
4700                } else {                } else {
4701                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4702                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4703                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4704                }                        ->set_user_data (manakai_has_reference =>
4705              } else {                                             $token->{attributes}->{charset}
4706                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4707              }                  }
4708                                if ($token->{attributes}->{content}) {
4709              !!!next-token;                    !!!cp ('t110');
4710              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4711            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4712              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4713              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4714              !!!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}};  
4715                }                }
4716    
4717                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4718                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4719                  !!!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}};  
4720                !!!next-token;                !!!next-token;
4721                redo B;                next B;
4722              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4723                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4724                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4725                       }->{$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]);  
4726                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4727                    !!!parse-error (type => 'in noscript', text => 'title',
4728                                    token => $token);
4729                  
4730                    $self->{insertion_mode} = IN_HEAD_IM;
4731                    ## Reprocess in the "in head" insertion mode...
4732                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4733                    !!!cp ('t112');
4734                    !!!parse-error (type => 'after head',
4735                                    text => $token->{tag_name}, token => $token);
4736                    push @{$self->{open_elements}},
4737                        [$self->{head_element}, $el_category->{head}];
4738                  } else {
4739                    !!!cp ('t113');
4740                }                }
4741    
4742                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4743                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4744                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4745                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4746                redo B;                pop @{$self->{open_elements}} # <head>
4747              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4748                ## NOTE: There are code clones for this "table in table"                next B;
4749                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4750                         $token->{tag_name} eq 'noframes') {
4751                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4752                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4753                my $i;                ## NOTE: There is a "as if in head" code clone.
4754                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4755                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4756                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4757                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4758                    last INSCOPE;                  push @{$self->{open_elements}},
4759                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4760                            table => 1, html => 1,                } else {
4761                           }->{$node->[1]}) {                  !!!cp ('t115');
4762                    last INSCOPE;                }
4763                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4764                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4765                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4766                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4767                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4768                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4769                    !!!cp ('t116');
4770                    ## NOTE: and scripting is disalbed
4771                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4772                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4773                    !!!nack ('t116.1');
4774                  !!!next-token;                  !!!next-token;
4775                  redo B;                  next B;
4776                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4777                    !!!cp ('t117');
4778                    !!!parse-error (type => 'in noscript', text => 'noscript',
4779                                    token => $token);
4780                    ## Ignore the token
4781                    !!!nack ('t117.1');
4782                    !!!next-token;
4783                    next B;
4784                  } else {
4785                    !!!cp ('t118');
4786                    #
4787                }                }
4788                } elsif ($token->{tag_name} eq 'script') {
4789                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4790                    !!!cp ('t119');
4791                    ## As if </noscript>
4792                    pop @{$self->{open_elements}};
4793                    !!!parse-error (type => 'in noscript', text => 'script',
4794                                    token => $token);
4795                                
4796                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4797                if ({                  ## Reprocess in the "in head" insertion mode...
4798                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4799                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4800                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4801                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4802                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4803                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4804                  $token = {type => 'end tag',                } else {
4805                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
                 redo B;  
4806                }                }
4807    
4808                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4809                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4810                  pop @{$self->{open_elements}} # <head>
4811                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4812                  next B;
4813                } elsif ($token->{tag_name} eq 'body' or
4814                         $token->{tag_name} eq 'frameset') {
4815                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4816                    !!!cp ('t122');
4817                    ## As if </noscript>
4818                    pop @{$self->{open_elements}};
4819                    !!!parse-error (type => 'in noscript',
4820                                    text => $token->{tag_name}, token => $token);
4821                    
4822                    ## Reprocess in the "in head" insertion mode...
4823                    ## As if </head>
4824                    pop @{$self->{open_elements}};
4825                    
4826                    ## Reprocess in the "after head" insertion mode...
4827                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4828                    !!!cp ('t124');
4829                    pop @{$self->{open_elements}};
4830                    
4831                    ## Reprocess in the "after head" insertion mode...
4832                  } else {
4833                    !!!cp ('t125');
4834                }                }
4835    
4836                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4837                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4838                  if ($token->{tag_name} eq 'body') {
4839                    !!!cp ('t126');
4840                    $self->{insertion_mode} = IN_BODY_IM;
4841                  } elsif ($token->{tag_name} eq 'frameset') {
4842                    !!!cp ('t127');
4843                    $self->{insertion_mode} = IN_FRAMESET_IM;
4844                  } else {
4845                    die "$0: tag name: $self->{tag_name}";
4846                  }
4847                  !!!nack ('t127.1');
4848                  !!!next-token;
4849                  next B;
4850                } else {
4851                  !!!cp ('t128');
4852                  #
4853                }
4854    
4855                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4856                  !!!cp ('t129');
4857                  ## As if </noscript>
4858                  pop @{$self->{open_elements}};
4859                  !!!parse-error (type => 'in noscript:/',
4860                                  text => $token->{tag_name}, token => $token);
4861                  
4862                  ## Reprocess in the "in head" insertion mode...
4863                  ## As if </head>
4864                  pop @{$self->{open_elements}};
4865    
4866                ## reprocess                ## Reprocess in the "after head" insertion mode...
4867                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4868                  !!!cp ('t130');
4869                  ## As if </head>
4870                  pop @{$self->{open_elements}};
4871    
4872                  ## Reprocess in the "after head" insertion mode...
4873              } else {              } else {
4874                #                !!!cp ('t131');
4875              }              }
4876            } elsif ($token->{type} eq 'end tag') {  
4877              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4878                ## have a table element in table scope              ## As if <body>
4879                my $i;              !!!insert-element ('body',, $token);
4880                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4881                  my $node = $self->{open_elements}->[$_];              ## reprocess
4882                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4883                    $i = $_;              next B;
4884                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4885                  } elsif ({              if ($token->{tag_name} eq 'head') {
4886                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4887                           }->{$node->[1]}) {                  !!!cp ('t132');
4888                    last INSCOPE;                  ## As if <head>
4889                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4890                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4891                unless (defined $i) {                  push @{$self->{open_elements}},
4892                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4893    
4894                    ## Reprocess in the "in head" insertion mode...
4895                    pop @{$self->{open_elements}};
4896                    $self->{insertion_mode} = AFTER_HEAD_IM;
4897                    !!!next-token;
4898                    next B;
4899                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4900                    !!!cp ('t133');
4901                    ## As if </noscript>
4902                    pop @{$self->{open_elements}};
4903                    !!!parse-error (type => 'in noscript:/',
4904                                    text => 'head', token => $token);
4905                    
4906                    ## Reprocess in the "in head" insertion mode...
4907                    pop @{$self->{open_elements}};
4908                    $self->{insertion_mode} = AFTER_HEAD_IM;
4909                    !!!next-token;
4910                    next B;
4911                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4912                    !!!cp ('t134');
4913                    pop @{$self->{open_elements}};
4914                    $self->{insertion_mode} = AFTER_HEAD_IM;
4915                    !!!next-token;
4916                    next B;
4917                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4918                    !!!cp ('t134.1');
4919                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4920                                    token => $token);
4921                  ## Ignore the token                  ## Ignore the token
4922                  !!!next-token;                  !!!next-token;
4923                  redo B;                  next B;
4924                  } else {
4925                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4926                }                }
4927                              } elsif ($token->{tag_name} eq 'noscript') {
4928                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4929                if ({                  !!!cp ('t136');
4930                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4931                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4932                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4933                  !!!back-token;                  next B;
4934                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4935                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4936                  redo B;                  !!!cp ('t137');
4937                    !!!parse-error (type => 'unmatched end tag',
4938                                    text => 'noscript', token => $token);
4939                    ## Ignore the token ## ISSUE: An issue in the spec.
4940                    !!!next-token;
4941                    next B;
4942                  } else {
4943                    !!!cp ('t138');
4944                    #
4945                }                }
4946                } elsif ({
4947                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4948                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4949                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4950                      $self->{insertion_mode} == IN_HEAD_IM or
4951                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4952                    !!!cp ('t140');
4953                    !!!parse-error (type => 'unmatched end tag',
4954                                    text => $token->{tag_name}, token => $token);
4955                    ## Ignore the token
4956                    !!!next-token;
4957                    next B;
4958                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4959                    !!!cp ('t140.1');
4960                    !!!parse-error (type => 'unmatched end tag',
4961                                    text => $token->{tag_name}, token => $token);
4962                    ## Ignore the token
4963                    !!!next-token;
4964                    next B;
4965                  } else {
4966                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4967                }                }
4968                } elsif ($token->{tag_name} eq 'p') {
4969                  !!!cp ('t142');
4970                  !!!parse-error (type => 'unmatched end tag',
4971                                  text => $token->{tag_name}, token => $token);
4972                  ## Ignore the token
4973                  !!!next-token;
4974                  next B;
4975                } elsif ($token->{tag_name} eq 'br') {
4976                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4977                    !!!cp ('t142.2');
4978                    ## (before head) as if <head>, (in head) as if </head>
4979                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4980                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4981                    $self->{insertion_mode} = AFTER_HEAD_IM;
4982      
4983                    ## Reprocess in the "after head" insertion mode...
4984                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4985                    !!!cp ('t143.2');
4986                    ## As if </head>
4987                    pop @{$self->{open_elements}};
4988                    $self->{insertion_mode} = AFTER_HEAD_IM;
4989      
4990                    ## Reprocess in the "after head" insertion mode...
4991                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4992                    !!!cp ('t143.3');
4993                    ## ISSUE: Two parse errors for <head><noscript></br>
4994                    !!!parse-error (type => 'unmatched end tag',
4995                                    text => 'br', token => $token);
4996                    ## As if </noscript>
4997                    pop @{$self->{open_elements}};
4998                    $self->{insertion_mode} = IN_HEAD_IM;
4999    
5000                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
5001                    ## As if </head>
5002                    pop @{$self->{open_elements}};
5003                    $self->{insertion_mode} = AFTER_HEAD_IM;
5004    
5005                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
5006                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5007                    !!!cp ('t143.4');
5008                    #
5009                  } else {
5010                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
5011                  }
5012    
5013                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5014                  !!!parse-error (type => 'unmatched end tag',
5015                                  text => 'br', token => $token);
5016                  ## Ignore the token
5017                !!!next-token;                !!!next-token;
5018                redo B;                next B;
5019              } elsif ({              } else {
5020                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
5021                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
5022                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5023                ## Ignore the token                ## Ignore the token
5024                !!!next-token;                !!!next-token;
5025                redo B;                next B;
5026                }
5027    
5028                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5029                  !!!cp ('t146');
5030                  ## As if </noscript>
5031                  pop @{$self->{open_elements}};
5032                  !!!parse-error (type => 'in noscript:/',
5033                                  text => $token->{tag_name}, token => $token);
5034                  
5035                  ## Reprocess in the "in head" insertion mode...
5036                  ## As if </head>
5037                  pop @{$self->{open_elements}};
5038    
5039                  ## Reprocess in the "after head" insertion mode...
5040                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5041                  !!!cp ('t147');
5042                  ## As if </head>
5043                  pop @{$self->{open_elements}};
5044    
5045                  ## Reprocess in the "after head" insertion mode...
5046                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5047    ## ISSUE: This case cannot be reached?
5048                  !!!cp ('t148');
5049                  !!!parse-error (type => 'unmatched end tag',
5050                                  text => $token->{tag_name}, token => $token);
5051                  ## Ignore the token ## ISSUE: An issue in the spec.
5052                  !!!next-token;
5053                  next B;
5054              } else {              } else {
5055                #                !!!cp ('t149');
5056              }              }
           } else {  
             #  
           }  
5057    
5058            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
5059            $in_body->($insert_to_foster);              ## As if <body>
5060            redo B;              !!!insert-element ('body',, $token);
5061          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
5062            if ($token->{type} eq 'character') {              ## reprocess
5063              ## NOTE: This is a code clone of "character in body".              next B;
5064          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5065            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5066              !!!cp ('t149.1');
5067    
5068              ## NOTE: As if <head>
5069              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5070              $self->{open_elements}->[-1]->[0]->append_child
5071                  ($self->{head_element});
5072              #push @{$self->{open_elements}},
5073              #    [$self->{head_element}, $el_category->{head}];
5074              #$self->{insertion_mode} = IN_HEAD_IM;
5075              ## NOTE: Reprocess.
5076    
5077              ## NOTE: As if </head>
5078              #pop @{$self->{open_elements}};
5079              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5080              ## NOTE: Reprocess.
5081              
5082              #
5083            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5084              !!!cp ('t149.2');
5085    
5086              ## NOTE: As if </head>
5087              pop @{$self->{open_elements}};
5088              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5089              ## NOTE: Reprocess.
5090    
5091              #
5092            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5093              !!!cp ('t149.3');
5094    
5095              !!!parse-error (type => 'in noscript:#eof', token => $token);
5096    
5097              ## As if </noscript>
5098              pop @{$self->{open_elements}};
5099              #$self->{insertion_mode} = IN_HEAD_IM;
5100              ## NOTE: Reprocess.
5101    
5102              ## NOTE: As if </head>
5103              pop @{$self->{open_elements}};
5104              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5105              ## NOTE: Reprocess.
5106    
5107              #
5108            } else {
5109              !!!cp ('t149.4');
5110              #
5111            }
5112    
5113            ## NOTE: As if <body>
5114            !!!insert-element ('body',, $token);
5115            $self->{insertion_mode} = IN_BODY_IM;
5116            ## NOTE: Reprocess.
5117            next B;
5118          } else {
5119            die "$0: $token->{type}: Unknown token type";
5120          }
5121    
5122              ## ISSUE: An issue in the spec.
5123        } elsif ($self->{insertion_mode} & BODY_IMS) {
5124              if ($token->{type} == CHARACTER_TOKEN) {
5125                !!!cp ('t150');
5126                ## NOTE: There is a code clone of "character in body".
5127              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5128                            
5129              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5130    
5131              !!!next-token;              !!!next-token;
5132              redo B;              next B;
5133            } 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') {  
5134              if ({              if ({
5135                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
5136                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
5137                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5138                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
5139                    ## have an element in table scope
5140                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
5141                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
5142                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
5143                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
5144                  my $node = $self->{open_elements}->[$_];  
5145                  if ($node->[1] eq 'caption') {                      ## Close the cell
5146                    $i = $_;                      !!!back-token; # <x>
5147                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
5148                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
5149                            table => 1, html => 1,                                line => $token->{line},
5150                           }->{$node->[1]}) {                                column => $token->{column}};
5151                    last INSCOPE;                      next B;
5152                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5153                        !!!cp ('t152');
5154                        ## ISSUE: This case can never be reached, maybe.
5155                        last;
5156                      }
5157                  }                  }
5158                } # INSCOPE  
5159                unless (defined $i) {                  !!!cp ('t153');
5160                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
5161                        text => $token->{tag_name}, token => $token);
5162                  ## Ignore the token                  ## Ignore the token
5163                    !!!nack ('t153.1');
5164                  !!!next-token;                  !!!next-token;
5165                  redo B;                  next B;
5166                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5167                                  !!!parse-error (type => 'not closed', text => 'caption',
5168                ## generate implied end tags                                  token => $token);
5169                if ({                  
5170                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
5171                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
5172                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
5173                  !!!back-token; # <?>                  INSCOPE: {
5174                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
5175                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
5176                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
5177                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
5178                  redo B;                        $i = $_;
5179                }                        last INSCOPE;
5180                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5181                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
5182                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
5183                }                      }
5184                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
5185    
5186                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
5187                      !!!parse-error (type => 'start tag not allowed',
5188                                      text => $token->{tag_name}, token => $token);
5189                      ## Ignore the token
5190                      !!!nack ('t157.1');
5191                      !!!next-token;
5192                      next B;
5193                    } # INSCOPE
5194                    
5195                    ## generate implied end tags
5196                    while ($self->{open_elements}->[-1]->[1]
5197                               & END_TAG_OPTIONAL_EL) {
5198                      !!!cp ('t158');
5199                      pop @{$self->{open_elements}};
5200                    }
5201    
5202                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5203                redo B;                    !!!cp ('t159');
5204                      !!!parse-error (type => 'not closed',
5205                                      text => $self->{open_elements}->[-1]->[0]
5206                                          ->manakai_local_name,
5207                                      token => $token);
5208                    } else {
5209                      !!!cp ('t160');
5210                    }
5211                    
5212                    splice @{$self->{open_elements}}, $i;
5213                    
5214                    $clear_up_to_marker->();
5215                    
5216                    $self->{insertion_mode} = IN_TABLE_IM;
5217                    
5218                    ## reprocess
5219                    !!!ack-later;
5220                    next B;
5221                  } else {
5222                    !!!cp ('t161');
5223                    #
5224                  }
5225              } else {              } else {
5226                  !!!cp ('t162');
5227                #                #
5228              }              }
5229            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5230              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5231                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
5232                my $i;                  ## have an element in table scope
5233                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
5234                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5235                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5236                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5237                    last INSCOPE;                      !!!cp ('t163');
5238                  } elsif ({                      $i = $_;
5239                            table => 1, html => 1,                      last INSCOPE;
5240                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5241                    last INSCOPE;                      !!!cp ('t164');
5242                        last INSCOPE;
5243                      }
5244                    } # INSCOPE
5245                      unless (defined $i) {
5246                        !!!cp ('t165');
5247                        !!!parse-error (type => 'unmatched end tag',
5248                                        text => $token->{tag_name},
5249                                        token => $token);
5250                        ## Ignore the token
5251                        !!!next-token;
5252                        next B;
5253                      }
5254                    
5255                    ## generate implied end tags
5256                    while ($self->{open_elements}->[-1]->[1]
5257                               & END_TAG_OPTIONAL_EL) {
5258                      !!!cp ('t166');
5259                      pop @{$self->{open_elements}};
5260                  }                  }
5261                } # INSCOPE  
5262                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5263                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
5264                      !!!cp ('t167');
5265                      !!!parse-error (type => 'not closed',
5266                                      text => $self->{open_elements}->[-1]->[0]
5267                                          ->manakai_local_name,
5268                                      token => $token);
5269                    } else {
5270                      !!!cp ('t168');
5271                    }
5272                    
5273                    splice @{$self->{open_elements}}, $i;
5274                    
5275                    $clear_up_to_marker->();
5276                    
5277                    $self->{insertion_mode} = IN_ROW_IM;
5278                    
5279                    !!!next-token;
5280                    next B;
5281                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5282                    !!!cp ('t169');
5283                    !!!parse-error (type => 'unmatched end tag',
5284                                    text => $token->{tag_name}, token => $token);
5285                  ## Ignore the token                  ## Ignore the token
5286                  !!!next-token;                  !!!next-token;
5287                  redo B;                  next B;
5288                }                } else {
5289                                  !!!cp ('t170');
5290                ## 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;  
5291                }                }
5292                } elsif ($token->{tag_name} eq 'caption') {
5293                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
5294                    ## have a table element in table scope
5295                    my $i;
5296                    INSCOPE: {
5297                      for (reverse 0..$#{$self->{open_elements}}) {
5298                        my $node = $self->{open_elements}->[$_];
5299                        if ($node->[1] & CAPTION_EL) {
5300                          !!!cp ('t171');
5301                          $i = $_;
5302                          last INSCOPE;
5303                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5304                          !!!cp ('t172');
5305                          last;
5306                        }
5307                      }
5308    
5309                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
5310                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
5311                                      text => $token->{tag_name}, token => $token);
5312                      ## Ignore the token
5313                      !!!next-token;
5314                      next B;
5315                    } # INSCOPE
5316                    
5317                    ## generate implied end tags
5318                    while ($self->{open_elements}->[-1]->[1]
5319                               & END_TAG_OPTIONAL_EL) {
5320                      !!!cp ('t174');
5321                      pop @{$self->{open_elements}};
5322                    }
5323                    
5324                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5325                      !!!cp ('t175');
5326                      !!!parse-error (type => 'not closed',
5327                                      text => $self->{open_elements}->[-1]->[0]
5328                                          ->manakai_local_name,
5329                                      token => $token);
5330                    } else {
5331                      !!!cp ('t176');
5332                    }
5333                    
5334                    splice @{$self->{open_elements}}, $i;
5335                    
5336                    $clear_up_to_marker->();
5337                    
5338                    $self->{insertion_mode} = IN_TABLE_IM;
5339                    
5340                    !!!next-token;
5341                    next B;
5342                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5343                    !!!cp ('t177');
5344                    !!!parse-error (type => 'unmatched end tag',
5345                                    text => $token->{tag_name}, token => $token);
5346                    ## Ignore the token
5347                    !!!next-token;
5348                    next B;
5349                  } else {
5350                    !!!cp ('t178');
5351                    #
5352                }                }
5353                } elsif ({
5354                          table => 1, tbody => 1, tfoot => 1,
5355                          thead => 1, tr => 1,
5356                         }->{$token->{tag_name}} and
5357                         $self->{insertion_mode} == IN_CELL_IM) {
5358                  ## have an element in table scope
5359                  my $i;
5360                  my $tn;
5361                  INSCOPE: {
5362                    for (reverse 0..$#{$self->{open_elements}}) {
5363                      my $node = $self->{open_elements}->[$_];
5364                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5365                        !!!cp ('t179');
5366                        $i = $_;
5367    
5368                        ## Close the cell
5369                        !!!back-token; # </x>
5370                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5371                                  line => $token->{line},
5372                                  column => $token->{column}};
5373                        next B;
5374                      } elsif ($node->[1] & TABLE_CELL_EL) {
5375                        !!!cp ('t180');
5376                        $tn = $node->[0]->manakai_local_name;
5377                        ## NOTE: There is exactly one |td| or |th| element
5378                        ## in scope in the stack of open elements by definition.
5379                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5380                        ## ISSUE: Can this be reached?
5381                        !!!cp ('t181');
5382                        last;
5383                      }
5384                    }
5385    
5386                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5387                    !!!parse-error (type => 'unmatched end tag',
5388                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5389                    ## Ignore the token
5390                $self->{insertion_mode} = 'in table';                  !!!next-token;
5391                    next B;
5392                !!!next-token;                } # INSCOPE
5393                redo B;              } elsif ($token->{tag_name} eq 'table' and
5394              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5395                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5396                                  token => $token);
5397    
5398                ## As if </caption>                ## As if </caption>
5399                ## have a table element in table scope                ## have a table element in table scope
5400                my $i;                my $i;
5401                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5402                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5403                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5404                      !!!cp ('t184');
5405                    $i = $_;                    $i = $_;
5406                    last INSCOPE;                    last INSCOPE;
5407                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5408                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5409                    last INSCOPE;                    last INSCOPE;
5410                  }                  }
5411                } # INSCOPE                } # INSCOPE
5412                unless (defined $i) {                unless (defined $i) {
5413                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5414                    !!!parse-error (type => 'unmatched end tag',
5415                                    text => 'caption', token => $token);
5416                  ## Ignore the token                  ## Ignore the token
5417                  !!!next-token;                  !!!next-token;
5418                  redo B;                  next B;
5419                }                }
5420                                
5421                ## generate implied end tags                ## generate implied end tags
5422                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5423                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5424                     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;  
5425                }                }
5426    
5427                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5428                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5429                    !!!parse-error (type => 'not closed',
5430                                    text => $self->{open_elements}->[-1]->[0]
5431                                        ->manakai_local_name,
5432                                    token => $token);
5433                  } else {
5434                    !!!cp ('t189');
5435                }                }
5436    
5437                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5438    
5439                $clear_up_to_marker->();                $clear_up_to_marker->();
5440    
5441                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5442    
5443                ## reprocess                ## reprocess
5444                redo B;                next B;
5445              } elsif ({              } elsif ({
5446                        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,  
5447                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5448                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5449                ## Ignore the token                  !!!cp ('t190');
5450                redo B;                  !!!parse-error (type => 'unmatched end tag',
5451              } 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');  
5452                  ## Ignore the token                  ## Ignore the token
5453                  !!!next-token;                  !!!next-token;
5454                  redo B;                  next B;
5455                } else {                } else {
5456                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5457                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5458                }                }
5459              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5460                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5461                          thead => 1, tr => 1,
5462                         }->{$token->{tag_name}} and
5463                         $self->{insertion_mode} == IN_CAPTION_IM) {
5464                  !!!cp ('t192');
5465                  !!!parse-error (type => 'unmatched end tag',
5466                                  text => $token->{tag_name}, token => $token);
5467                ## Ignore the token                ## Ignore the token
5468                !!!next-token;                !!!next-token;
5469                redo B;                next B;
5470              } else {              } else {
5471                #                !!!cp ('t193');
5472                  #
5473              }              }
5474            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5475              #          for my $entry (@{$self->{open_elements}}) {
5476              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5477                !!!cp ('t75');
5478                !!!parse-error (type => 'in body:#eof', token => $token);
5479                last;
5480            }            }
5481            }
5482    
5483            ## As if </colgroup>          ## Stop parsing.
5484            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5485              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5486              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5487          }
5488    
5489          $insert = $insert_to_current;
5490          #
5491        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5492          if ($token->{type} == CHARACTER_TOKEN) {
5493            if (not $open_tables->[-1]->[1] and # tainted
5494                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5495              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5496                  
5497              unless (length $token->{data}) {
5498                !!!cp ('t194');
5499              !!!next-token;              !!!next-token;
5500              redo B;              next B;
5501            } else {            } else {
5502              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5503            }            }
5504          } 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;  
               }  
             }  
5505    
5506              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5507    
5508              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5509              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5510              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5511              ## result in a new Text node.              ## result in a new Text node.
5512              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5513                
5514              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]}) {  
5515                # MUST                # MUST
5516                my $foster_parent_element;                my $foster_parent_element;
5517                my $next_sibling;                my $next_sibling;
5518                my $prev_sibling;                my $prev_sibling;
5519                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5520                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5521                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5522                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5523                        !!!cp ('t196');
5524                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5525                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5526                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5527                    } else {                    } else {
5528                        !!!cp ('t197');
5529                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5530                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5531                    }                    }
# Line 3807  sub _tree_construction_main ($) { Line 5537  sub _tree_construction_main ($) {
5537                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5538                if (defined $prev_sibling and                if (defined $prev_sibling and
5539                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5540                    !!!cp ('t198');
5541                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5542                } else {                } else {
5543                    !!!cp ('t199');
5544                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5545                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5546                     $next_sibling);                     $next_sibling);
5547                }                }
5548              } else {            $open_tables->[-1]->[1] = 1; # tainted
5549                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5550              !!!cp ('t200');
5551              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5552            }
5553                
5554            !!!next-token;
5555            next B;
5556          } elsif ($token->{type} == START_TAG_TOKEN) {
5557            if ({
5558                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5559                 th => 1, td => 1,
5560                }->{$token->{tag_name}}) {
5561              if ($self->{insertion_mode} == IN_TABLE_IM) {
5562                ## Clear back to table context
5563                while (not ($self->{open_elements}->[-1]->[1]
5564                                & TABLE_SCOPING_EL)) {
5565                  !!!cp ('t201');
5566                  pop @{$self->{open_elements}};
5567              }              }
5568                            
5569              !!!next-token;              !!!insert-element ('tbody',, $token);
5570              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5571            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5572              ## Copied from 'in table'            }
5573              my $comment = $self->{document}->create_comment ($token->{data});            
5574              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5575              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5576              redo B;                !!!cp ('t202');
5577            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5578              if ({              }
5579                   tr => 1,                  
5580                   th => 1, td => 1,              ## Clear back to table body context
5581                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5582                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5583                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5584                  ## ISSUE: Can this case be reached?
5585                  pop @{$self->{open_elements}};
5586                }
5587                    
5588                    $self->{insertion_mode} = IN_ROW_IM;
5589                    if ($token->{tag_name} eq 'tr') {
5590                      !!!cp ('t204');
5591                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5592                      !!!nack ('t204');
5593                      !!!next-token;
5594                      next B;
5595                    } else {
5596                      !!!cp ('t205');
5597                      !!!insert-element ('tr',, $token);
5598                      ## reprocess in the "in row" insertion mode
5599                    }
5600                  } else {
5601                    !!!cp ('t206');
5602                }                }
5603    
5604                ## Clear back to table body context                ## Clear back to table row context
5605                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5606                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5607                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5608                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5609                }                }
5610                                
5611                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5612                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5613                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5614                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5615                } else {                
5616                  !!!insert-element ('tr');                !!!nack ('t207.1');
5617                  ## reprocess                !!!next-token;
5618                }                next B;
               redo B;  
5619              } elsif ({              } elsif ({
5620                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5621                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5622                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5623                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5624                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5625                my $i;                  ## As if </tr>
5626                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5627                  my $node = $self->{open_elements}->[$_];                  my $i;
5628                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5629                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5630                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5631                    $i = $_;                      !!!cp ('t208');
5632                    last INSCOPE;                      $i = $_;
5633                  } elsif ({                      last INSCOPE;
5634                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5635                           }->{$node->[1]}) {                      !!!cp ('t209');
5636                    last INSCOPE;                      last INSCOPE;
5637                      }
5638                    } # INSCOPE
5639                    unless (defined $i) {
5640                      !!!cp ('t210');
5641    ## TODO: This type is wrong.
5642                      !!!parse-error (type => 'unmacthed end tag',
5643                                      text => $token->{tag_name}, token => $token);
5644                      ## Ignore the token
5645                      !!!nack ('t210.1');
5646                      !!!next-token;
5647                      next B;
5648                    }
5649                    
5650                    ## Clear back to table row context
5651                    while (not ($self->{open_elements}->[-1]->[1]
5652                                    & TABLE_ROW_SCOPING_EL)) {
5653                      !!!cp ('t211');
5654                      ## ISSUE: Can this case be reached?
5655                      pop @{$self->{open_elements}};
5656                    }
5657                    
5658                    pop @{$self->{open_elements}}; # tr
5659                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5660                    if ($token->{tag_name} eq 'tr') {
5661                      !!!cp ('t212');
5662                      ## reprocess
5663                      !!!ack-later;
5664                      next B;
5665                    } else {
5666                      !!!cp ('t213');
5667                      ## reprocess in the "in table body" insertion mode...
5668                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5669                }                }
5670    
5671                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5672                while (not {                  ## have an element in table scope
5673                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5674                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5675                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5676                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5677                        !!!cp ('t214');
5678                        $i = $_;
5679                        last INSCOPE;
5680                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5681                        !!!cp ('t215');
5682                        last INSCOPE;
5683                      }
5684                    } # INSCOPE
5685                    unless (defined $i) {
5686                      !!!cp ('t216');
5687    ## TODO: This erorr type is wrong.
5688                      !!!parse-error (type => 'unmatched end tag',
5689                                      text => $token->{tag_name}, token => $token);
5690                      ## Ignore the token
5691                      !!!nack ('t216.1');
5692                      !!!next-token;
5693                      next B;
5694                    }
5695    
5696                    ## Clear back to table body context
5697                    while (not ($self->{open_elements}->[-1]->[1]
5698                                    & TABLE_ROWS_SCOPING_EL)) {
5699                      !!!cp ('t217');
5700                      ## ISSUE: Can this state be reached?
5701                      pop @{$self->{open_elements}};
5702                    }
5703                    
5704                    ## As if <{current node}>
5705                    ## have an element in table scope
5706                    ## true by definition
5707                    
5708                    ## Clear back to table body context
5709                    ## nop by definition
5710                    
5711                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5712                    $self->{insertion_mode} = IN_TABLE_IM;
5713                    ## reprocess in "in table" insertion mode...
5714                  } else {
5715                    !!!cp ('t218');
5716                }                }
5717    
5718                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5719                ## have an element in table scope                  ## Clear back to table context
5720                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5721                                    & TABLE_SCOPING_EL)) {
5722                ## Clear back to table body context                    !!!cp ('t219');
5723                ## nop by definition                    ## ISSUE: Can this state be reached?
5724                      pop @{$self->{open_elements}};
5725                pop @{$self->{open_elements}};                  }
5726                $self->{insertion_mode} = 'in table';                  
5727                ## reprocess                  !!!insert-element ('colgroup',, $token);
5728                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5729                    ## reprocess
5730                    !!!ack-later;
5731                    next B;
5732                  } elsif ({
5733                            caption => 1,
5734                            colgroup => 1,
5735                            tbody => 1, tfoot => 1, thead => 1,
5736                           }->{$token->{tag_name}}) {
5737                    ## Clear back to table context
5738                    while (not ($self->{open_elements}->[-1]->[1]
5739                                    & TABLE_SCOPING_EL)) {
5740                      !!!cp ('t220');
5741                      ## ISSUE: Can this state be reached?
5742                      pop @{$self->{open_elements}};
5743                    }
5744                    
5745                    push @$active_formatting_elements, ['#marker', '']
5746                        if $token->{tag_name} eq 'caption';
5747                    
5748                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5749                    $self->{insertion_mode} = {
5750                                               caption => IN_CAPTION_IM,
5751                                               colgroup => IN_COLUMN_GROUP_IM,
5752                                               tbody => IN_TABLE_BODY_IM,
5753                                               tfoot => IN_TABLE_BODY_IM,
5754                                               thead => IN_TABLE_BODY_IM,
5755                                              }->{$token->{tag_name}};
5756                    !!!next-token;
5757                    !!!nack ('t220.1');
5758                    next B;
5759                  } else {
5760                    die "$0: in table: <>: $token->{tag_name}";
5761                  }
5762              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5763                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5764                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5765                                      ->manakai_local_name,
5766                                  token => $token);
5767    
5768                ## As if </table>                ## As if </table>
5769                ## have a table element in table scope                ## have a table element in table scope
5770                my $i;                my $i;
5771                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5773                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5774                      !!!cp ('t221');
5775                    $i = $_;                    $i = $_;
5776                    last INSCOPE;                    last INSCOPE;
5777                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5778                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5779                    last INSCOPE;                    last INSCOPE;
5780                  }                  }
5781                } # INSCOPE                } # INSCOPE
5782                unless (defined $i) {                unless (defined $i) {
5783                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5784    ## TODO: The following is wrong, maybe.
5785                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5786                                    token => $token);
5787                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5788                    !!!nack ('t223.1');
5789                  !!!next-token;                  !!!next-token;
5790                  redo B;                  next B;
5791                }                }
5792                                
5793    ## TODO: Followings are removed from the latest spec.
5794                ## generate implied end tags                ## generate implied end tags
5795                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5796                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5797                     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;  
5798                }                }
5799    
5800                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5801                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5802                    ## NOTE: |<table><tr><table>|
5803                    !!!parse-error (type => 'not closed',
5804                                    text => $self->{open_elements}->[-1]->[0]
5805                                        ->manakai_local_name,
5806                                    token => $token);
5807                  } else {
5808                    !!!cp ('t226');
5809                }                }
5810    
5811                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5812                  pop @{$open_tables};
5813    
5814                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5815    
5816                ## reprocess            ## reprocess
5817                redo B;            !!!ack-later;
5818              } else {            next B;
5819                #          } elsif ($token->{tag_name} eq 'style') {
5820              }            if (not $open_tables->[-1]->[1]) { # tainted
5821            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5822              if ({              ## NOTE: This is a "as if in head" code clone.
5823                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5824                  }->{$token->{tag_name}}) {              next B;
5825                ## have an element in table scope            } else {
5826                my $i;              !!!cp ('t227.7');
5827                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5828                  my $node = $self->{open_elements}->[$_];            }
5829                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5830                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5831                    last INSCOPE;              !!!cp ('t227.6');
5832                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5833                            table => 1, html => 1,              $script_start_tag->();
5834                           }->{$node->[1]}) {              next B;
5835                    last INSCOPE;            } else {
5836                  }              !!!cp ('t227.5');
5837                } # INSCOPE              #
5838                unless (defined $i) {            }
5839                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5840                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5841                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5842                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5843                }                if ($type eq 'hidden') {
5844                    !!!cp ('t227.3');
5845                    !!!parse-error (type => 'in table',
5846                                    text => $token->{tag_name}, token => $token);
5847    
5848                ## 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}};  
               }  
5849    
5850                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;  
               }  
5851    
               ## 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]);  
5852                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
5853    
5854                ## As if <{current node}>                  !!!next-token;
5855                ## have an element in table scope                  !!!ack ('t227.2.1');
5856                ## true by definition                  next B;
5857                  } else {
5858                ## Clear back to table body context                  !!!cp ('t227.2');
5859                ## nop by definition                  #
5860                  }
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
5861              } else {              } else {
5862                  !!!cp ('t227.1');
5863                #                #
5864              }              }
5865            } else {            } else {
5866                !!!cp ('t227.4');
5867              #              #
5868            }            }
5869                      } else {
5870            ## As if in table            !!!cp ('t227');
5871            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5872            $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;  
               }  
             }  
5873    
5874              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table', text => $token->{tag_name},
5875                            token => $token);
5876    
5877              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5878              ## ISSUE: Spec says that "whenever a node would be inserted          #
5879              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5880              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5881              $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>  
5882                ## have an element in table scope                ## have an element in table scope
5883                my $i;                my $i;
5884                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5885                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5886                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5887                      !!!cp ('t228');
5888                    $i = $_;                    $i = $_;
5889                    last INSCOPE;                    last INSCOPE;
5890                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5891                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5892                    last INSCOPE;                    last INSCOPE;
5893                  }                  }
5894                } # INSCOPE                } # INSCOPE
5895                unless (defined $i) {                unless (defined $i) {
5896                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5897                    !!!parse-error (type => 'unmatched end tag',
5898                                    text => $token->{tag_name}, token => $token);
5899                  ## Ignore the token                  ## Ignore the token
5900                    !!!nack ('t230.1');
5901                  !!!next-token;                  !!!next-token;
5902                  redo B;                  next B;
5903                  } else {
5904                    !!!cp ('t232');
5905                }                }
5906    
5907                ## Clear back to table row context                ## Clear back to table row context
5908                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5909                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5910                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5911                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5912                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5913                }                }
5914    
5915                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5916                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5917                ## reprocess                !!!next-token;
5918                redo B;                !!!nack ('t231.1');
5919                  next B;
5920              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5921                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5922                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5923                    ## have an element in table scope
5924                ## As if </table>                  my $i;
5925                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5926                my $i;                    my $node = $self->{open_elements}->[$_];
5927                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5928                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5929                  if ($node->[1] eq 'table') {                      $i = $_;
5930                    $i = $_;                      last INSCOPE;
5931                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5932                  } elsif ({                      !!!cp ('t234');
5933                            table => 1, html => 1,                      last INSCOPE;
5934                           }->{$node->[1]}) {                    }
5935                    last INSCOPE;                  } # INSCOPE
5936                    unless (defined $i) {
5937                      !!!cp ('t235');
5938    ## TODO: The following is wrong.
5939                      !!!parse-error (type => 'unmatched end tag',
5940                                      text => $token->{type}, token => $token);
5941                      ## Ignore the token
5942                      !!!nack ('t236.1');
5943                      !!!next-token;
5944                      next B;
5945                  }                  }
5946                } # INSCOPE                  
5947                unless (defined $i) {                  ## Clear back to table row context
5948                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5949                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5950                  !!!next-token;                    !!!cp ('t236');
5951                  redo B;  ## ISSUE: Can this state be reached?
5952                }                    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;  
5953                  }                  }
5954                } # INSCOPE                  
5955                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5956                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5957                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5958                  !!!next-token;                }
5959                  redo B;  
5960                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5961                    ## have an element in table scope
5962                ## Clear back to table row context                  my $i;
5963                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5964                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5965                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5966                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5967                        $i = $_;
5968                        last INSCOPE;
5969                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5970                        !!!cp ('t238');
5971                        last INSCOPE;
5972                      }
5973                    } # INSCOPE
5974                    unless (defined $i) {
5975                      !!!cp ('t239');
5976                      !!!parse-error (type => 'unmatched end tag',
5977                                      text => $token->{tag_name}, token => $token);
5978                      ## Ignore the token
5979                      !!!nack ('t239.1');
5980                      !!!next-token;
5981                      next B;
5982                    }
5983                    
5984                    ## Clear back to table body context
5985                    while (not ($self->{open_elements}->[-1]->[1]
5986                                    & TABLE_ROWS_SCOPING_EL)) {
5987                      !!!cp ('t240');
5988                      pop @{$self->{open_elements}};
5989                    }
5990                    
5991                    ## As if <{current node}>
5992                    ## have an element in table scope
5993                    ## true by definition
5994                    
5995                    ## Clear back to table body context
5996                    ## nop by definition
5997                    
5998                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5999                    $self->{insertion_mode} = IN_TABLE_IM;
6000                    ## reprocess in the "in table" insertion mode...
6001                }                }
6002    
6003                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
6004                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
6005                !!!next-token;                ## the code for <table> in the "in table" insertion mode
6006                redo B;                ## is synced with it.
6007              } elsif ($token->{tag_name} eq 'table') {  
6008                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
6009                my $i;                my $i;
6010                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6011                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6012                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
6013                      !!!cp ('t241');
6014                    $i = $_;                    $i = $_;
6015                    last INSCOPE;                    last INSCOPE;
6016                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6017                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
6018                    last INSCOPE;                    last INSCOPE;
6019                  }                  }
6020                } # INSCOPE                } # INSCOPE
6021                unless (defined $i) {                unless (defined $i) {
6022                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
6023                    !!!parse-error (type => 'unmatched end tag',
6024                                    text => $token->{tag_name}, token => $token);
6025                  ## Ignore the token                  ## Ignore the token
6026                    !!!nack ('t243.1');
6027                  !!!next-token;                  !!!next-token;
6028                  redo B;                  next B;
6029                }                }
6030                    
6031                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
6032                while (not {                pop @{$open_tables};
6033                  tr => 1, html => 1,                
6034                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
6035                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
6036                  pop @{$self->{open_elements}};                !!!next-token;
6037                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
6038              } elsif ({              } elsif ({
6039                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6040                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
6041                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
6042                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
6043                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
6044                  my $node = $self->{open_elements}->[$_];                  my $i;
6045                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6046                    $i = $_;                    my $node = $self->{open_elements}->[$_];
6047                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6048                  } elsif ({                      !!!cp ('t247');
6049                            table => 1, html => 1,                      $i = $_;
6050                           }->{$node->[1]}) {                      last INSCOPE;
6051                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6052                        !!!cp ('t248');
6053                        last INSCOPE;
6054                      }
6055                    } # INSCOPE
6056                      unless (defined $i) {
6057                        !!!cp ('t249');
6058                        !!!parse-error (type => 'unmatched end tag',
6059                                        text => $token->{tag_name}, token => $token);
6060                        ## Ignore the token
6061                        !!!nack ('t249.1');
6062                        !!!next-token;
6063                        next B;
6064                      }
6065                    
6066                    ## As if </tr>
6067                    ## have an element in table scope
6068                    my $i;
6069                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6070                      my $node = $self->{open_elements}->[$_];
6071                      if ($node->[1] & TABLE_ROW_EL) {
6072                        !!!cp ('t250');
6073                        $i = $_;
6074                        last INSCOPE;
6075                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
6076                        !!!cp ('t251');
6077                        last INSCOPE;
6078                      }
6079                    } # INSCOPE
6080                      unless (defined $i) {
6081                        !!!cp ('t252');
6082                        !!!parse-error (type => 'unmatched end tag',
6083                                        text => 'tr', token => $token);
6084                        ## Ignore the token
6085                        !!!nack ('t252.1');
6086                        !!!next-token;
6087                        next B;
6088                      }
6089                    
6090                    ## Clear back to table row context
6091                    while (not ($self->{open_elements}->[-1]->[1]
6092                                    & TABLE_ROW_SCOPING_EL)) {
6093                      !!!cp ('t253');
6094    ## ISSUE: Can this case be reached?
6095                      pop @{$self->{open_elements}};
6096                  }                  }
6097                } # INSCOPE                  
6098                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
6099                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
6100                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
6101                }                }
6102    
               ## As if </tr>  
6103                ## have an element in table scope                ## have an element in table scope
6104                my $i;                my $i;
6105                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6106                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6107                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6108                      !!!cp ('t254');
6109                    $i = $_;                    $i = $_;
6110                    last INSCOPE;                    last INSCOPE;
6111                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6112                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6113                    last INSCOPE;                    last INSCOPE;
6114                  }                  }
6115                } # INSCOPE                } # INSCOPE
6116                unless (defined $i) {                unless (defined $i) {
6117                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
6118                    !!!parse-error (type => 'unmatched end tag',
6119                                    text => $token->{tag_name}, token => $token);
6120                  ## Ignore the token                  ## Ignore the token
6121                    !!!nack ('t256.1');
6122                  !!!next-token;                  !!!next-token;
6123                  redo B;                  next B;
6124                }                }
6125    
6126                ## Clear back to table row context                ## Clear back to table body context
6127                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6128                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6129                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6130                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6131                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6132                }                }
6133    
6134                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
6135                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
6136                ## reprocess                !!!nack ('t257.1');
6137                redo B;                !!!next-token;
6138                  next B;
6139              } elsif ({              } elsif ({
6140                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6141                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6142                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6143                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6144                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6145                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6146                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6147                !!!next-token;                            text => $token->{tag_name}, token => $token);
6148                redo B;            ## Ignore the token
6149              } else {            !!!nack ('t258.1');
6150                #             !!!next-token;
6151              }            next B;
6152            } else {          } else {
6153              #            !!!cp ('t259');
6154            }            !!!parse-error (type => 'in table:/',
6155                              text => $token->{tag_name}, token => $token);
6156    
6157            ## As if in table            $insert = $insert_to_foster;
6158            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
6159            $in_body->($insert_to_foster);          }
6160            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6161          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6162            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
6163              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
6164              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
6165                          #
6166              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6167              !!!cp ('t259.2');
6168              #
6169            }
6170    
6171              !!!next-token;          ## Stop parsing
6172              redo B;          last B;
6173            } elsif ($token->{type} eq 'comment') {        } else {
6174              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
6175              my $comment = $self->{document}->create_comment ($token->{data});        }
6176              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6177              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
6178              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6179            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6180              if ({                unless (length $token->{data}) {
6181                   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  
6182                  !!!next-token;                  !!!next-token;
6183                  redo B;                  next B;
6184                }                }
6185                }
6186                ## Close the cell              
6187                !!!back-token; # <?>              !!!cp ('t261');
6188                $token = {type => 'end tag', tag_name => $tn};              #
6189                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
6190              } else {              if ($token->{tag_name} eq 'col') {
6191                  !!!cp ('t262');
6192                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6193                  pop @{$self->{open_elements}};
6194                  !!!ack ('t262.1');
6195                  !!!next-token;
6196                  next B;
6197                } else {
6198                  !!!cp ('t263');
6199                #                #
6200              }              }
6201            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
6202              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
6203                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6204                my $i;                  !!!cp ('t264');
6205                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
6206                  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});  
6207                  ## Ignore the token                  ## Ignore the token
6208                  !!!next-token;                  !!!next-token;
6209                  redo B;                  next B;
6210                }                } else {
6211                                  !!!cp ('t265');
6212                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
6213                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
6214                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
6215                     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]);  
6216                }                }
6217                } elsif ($token->{tag_name} eq 'col') {
6218                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
6219                  !!!parse-error (type => 'unmatched end tag',
6220                $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});  
6221                ## Ignore the token                ## Ignore the token
6222                !!!next-token;                !!!next-token;
6223                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;  
6224              } else {              } else {
6225                #                !!!cp ('t267');
6226                  #
6227              }              }
6228          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6229            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6230                @{$self->{open_elements}} == 1) { # redundant, maybe
6231              !!!cp ('t270.2');
6232              ## Stop parsing.
6233              last B;
6234            } else {
6235              ## NOTE: As if </colgroup>.
6236              !!!cp ('t270.1');
6237              pop @{$self->{open_elements}}; # colgroup
6238              $self->{insertion_mode} = IN_TABLE_IM;
6239              ## Reprocess.
6240              next B;
6241            }
6242          } else {
6243            die "$0: $token->{type}: Unknown token type";
6244          }
6245    
6246              ## As if </colgroup>
6247              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6248                !!!cp ('t269');
6249    ## TODO: Wrong error type?
6250                !!!parse-error (type => 'unmatched end tag',
6251                                text => 'colgroup', token => $token);
6252                ## Ignore the token
6253                !!!nack ('t269.1');
6254                !!!next-token;
6255                next B;
6256            } else {            } else {
6257              #              !!!cp ('t270');
6258                pop @{$self->{open_elements}}; # colgroup
6259                $self->{insertion_mode} = IN_TABLE_IM;
6260                !!!ack-later;
6261                ## reprocess
6262                next B;
6263              }
6264        } elsif ($self->{insertion_mode} & SELECT_IMS) {
6265          if ($token->{type} == CHARACTER_TOKEN) {
6266            !!!cp ('t271');
6267            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6268            !!!next-token;
6269            next B;
6270          } elsif ($token->{type} == START_TAG_TOKEN) {
6271            if ($token->{tag_name} eq 'option') {
6272              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6273                !!!cp ('t272');
6274                ## As if </option>
6275                pop @{$self->{open_elements}};
6276              } else {
6277                !!!cp ('t273');
6278            }            }
             
           $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}};  
               }  
6279    
6280                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6281                !!!next-token;            !!!nack ('t273.1');
6282                redo B;            !!!next-token;
6283              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6284                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6285                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6286                  pop @{$self->{open_elements}};              !!!cp ('t274');
6287                }              ## As if </option>
6288                pop @{$self->{open_elements}};
6289              } else {
6290                !!!cp ('t275');
6291              }
6292    
6293                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6294                  ## As if </optgroup>              !!!cp ('t276');
6295                  pop @{$self->{open_elements}};              ## As if </optgroup>
6296                }              pop @{$self->{open_elements}};
6297              } else {
6298                !!!cp ('t277');
6299              }
6300    
6301                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6302                !!!next-token;            !!!nack ('t277.1');
6303                redo B;            !!!next-token;
6304              } elsif ($token->{tag_name} eq 'select') {            next B;
6305                !!!parse-error (type => 'not closed:select');          } elsif ({
6306                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6307                ## have an element in table scope                   }->{$token->{tag_name}} or
6308                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6309                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6310                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6311                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6312                    $i = $_;                     tr => 1, td => 1, th => 1,
6313                    last INSCOPE;                    }->{$token->{tag_name}})) {
6314                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6315                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6316                           }->{$node->[1]}) {                            token => $token);
6317                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6318                  }            ## as if there were </select> (otherwise).
6319                } # INSCOPE            ## have an element in table scope
6320                unless (defined $i) {            my $i;
6321                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6322                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6323                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6324                  redo B;                !!!cp ('t278');
6325                }                $i = $_;
6326                  last INSCOPE;
6327                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6328                  !!!cp ('t279');
6329                  last INSCOPE;
6330                }
6331              } # INSCOPE
6332              unless (defined $i) {
6333                !!!cp ('t280');
6334                !!!parse-error (type => 'unmatched end tag',
6335                                text => 'select', token => $token);
6336                ## Ignore the token
6337                !!!nack ('t280.1');
6338                !!!next-token;
6339                next B;
6340              }
6341                                
6342                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6343              splice @{$self->{open_elements}}, $i;
6344    
6345                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6346    
6347                !!!next-token;            if ($token->{tag_name} eq 'select') {
6348                redo B;              !!!nack ('t281.2');
6349              } else {              !!!next-token;
6350                #              next B;
6351              } else {
6352                !!!cp ('t281.1');
6353                !!!ack-later;
6354                ## Reprocess the token.
6355                next B;
6356              }
6357            } else {
6358              !!!cp ('t282');
6359              !!!parse-error (type => 'in select',
6360                              text => $token->{tag_name}, token => $token);
6361              ## Ignore the token
6362              !!!nack ('t282.1');
6363              !!!next-token;
6364              next B;
6365            }
6366          } elsif ($token->{type} == END_TAG_TOKEN) {
6367            if ($token->{tag_name} eq 'optgroup') {
6368              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6369                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6370                !!!cp ('t283');
6371                ## As if </option>
6372                splice @{$self->{open_elements}}, -2;
6373              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6374                !!!cp ('t284');
6375                pop @{$self->{open_elements}};
6376              } else {
6377                !!!cp ('t285');
6378                !!!parse-error (type => 'unmatched end tag',
6379                                text => $token->{tag_name}, token => $token);
6380                ## Ignore the token
6381              }
6382              !!!nack ('t285.1');
6383              !!!next-token;
6384              next B;
6385            } elsif ($token->{tag_name} eq 'option') {
6386              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6387                !!!cp ('t286');
6388                pop @{$self->{open_elements}};
6389              } else {
6390                !!!cp ('t287');
6391                !!!parse-error (type => 'unmatched end tag',
6392                                text => $token->{tag_name}, token => $token);
6393                ## Ignore the token
6394              }
6395              !!!nack ('t287.1');
6396              !!!next-token;
6397              next B;
6398            } elsif ($token->{tag_name} eq 'select') {
6399              ## have an element in table scope
6400              my $i;
6401              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6402                my $node = $self->{open_elements}->[$_];
6403                if ($node->[1] & SELECT_EL) {
6404                  !!!cp ('t288');
6405                  $i = $_;
6406                  last INSCOPE;
6407                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6408                  !!!cp ('t289');
6409                  last INSCOPE;
6410              }              }
6411            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6412              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6413                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6414                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6415                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6416                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6417                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6418                  pop @{$self->{open_elements}};              !!!next-token;
6419                } else {              next B;
6420                  !!!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;  
               }  
6421                                
6422                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6423              splice @{$self->{open_elements}}, $i;
6424    
6425                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6426    
6427                !!!next-token;            !!!nack ('t291.1');
6428                redo B;            !!!next-token;
6429              } elsif ({            next B;
6430                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6431                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6432                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6433                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6434                                   }->{$token->{tag_name}}) {
6435                ## have an element in table scope  ## TODO: The following is wrong?
6436                my $i;            !!!parse-error (type => 'unmatched end tag',
6437                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;  
               }  
6438                                
6439                ## As if </select>            ## have an element in table scope
6440                ## have an element in table scope            my $i;
6441                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6442                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6443                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6444                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6445                    $i = $_;                $i = $_;
6446                    last INSCOPE;                last INSCOPE;
6447                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6448                            table => 1, html => 1,                !!!cp ('t293');
6449                           }->{$node->[1]}) {                last INSCOPE;
6450                    last INSCOPE;              }
6451                  }            } # INSCOPE
6452                } # INSCOPE            unless (defined $i) {
6453                unless (defined $i) {              !!!cp ('t294');
6454                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6455                  ## Ignore the </select> token              !!!nack ('t294.1');
6456                  !!!next-token; ## TODO: ok?              !!!next-token;
6457                  redo B;              next B;
6458                }            }
6459                                
6460                splice @{$self->{open_elements}}, $i;            ## As if </select>
6461              ## have an element in table scope
6462                $self->_reset_insertion_mode;            undef $i;
6463              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6464                ## reprocess              my $node = $self->{open_elements}->[$_];
6465                redo B;              if ($node->[1] & SELECT_EL) {
6466              } else {                !!!cp ('t295');
6467                #                $i = $_;
6468                  last INSCOPE;
6469                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6470    ## ISSUE: Can this state be reached?
6471                  !!!cp ('t296');
6472                  last INSCOPE;
6473              }              }
6474            } else {            } # INSCOPE
6475              #            unless (defined $i) {
6476                !!!cp ('t297');
6477    ## TODO: The following error type is correct?
6478                !!!parse-error (type => 'unmatched end tag',
6479                                text => 'select', token => $token);
6480                ## Ignore the </select> token
6481                !!!nack ('t297.1');
6482                !!!next-token; ## TODO: ok?
6483                next B;
6484            }            }
6485                  
6486              !!!cp ('t298');
6487              splice @{$self->{open_elements}}, $i;
6488    
6489            !!!parse-error (type => 'in select:'.$token->{tag_name});            $self->_reset_insertion_mode;
6490    
6491              !!!ack-later;
6492              ## reprocess
6493              next B;
6494            } else {
6495              !!!cp ('t299');
6496              !!!parse-error (type => 'in select:/',
6497                              text => $token->{tag_name}, token => $token);
6498            ## Ignore the token            ## Ignore the token
6499              !!!nack ('t299.3');
6500            !!!next-token;            !!!next-token;
6501            redo B;            next B;
6502          } elsif ($self->{insertion_mode} eq 'after body') {          }
6503            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6504              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6505                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6506                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6507                            !!!parse-error (type => 'in body:#eof', token => $token);
6508                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6509              !!!cp ('t299.2');
6510            }
6511    
6512                unless (length $token->{data}) {          ## Stop parsing.
6513                  !!!next-token;          last B;
6514                  redo B;        } else {
6515                }          die "$0: $token->{type}: Unknown token type";
6516              }        }
6517                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6518              #        if ($token->{type} == CHARACTER_TOKEN) {
6519              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6520            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6521              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6522              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6523                  
6524              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6525              
6526              unless (length $token->{data}) {
6527                !!!cp ('t300');
6528              !!!next-token;              !!!next-token;
6529              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});  
6530            }            }
6531            }
6532            
6533            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6534              !!!cp ('t301');
6535              !!!parse-error (type => 'after html:#text', token => $token);
6536    
6537            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6538            ## reprocess          } else {
6539            redo B;            !!!cp ('t302');
6540          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6541            if ($token->{type} eq 'character') {          
6542              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6543                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6544    
6545                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6546                  !!!next-token;          ## reprocess
6547                  redo B;          next B;
6548                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6549              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6550              !!!cp ('t303');
6551              !!!parse-error (type => 'after html',
6552                              text => $token->{tag_name}, token => $token);
6553              
6554              ## Reprocess in the "after body" insertion mode.
6555            } else {
6556              !!!cp ('t304');
6557            }
6558    
6559              #          ## "after body" insertion mode
6560            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6561              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6562              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6563            $self->{insertion_mode} = IN_BODY_IM;
6564            !!!ack-later;
6565            ## reprocess
6566            next B;
6567          } elsif ($token->{type} == END_TAG_TOKEN) {
6568            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6569              !!!cp ('t305');
6570              !!!parse-error (type => 'after html:/',
6571                              text => $token->{tag_name}, token => $token);
6572              
6573              $self->{insertion_mode} = AFTER_BODY_IM;
6574              ## Reprocess in the "after body" insertion mode.
6575            } else {
6576              !!!cp ('t306');
6577            }
6578    
6579            ## "after body" insertion mode
6580            if ($token->{tag_name} eq 'html') {
6581              if (defined $self->{inner_html_node}) {
6582                !!!cp ('t307');
6583                !!!parse-error (type => 'unmatched end tag',
6584                                text => 'html', token => $token);
6585                ## Ignore the token
6586              !!!next-token;              !!!next-token;
6587              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 {  
               #  
             }  
6588            } else {            } else {
6589              #              !!!cp ('t308');
6590                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6591                !!!next-token;
6592                next B;
6593              }
6594            } else {
6595              !!!cp ('t309');
6596              !!!parse-error (type => 'after body:/',
6597                              text => $token->{tag_name}, token => $token);
6598    
6599              $self->{insertion_mode} = IN_BODY_IM;
6600              ## reprocess
6601              next B;
6602            }
6603          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6604            !!!cp ('t309.2');
6605            ## Stop parsing
6606            last B;
6607          } else {
6608            die "$0: $token->{type}: Unknown token type";
6609          }
6610        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6611          if ($token->{type} == CHARACTER_TOKEN) {
6612            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6613              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6614              
6615              unless (length $token->{data}) {
6616                !!!cp ('t310');
6617                !!!next-token;
6618                next B;
6619              }
6620            }
6621            
6622            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6623              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6624                !!!cp ('t311');
6625                !!!parse-error (type => 'in frameset:#text', token => $token);
6626              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6627                !!!cp ('t312');
6628                !!!parse-error (type => 'after frameset:#text', token => $token);
6629              } else { # "after after frameset"
6630                !!!cp ('t313');
6631                !!!parse-error (type => 'after html:#text', token => $token);
6632            }            }
6633                        
6634            if (defined $token->{tag_name}) {            ## Ignore the token.
6635              !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if (length $token->{data}) {
6636                !!!cp ('t314');
6637                ## reprocess the rest of characters
6638            } else {            } else {
6639              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
6640                !!!next-token;
6641              }
6642              next B;
6643            }
6644            
6645            die qq[$0: Character "$token->{data}"];
6646          } elsif ($token->{type} == START_TAG_TOKEN) {
6647            if ($token->{tag_name} eq 'frameset' and
6648                $self->{insertion_mode} == IN_FRAMESET_IM) {
6649              !!!cp ('t318');
6650              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6651              !!!nack ('t318.1');
6652              !!!next-token;
6653              next B;
6654            } elsif ($token->{tag_name} eq 'frame' and
6655                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6656              !!!cp ('t319');
6657              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6658              pop @{$self->{open_elements}};
6659              !!!ack ('t319.1');
6660              !!!next-token;
6661              next B;
6662            } elsif ($token->{tag_name} eq 'noframes') {
6663              !!!cp ('t320');
6664              ## NOTE: As if in head.
6665              $parse_rcdata->(CDATA_CONTENT_MODEL);
6666              next B;
6667    
6668              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6669              ## has no parse error.
6670            } else {
6671              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6672                !!!cp ('t321');
6673                !!!parse-error (type => 'in frameset',
6674                                text => $token->{tag_name}, token => $token);
6675              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6676                !!!cp ('t322');
6677                !!!parse-error (type => 'after frameset',
6678                                text => $token->{tag_name}, token => $token);
6679              } else { # "after after frameset"
6680                !!!cp ('t322.2');
6681                !!!parse-error (type => 'after after frameset',
6682                                text => $token->{tag_name}, token => $token);
6683            }            }
6684            ## Ignore the token            ## Ignore the token
6685              !!!nack ('t322.1');
6686            !!!next-token;            !!!next-token;
6687            redo B;            next B;
6688          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6689            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6690              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'frameset' and
6691                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{insertion_mode} == IN_FRAMESET_IM) {
6692              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6693                  @{$self->{open_elements}} == 1) {
6694                !!!cp ('t325');
6695                !!!parse-error (type => 'unmatched end tag',
6696                                text => $token->{tag_name}, token => $token);
6697                ## Ignore the token
6698                !!!next-token;
6699              } else {
6700                !!!cp ('t326');
6701                pop @{$self->{open_elements}};
6702                !!!next-token;
6703              }
6704    
6705                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
6706                  !!!next-token;                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6707                  redo B;              !!!cp ('t327');
6708                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6709              } else {
6710                !!!cp ('t328');
6711              }
6712              next B;
6713            } elsif ($token->{tag_name} eq 'html' and
6714                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6715              !!!cp ('t329');
6716              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6717              !!!next-token;
6718              next B;
6719            } else {
6720              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6721                !!!cp ('t330');
6722                !!!parse-error (type => 'in frameset:/',
6723                                text => $token->{tag_name}, token => $token);
6724              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6725                !!!cp ('t330.1');
6726                !!!parse-error (type => 'after frameset:/',
6727                                text => $token->{tag_name}, token => $token);
6728              } else { # "after after html"
6729                !!!cp ('t331');
6730                !!!parse-error (type => 'after after frameset:/',
6731                                text => $token->{tag_name}, token => $token);
6732              }
6733              ## Ignore the token
6734              !!!next-token;
6735              next B;
6736            }
6737          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6738            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6739                    @{$self->{open_elements}} == 1) { # redundant, maybe
6740              !!!cp ('t331.1');
6741              !!!parse-error (type => 'in body:#eof', token => $token);
6742            } else {
6743              !!!cp ('t331.2');
6744            }
6745            
6746            ## Stop parsing
6747            last B;
6748          } else {
6749            die "$0: $token->{type}: Unknown token type";
6750          }
6751    
6752          ## ISSUE: An issue in spec here
6753        } else {
6754          die "$0: $self->{insertion_mode}: Unknown insertion mode";
6755        }
6756    
6757        ## "in body" insertion mode
6758        if ($token->{type} == START_TAG_TOKEN) {
6759          if ($token->{tag_name} eq 'script') {
6760            !!!cp ('t332');
6761            ## NOTE: This is an "as if in head" code clone
6762            $script_start_tag->();
6763            next B;
6764          } elsif ($token->{tag_name} eq 'style') {
6765            !!!cp ('t333');
6766            ## NOTE: This is an "as if in head" code clone
6767            $parse_rcdata->(CDATA_CONTENT_MODEL);
6768            next B;
6769          } elsif ({
6770                    base => 1, link => 1,
6771                   }->{$token->{tag_name}}) {
6772            !!!cp ('t334');
6773            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6774            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6775            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6776            !!!ack ('t334.1');
6777            !!!next-token;
6778            next B;
6779          } elsif ($token->{tag_name} eq 'meta') {
6780            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6781            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6782            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6783    
6784            unless ($self->{confident}) {
6785              if ($token->{attributes}->{charset}) {
6786                !!!cp ('t335');
6787                ## NOTE: Whether the encoding is supported or not is handled
6788                ## in the {change_encoding} callback.
6789                $self->{change_encoding}
6790                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6791                
6792                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6793                    ->set_user_data (manakai_has_reference =>
6794                                         $token->{attributes}->{charset}
6795                                             ->{has_reference});
6796              } elsif ($token->{attributes}->{content}) {
6797                if ($token->{attributes}->{content}->{value}
6798                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6799                        [\x09-\x0D\x20]*=
6800                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6801                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6802                  !!!cp ('t336');
6803                  ## NOTE: Whether the encoding is supported or not is handled
6804                  ## in the {change_encoding} callback.
6805                  $self->{change_encoding}
6806                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6807                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6808                      ->set_user_data (manakai_has_reference =>
6809                                           $token->{attributes}->{content}
6810                                                 ->{has_reference});
6811              }              }
6812              }
6813            } else {
6814              if ($token->{attributes}->{charset}) {
6815                !!!cp ('t337');
6816                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6817                    ->set_user_data (manakai_has_reference =>
6818                                         $token->{attributes}->{charset}
6819                                             ->{has_reference});
6820              }
6821              if ($token->{attributes}->{content}) {
6822                !!!cp ('t338');
6823                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6824                    ->set_user_data (manakai_has_reference =>
6825                                         $token->{attributes}->{content}
6826                                             ->{has_reference});
6827              }
6828            }
6829    
6830              #          !!!ack ('t338.1');
6831            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6832              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6833              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6834              !!!next-token;          !!!cp ('t341');
6835              redo B;          ## NOTE: This is an "as if in head" code clone
6836            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6837              if ($token->{tag_name} eq 'noframes') {          next B;
6838                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6839                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6840              } else {                
6841                #          if (@{$self->{open_elements}} == 1 or
6842                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6843              !!!cp ('t342');
6844              ## Ignore the token
6845            } else {
6846              my $body_el = $self->{open_elements}->[1]->[0];
6847              for my $attr_name (keys %{$token->{attributes}}) {
6848                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6849                  !!!cp ('t343');
6850                  $body_el->set_attribute_ns
6851                    (undef, [undef, $attr_name],
6852                     $token->{attributes}->{$attr_name}->{value});
6853              }              }
6854            } elsif ($token->{type} eq 'end tag') {            }
6855              if ($token->{tag_name} eq 'html') {          }
6856                $phase = 'trailing end';          !!!nack ('t343.1');
6857            !!!next-token;
6858            next B;
6859          } elsif ({
6860                    address => 1, blockquote => 1, center => 1, dir => 1,
6861                    div => 1, dl => 1, fieldset => 1,
6862                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6863                    menu => 1, ol => 1, p => 1, ul => 1,
6864                    pre => 1, listing => 1,
6865                    form => 1,
6866                    table => 1,
6867                    hr => 1,
6868                   }->{$token->{tag_name}}) {
6869            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6870              !!!cp ('t350');
6871              !!!parse-error (type => 'in form:form', token => $token);
6872              ## Ignore the token
6873              !!!nack ('t350.1');
6874              !!!next-token;
6875              next B;
6876            }
6877    
6878            ## has a p element in scope
6879            INSCOPE: for (reverse @{$self->{open_elements}}) {
6880              if ($_->[1] & P_EL) {
6881                !!!cp ('t344');
6882                !!!back-token; # <form>
6883                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6884                          line => $token->{line}, column => $token->{column}};
6885                next B;
6886              } elsif ($_->[1] & SCOPING_EL) {
6887                !!!cp ('t345');
6888                last INSCOPE;
6889              }
6890            } # INSCOPE
6891              
6892            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6893            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6894              !!!nack ('t346.1');
6895              !!!next-token;
6896              if ($token->{type} == CHARACTER_TOKEN) {
6897                $token->{data} =~ s/^\x0A//;
6898                unless (length $token->{data}) {
6899                  !!!cp ('t346');
6900                !!!next-token;                !!!next-token;
               redo B;  
6901              } else {              } else {
6902                #                !!!cp ('t349');
6903              }              }
6904            } else {            } else {
6905              #              !!!cp ('t348');
6906              }
6907            } elsif ($token->{tag_name} eq 'form') {
6908              !!!cp ('t347.1');
6909              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6910    
6911              !!!nack ('t347.2');
6912              !!!next-token;
6913            } elsif ($token->{tag_name} eq 'table') {
6914              !!!cp ('t382');
6915              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6916              
6917              $self->{insertion_mode} = IN_TABLE_IM;
6918    
6919              !!!nack ('t382.1');
6920              !!!next-token;
6921            } elsif ($token->{tag_name} eq 'hr') {
6922              !!!cp ('t386');
6923              pop @{$self->{open_elements}};
6924            
6925              !!!nack ('t386.1');
6926              !!!next-token;
6927            } else {
6928              !!!nack ('t347.1');
6929              !!!next-token;
6930            }
6931            next B;
6932          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6933            ## has a p element in scope
6934            INSCOPE: for (reverse @{$self->{open_elements}}) {
6935              if ($_->[1] & P_EL) {
6936                !!!cp ('t353');
6937                !!!back-token; # <x>
6938                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6939                          line => $token->{line}, column => $token->{column}};
6940                next B;
6941              } elsif ($_->[1] & SCOPING_EL) {
6942                !!!cp ('t354');
6943                last INSCOPE;
6944            }            }
6945            } # INSCOPE
6946                        
6947            if (defined $token->{tag_name}) {          ## Step 1
6948              !!!parse-error (type => 'after frameset:'.$token->{tag_name});          my $i = -1;
6949            my $node = $self->{open_elements}->[$i];
6950            my $li_or_dtdd = {li => {li => 1},
6951                              dt => {dt => 1, dd => 1},
6952                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6953            LI: {
6954              ## Step 2
6955              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6956                if ($i != -1) {
6957                  !!!cp ('t355');
6958                  !!!parse-error (type => 'not closed',
6959                                  text => $self->{open_elements}->[-1]->[0]
6960                                      ->manakai_local_name,
6961                                  token => $token);
6962                } else {
6963                  !!!cp ('t356');
6964                }
6965                splice @{$self->{open_elements}}, $i;
6966                last LI;
6967            } else {            } else {
6968              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6969            }            }
6970              
6971              ## Step 3
6972              if (not ($node->[1] & FORMATTING_EL) and
6973                  #not $phrasing_category->{$node->[1]} and
6974                  ($node->[1] & SPECIAL_EL or
6975                   $node->[1] & SCOPING_EL) and
6976                  not ($node->[1] & ADDRESS_EL) and
6977                  not ($node->[1] & DIV_EL)) {
6978                !!!cp ('t358');
6979                last LI;
6980              }
6981              
6982              !!!cp ('t359');
6983              ## Step 4
6984              $i--;
6985              $node = $self->{open_elements}->[$i];
6986              redo LI;
6987            } # LI
6988              
6989            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6990            !!!nack ('t359.1');
6991            !!!next-token;
6992            next B;
6993          } elsif ($token->{tag_name} eq 'plaintext') {
6994            ## has a p element in scope
6995            INSCOPE: for (reverse @{$self->{open_elements}}) {
6996              if ($_->[1] & P_EL) {
6997                !!!cp ('t367');
6998                !!!back-token; # <plaintext>
6999                $token = {type => END_TAG_TOKEN, tag_name => 'p',
7000                          line => $token->{line}, column => $token->{column}};
7001                next B;
7002              } elsif ($_->[1] & SCOPING_EL) {
7003                !!!cp ('t368');
7004                last INSCOPE;
7005              }
7006            } # INSCOPE
7007              
7008            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7009              
7010            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7011              
7012            !!!nack ('t368.1');
7013            !!!next-token;
7014            next B;
7015          } elsif ($token->{tag_name} eq 'a') {
7016            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7017              my $node = $active_formatting_elements->[$i];
7018              if ($node->[1] & A_EL) {
7019                !!!cp ('t371');
7020                !!!parse-error (type => 'in a:a', token => $token);
7021                
7022                !!!back-token; # <a>
7023                $token = {type => END_TAG_TOKEN, tag_name => 'a',
7024                          line => $token->{line}, column => $token->{column}};
7025                $formatting_end_tag->($token);
7026                
7027                AFE2: for (reverse 0..$#$active_formatting_elements) {
7028                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7029                    !!!cp ('t372');
7030                    splice @$active_formatting_elements, $_, 1;
7031                    last AFE2;
7032                  }
7033                } # AFE2
7034                OE: for (reverse 0..$#{$self->{open_elements}}) {
7035                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7036                    !!!cp ('t373');
7037                    splice @{$self->{open_elements}}, $_, 1;
7038                    last OE;
7039                  }
7040                } # OE
7041                last AFE;
7042              } elsif ($node->[0] eq '#marker') {
7043                !!!cp ('t374');
7044                last AFE;
7045              }
7046            } # AFE
7047              
7048            $reconstruct_active_formatting_elements->($insert_to_current);
7049    
7050            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7051            push @$active_formatting_elements, $self->{open_elements}->[-1];
7052    
7053            !!!nack ('t374.1');
7054            !!!next-token;
7055            next B;
7056          } elsif ($token->{tag_name} eq 'nobr') {
7057            $reconstruct_active_formatting_elements->($insert_to_current);
7058    
7059            ## has a |nobr| element in scope
7060            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7061              my $node = $self->{open_elements}->[$_];
7062              if ($node->[1] & NOBR_EL) {
7063                !!!cp ('t376');
7064                !!!parse-error (type => 'in nobr:nobr', token => $token);
7065                !!!back-token; # <nobr>
7066                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7067                          line => $token->{line}, column => $token->{column}};
7068                next B;
7069              } elsif ($node->[1] & SCOPING_EL) {
7070                !!!cp ('t377');
7071                last INSCOPE;
7072              }
7073            } # INSCOPE
7074            
7075            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7076            push @$active_formatting_elements, $self->{open_elements}->[-1];
7077            
7078            !!!nack ('t377.1');
7079            !!!next-token;
7080            next B;
7081          } elsif ($token->{tag_name} eq 'button') {
7082            ## has a button element in scope
7083            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7084              my $node = $self->{open_elements}->[$_];
7085              if ($node->[1] & BUTTON_EL) {
7086                !!!cp ('t378');
7087                !!!parse-error (type => 'in button:button', token => $token);
7088                !!!back-token; # <button>
7089                $token = {type => END_TAG_TOKEN, tag_name => 'button',
7090                          line => $token->{line}, column => $token->{column}};
7091                next B;
7092              } elsif ($node->[1] & SCOPING_EL) {
7093                !!!cp ('t379');
7094                last INSCOPE;
7095              }
7096            } # INSCOPE
7097              
7098            $reconstruct_active_formatting_elements->($insert_to_current);
7099              
7100            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7101    
7102            ## TODO: associate with $self->{form_element} if defined
7103    
7104            push @$active_formatting_elements, ['#marker', ''];
7105    
7106            !!!nack ('t379.1');
7107            !!!next-token;
7108            next B;
7109          } elsif ({
7110                    xmp => 1,
7111                    iframe => 1,
7112                    noembed => 1,
7113                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7114                    noscript => 0, ## TODO: 1 if scripting is enabled
7115                   }->{$token->{tag_name}}) {
7116            if ($token->{tag_name} eq 'xmp') {
7117              !!!cp ('t381');
7118              $reconstruct_active_formatting_elements->($insert_to_current);
7119            } else {
7120              !!!cp ('t399');
7121            }
7122            ## NOTE: There is an "as if in body" code clone.
7123            $parse_rcdata->(CDATA_CONTENT_MODEL);
7124            next B;
7125          } elsif ($token->{tag_name} eq 'isindex') {
7126            !!!parse-error (type => 'isindex', token => $token);
7127            
7128            if (defined $self->{form_element}) {
7129              !!!cp ('t389');
7130            ## Ignore the token            ## Ignore the token
7131              !!!nack ('t389'); ## NOTE: Not acknowledged.
7132            !!!next-token;            !!!next-token;
7133            redo B;            next B;
7134            } else {
7135              !!!ack ('t391.1');
7136    
7137            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
7138              my $form_attrs;
7139              $form_attrs->{action} = $at->{action} if $at->{action};
7140              my $prompt_attr = $at->{prompt};
7141              $at->{name} = {name => 'name', value => 'isindex'};
7142              delete $at->{action};
7143              delete $at->{prompt};
7144              my @tokens = (
7145                            {type => START_TAG_TOKEN, tag_name => 'form',
7146                             attributes => $form_attrs,
7147                             line => $token->{line}, column => $token->{column}},
7148                            {type => START_TAG_TOKEN, tag_name => 'hr',
7149                             line => $token->{line}, column => $token->{column}},
7150                            {type => START_TAG_TOKEN, tag_name => 'p',
7151                             line => $token->{line}, column => $token->{column}},
7152                            {type => START_TAG_TOKEN, tag_name => 'label',
7153                             line => $token->{line}, column => $token->{column}},
7154                           );
7155              if ($prompt_attr) {
7156                !!!cp ('t390');
7157                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7158                               #line => $token->{line}, column => $token->{column},
7159                              };
7160              } else {
7161                !!!cp ('t391');
7162                push @tokens, {type => CHARACTER_TOKEN,
7163                               data => 'This is a searchable index. Insert your search keywords here: ',
7164                               #line => $token->{line}, column => $token->{column},
7165                              }; # SHOULD
7166                ## TODO: make this configurable
7167              }
7168              push @tokens,
7169                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7170                             line => $token->{line}, column => $token->{column}},
7171                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7172                            {type => END_TAG_TOKEN, tag_name => 'label',
7173                             line => $token->{line}, column => $token->{column}},
7174                            {type => END_TAG_TOKEN, tag_name => 'p',
7175                             line => $token->{line}, column => $token->{column}},
7176                            {type => START_TAG_TOKEN, tag_name => 'hr',
7177                             line => $token->{line}, column => $token->{column}},
7178                            {type => END_TAG_TOKEN, tag_name => 'form',
7179                             line => $token->{line}, column => $token->{column}};
7180              !!!back-token (@tokens);
7181              !!!next-token;
7182              next B;
7183            }
7184          } elsif ($token->{tag_name} eq 'textarea') {
7185            my $tag_name = $token->{tag_name};
7186            my $el;
7187            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7188            
7189            ## TODO: $self->{form_element} if defined
7190            $self->{content_model} = RCDATA_CONTENT_MODEL;
7191            delete $self->{escape}; # MUST
7192            
7193            $insert->($el);
7194            
7195            my $text = '';
7196            !!!nack ('t392.1');
7197            !!!next-token;
7198            if ($token->{type} == CHARACTER_TOKEN) {
7199              $token->{data} =~ s/^\x0A//;
7200              unless (length $token->{data}) {
7201                !!!cp ('t392');
7202                !!!next-token;
7203              } else {
7204                !!!cp ('t393');
7205              }
7206          } else {          } else {
7207            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
7208            }
7209            while ($token->{type} == CHARACTER_TOKEN) {
7210              !!!cp ('t395');
7211              $text .= $token->{data};
7212              !!!next-token;
7213            }
7214            if (length $text) {
7215              !!!cp ('t396');
7216              $el->manakai_append_text ($text);
7217            }
7218            
7219            $self->{content_model} = PCDATA_CONTENT_MODEL;
7220            
7221            if ($token->{type} == END_TAG_TOKEN and
7222                $token->{tag_name} eq $tag_name) {
7223              !!!cp ('t397');
7224              ## Ignore the token
7225            } else {
7226              !!!cp ('t398');
7227              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7228          }          }
       }  
     } 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  
7229          !!!next-token;          !!!next-token;
7230          redo B;          next B;
7231        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
7232          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
7233          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
7234            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7235              my $node = $self->{open_elements}->[$_];
7236              if ($node->[1] & RUBY_EL) {
7237                !!!cp ('t398.1');
7238                ## generate implied end tags
7239                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7240                  !!!cp ('t398.2');
7241                  pop @{$self->{open_elements}};
7242                }
7243                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7244                  !!!cp ('t398.3');
7245                  !!!parse-error (type => 'not closed',
7246                                  text => $self->{open_elements}->[-1]->[0]
7247                                      ->manakai_local_name,
7248                                  token => $token);
7249                  pop @{$self->{open_elements}}
7250                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7251                }
7252                last INSCOPE;
7253              } elsif ($node->[1] & SCOPING_EL) {
7254                !!!cp ('t398.4');
7255                last INSCOPE;
7256              }
7257            } # INSCOPE
7258    
7259            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7260    
7261            !!!nack ('t398.5');
7262          !!!next-token;          !!!next-token;
7263          redo B;          redo B;
7264        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
7265          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
7266            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
7267            ## As if in the main phase.  
7268            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7269            ## just before the phase has been changed to the trailing  
7270            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7271            $reconstruct_active_formatting_elements->($insert_to_current)  
7272              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
7273            
7274            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7275            
7276            if ($self->{self_closing}) {
7277              pop @{$self->{open_elements}};
7278              !!!ack ('t398.1');
7279            } else {
7280              !!!cp ('t398.2');
7281              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7282              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7283              ## mode, "in body" (not "in foreign content") secondary insertion
7284              ## mode, maybe.
7285            }
7286    
7287            !!!next-token;
7288            next B;
7289          } elsif ({
7290                    caption => 1, col => 1, colgroup => 1, frame => 1,
7291                    frameset => 1, head => 1, option => 1, optgroup => 1,
7292                    tbody => 1, td => 1, tfoot => 1, th => 1,
7293                    thead => 1, tr => 1,
7294                   }->{$token->{tag_name}}) {
7295            !!!cp ('t401');
7296            !!!parse-error (type => 'in body',
7297                            text => $token->{tag_name}, token => $token);
7298            ## Ignore the token
7299            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7300            !!!next-token;
7301            next B;
7302            
7303            ## ISSUE: An issue on HTML5 new elements in the spec.
7304          } else {
7305            if ($token->{tag_name} eq 'image') {
7306              !!!cp ('t384');
7307              !!!parse-error (type => 'image', token => $token);
7308              $token->{tag_name} = 'img';
7309            } else {
7310              !!!cp ('t385');
7311            }
7312    
7313            ## NOTE: There is an "as if <br>" code clone.
7314            $reconstruct_active_formatting_elements->($insert_to_current);
7315            
7316            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7317    
7318            if ({
7319                 applet => 1, marquee => 1, object => 1,
7320                }->{$token->{tag_name}}) {
7321              !!!cp ('t380');
7322              push @$active_formatting_elements, ['#marker', ''];
7323              !!!nack ('t380.1');
7324            } elsif ({
7325                      b => 1, big => 1, em => 1, font => 1, i => 1,
7326                      s => 1, small => 1, strile => 1,
7327                      strong => 1, tt => 1, u => 1,
7328                     }->{$token->{tag_name}}) {
7329              !!!cp ('t375');
7330              push @$active_formatting_elements, $self->{open_elements}->[-1];
7331              !!!nack ('t375.1');
7332            } elsif ($token->{tag_name} eq 'input') {
7333              !!!cp ('t388');
7334              ## TODO: associate with $self->{form_element} if defined
7335              pop @{$self->{open_elements}};
7336              !!!ack ('t388.2');
7337            } elsif ({
7338                      area => 1, basefont => 1, bgsound => 1, br => 1,
7339                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7340                      #image => 1,
7341                     }->{$token->{tag_name}}) {
7342              !!!cp ('t388.1');
7343              pop @{$self->{open_elements}};
7344              !!!ack ('t388.3');
7345            } elsif ($token->{tag_name} eq 'select') {
7346              ## TODO: associate with $self->{form_element} if defined
7347            
7348              if ($self->{insertion_mode} & TABLE_IMS or
7349                  $self->{insertion_mode} & BODY_TABLE_IMS or
7350                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7351                !!!cp ('t400.1');
7352                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7353              } else {
7354                !!!cp ('t400.2');
7355                $self->{insertion_mode} = IN_SELECT_IM;
7356              }
7357              !!!nack ('t400.3');
7358            } else {
7359              !!!nack ('t402');
7360            }
7361            
7362            !!!next-token;
7363            next B;
7364          }
7365        } elsif ($token->{type} == END_TAG_TOKEN) {
7366          if ($token->{tag_name} eq 'body') {
7367            ## has a |body| element in scope
7368            my $i;
7369            INSCOPE: {
7370              for (reverse @{$self->{open_elements}}) {
7371                if ($_->[1] & BODY_EL) {
7372                  !!!cp ('t405');
7373                  $i = $_;
7374                  last INSCOPE;
7375                } elsif ($_->[1] & SCOPING_EL) {
7376                  !!!cp ('t405.1');
7377                  last;
7378                }
7379              }
7380    
7381              !!!parse-error (type => 'start tag not allowed',
7382                              text => $token->{tag_name}, token => $token);
7383              ## NOTE: Ignore the token.
7384              !!!next-token;
7385              next B;
7386            } # INSCOPE
7387    
7388            for (@{$self->{open_elements}}) {
7389              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7390                !!!cp ('t403');
7391                !!!parse-error (type => 'not closed',
7392                                text => $_->[0]->manakai_local_name,
7393                                token => $token);
7394                last;
7395              } else {
7396                !!!cp ('t404');
7397              }
7398            }
7399    
7400            $self->{insertion_mode} = AFTER_BODY_IM;
7401            !!!next-token;
7402            next B;
7403          } elsif ($token->{tag_name} eq 'html') {
7404            ## TODO: Update this code.  It seems that the code below is not
7405            ## up-to-date, though it has same effect as speced.
7406            if (@{$self->{open_elements}} > 1 and
7407                $self->{open_elements}->[1]->[1] & BODY_EL) {
7408              ## ISSUE: There is an issue in the spec.
7409              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7410                !!!cp ('t406');
7411                !!!parse-error (type => 'not closed',
7412                                text => $self->{open_elements}->[1]->[0]
7413                                    ->manakai_local_name,
7414                                token => $token);
7415              } else {
7416                !!!cp ('t407');
7417              }
7418              $self->{insertion_mode} = AFTER_BODY_IM;
7419              ## reprocess
7420              next B;
7421            } else {
7422              !!!cp ('t408');
7423              !!!parse-error (type => 'unmatched end tag',
7424                              text => $token->{tag_name}, token => $token);
7425              ## Ignore the token
7426              !!!next-token;
7427              next B;
7428            }
7429          } elsif ({
7430                    address => 1, blockquote => 1, center => 1, dir => 1,
7431                    div => 1, dl => 1, fieldset => 1, listing => 1,
7432                    menu => 1, ol => 1, pre => 1, ul => 1,
7433                    dd => 1, dt => 1, li => 1,
7434                    applet => 1, button => 1, marquee => 1, object => 1,
7435                   }->{$token->{tag_name}}) {
7436            ## has an element in scope
7437            my $i;
7438            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7439              my $node = $self->{open_elements}->[$_];
7440              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7441                !!!cp ('t410');
7442                $i = $_;
7443                last INSCOPE;
7444              } elsif ($node->[1] & SCOPING_EL) {
7445                !!!cp ('t411');
7446                last INSCOPE;
7447              }
7448            } # INSCOPE
7449    
7450            unless (defined $i) { # has an element in scope
7451              !!!cp ('t413');
7452              !!!parse-error (type => 'unmatched end tag',
7453                              text => $token->{tag_name}, token => $token);
7454              ## NOTE: Ignore the token.
7455            } else {
7456              ## Step 1. generate implied end tags
7457              while ({
7458                      ## END_TAG_OPTIONAL_EL
7459                      dd => ($token->{tag_name} ne 'dd'),
7460                      dt => ($token->{tag_name} ne 'dt'),
7461                      li => ($token->{tag_name} ne 'li'),
7462                      p => 1,
7463                      rt => 1,
7464                      rp => 1,
7465                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7466                !!!cp ('t409');
7467                pop @{$self->{open_elements}};
7468              }
7469    
7470              ## Step 2.
7471              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7472                      ne $token->{tag_name}) {
7473                !!!cp ('t412');
7474                !!!parse-error (type => 'not closed',
7475                                text => $self->{open_elements}->[-1]->[0]
7476                                    ->manakai_local_name,
7477                                token => $token);
7478              } else {
7479                !!!cp ('t414');
7480              }
7481    
7482              ## Step 3.
7483              splice @{$self->{open_elements}}, $i;
7484    
7485              ## Step 4.
7486              $clear_up_to_marker->()
7487                  if {
7488                    applet => 1, button => 1, marquee => 1, object => 1,
7489                  }->{$token->{tag_name}};
7490            }
7491            !!!next-token;
7492            next B;
7493          } elsif ($token->{tag_name} eq 'form') {
7494            undef $self->{form_element};
7495    
7496            ## has an element in scope
7497            my $i;
7498            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7499              my $node = $self->{open_elements}->[$_];
7500              if ($node->[1] & FORM_EL) {
7501                !!!cp ('t418');
7502                $i = $_;
7503                last INSCOPE;
7504              } elsif ($node->[1] & SCOPING_EL) {
7505                !!!cp ('t419');
7506                last INSCOPE;
7507              }
7508            } # INSCOPE
7509    
7510            unless (defined $i) { # has an element in scope
7511              !!!cp ('t421');
7512              !!!parse-error (type => 'unmatched end tag',
7513                              text => $token->{tag_name}, token => $token);
7514              ## NOTE: Ignore the token.
7515            } else {
7516              ## Step 1. generate implied end tags
7517              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7518                !!!cp ('t417');
7519                pop @{$self->{open_elements}};
7520              }
7521                        
7522            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7523              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7524                      ne $token->{tag_name}) {
7525                !!!cp ('t417.1');
7526                !!!parse-error (type => 'not closed',
7527                                text => $self->{open_elements}->[-1]->[0]
7528                                    ->manakai_local_name,
7529                                token => $token);
7530              } else {
7531                !!!cp ('t420');
7532              }  
7533                        
7534            unless (length $token->{data}) {            ## Step 3.
7535              !!!next-token;            splice @{$self->{open_elements}}, $i;
7536              redo B;          }
7537    
7538            !!!next-token;
7539            next B;
7540          } elsif ({
7541                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7542                   }->{$token->{tag_name}}) {
7543            ## has an element in scope
7544            my $i;
7545            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7546              my $node = $self->{open_elements}->[$_];
7547              if ($node->[1] & HEADING_EL) {
7548                !!!cp ('t423');
7549                $i = $_;
7550                last INSCOPE;
7551              } elsif ($node->[1] & SCOPING_EL) {
7552                !!!cp ('t424');
7553                last INSCOPE;
7554              }
7555            } # INSCOPE
7556    
7557            unless (defined $i) { # has an element in scope
7558              !!!cp ('t425.1');
7559              !!!parse-error (type => 'unmatched end tag',
7560                              text => $token->{tag_name}, token => $token);
7561              ## NOTE: Ignore the token.
7562            } else {
7563              ## Step 1. generate implied end tags
7564              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7565                !!!cp ('t422');
7566                pop @{$self->{open_elements}};
7567            }            }
7568              
7569              ## Step 2.
7570              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7571                      ne $token->{tag_name}) {
7572                !!!cp ('t425');
7573                !!!parse-error (type => 'unmatched end tag',
7574                                text => $token->{tag_name}, token => $token);
7575              } else {
7576                !!!cp ('t426');
7577              }
7578    
7579              ## Step 3.
7580              splice @{$self->{open_elements}}, $i;
7581          }          }
7582            
7583            !!!next-token;
7584            next B;
7585          } elsif ($token->{tag_name} eq 'p') {
7586            ## has an element in scope
7587            my $i;
7588            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7589              my $node = $self->{open_elements}->[$_];
7590              if ($node->[1] & P_EL) {
7591                !!!cp ('t410.1');
7592                $i = $_;
7593                last INSCOPE;
7594              } elsif ($node->[1] & SCOPING_EL) {
7595                !!!cp ('t411.1');
7596                last INSCOPE;
7597              }
7598            } # INSCOPE
7599    
7600          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7601          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7602          ## reprocess                    ne $token->{tag_name}) {
7603          redo B;              !!!cp ('t412.1');
7604        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7605                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7606          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7607          $phase = 'main';                              token => $token);
7608          ## reprocess            } else {
7609          redo B;              !!!cp ('t414.1');
7610        } elsif ($token->{type} eq 'end-of-file') {            }
7611          ## Stop parsing  
7612          last B;            splice @{$self->{open_elements}}, $i;
7613            } else {
7614              !!!cp ('t413.1');
7615              !!!parse-error (type => 'unmatched end tag',
7616                              text => $token->{tag_name}, token => $token);
7617    
7618              !!!cp ('t415.1');
7619              ## As if <p>, then reprocess the current token
7620              my $el;
7621              !!!create-element ($el, $HTML_NS, 'p',, $token);
7622              $insert->($el);
7623              ## NOTE: Not inserted into |$self->{open_elements}|.
7624            }
7625    
7626            !!!next-token;
7627            next B;
7628          } elsif ({
7629                    a => 1,
7630                    b => 1, big => 1, em => 1, font => 1, i => 1,
7631                    nobr => 1, s => 1, small => 1, strile => 1,
7632                    strong => 1, tt => 1, u => 1,
7633                   }->{$token->{tag_name}}) {
7634            !!!cp ('t427');
7635            $formatting_end_tag->($token);
7636            next B;
7637          } elsif ($token->{tag_name} eq 'br') {
7638            !!!cp ('t428');
7639            !!!parse-error (type => 'unmatched end tag',
7640                            text => 'br', token => $token);
7641    
7642            ## As if <br>
7643            $reconstruct_active_formatting_elements->($insert_to_current);
7644            
7645            my $el;
7646            !!!create-element ($el, $HTML_NS, 'br',, $token);
7647            $insert->($el);
7648            
7649            ## Ignore the token.
7650            !!!next-token;
7651            next B;
7652          } elsif ({
7653                    caption => 1, col => 1, colgroup => 1, frame => 1,
7654                    frameset => 1, head => 1, option => 1, optgroup => 1,
7655                    tbody => 1, td => 1, tfoot => 1, th => 1,
7656                    thead => 1, tr => 1,
7657                    area => 1, basefont => 1, bgsound => 1,
7658                    embed => 1, hr => 1, iframe => 1, image => 1,
7659                    img => 1, input => 1, isindex => 1, noembed => 1,
7660                    noframes => 1, param => 1, select => 1, spacer => 1,
7661                    table => 1, textarea => 1, wbr => 1,
7662                    noscript => 0, ## TODO: if scripting is enabled
7663                   }->{$token->{tag_name}}) {
7664            !!!cp ('t429');
7665            !!!parse-error (type => 'unmatched end tag',
7666                            text => $token->{tag_name}, token => $token);
7667            ## Ignore the token
7668            !!!next-token;
7669            next B;
7670            
7671            ## ISSUE: Issue on HTML5 new elements in spec
7672            
7673        } else {        } else {
7674          die "$0: $token->{type}: Unknown token";          ## Step 1
7675            my $node_i = -1;
7676            my $node = $self->{open_elements}->[$node_i];
7677    
7678            ## Step 2
7679            S2: {
7680              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7681                ## Step 1
7682                ## generate implied end tags
7683                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7684                  !!!cp ('t430');
7685                  ## NOTE: |<ruby><rt></ruby>|.
7686                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7687                  ## which seems wrong.
7688                  pop @{$self->{open_elements}};
7689                  $node_i++;
7690                }
7691            
7692                ## Step 2
7693                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7694                        ne $token->{tag_name}) {
7695                  !!!cp ('t431');
7696                  ## NOTE: <x><y></x>
7697                  !!!parse-error (type => 'not closed',
7698                                  text => $self->{open_elements}->[-1]->[0]
7699                                      ->manakai_local_name,
7700                                  token => $token);
7701                } else {
7702                  !!!cp ('t432');
7703                }
7704                
7705                ## Step 3
7706                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7707    
7708                !!!next-token;
7709                last S2;
7710              } else {
7711                ## Step 3
7712                if (not ($node->[1] & FORMATTING_EL) and
7713                    #not $phrasing_category->{$node->[1]} and
7714                    ($node->[1] & SPECIAL_EL or
7715                     $node->[1] & SCOPING_EL)) {
7716                  !!!cp ('t433');
7717                  !!!parse-error (type => 'unmatched end tag',
7718                                  text => $token->{tag_name}, token => $token);
7719                  ## Ignore the token
7720                  !!!next-token;
7721                  last S2;
7722                }
7723    
7724                !!!cp ('t434');
7725              }
7726              
7727              ## Step 4
7728              $node_i--;
7729              $node = $self->{open_elements}->[$node_i];
7730              
7731              ## Step 5;
7732              redo S2;
7733            } # S2
7734            next B;
7735        }        }
7736      }      }
7737        next B;
7738      } continue { # B
7739        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7740          ## NOTE: The code below is executed in cases where it does not have
7741          ## to be, but it it is harmless even in those cases.
7742          ## has an element in scope
7743          INSCOPE: {
7744            for (reverse 0..$#{$self->{open_elements}}) {
7745              my $node = $self->{open_elements}->[$_];
7746              if ($node->[1] & FOREIGN_EL) {
7747                last INSCOPE;
7748              } elsif ($node->[1] & SCOPING_EL) {
7749                last;
7750              }
7751            }
7752            
7753            ## NOTE: No foreign element in scope.
7754            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7755          } # INSCOPE
7756        }
7757    } # B    } # B
7758    
7759    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4898  sub _tree_construction_main ($) { Line 7761  sub _tree_construction_main ($) {
7761    ## TODO: script stuffs    ## TODO: script stuffs
7762  } # _tree_construct_main  } # _tree_construct_main
7763    
7764  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7765    my $class = shift;    my $class = shift;
7766    my $node = shift;    my $node = shift;
7767    my $s = \$_[0];    #my $s = \$_[0];
7768    my $onerror = $_[1];    my $onerror = $_[1];
7769      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7770    
7771      ## ISSUE: Should {confident} be true?
7772    
7773    my $nt = $node->node_type;    my $nt = $node->node_type;
7774    if ($nt == 9) {    if ($nt == 9) {
# Line 4919  sub set_inner_html ($$$) { Line 7785  sub set_inner_html ($$$) {
7785      }      }
7786    
7787      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7788      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7789    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7790      ## TODO: If non-html element      ## TODO: If non-html element
7791    
7792      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7793    
7794    ## TODO: Support for $get_wrapper
7795    
7796      ## Step 1 # MUST      ## Step 1 # MUST
7797      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7798      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
7799      ## TODO: Mark as HTML document      $doc->manakai_is_html (1);
7800      my $p = $class->new;      my $p = $class->new;
7801      $p->{document} = $doc;      $p->{document} = $doc;
7802    
7803      ## Step 9 # MUST      ## Step 8 # MUST
7804      my $i = 0;      my $i = 0;
7805      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7806      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7807      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7808        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7809        $input = $get_wrapper->($input);
7810        $p->{set_nc} = sub {
7811        my $self = shift;        my $self = shift;
7812    
7813        pop @{$self->{prev_input_character}};        my $char = '';
7814        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        if (defined $self->{next_nc}) {
7815            $char = $self->{next_nc};
7816            delete $self->{next_nc};
7817            $self->{nc} = ord $char;
7818          } else {
7819            $self->{char_buffer} = '';
7820            $self->{char_buffer_pos} = 0;
7821            
7822            my $count = $input->manakai_read_until
7823                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7824                 $self->{char_buffer_pos});
7825            if ($count) {
7826              $self->{line_prev} = $self->{line};
7827              $self->{column_prev} = $self->{column};
7828              $self->{column}++;
7829              $self->{nc}
7830                  = ord substr ($self->{char_buffer},
7831                                $self->{char_buffer_pos}++, 1);
7832              return;
7833            }
7834            
7835            if ($input->read ($char, 1)) {
7836              $self->{nc} = ord $char;
7837            } else {
7838              $self->{nc} = -1;
7839              return;
7840            }
7841          }
7842    
7843          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7844          $p->{column}++;
7845    
7846        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{nc} == 0x000A) { # LF
7847        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
7848        $column++;          $p->{column} = 0;
7849            !!!cp ('i1');
7850        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{nc} == 0x000D) { # CR
7851          $line++;  ## TODO: support for abort/streaming
7852          $column = 0;          my $next = '';
7853        } elsif ($self->{next_input_character} == 0x000D) { # CR          if ($input->read ($next, 1) and $next ne "\x0A") {
7854          $i++ if substr ($$s, $i, 1) eq "\x0A";            $self->{next_nc} = $next;
7855          $self->{next_input_character} = 0x000A; # LF # MUST          }
7856          $line++;          $self->{nc} = 0x000A; # LF # MUST
7857          $column = 0;          $p->{line}++;
7858        } elsif ($self->{next_input_character} > 0x10FFFF) {          $p->{column} = 0;
7859          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i2');
7860        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{nc} == 0x0000) { # NULL
7861            !!!cp ('i4');
7862          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7863          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7864        }        }
7865      };      };
7866      $p->{prev_input_character} = [-1, -1, -1];  
7867      $p->{next_input_character} = -1;      $p->{read_until} = sub {
7868              #my ($scalar, $specials_range, $offset) = @_;
7869          return 0 if defined $p->{next_nc};
7870    
7871          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7872          my $offset = $_[2] || 0;
7873          
7874          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7875            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7876            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7877              substr ($_[0], $offset)
7878                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7879              my $count = $+[0] - $-[0];
7880              if ($count) {
7881                $p->{column} += $count;
7882                $p->{char_buffer_pos} += $count;
7883                $p->{line_prev} = $p->{line};
7884                $p->{column_prev} = $p->{column} - 1;
7885                $p->{prev_char} = [-1, -1, -1];
7886                $p->{nc} = -1;
7887              }
7888              return $count;
7889            } else {
7890              return 0;
7891            }
7892          } else {
7893            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7894            if ($count) {
7895              $p->{column} += $count;
7896              $p->{column_prev} += $count;
7897              $p->{prev_char} = [-1, -1, -1];
7898              $p->{nc} = -1;
7899            }
7900            return $count;
7901          }
7902        }; # $p->{read_until}
7903    
7904      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7905        my (%opt) = @_;        my (%opt) = @_;
7906        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7907          my $column = $opt{column};
7908          if (defined $opt{token} and defined $opt{token}->{line}) {
7909            $line = $opt{token}->{line};
7910            $column = $opt{token}->{column};
7911          }
7912          warn "Parse error ($opt{type}) at line $line column $column\n";
7913      };      };
7914      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7915        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7916      };      };
7917            
7918        my $char_onerror = sub {
7919          my (undef, $type, %opt) = @_;
7920          $ponerror->(layer => 'encode',
7921                      line => $p->{line}, column => $p->{column} + 1,
7922                      %opt, type => $type);
7923        }; # $char_onerror
7924        $input->onerror ($char_onerror);
7925    
7926      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7927      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7928    
7929      ## Step 2      ## Step 2
7930      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7931      $p->{content_model_flag} = {      $p->{content_model} = {
7932        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7933        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7934        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7935        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7936        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7937        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7938        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7939        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7940        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7941        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7942      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7943         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7944            unless defined $p->{content_model};
7945            ## ISSUE: What is "the name of the element"? local name?
7946    
7947      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7948          ## TODO: Foreign element OK?
7949    
7950      ## Step 4      ## Step 3
7951      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7952        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7953    
7954      ## Step 5 # MUST      ## Step 4 # MUST
7955      $doc->append_child ($root);      $doc->append_child ($root);
7956    
7957      ## Step 6 # MUST      ## Step 5 # MUST
7958      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7959    
7960      undef $p->{head_element};      undef $p->{head_element};
7961    
7962      ## Step 7 # MUST      ## Step 6 # MUST
7963      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7964    
7965      ## Step 8 # MUST      ## Step 7 # MUST
7966      my $anode = $node;      my $anode = $node;
7967      AN: while (defined $anode) {      AN: while (defined $anode) {
7968        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7969          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7970          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7971            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7972                !!!cp ('i5');
7973              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7974              last AN;              last AN;
7975            }            }
# Line 5023  sub set_inner_html ($$$) { Line 7978  sub set_inner_html ($$$) {
7978        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7979      } # AN      } # AN
7980            
7981      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7982      {      {
7983        my $self = $p;        my $self = $p;
7984        !!!next-token;        !!!next-token;
7985      }      }
7986      $p->_tree_construction_main;      $p->_tree_construction_main;
7987    
7988      ## Step 11 # MUST      ## Step 10 # MUST
7989      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7990      for (@cn) {      for (@cn) {
7991        $node->remove_child ($_);        $node->remove_child ($_);
7992      }      }
7993      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7994    
7995      ## Step 12 # MUST      ## Step 11 # MUST
7996      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7997      for (@cn) {      for (@cn) {
7998        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5047  sub set_inner_html ($$$) { Line 8001  sub set_inner_html ($$$) {
8001      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8002    
8003      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8004    
8005        delete $p->{parse_error}; # delete loop
8006    } else {    } else {
8007      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";
8008    }    }
# Line 5054  sub set_inner_html ($$$) { Line 8010  sub set_inner_html ($$$) {
8010    
8011  } # tree construction stage  } # tree construction stage
8012    
8013  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
8014    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  
8015    
8016  1;  1;
8017  # $Date$  # $Date$

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.183

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24