/[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.12 by wakaba, Sat Jun 23 04:38:50 2007 UTC revision 1.185 by wakaba, Mon Sep 15 09:27:53 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  sub parse_string ($$$;$) {      ## Step 3
425    my $self = shift->new;      if ($byte_buffer =~ /^\xFE\xFF/) {
426    my $s = \$_[0];        $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          !!!parse-error (type => 'charset label detected',
555                          text => $self->{input_encoding},
556                          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      $self->{next_input_character} = -1 and return if $i >= length $$s;  
658      $self->{next_input_character} = ord substr $$s, $i++, 1;      my $char = '';
659      $column++;      if (defined $self->{next_nc}) {
660              $char = $self->{next_nc};
661      if ($self->{next_input_character} == 0x000A) { # LF        delete $self->{next_nc};
662        $line++;        $self->{nc} = ord $char;
663        $column = 0;      } else {
664      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{char_buffer} = '';
665        if ($i >= length $$s) {        $self->{char_buffer_pos} = 0;
666          #  
667          my $count = $input->manakai_read_until
668             ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
669          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 {        } else {
681          my $next_char = ord substr $$s, $i++, 1;          $self->{nc} = -1;
682          if ($next_char == 0x000A) { # LF          return;
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
683        }        }
684        $self->{next_input_character} = 0x000A; # LF # MUST      }
685        $line++;  
686        $column = 0;      ($self->{line_prev}, $self->{column_prev})
687      } elsif ($self->{next_input_character} > 0x10FFFF) {          = ($self->{line}, $self->{column});
688        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST      $self->{column}++;
689      } elsif ($self->{next_input_character} == 0x0000) { # NULL      
690        if ($self->{nc} == 0x000A) { # LF
691          !!!cp ('j1');
692          $self->{line}++;
693          $self->{column} = 0;
694        } elsif ($self->{nc} == 0x000D) { # CR
695          !!!cp ('j2');
696    ## TODO: support for abort/streaming
697          my $next = '';
698          if ($input->read ($next, 1) and $next ne "\x0A") {
699            $self->{next_nc} = $next;
700          }
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    
711      $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->{nc} = -1;
730            }
731            return $count;
732          } else {
733            return 0;
734          }
735        } else {
736          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
737          if ($count) {
738            $self->{column} += $count;
739            $self->{line_prev} = $self->{line};
740            $self->{column_prev} = $self->{column} - 1;
741            $self->{nc} = -1;
742          }
743          return $count;
744        }
745      }; # $self->{read_until}
746    
747    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
748      my (%opt) = @_;      my (%opt) = @_;
749      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
750        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
751        warn "Parse error ($opt{type}) at line $line column $column\n";
752    };    };
753    $self->{parse_error} = sub {    $self->{parse_error} = sub {
754      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
755    };    };
756    
757      my $char_onerror = sub {
758        my (undef, $type, %opt) = @_;
759        !!!parse-error (layer => 'encode',
760                        line => $self->{line}, column => $self->{column} + 1,
761                        %opt, type => $type);
762      }; # $char_onerror
763    
764      if ($_[3]) {
765        $input = $_[3]->($input);
766        $input->onerror ($char_onerror);
767      } else {
768        $input->onerror ($char_onerror) unless defined $input->onerror;
769      }
770    
771    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
772    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
773    $self->_construct_tree;    $self->_construct_tree;
774    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
775    
776      delete $self->{parse_error}; # remove loop
777    
778    return $self->{document};    return $self->{document};
779  } # parse_string  } # parse_char_stream
780    
781  sub new ($) {  sub new ($) {
782    my $class = shift;    my $class = shift;
783    my $self = bless {}, $class;    my $self = bless {
784    $self->{set_next_input_character} = sub {      level => {must => 'm',
785      $self->{next_input_character} = -1;                should => 's',
786                  warn => 'w',
787                  info => 'i',
788                  uncertain => 'u'},
789      }, $class;
790      $self->{set_nc} = sub {
791        $self->{nc} = -1;
792    };    };
793    $self->{parse_error} = sub {    $self->{parse_error} = sub {
794      #      #
795    };    };
796      $self->{change_encoding} = sub {
797        # if ($_[0] is a supported encoding) {
798        #   run "change the encoding" algorithm;
799        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
800        # }
801      };
802      $self->{application_cache_selection} = sub {
803        #
804      };
805    return $self;    return $self;
806  } # new  } # new
807    
808    sub CM_ENTITY () { 0b001 } # & markup in data
809    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
810    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
811    
812    sub PLAINTEXT_CONTENT_MODEL () { 0 }
813    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
814    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
815    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
816    
817    sub DATA_STATE () { 0 }
818    #sub ENTITY_DATA_STATE () { 1 }
819    sub TAG_OPEN_STATE () { 2 }
820    sub CLOSE_TAG_OPEN_STATE () { 3 }
821    sub TAG_NAME_STATE () { 4 }
822    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
823    sub ATTRIBUTE_NAME_STATE () { 6 }
824    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
825    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
826    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
827    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
828    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
829    #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
830    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
831    sub COMMENT_START_STATE () { 14 }
832    sub COMMENT_START_DASH_STATE () { 15 }
833    sub COMMENT_STATE () { 16 }
834    sub COMMENT_END_STATE () { 17 }
835    sub COMMENT_END_DASH_STATE () { 18 }
836    sub BOGUS_COMMENT_STATE () { 19 }
837    sub DOCTYPE_STATE () { 20 }
838    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
839    sub DOCTYPE_NAME_STATE () { 22 }
840    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
841    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
842    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
843    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
844    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
845    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
846    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
847    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
848    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
849    sub BOGUS_DOCTYPE_STATE () { 32 }
850    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
851    sub SELF_CLOSING_START_TAG_STATE () { 34 }
852    sub CDATA_SECTION_STATE () { 35 }
853    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
854    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
855    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
856    sub CDATA_RCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
857    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
858    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
859    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
860    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
861    ## NOTE: "Entity data state", "entity in attribute value state", and
862    ## "consume a character reference" algorithm are jointly implemented
863    ## using the following six states:
864    sub ENTITY_STATE () { 44 }
865    sub ENTITY_HASH_STATE () { 45 }
866    sub NCR_NUM_STATE () { 46 }
867    sub HEXREF_X_STATE () { 47 }
868    sub HEXREF_HEX_STATE () { 48 }
869    sub ENTITY_NAME_STATE () { 49 }
870    sub PCDATA_STATE () { 50 } # "data state" in the spec
871    
872    sub DOCTYPE_TOKEN () { 1 }
873    sub COMMENT_TOKEN () { 2 }
874    sub START_TAG_TOKEN () { 3 }
875    sub END_TAG_TOKEN () { 4 }
876    sub END_OF_FILE_TOKEN () { 5 }
877    sub CHARACTER_TOKEN () { 6 }
878    
879    sub AFTER_HTML_IMS () { 0b100 }
880    sub HEAD_IMS ()       { 0b1000 }
881    sub BODY_IMS ()       { 0b10000 }
882    sub BODY_TABLE_IMS () { 0b100000 }
883    sub TABLE_IMS ()      { 0b1000000 }
884    sub ROW_IMS ()        { 0b10000000 }
885    sub BODY_AFTER_IMS () { 0b100000000 }
886    sub FRAME_IMS ()      { 0b1000000000 }
887    sub SELECT_IMS ()     { 0b10000000000 }
888    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
889        ## NOTE: "in foreign content" insertion mode is special; it is combined
890        ## with the secondary insertion mode.  In this parser, they are stored
891        ## together in the bit-or'ed form.
892    
893    ## NOTE: "initial" and "before html" insertion modes have no constants.
894    
895    ## NOTE: "after after body" insertion mode.
896    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
897    
898    ## NOTE: "after after frameset" insertion mode.
899    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
900    
901    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
902    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
903    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
904    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
905    sub IN_BODY_IM () { BODY_IMS }
906    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
907    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
908    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
909    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
910    sub IN_TABLE_IM () { TABLE_IMS }
911    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
912    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
913    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
914    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
915    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
916    sub IN_COLUMN_GROUP_IM () { 0b10 }
917    
918  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
919    
920  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
921    my $self = shift;    my $self = shift;
922    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
923    $self->{content_model_flag} = 'PCDATA'; # be    #$self->{s_kwd}; # state keyword - initialized when used
924    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    #$self->{entity__value}; # initialized when used
925    undef $self->{current_attribute};    #$self->{entity__match}; # initialized when used
926    undef $self->{last_emitted_start_tag_name};    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
927    undef $self->{last_attribute_value_state};    undef $self->{ct}; # current token
928    $self->{char} = [];    undef $self->{ca}; # current attribute
929    # $self->{next_input_character}    undef $self->{last_stag_name}; # last emitted start tag name
930      #$self->{prev_state}; # initialized when used
931      delete $self->{self_closing};
932      $self->{char_buffer} = '';
933      $self->{char_buffer_pos} = 0;
934      $self->{nc} = -1; # next input character
935      #$self->{next_nc}
936    !!!next-input-character;    !!!next-input-character;
937    $self->{token} = [];    $self->{token} = [];
938      # $self->{escape}
939  } # _initialize_tokenizer  } # _initialize_tokenizer
940    
941  ## A token has:  ## A token has:
942  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
943  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
944  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
945      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
946  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{pubid} (DOCTYPE_TOKEN)
947  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{sysid} (DOCTYPE_TOKEN)
948  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
949    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
950  ## Macros  ##        ->{name}
951  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
952  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
953  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
954    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
955    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
956    ##     while the token is pushed back to the stack.
957    
958  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
959    
# Line 444  sub _initialize_tokenizer ($) { Line 963  sub _initialize_tokenizer ($) {
963  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
964  ## and removed from the list.  ## and removed from the list.
965    
966    ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
967    ## (This requirement was dropped from HTML5 spec, unfortunately.)
968    
969  sub _get_next_token ($) {  sub _get_next_token ($) {
970    my $self = shift;    my $self = shift;
971    
972      if ($self->{self_closing}) {
973        !!!parse-error (type => 'nestc', token => $self->{ct});
974        ## NOTE: The |self_closing| flag is only set by start tag token.
975        ## In addition, when a start tag token is emitted, it is always set to
976        ## |ct|.
977        delete $self->{self_closing};
978      }
979    
980    if (@{$self->{token}}) {    if (@{$self->{token}}) {
981        $self->{self_closing} = $self->{token}->[0]->{self_closing};
982      return shift @{$self->{token}};      return shift @{$self->{token}};
983    }    }
984    
985    A: {    A: {
986      if ($self->{state} eq 'data') {      if ($self->{state} == PCDATA_STATE) {
987        if ($self->{next_input_character} == 0x0026) { # &        ## NOTE: Same as |DATA_STATE|, but only for |PCDATA| content model.
988          if ($self->{content_model_flag} eq 'PCDATA' or  
989              $self->{content_model_flag} eq 'RCDATA') {        if ($self->{nc} == 0x0026) { # &
990            $self->{state} = 'entity data';          !!!cp (0.1);
991            ## NOTE: In the spec, the tokenizer is switched to the
992            ## "entity data state".  In this implementation, the tokenizer
993            ## is switched to the |ENTITY_STATE|, which is an implementation
994            ## of the "consume a character reference" algorithm.
995            $self->{entity_add} = -1;
996            $self->{prev_state} = DATA_STATE;
997            $self->{state} = ENTITY_STATE;
998            !!!next-input-character;
999            redo A;
1000          } elsif ($self->{nc} == 0x003C) { # <
1001            !!!cp (0.2);
1002            $self->{state} = TAG_OPEN_STATE;
1003            !!!next-input-character;
1004            redo A;
1005          } elsif ($self->{nc} == -1) {
1006            !!!cp (0.3);
1007            !!!emit ({type => END_OF_FILE_TOKEN,
1008                      line => $self->{line}, column => $self->{column}});
1009            last A; ## TODO: ok?
1010          } else {
1011            !!!cp (0.4);
1012            #
1013          }
1014    
1015          # Anything else
1016          my $token = {type => CHARACTER_TOKEN,
1017                       data => chr $self->{nc},
1018                       line => $self->{line}, column => $self->{column},
1019                      };
1020          $self->{read_until}->($token->{data}, q[<&], length $token->{data});
1021    
1022          ## Stay in the state.
1023          !!!next-input-character;
1024          !!!emit ($token);
1025          redo A;
1026        } elsif ($self->{state} == DATA_STATE) {
1027          $self->{s_kwd} = '' unless defined $self->{s_kwd};
1028          if ($self->{nc} == 0x0026) { # &
1029            $self->{s_kwd} = '';
1030            if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
1031                not $self->{escape}) {
1032              !!!cp (1);
1033              ## NOTE: In the spec, the tokenizer is switched to the
1034              ## "entity data state".  In this implementation, the tokenizer
1035              ## is switched to the |ENTITY_STATE|, which is an implementation
1036              ## of the "consume a character reference" algorithm.
1037              $self->{entity_add} = -1;
1038              $self->{prev_state} = DATA_STATE;
1039              $self->{state} = ENTITY_STATE;
1040            !!!next-input-character;            !!!next-input-character;
1041            redo A;            redo A;
1042          } else {          } else {
1043              !!!cp (2);
1044              #
1045            }
1046          } elsif ($self->{nc} == 0x002D) { # -
1047            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1048              $self->{s_kwd} .= '-';
1049              
1050              if ($self->{s_kwd} eq '<!--') {
1051                !!!cp (3);
1052                $self->{escape} = 1; # unless $self->{escape};
1053                $self->{s_kwd} = '--';
1054                #
1055              } elsif ($self->{s_kwd} eq '---') {
1056                !!!cp (4);
1057                $self->{s_kwd} = '--';
1058                #
1059              } else {
1060                !!!cp (5);
1061                #
1062              }
1063            }
1064            
1065            #
1066          } elsif ($self->{nc} == 0x0021) { # !
1067            if (length $self->{s_kwd}) {
1068              !!!cp (5.1);
1069              $self->{s_kwd} .= '!';
1070              #
1071            } else {
1072              !!!cp (5.2);
1073              #$self->{s_kwd} = '';
1074            #            #
1075          }          }
1076        } elsif ($self->{next_input_character} == 0x003C) { # <          #
1077          if ($self->{content_model_flag} ne 'PLAINTEXT') {        } elsif ($self->{nc} == 0x003C) { # <
1078            $self->{state} = 'tag open';          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1079                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1080                 not $self->{escape})) {
1081              !!!cp (6);
1082              $self->{state} = TAG_OPEN_STATE;
1083            !!!next-input-character;            !!!next-input-character;
1084            redo A;            redo A;
1085          } else {          } else {
1086              !!!cp (7);
1087              $self->{s_kwd} = '';
1088            #            #
1089          }          }
1090        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
1091          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
1092                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1093              if ($self->{s_kwd} eq '--') {
1094                !!!cp (8);
1095                delete $self->{escape};
1096              } else {
1097                !!!cp (9);
1098              }
1099            } else {
1100              !!!cp (10);
1101            }
1102            
1103            $self->{s_kwd} = '';
1104            #
1105          } elsif ($self->{nc} == -1) {
1106            !!!cp (11);
1107            $self->{s_kwd} = '';
1108            !!!emit ({type => END_OF_FILE_TOKEN,
1109                      line => $self->{line}, column => $self->{column}});
1110          last A; ## TODO: ok?          last A; ## TODO: ok?
1111          } else {
1112            !!!cp (12);
1113            $self->{s_kwd} = '';
1114            #
1115        }        }
       # Anything else  
       my $token = {type => 'character',  
                    data => chr $self->{next_input_character}};  
       ## Stay in the data state  
       !!!next-input-character;  
1116    
1117        !!!emit ($token);        # Anything else
1118          my $token = {type => CHARACTER_TOKEN,
1119        redo A;                     data => chr $self->{nc},
1120      } elsif ($self->{state} eq 'entity data') {                     line => $self->{line}, column => $self->{column},
1121        ## (cannot happen in CDATA state)                    };
1122                if ($self->{read_until}->($token->{data}, q[-!<>&],
1123        my $token = $self->_tokenize_attempt_to_consume_an_entity;                                  length $token->{data})) {
1124            $self->{s_kwd} = '';
1125        $self->{state} = 'data';        }
       # next-input-character is already done  
1126    
1127        unless (defined $token) {        ## Stay in the data state.
1128          !!!emit ({type => 'character', data => '&'});        if ($self->{content_model} == PCDATA_CONTENT_MODEL) {
1129            !!!cp (13);
1130            $self->{state} = PCDATA_STATE;
1131        } else {        } else {
1132          !!!emit ($token);          !!!cp (14);
1133            ## Stay in the state.
1134        }        }
1135          !!!next-input-character;
1136          !!!emit ($token);
1137        redo A;        redo A;
1138      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1139        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1140            $self->{content_model_flag} eq 'CDATA') {          if ($self->{nc} == 0x002F) { # /
1141          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
1142            !!!next-input-character;            !!!next-input-character;
1143            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1144            redo A;            redo A;
1145            } elsif ($self->{nc} == 0x0021) { # !
1146              !!!cp (15.1);
1147              $self->{s_kwd} = '<' unless $self->{escape};
1148              #
1149          } else {          } else {
1150            ## reconsume            !!!cp (16);
1151            $self->{state} = 'data';            #
   
           !!!emit ({type => 'character', data => '<'});  
   
           redo A;  
1152          }          }
1153        } elsif ($self->{content_model_flag} eq 'PCDATA') {  
1154          if ($self->{next_input_character} == 0x0021) { # !          ## reconsume
1155            $self->{state} = 'markup declaration open';          $self->{state} = DATA_STATE;
1156            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1157                      line => $self->{line_prev},
1158                      column => $self->{column_prev},
1159                     });
1160            redo A;
1161          } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1162            if ($self->{nc} == 0x0021) { # !
1163              !!!cp (17);
1164              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1165            !!!next-input-character;            !!!next-input-character;
1166            redo A;            redo A;
1167          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1168            $self->{state} = 'close tag open';            !!!cp (18);
1169              $self->{state} = CLOSE_TAG_OPEN_STATE;
1170            !!!next-input-character;            !!!next-input-character;
1171            redo A;            redo A;
1172          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{nc} and
1173                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1174            $self->{current_token}            !!!cp (19);
1175              = {type => 'start tag',            $self->{ct}
1176                 tag_name => chr ($self->{next_input_character} + 0x0020)};              = {type => START_TAG_TOKEN,
1177            $self->{state} = 'tag name';                 tag_name => chr ($self->{nc} + 0x0020),
1178                   line => $self->{line_prev},
1179                   column => $self->{column_prev}};
1180              $self->{state} = TAG_NAME_STATE;
1181            !!!next-input-character;            !!!next-input-character;
1182            redo A;            redo A;
1183          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{nc} and
1184                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1185            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1186                              tag_name => chr ($self->{next_input_character})};            $self->{ct} = {type => START_TAG_TOKEN,
1187            $self->{state} = 'tag name';                                      tag_name => chr ($self->{nc}),
1188                                        line => $self->{line_prev},
1189                                        column => $self->{column_prev}};
1190              $self->{state} = TAG_NAME_STATE;
1191            !!!next-input-character;            !!!next-input-character;
1192            redo A;            redo A;
1193          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1194            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1195            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1196                              line => $self->{line_prev},
1197                              column => $self->{column_prev});
1198              $self->{state} = DATA_STATE;
1199            !!!next-input-character;            !!!next-input-character;
1200    
1201            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1202                        line => $self->{line_prev},
1203                        column => $self->{column_prev},
1204                       });
1205    
1206            redo A;            redo A;
1207          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1208            !!!parse-error (type => 'pio');            !!!cp (22);
1209            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1210            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1211                              column => $self->{column_prev});
1212              $self->{state} = BOGUS_COMMENT_STATE;
1213              $self->{ct} = {type => COMMENT_TOKEN, data => '',
1214                                        line => $self->{line_prev},
1215                                        column => $self->{column_prev},
1216                                       };
1217              ## $self->{nc} is intentionally left as is
1218            redo A;            redo A;
1219          } else {          } else {
1220            !!!parse-error (type => 'bare stago');            !!!cp (23);
1221            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1222                              line => $self->{line_prev},
1223                              column => $self->{column_prev});
1224              $self->{state} = DATA_STATE;
1225            ## reconsume            ## reconsume
1226    
1227            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1228                        line => $self->{line_prev},
1229                        column => $self->{column_prev},
1230                       });
1231    
1232            redo A;            redo A;
1233          }          }
1234        } else {        } else {
1235          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1236        }        }
1237      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1238        if ($self->{content_model_flag} eq 'RCDATA' or        ## NOTE: The "close tag open state" in the spec is implemented as
1239            $self->{content_model_flag} eq 'CDATA') {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_RCDATA_CLOSE_TAG_STATE|.
1240          my @next_char;  
1241          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"</"
1242            push @next_char, $self->{next_input_character};        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1243            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);          if (defined $self->{last_stag_name}) {
1244            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            $self->{state} = CDATA_RCDATA_CLOSE_TAG_STATE;
1245            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {            $self->{s_kwd} = '';
1246              !!!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 => '</'});  
   
1247            redo A;            redo A;
1248          } else {          } else {
1249            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1250            !!!back-next-input-character (@next_char);            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1251            # and consume...            !!!cp (28);
1252              $self->{state} = DATA_STATE;
1253              ## Reconsume.
1254              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1255                        line => $l, column => $c,
1256                       });
1257              redo A;
1258          }          }
1259        }        }
1260          
1261        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{nc} and
1262            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1263          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1264                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{ct}
1265          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1266          !!!next-input-character;                 tag_name => chr ($self->{nc} + 0x0020),
1267          redo A;                 line => $l, column => $c};
1268        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1269                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1270          $self->{current_token} = {type => 'end tag',          redo A;
1271                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{nc} and
1272          $self->{state} = 'tag name';                 $self->{nc} <= 0x007A) { # a..z
1273          !!!next-input-character;          !!!cp (30);
1274          redo A;          $self->{ct} = {type => END_TAG_TOKEN,
1275        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{nc}),
1276          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1277          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1278            !!!next-input-character;
1279            redo A;
1280          } elsif ($self->{nc} == 0x003E) { # >
1281            !!!cp (31);
1282            !!!parse-error (type => 'empty end tag',
1283                            line => $self->{line_prev}, ## "<" in "</>"
1284                            column => $self->{column_prev} - 1);
1285            $self->{state} = DATA_STATE;
1286          !!!next-input-character;          !!!next-input-character;
1287          redo A;          redo A;
1288        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1289            !!!cp (32);
1290          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1291          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1292          # reconsume          # reconsume
1293    
1294          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1295                      line => $l, column => $c,
1296                     });
1297    
1298          redo A;          redo A;
1299        } else {        } else {
1300            !!!cp (33);
1301          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1302          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1303          ## $self->{next_input_character} is intentionally left as is          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1304          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1305        }                                    column => $self->{column_prev} - 1,
1306      } elsif ($self->{state} eq 'tag name') {                                   };
1307        if ($self->{next_input_character} == 0x0009 or # HT          ## NOTE: $self->{nc} is intentionally left as is.
1308            $self->{next_input_character} == 0x000A or # LF          ## Although the "anything else" case of the spec not explicitly
1309            $self->{next_input_character} == 0x000B or # VT          ## states that the next input character is to be reconsumed,
1310            $self->{next_input_character} == 0x000C or # FF          ## it will be included to the |data| of the comment token
1311            $self->{next_input_character} == 0x0020) { # SP          ## generated from the bogus end tag, as defined in the
1312          $self->{state} = 'before attribute name';          ## "bogus comment state" entry.
1313          !!!next-input-character;          redo A;
1314          redo A;        }
1315        } elsif ($self->{next_input_character} == 0x003E) { # >      } elsif ($self->{state} == CDATA_RCDATA_CLOSE_TAG_STATE) {
1316          if ($self->{current_token}->{type} eq 'start tag') {        my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1317            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};        if (length $ch) {
1318          } elsif ($self->{current_token}->{type} eq 'end tag') {          my $CH = $ch;
1319            $self->{content_model_flag} = 'PCDATA'; # MUST          $ch =~ tr/a-z/A-Z/;
1320            if ($self->{current_token}->{attributes}) {          my $nch = chr $self->{nc};
1321              !!!parse-error (type => 'end tag attribute');          if ($nch eq $ch or $nch eq $CH) {
1322            }            !!!cp (24);
1323              ## Stay in the state.
1324              $self->{s_kwd} .= $nch;
1325              !!!next-input-character;
1326              redo A;
1327          } else {          } else {
1328            die "$0: $self->{current_token}->{type}: Unknown token type";            !!!cp (25);
1329              $self->{state} = DATA_STATE;
1330              ## Reconsume.
1331              !!!emit ({type => CHARACTER_TOKEN,
1332                        data => '</' . $self->{s_kwd},
1333                        line => $self->{line_prev},
1334                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1335                       });
1336              redo A;
1337          }          }
1338          $self->{state} = 'data';        } else { # after "<{tag-name}"
1339          !!!next-input-character;          unless ({
1340                     0x0009 => 1, # HT
1341          !!!emit ($self->{current_token}); # start tag or end tag                   0x000A => 1, # LF
1342          undef $self->{current_token};                   0x000B => 1, # VT
1343                     0x000C => 1, # FF
1344          redo A;                   0x0020 => 1, # SP
1345        } elsif (0x0041 <= $self->{next_input_character} and                   0x003E => 1, # >
1346                 $self->{next_input_character} <= 0x005A) { # A..Z                   0x002F => 1, # /
1347          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);                   -1 => 1, # EOF
1348                    }->{$self->{nc}}) {
1349              !!!cp (26);
1350              ## Reconsume.
1351              $self->{state} = DATA_STATE;
1352              !!!emit ({type => CHARACTER_TOKEN,
1353                        data => '</' . $self->{s_kwd},
1354                        line => $self->{line_prev},
1355                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1356                       });
1357              redo A;
1358            } else {
1359              !!!cp (27);
1360              $self->{ct}
1361                  = {type => END_TAG_TOKEN,
1362                     tag_name => $self->{last_stag_name},
1363                     line => $self->{line_prev},
1364                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1365              $self->{state} = TAG_NAME_STATE;
1366              ## Reconsume.
1367              redo A;
1368            }
1369          }
1370        } elsif ($self->{state} == TAG_NAME_STATE) {
1371          if ($self->{nc} == 0x0009 or # HT
1372              $self->{nc} == 0x000A or # LF
1373              $self->{nc} == 0x000B or # VT
1374              $self->{nc} == 0x000C or # FF
1375              $self->{nc} == 0x0020) { # SP
1376            !!!cp (34);
1377            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1378            !!!next-input-character;
1379            redo A;
1380          } elsif ($self->{nc} == 0x003E) { # >
1381            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1382              !!!cp (35);
1383              $self->{last_stag_name} = $self->{ct}->{tag_name};
1384            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1385              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1386              #if ($self->{ct}->{attributes}) {
1387              #  ## NOTE: This should never be reached.
1388              #  !!! cp (36);
1389              #  !!! parse-error (type => 'end tag attribute');
1390              #} else {
1391                !!!cp (37);
1392              #}
1393            } else {
1394              die "$0: $self->{ct}->{type}: Unknown token type";
1395            }
1396            $self->{state} = DATA_STATE;
1397            !!!next-input-character;
1398    
1399            !!!emit ($self->{ct}); # start tag or end tag
1400    
1401            redo A;
1402          } elsif (0x0041 <= $self->{nc} and
1403                   $self->{nc} <= 0x005A) { # A..Z
1404            !!!cp (38);
1405            $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1406            # start tag or end tag            # start tag or end tag
1407          ## Stay in this state          ## Stay in this state
1408          !!!next-input-character;          !!!next-input-character;
1409          redo A;          redo A;
1410        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1411          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1412          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1413            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (39);
1414          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1415            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1416            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1417              !!!parse-error (type => 'end tag attribute');            #if ($self->{ct}->{attributes}) {
1418            }            #  ## NOTE: This state should never be reached.
1419              #  !!! cp (40);
1420              #  !!! parse-error (type => 'end tag attribute');
1421              #} else {
1422                !!!cp (41);
1423              #}
1424          } else {          } else {
1425            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1426          }          }
1427          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1428          # reconsume          # reconsume
1429    
1430          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1431    
1432          redo A;          redo A;
1433        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1434            !!!cp (42);
1435            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1436          !!!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  
1437          redo A;          redo A;
1438        } else {        } else {
1439          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1440            $self->{ct}->{tag_name} .= chr $self->{nc};
1441            # start tag or end tag            # start tag or end tag
1442          ## Stay in the state          ## Stay in the state
1443          !!!next-input-character;          !!!next-input-character;
1444          redo A;          redo A;
1445        }        }
1446      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1447        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1448            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1449            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1450            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1451            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1452            !!!cp (45);
1453          ## Stay in the state          ## Stay in the state
1454          !!!next-input-character;          !!!next-input-character;
1455          redo A;          redo A;
1456        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1457          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1458            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (46);
1459          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1460            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1461            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1462              if ($self->{ct}->{attributes}) {
1463                !!!cp (47);
1464              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1465              } else {
1466                !!!cp (48);
1467            }            }
1468          } else {          } else {
1469            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1470          }          }
1471          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1472          !!!next-input-character;          !!!next-input-character;
1473    
1474          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1475    
1476          redo A;          redo A;
1477        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1478                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1479          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1480                                value => ''};          $self->{ca}
1481          $self->{state} = 'attribute name';              = {name => chr ($self->{nc} + 0x0020),
1482                   value => '',
1483                   line => $self->{line}, column => $self->{column}};
1484            $self->{state} = ATTRIBUTE_NAME_STATE;
1485          !!!next-input-character;          !!!next-input-character;
1486          redo A;          redo A;
1487        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1488            !!!cp (50);
1489            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!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  
1491          redo A;          redo A;
1492        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
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 (52);
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}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1499              if ($self->{ct}->{attributes}) {
1500                !!!cp (53);
1501              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1502              } else {
1503                !!!cp (54);
1504            }            }
1505          } else {          } else {
1506            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1507          }          }
1508          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1509          # reconsume          # reconsume
1510    
1511          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1512    
1513          redo A;          redo A;
1514        } else {        } else {
1515          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1516                                value => ''};               0x0022 => 1, # "
1517          $self->{state} = 'attribute name';               0x0027 => 1, # '
1518                 0x003D => 1, # =
1519                }->{$self->{nc}}) {
1520              !!!cp (55);
1521              !!!parse-error (type => 'bad attribute name');
1522            } else {
1523              !!!cp (56);
1524            }
1525            $self->{ca}
1526                = {name => chr ($self->{nc}),
1527                   value => '',
1528                   line => $self->{line}, column => $self->{column}};
1529            $self->{state} = ATTRIBUTE_NAME_STATE;
1530          !!!next-input-character;          !!!next-input-character;
1531          redo A;          redo A;
1532        }        }
1533      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1534        my $before_leave = sub {        my $before_leave = sub {
1535          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1536              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1537            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1538            ## Discard $self->{current_attribute} # MUST            !!!parse-error (type => 'duplicate attribute', text => $self->{ca}->{name}, line => $self->{ca}->{line}, column => $self->{ca}->{column});
1539          } else {            ## Discard $self->{ca} # MUST
1540            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}          } else {
1541              = $self->{current_attribute};            !!!cp (58);
1542              $self->{ct}->{attributes}->{$self->{ca}->{name}}
1543                = $self->{ca};
1544          }          }
1545        }; # $before_leave        }; # $before_leave
1546    
1547        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1548            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1549            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1550            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1551            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1552            !!!cp (59);
1553          $before_leave->();          $before_leave->();
1554          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1555          !!!next-input-character;          !!!next-input-character;
1556          redo A;          redo A;
1557        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1558            !!!cp (60);
1559          $before_leave->();          $before_leave->();
1560          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1561          !!!next-input-character;          !!!next-input-character;
1562          redo A;          redo A;
1563        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1564          $before_leave->();          $before_leave->();
1565          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1566            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (61);
1567          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1568            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1569            if ($self->{current_token}->{attributes}) {            !!!cp (62);
1570              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1571              if ($self->{ct}->{attributes}) {
1572              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1573            }            }
1574          } else {          } else {
1575            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1576          }          }
1577          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1578          !!!next-input-character;          !!!next-input-character;
1579    
1580          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1581    
1582          redo A;          redo A;
1583        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1584                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1585          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1586            $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1587          ## Stay in the state          ## Stay in the state
1588          !!!next-input-character;          !!!next-input-character;
1589          redo A;          redo A;
1590        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1591            !!!cp (64);
1592          $before_leave->();          $before_leave->();
1593            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1594          !!!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  
1595          redo A;          redo A;
1596        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1597          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1598          $before_leave->();          $before_leave->();
1599          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1600            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (66);
1601          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1602            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1603            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1604              if ($self->{ct}->{attributes}) {
1605                !!!cp (67);
1606              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1607              } else {
1608                ## NOTE: This state should never be reached.
1609                !!!cp (68);
1610            }            }
1611          } else {          } else {
1612            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1613          }          }
1614          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1615          # reconsume          # reconsume
1616    
1617          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1618    
1619          redo A;          redo A;
1620        } else {        } else {
1621          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x0022 or # "
1622                $self->{nc} == 0x0027) { # '
1623              !!!cp (69);
1624              !!!parse-error (type => 'bad attribute name');
1625            } else {
1626              !!!cp (70);
1627            }
1628            $self->{ca}->{name} .= chr ($self->{nc});
1629          ## Stay in the state          ## Stay in the state
1630          !!!next-input-character;          !!!next-input-character;
1631          redo A;          redo A;
1632        }        }
1633      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1634        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1635            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1636            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1637            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1638            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1639            !!!cp (71);
1640          ## Stay in the state          ## Stay in the state
1641          !!!next-input-character;          !!!next-input-character;
1642          redo A;          redo A;
1643        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1644          $self->{state} = 'before attribute value';          !!!cp (72);
1645            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1646          !!!next-input-character;          !!!next-input-character;
1647          redo A;          redo A;
1648        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1649          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1650            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (73);
1651          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1652            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1653            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1654              if ($self->{ct}->{attributes}) {
1655                !!!cp (74);
1656              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1657              } else {
1658                ## NOTE: This state should never be reached.
1659                !!!cp (75);
1660            }            }
1661          } else {          } else {
1662            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1663          }          }
1664          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1665          !!!next-input-character;          !!!next-input-character;
1666    
1667          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1668    
1669          redo A;          redo A;
1670        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1671                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1672          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1673                                value => ''};          $self->{ca}
1674          $self->{state} = 'attribute name';              = {name => chr ($self->{nc} + 0x0020),
1675                   value => '',
1676                   line => $self->{line}, column => $self->{column}};
1677            $self->{state} = ATTRIBUTE_NAME_STATE;
1678          !!!next-input-character;          !!!next-input-character;
1679          redo A;          redo A;
1680        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1681            !!!cp (77);
1682            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1683          !!!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  
1684          redo A;          redo A;
1685        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1686          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1687          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1688            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (79);
1689          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1690            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1691            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1692              if ($self->{ct}->{attributes}) {
1693                !!!cp (80);
1694              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1695              } else {
1696                ## NOTE: This state should never be reached.
1697                !!!cp (81);
1698            }            }
1699          } else {          } else {
1700            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1701          }          }
1702          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1703          # reconsume          # reconsume
1704    
1705          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1706    
1707          redo A;          redo A;
1708        } else {        } else {
1709          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{nc} == 0x0022 or # "
1710                                value => ''};              $self->{nc} == 0x0027) { # '
1711          $self->{state} = 'attribute name';            !!!cp (78);
1712              !!!parse-error (type => 'bad attribute name');
1713            } else {
1714              !!!cp (82);
1715            }
1716            $self->{ca}
1717                = {name => chr ($self->{nc}),
1718                   value => '',
1719                   line => $self->{line}, column => $self->{column}};
1720            $self->{state} = ATTRIBUTE_NAME_STATE;
1721          !!!next-input-character;          !!!next-input-character;
1722          redo A;                  redo A;        
1723        }        }
1724      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1725        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1726            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1727            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1728            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1729            $self->{next_input_character} == 0x0020) { # SP                  $self->{nc} == 0x0020) { # SP      
1730            !!!cp (83);
1731          ## Stay in the state          ## Stay in the state
1732          !!!next-input-character;          !!!next-input-character;
1733          redo A;          redo A;
1734        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1735          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1736            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1737          !!!next-input-character;          !!!next-input-character;
1738          redo A;          redo A;
1739        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1740          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1741            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1742          ## reconsume          ## reconsume
1743          redo A;          redo A;
1744        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1745          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1746            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1747          !!!next-input-character;          !!!next-input-character;
1748          redo A;          redo A;
1749        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1750          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1751            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1752          } elsif ($self->{current_token}->{type} eq 'end tag') {            !!!cp (87);
1753            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{last_stag_name} = $self->{ct}->{tag_name};
1754            if ($self->{current_token}->{attributes}) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1755              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1756              if ($self->{ct}->{attributes}) {
1757                !!!cp (88);
1758              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1759              } else {
1760                ## NOTE: This state should never be reached.
1761                !!!cp (89);
1762            }            }
1763          } else {          } else {
1764            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1765          }          }
1766          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1767          !!!next-input-character;          !!!next-input-character;
1768    
1769          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1770    
1771          redo A;          redo A;
1772        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1773          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1774          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1775            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (90);
1776          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1777            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1778            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1779              if ($self->{ct}->{attributes}) {
1780                !!!cp (91);
1781              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1782              } else {
1783                ## NOTE: This state should never be reached.
1784                !!!cp (92);
1785            }            }
1786          } else {          } else {
1787            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1788          }          }
1789          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1790          ## reconsume          ## reconsume
1791    
1792          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1793    
1794          redo A;          redo A;
1795        } else {        } else {
1796          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x003D) { # =
1797          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1798              !!!parse-error (type => 'bad attribute value');
1799            } else {
1800              !!!cp (94);
1801            }
1802            $self->{ca}->{value} .= chr ($self->{nc});
1803            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1804          !!!next-input-character;          !!!next-input-character;
1805          redo A;          redo A;
1806        }        }
1807      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1808        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1809          $self->{state} = 'before attribute name';          !!!cp (95);
1810            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1811          !!!next-input-character;          !!!next-input-character;
1812          redo A;          redo A;
1813        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1814          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1815          $self->{state} = 'entity in attribute value';          ## NOTE: In the spec, the tokenizer is switched to the
1816            ## "entity in attribute value state".  In this implementation, the
1817            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1818            ## implementation of the "consume a character reference" algorithm.
1819            $self->{prev_state} = $self->{state};
1820            $self->{entity_add} = 0x0022; # "
1821            $self->{state} = ENTITY_STATE;
1822          !!!next-input-character;          !!!next-input-character;
1823          redo A;          redo A;
1824        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1825          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1826          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1827            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (97);
1828          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1829            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1830            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1831              if ($self->{ct}->{attributes}) {
1832                !!!cp (98);
1833              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1834              } else {
1835                ## NOTE: This state should never be reached.
1836                !!!cp (99);
1837            }            }
1838          } else {          } else {
1839            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1840          }          }
1841          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1842          ## reconsume          ## reconsume
1843    
1844          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1845    
1846          redo A;          redo A;
1847        } else {        } else {
1848          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1849            $self->{ca}->{value} .= chr ($self->{nc});
1850            $self->{read_until}->($self->{ca}->{value},
1851                                  q["&],
1852                                  length $self->{ca}->{value});
1853    
1854          ## Stay in the state          ## Stay in the state
1855          !!!next-input-character;          !!!next-input-character;
1856          redo A;          redo A;
1857        }        }
1858      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1859        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1860          $self->{state} = 'before attribute name';          !!!cp (101);
1861            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1862            !!!next-input-character;
1863            redo A;
1864          } elsif ($self->{nc} == 0x0026) { # &
1865            !!!cp (102);
1866            ## NOTE: In the spec, the tokenizer is switched to the
1867            ## "entity in attribute value state".  In this implementation, the
1868            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1869            ## implementation of the "consume a character reference" algorithm.
1870            $self->{entity_add} = 0x0027; # '
1871            $self->{prev_state} = $self->{state};
1872            $self->{state} = ENTITY_STATE;
1873          !!!next-input-character;          !!!next-input-character;
1874          redo A;          redo A;
1875        } 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) {  
1876          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1877          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1878            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (103);
1879          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1880            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1881            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1882              if ($self->{ct}->{attributes}) {
1883                !!!cp (104);
1884              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1885              } else {
1886                ## NOTE: This state should never be reached.
1887                !!!cp (105);
1888            }            }
1889          } else {          } else {
1890            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1891          }          }
1892          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1893          ## reconsume          ## reconsume
1894    
1895          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1896    
1897          redo A;          redo A;
1898        } else {        } else {
1899          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1900            $self->{ca}->{value} .= chr ($self->{nc});
1901            $self->{read_until}->($self->{ca}->{value},
1902                                  q['&],
1903                                  length $self->{ca}->{value});
1904    
1905          ## Stay in the state          ## Stay in the state
1906          !!!next-input-character;          !!!next-input-character;
1907          redo A;          redo A;
1908        }        }
1909      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1910        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1911            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1912            $self->{next_input_character} == 0x000B or # HT            $self->{nc} == 0x000B or # HT
1913            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1914            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1915          $self->{state} = 'before attribute name';          !!!cp (107);
1916          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1917          redo A;          !!!next-input-character;
1918        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1919          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{nc} == 0x0026) { # &
1920          $self->{state} = 'entity in attribute value';          !!!cp (108);
1921          !!!next-input-character;          ## NOTE: In the spec, the tokenizer is switched to the
1922          redo A;          ## "entity in attribute value state".  In this implementation, the
1923        } elsif ($self->{next_input_character} == 0x003E) { # >          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1924          if ($self->{current_token}->{type} eq 'start tag') {          ## implementation of the "consume a character reference" algorithm.
1925            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          $self->{entity_add} = -1;
1926          } elsif ($self->{current_token}->{type} eq 'end tag') {          $self->{prev_state} = $self->{state};
1927            $self->{content_model_flag} = 'PCDATA'; # MUST          $self->{state} = ENTITY_STATE;
1928            if ($self->{current_token}->{attributes}) {          !!!next-input-character;
1929            redo A;
1930          } elsif ($self->{nc} == 0x003E) { # >
1931            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1932              !!!cp (109);
1933              $self->{last_stag_name} = $self->{ct}->{tag_name};
1934            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1935              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1936              if ($self->{ct}->{attributes}) {
1937                !!!cp (110);
1938              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1939              } else {
1940                ## NOTE: This state should never be reached.
1941                !!!cp (111);
1942            }            }
1943          } else {          } else {
1944            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1945          }          }
1946          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1947          !!!next-input-character;          !!!next-input-character;
1948    
1949          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1950    
1951          redo A;          redo A;
1952        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{nc} == -1) {
                $self->{next_input_character} == -1) {  
1953          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1954          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1955            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            !!!cp (112);
1956          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{last_stag_name} = $self->{ct}->{tag_name};
1957            $self->{content_model_flag} = 'PCDATA'; # MUST          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1958            if ($self->{current_token}->{attributes}) {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1959              if ($self->{ct}->{attributes}) {
1960                !!!cp (113);
1961              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1962              } else {
1963                ## NOTE: This state should never be reached.
1964                !!!cp (114);
1965            }            }
1966          } else {          } else {
1967            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1968          }          }
1969          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1970          ## reconsume          ## reconsume
1971    
1972          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
         undef $self->{current_token};  
1973    
1974          redo A;          redo A;
1975        } else {        } else {
1976          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1977                 0x0022 => 1, # "
1978                 0x0027 => 1, # '
1979                 0x003D => 1, # =
1980                }->{$self->{nc}}) {
1981              !!!cp (115);
1982              !!!parse-error (type => 'bad attribute value');
1983            } else {
1984              !!!cp (116);
1985            }
1986            $self->{ca}->{value} .= chr ($self->{nc});
1987            $self->{read_until}->($self->{ca}->{value},
1988                                  q["'=& >],
1989                                  length $self->{ca}->{value});
1990    
1991          ## Stay in the state          ## Stay in the state
1992          !!!next-input-character;          !!!next-input-character;
1993          redo A;          redo A;
1994        }        }
1995      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1996        my $token = $self->_tokenize_attempt_to_consume_an_entity;        if ($self->{nc} == 0x0009 or # HT
1997              $self->{nc} == 0x000A or # LF
1998              $self->{nc} == 0x000B or # VT
1999              $self->{nc} == 0x000C or # FF
2000              $self->{nc} == 0x0020) { # SP
2001            !!!cp (118);
2002            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2003            !!!next-input-character;
2004            redo A;
2005          } elsif ($self->{nc} == 0x003E) { # >
2006            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2007              !!!cp (119);
2008              $self->{last_stag_name} = $self->{ct}->{tag_name};
2009            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2010              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2011              if ($self->{ct}->{attributes}) {
2012                !!!cp (120);
2013                !!!parse-error (type => 'end tag attribute');
2014              } else {
2015                ## NOTE: This state should never be reached.
2016                !!!cp (121);
2017              }
2018            } else {
2019              die "$0: $self->{ct}->{type}: Unknown token type";
2020            }
2021            $self->{state} = DATA_STATE;
2022            !!!next-input-character;
2023    
2024        unless (defined $token) {          !!!emit ($self->{ct}); # start tag or end tag
2025          $self->{current_attribute}->{value} .= '&';  
2026            redo A;
2027          } elsif ($self->{nc} == 0x002F) { # /
2028            !!!cp (122);
2029            $self->{state} = SELF_CLOSING_START_TAG_STATE;
2030            !!!next-input-character;
2031            redo A;
2032          } elsif ($self->{nc} == -1) {
2033            !!!parse-error (type => 'unclosed tag');
2034            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2035              !!!cp (122.3);
2036              $self->{last_stag_name} = $self->{ct}->{tag_name};
2037            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2038              if ($self->{ct}->{attributes}) {
2039                !!!cp (122.1);
2040                !!!parse-error (type => 'end tag attribute');
2041              } else {
2042                ## NOTE: This state should never be reached.
2043                !!!cp (122.2);
2044              }
2045            } else {
2046              die "$0: $self->{ct}->{type}: Unknown token type";
2047            }
2048            $self->{state} = DATA_STATE;
2049            ## Reconsume.
2050            !!!emit ($self->{ct}); # start tag or end tag
2051            redo A;
2052        } else {        } else {
2053          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
2054          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
2055            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2056            ## reconsume
2057            redo A;
2058        }        }
2059        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
2060          if ($self->{nc} == 0x003E) { # >
2061            if ($self->{ct}->{type} == END_TAG_TOKEN) {
2062              !!!cp ('124.2');
2063              !!!parse-error (type => 'nestc', token => $self->{ct});
2064              ## TODO: Different type than slash in start tag
2065              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2066              if ($self->{ct}->{attributes}) {
2067                !!!cp ('124.4');
2068                !!!parse-error (type => 'end tag attribute');
2069              } else {
2070                !!!cp ('124.5');
2071              }
2072              ## TODO: Test |<title></title/>|
2073            } else {
2074              !!!cp ('124.3');
2075              $self->{self_closing} = 1;
2076            }
2077    
2078        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
2079        # 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  
2080    
2081            !!!emit ($token);          !!!emit ($self->{ct}); # start tag or end tag
2082    
2083            redo A;          redo A;
2084          } elsif ($self->{nc} == -1) {
2085            !!!parse-error (type => 'unclosed tag');
2086            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2087              !!!cp (124.7);
2088              $self->{last_stag_name} = $self->{ct}->{tag_name};
2089            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2090              if ($self->{ct}->{attributes}) {
2091                !!!cp (124.5);
2092                !!!parse-error (type => 'end tag attribute');
2093              } else {
2094                ## NOTE: This state should never be reached.
2095                !!!cp (124.6);
2096              }
2097          } else {          } else {
2098            $token->{data} .= chr ($self->{next_input_character});            die "$0: $self->{ct}->{type}: Unknown token type";
           !!!next-input-character;  
           redo BC;  
2099          }          }
2100        } # BC          $self->{state} = DATA_STATE;
2101      } elsif ($self->{state} eq 'markup declaration open') {          ## Reconsume.
2102            !!!emit ($self->{ct}); # start tag or end tag
2103            redo A;
2104          } else {
2105            !!!cp ('124.4');
2106            !!!parse-error (type => 'nestc');
2107            ## TODO: This error type is wrong.
2108            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2109            ## Reconsume.
2110            redo A;
2111          }
2112        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2113        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2114    
2115        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2116        push @next_char, $self->{next_input_character};        ## consumes characters one-by-one basis.
2117                
2118        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x003E) { # >
2119            !!!cp (124);
2120            $self->{state} = DATA_STATE;
2121          !!!next-input-character;          !!!next-input-character;
2122          push @next_char, $self->{next_input_character};  
2123          if ($self->{next_input_character} == 0x002D) { # -          !!!emit ($self->{ct}); # comment
2124            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2125            $self->{state} = 'comment';        } elsif ($self->{nc} == -1) {
2126            !!!next-input-character;          !!!cp (125);
2127            redo A;          $self->{state} = DATA_STATE;
2128          }          ## reconsume
2129        } elsif ($self->{next_input_character} == 0x0044 or # D  
2130                 $self->{next_input_character} == 0x0064) { # d          !!!emit ($self->{ct}); # comment
2131            redo A;
2132          } else {
2133            !!!cp (126);
2134            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2135            $self->{read_until}->($self->{ct}->{data},
2136                                  q[>],
2137                                  length $self->{ct}->{data});
2138    
2139            ## Stay in the state.
2140          !!!next-input-character;          !!!next-input-character;
2141          push @next_char, $self->{next_input_character};          redo A;
2142          if ($self->{next_input_character} == 0x004F or # O        }
2143              $self->{next_input_character} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2144            !!!next-input-character;        ## (only happen if PCDATA state)
2145            push @next_char, $self->{next_input_character};        
2146            if ($self->{next_input_character} == 0x0043 or # C        if ($self->{nc} == 0x002D) { # -
2147                $self->{next_input_character} == 0x0063) { # c          !!!cp (133);
2148              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2149              push @next_char, $self->{next_input_character};          !!!next-input-character;
2150              if ($self->{next_input_character} == 0x0054 or # T          redo A;
2151                  $self->{next_input_character} == 0x0074) { # t        } elsif ($self->{nc} == 0x0044 or # D
2152                !!!next-input-character;                 $self->{nc} == 0x0064) { # d
2153                push @next_char, $self->{next_input_character};          ## ASCII case-insensitive.
2154                if ($self->{next_input_character} == 0x0059 or # Y          !!!cp (130);
2155                    $self->{next_input_character} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2156                  !!!next-input-character;          $self->{s_kwd} = chr $self->{nc};
2157                  push @next_char, $self->{next_input_character};          !!!next-input-character;
2158                  if ($self->{next_input_character} == 0x0050 or # P          redo A;
2159                      $self->{next_input_character} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2160                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2161                    push @next_char, $self->{next_input_character};                 $self->{nc} == 0x005B) { # [
2162                    if ($self->{next_input_character} == 0x0045 or # E          !!!cp (135.4);                
2163                        $self->{next_input_character} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2164                      ## ISSUE: What a stupid code this is!          $self->{s_kwd} = '[';
2165                      $self->{state} = 'DOCTYPE';          !!!next-input-character;
2166                      !!!next-input-character;          redo A;
2167                      redo A;        } else {
2168                    }          !!!cp (136);
                 }  
               }  
             }  
           }  
         }  
2169        }        }
2170    
2171        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment',
2172        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2173        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2174        $self->{state} = 'bogus comment';        ## Reconsume.
2175          $self->{state} = BOGUS_COMMENT_STATE;
2176          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2177                                    line => $self->{line_prev},
2178                                    column => $self->{column_prev} - 1,
2179                                   };
2180        redo A;        redo A;
2181              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2182        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2183        ## 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);
2184      } elsif ($self->{state} eq 'comment') {          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2185        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2186          $self->{state} = 'comment dash';                                    column => $self->{column_prev} - 2,
2187                                     };
2188            $self->{state} = COMMENT_START_STATE;
2189            !!!next-input-character;
2190            redo A;
2191          } else {
2192            !!!cp (128);
2193            !!!parse-error (type => 'bogus comment',
2194                            line => $self->{line_prev},
2195                            column => $self->{column_prev} - 2);
2196            $self->{state} = BOGUS_COMMENT_STATE;
2197            ## Reconsume.
2198            $self->{ct} = {type => COMMENT_TOKEN,
2199                                      data => '-',
2200                                      line => $self->{line_prev},
2201                                      column => $self->{column_prev} - 2,
2202                                     };
2203            redo A;
2204          }
2205        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2206          ## ASCII case-insensitive.
2207          if ($self->{nc} == [
2208                undef,
2209                0x004F, # O
2210                0x0043, # C
2211                0x0054, # T
2212                0x0059, # Y
2213                0x0050, # P
2214              ]->[length $self->{s_kwd}] or
2215              $self->{nc} == [
2216                undef,
2217                0x006F, # o
2218                0x0063, # c
2219                0x0074, # t
2220                0x0079, # y
2221                0x0070, # p
2222              ]->[length $self->{s_kwd}]) {
2223            !!!cp (131);
2224            ## Stay in the state.
2225            $self->{s_kwd} .= chr $self->{nc};
2226            !!!next-input-character;
2227            redo A;
2228          } elsif ((length $self->{s_kwd}) == 6 and
2229                   ($self->{nc} == 0x0045 or # E
2230                    $self->{nc} == 0x0065)) { # e
2231            !!!cp (129);
2232            $self->{state} = DOCTYPE_STATE;
2233            $self->{ct} = {type => DOCTYPE_TOKEN,
2234                                      quirks => 1,
2235                                      line => $self->{line_prev},
2236                                      column => $self->{column_prev} - 7,
2237                                     };
2238            !!!next-input-character;
2239            redo A;
2240          } else {
2241            !!!cp (132);        
2242            !!!parse-error (type => 'bogus comment',
2243                            line => $self->{line_prev},
2244                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2245            $self->{state} = BOGUS_COMMENT_STATE;
2246            ## Reconsume.
2247            $self->{ct} = {type => COMMENT_TOKEN,
2248                                      data => $self->{s_kwd},
2249                                      line => $self->{line_prev},
2250                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2251                                     };
2252            redo A;
2253          }
2254        } elsif ($self->{state} == MD_CDATA_STATE) {
2255          if ($self->{nc} == {
2256                '[' => 0x0043, # C
2257                '[C' => 0x0044, # D
2258                '[CD' => 0x0041, # A
2259                '[CDA' => 0x0054, # T
2260                '[CDAT' => 0x0041, # A
2261              }->{$self->{s_kwd}}) {
2262            !!!cp (135.1);
2263            ## Stay in the state.
2264            $self->{s_kwd} .= chr $self->{nc};
2265            !!!next-input-character;
2266            redo A;
2267          } elsif ($self->{s_kwd} eq '[CDATA' and
2268                   $self->{nc} == 0x005B) { # [
2269            !!!cp (135.2);
2270            $self->{ct} = {type => CHARACTER_TOKEN,
2271                                      data => '',
2272                                      line => $self->{line_prev},
2273                                      column => $self->{column_prev} - 7};
2274            $self->{state} = CDATA_SECTION_STATE;
2275            !!!next-input-character;
2276            redo A;
2277          } else {
2278            !!!cp (135.3);
2279            !!!parse-error (type => 'bogus comment',
2280                            line => $self->{line_prev},
2281                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2282            $self->{state} = BOGUS_COMMENT_STATE;
2283            ## Reconsume.
2284            $self->{ct} = {type => COMMENT_TOKEN,
2285                                      data => $self->{s_kwd},
2286                                      line => $self->{line_prev},
2287                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2288                                     };
2289            redo A;
2290          }
2291        } elsif ($self->{state} == COMMENT_START_STATE) {
2292          if ($self->{nc} == 0x002D) { # -
2293            !!!cp (137);
2294            $self->{state} = COMMENT_START_DASH_STATE;
2295            !!!next-input-character;
2296            redo A;
2297          } elsif ($self->{nc} == 0x003E) { # >
2298            !!!cp (138);
2299            !!!parse-error (type => 'bogus comment');
2300            $self->{state} = DATA_STATE;
2301            !!!next-input-character;
2302    
2303            !!!emit ($self->{ct}); # comment
2304    
2305            redo A;
2306          } elsif ($self->{nc} == -1) {
2307            !!!cp (139);
2308            !!!parse-error (type => 'unclosed comment');
2309            $self->{state} = DATA_STATE;
2310            ## reconsume
2311    
2312            !!!emit ($self->{ct}); # comment
2313    
2314            redo A;
2315          } else {
2316            !!!cp (140);
2317            $self->{ct}->{data} # comment
2318                .= chr ($self->{nc});
2319            $self->{state} = COMMENT_STATE;
2320            !!!next-input-character;
2321            redo A;
2322          }
2323        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2324          if ($self->{nc} == 0x002D) { # -
2325            !!!cp (141);
2326            $self->{state} = COMMENT_END_STATE;
2327          !!!next-input-character;          !!!next-input-character;
2328          redo A;          redo A;
2329        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2330            !!!cp (142);
2331            !!!parse-error (type => 'bogus comment');
2332            $self->{state} = DATA_STATE;
2333            !!!next-input-character;
2334    
2335            !!!emit ($self->{ct}); # comment
2336    
2337            redo A;
2338          } elsif ($self->{nc} == -1) {
2339            !!!cp (143);
2340          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2341          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2342          ## reconsume          ## reconsume
2343    
2344          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
         undef $self->{current_token};  
2345    
2346          redo A;          redo A;
2347        } else {        } else {
2348          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (144);
2349            $self->{ct}->{data} # comment
2350                .= '-' . chr ($self->{nc});
2351            $self->{state} = COMMENT_STATE;
2352            !!!next-input-character;
2353            redo A;
2354          }
2355        } elsif ($self->{state} == COMMENT_STATE) {
2356          if ($self->{nc} == 0x002D) { # -
2357            !!!cp (145);
2358            $self->{state} = COMMENT_END_DASH_STATE;
2359            !!!next-input-character;
2360            redo A;
2361          } elsif ($self->{nc} == -1) {
2362            !!!cp (146);
2363            !!!parse-error (type => 'unclosed comment');
2364            $self->{state} = DATA_STATE;
2365            ## reconsume
2366    
2367            !!!emit ($self->{ct}); # comment
2368    
2369            redo A;
2370          } else {
2371            !!!cp (147);
2372            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2373            $self->{read_until}->($self->{ct}->{data},
2374                                  q[-],
2375                                  length $self->{ct}->{data});
2376    
2377          ## Stay in the state          ## Stay in the state
2378          !!!next-input-character;          !!!next-input-character;
2379          redo A;          redo A;
2380        }        }
2381      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2382        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2383          $self->{state} = 'comment end';          !!!cp (148);
2384            $self->{state} = COMMENT_END_STATE;
2385          !!!next-input-character;          !!!next-input-character;
2386          redo A;          redo A;
2387        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2388            !!!cp (149);
2389          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2390          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2391          ## reconsume          ## reconsume
2392    
2393          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
         undef $self->{current_token};  
2394    
2395          redo A;          redo A;
2396        } else {        } else {
2397          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2398          $self->{state} = 'comment';          $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2399            $self->{state} = COMMENT_STATE;
2400          !!!next-input-character;          !!!next-input-character;
2401          redo A;          redo A;
2402        }        }
2403      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2404        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2405          $self->{state} = 'data';          !!!cp (151);
2406            $self->{state} = DATA_STATE;
2407          !!!next-input-character;          !!!next-input-character;
2408    
2409          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
         undef $self->{current_token};  
2410    
2411          redo A;          redo A;
2412        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2413          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2414          $self->{current_token}->{data} .= '-'; # comment          !!!parse-error (type => 'dash in comment',
2415                            line => $self->{line_prev},
2416                            column => $self->{column_prev});
2417            $self->{ct}->{data} .= '-'; # comment
2418          ## Stay in the state          ## Stay in the state
2419          !!!next-input-character;          !!!next-input-character;
2420          redo A;          redo A;
2421        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2422            !!!cp (153);
2423          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2424          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2425          ## reconsume          ## reconsume
2426    
2427          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
         undef $self->{current_token};  
2428    
2429          redo A;          redo A;
2430        } else {        } else {
2431          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2432          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2433          $self->{state} = 'comment';                          line => $self->{line_prev},
2434                            column => $self->{column_prev});
2435            $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2436            $self->{state} = COMMENT_STATE;
2437          !!!next-input-character;          !!!next-input-character;
2438          redo A;          redo A;
2439        }        }
2440      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2441        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2442            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2443            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2444            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2445            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2446          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2447            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2448          !!!next-input-character;          !!!next-input-character;
2449          redo A;          redo A;
2450        } else {        } else {
2451            !!!cp (156);
2452          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2453          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2454          ## reconsume          ## reconsume
2455          redo A;          redo A;
2456        }        }
2457      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2458        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2459            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2460            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2461            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2462            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2463            !!!cp (157);
2464          ## Stay in the state          ## Stay in the state
2465          !!!next-input-character;          !!!next-input-character;
2466          redo A;          redo A;
2467        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{nc} == 0x003E) { # >
2468                 $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) { # >  
2469          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2470          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2471          !!!next-input-character;          !!!next-input-character;
2472    
2473          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2474    
2475          redo A;          redo A;
2476        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2477            !!!cp (159);
2478          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2479          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2480          ## reconsume          ## reconsume
2481    
2482          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2483    
2484          redo A;          redo A;
2485        } else {        } else {
2486          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
2487                            name => chr ($self->{next_input_character}),          $self->{ct}->{name} = chr $self->{nc};
2488                            error => 1};          delete $self->{ct}->{quirks};
2489  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2490          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2491          !!!next-input-character;          !!!next-input-character;
2492          redo A;          redo A;
2493        }        }
2494      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2495        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
2496            $self->{next_input_character} == 0x000A or # LF        if ($self->{nc} == 0x0009 or # HT
2497            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000A or # LF
2498            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000B or # VT
2499            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x000C or # FF
2500          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{nc} == 0x0020) { # SP
2501          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2502            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2503          !!!next-input-character;          !!!next-input-character;
2504          redo A;          redo A;
2505        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2506          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
2507          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2508          !!!next-input-character;          !!!next-input-character;
2509    
2510          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
         undef $self->{current_token};  
2511    
2512          redo A;          redo A;
2513        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{nc} == -1) {
2514                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
2515          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2516          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2517            ## reconsume
2518    
2519            $self->{ct}->{quirks} = 1;
2520            !!!emit ($self->{ct}); # DOCTYPE
2521    
2522            redo A;
2523          } else {
2524            !!!cp (164);
2525            $self->{ct}->{name}
2526              .= chr ($self->{nc}); # DOCTYPE
2527            ## Stay in the state
2528            !!!next-input-character;
2529            redo A;
2530          }
2531        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2532          if ($self->{nc} == 0x0009 or # HT
2533              $self->{nc} == 0x000A or # LF
2534              $self->{nc} == 0x000B or # VT
2535              $self->{nc} == 0x000C or # FF
2536              $self->{nc} == 0x0020) { # SP
2537            !!!cp (165);
2538          ## Stay in the state          ## Stay in the state
2539          !!!next-input-character;          !!!next-input-character;
2540          redo A;          redo A;
2541        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2542            !!!cp (166);
2543            $self->{state} = DATA_STATE;
2544            !!!next-input-character;
2545    
2546            !!!emit ($self->{ct}); # DOCTYPE
2547    
2548            redo A;
2549          } elsif ($self->{nc} == -1) {
2550            !!!cp (167);
2551          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2552          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2553          ## reconsume          ## reconsume
2554    
2555          !!!emit ($self->{current_token});          $self->{ct}->{quirks} = 1;
2556          undef $self->{current_token};          !!!emit ($self->{ct}); # DOCTYPE
2557    
2558            redo A;
2559          } elsif ($self->{nc} == 0x0050 or # P
2560                   $self->{nc} == 0x0070) { # p
2561            $self->{state} = PUBLIC_STATE;
2562            $self->{s_kwd} = chr $self->{nc};
2563            !!!next-input-character;
2564            redo A;
2565          } elsif ($self->{nc} == 0x0053 or # S
2566                   $self->{nc} == 0x0073) { # s
2567            $self->{state} = SYSTEM_STATE;
2568            $self->{s_kwd} = chr $self->{nc};
2569            !!!next-input-character;
2570            redo A;
2571          } else {
2572            !!!cp (180);
2573            !!!parse-error (type => 'string after DOCTYPE name');
2574            $self->{ct}->{quirks} = 1;
2575    
2576            $self->{state} = BOGUS_DOCTYPE_STATE;
2577            !!!next-input-character;
2578            redo A;
2579          }
2580        } elsif ($self->{state} == PUBLIC_STATE) {
2581          ## ASCII case-insensitive
2582          if ($self->{nc} == [
2583                undef,
2584                0x0055, # U
2585                0x0042, # B
2586                0x004C, # L
2587                0x0049, # I
2588              ]->[length $self->{s_kwd}] or
2589              $self->{nc} == [
2590                undef,
2591                0x0075, # u
2592                0x0062, # b
2593                0x006C, # l
2594                0x0069, # i
2595              ]->[length $self->{s_kwd}]) {
2596            !!!cp (175);
2597            ## Stay in the state.
2598            $self->{s_kwd} .= chr $self->{nc};
2599            !!!next-input-character;
2600            redo A;
2601          } elsif ((length $self->{s_kwd}) == 5 and
2602                   ($self->{nc} == 0x0043 or # C
2603                    $self->{nc} == 0x0063)) { # c
2604            !!!cp (168);
2605            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2606            !!!next-input-character;
2607          redo A;          redo A;
2608        } else {        } else {
2609          $self->{current_token}->{name}          !!!cp (169);
2610            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after DOCTYPE name',
2611          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');                          line => $self->{line_prev},
2612                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2613            $self->{ct}->{quirks} = 1;
2614    
2615            $self->{state} = BOGUS_DOCTYPE_STATE;
2616            ## Reconsume.
2617            redo A;
2618          }
2619        } elsif ($self->{state} == SYSTEM_STATE) {
2620          ## ASCII case-insensitive
2621          if ($self->{nc} == [
2622                undef,
2623                0x0059, # Y
2624                0x0053, # S
2625                0x0054, # T
2626                0x0045, # E
2627              ]->[length $self->{s_kwd}] or
2628              $self->{nc} == [
2629                undef,
2630                0x0079, # y
2631                0x0073, # s
2632                0x0074, # t
2633                0x0065, # e
2634              ]->[length $self->{s_kwd}]) {
2635            !!!cp (170);
2636            ## Stay in the state.
2637            $self->{s_kwd} .= chr $self->{nc};
2638            !!!next-input-character;
2639            redo A;
2640          } elsif ((length $self->{s_kwd}) == 5 and
2641                   ($self->{nc} == 0x004D or # M
2642                    $self->{nc} == 0x006D)) { # m
2643            !!!cp (171);
2644            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2645            !!!next-input-character;
2646            redo A;
2647          } else {
2648            !!!cp (172);
2649            !!!parse-error (type => 'string after DOCTYPE name',
2650                            line => $self->{line_prev},
2651                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2652            $self->{ct}->{quirks} = 1;
2653    
2654            $self->{state} = BOGUS_DOCTYPE_STATE;
2655            ## Reconsume.
2656            redo A;
2657          }
2658        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2659          if ({
2660                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2661                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2662              }->{$self->{nc}}) {
2663            !!!cp (181);
2664          ## Stay in the state          ## Stay in the state
2665          !!!next-input-character;          !!!next-input-character;
2666          redo A;          redo A;
2667          } elsif ($self->{nc} eq 0x0022) { # "
2668            !!!cp (182);
2669            $self->{ct}->{pubid} = ''; # DOCTYPE
2670            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2671            !!!next-input-character;
2672            redo A;
2673          } elsif ($self->{nc} eq 0x0027) { # '
2674            !!!cp (183);
2675            $self->{ct}->{pubid} = ''; # DOCTYPE
2676            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2677            !!!next-input-character;
2678            redo A;
2679          } elsif ($self->{nc} eq 0x003E) { # >
2680            !!!cp (184);
2681            !!!parse-error (type => 'no PUBLIC literal');
2682    
2683            $self->{state} = DATA_STATE;
2684            !!!next-input-character;
2685    
2686            $self->{ct}->{quirks} = 1;
2687            !!!emit ($self->{ct}); # DOCTYPE
2688    
2689            redo A;
2690          } elsif ($self->{nc} == -1) {
2691            !!!cp (185);
2692            !!!parse-error (type => 'unclosed DOCTYPE');
2693    
2694            $self->{state} = DATA_STATE;
2695            ## reconsume
2696    
2697            $self->{ct}->{quirks} = 1;
2698            !!!emit ($self->{ct}); # DOCTYPE
2699    
2700            redo A;
2701          } else {
2702            !!!cp (186);
2703            !!!parse-error (type => 'string after PUBLIC');
2704            $self->{ct}->{quirks} = 1;
2705    
2706            $self->{state} = BOGUS_DOCTYPE_STATE;
2707            !!!next-input-character;
2708            redo A;
2709        }        }
2710      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2711        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0022) { # "
2712            $self->{next_input_character} == 0x000A or # LF          !!!cp (187);
2713            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2714            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2715            $self->{next_input_character} == 0x0020) { # SP          redo A;
2716          } elsif ($self->{nc} == 0x003E) { # >
2717            !!!cp (188);
2718            !!!parse-error (type => 'unclosed PUBLIC literal');
2719    
2720            $self->{state} = DATA_STATE;
2721            !!!next-input-character;
2722    
2723            $self->{ct}->{quirks} = 1;
2724            !!!emit ($self->{ct}); # DOCTYPE
2725    
2726            redo A;
2727          } elsif ($self->{nc} == -1) {
2728            !!!cp (189);
2729            !!!parse-error (type => 'unclosed PUBLIC literal');
2730    
2731            $self->{state} = DATA_STATE;
2732            ## reconsume
2733    
2734            $self->{ct}->{quirks} = 1;
2735            !!!emit ($self->{ct}); # DOCTYPE
2736    
2737            redo A;
2738          } else {
2739            !!!cp (190);
2740            $self->{ct}->{pubid} # DOCTYPE
2741                .= chr $self->{nc};
2742            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2743                                  length $self->{ct}->{pubid});
2744    
2745          ## Stay in the state          ## Stay in the state
2746          !!!next-input-character;          !!!next-input-character;
2747          redo A;          redo A;
2748        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2749          $self->{state} = 'data';      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2750          if ($self->{nc} == 0x0027) { # '
2751            !!!cp (191);
2752            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2753            !!!next-input-character;
2754            redo A;
2755          } elsif ($self->{nc} == 0x003E) { # >
2756            !!!cp (192);
2757            !!!parse-error (type => 'unclosed PUBLIC literal');
2758    
2759            $self->{state} = DATA_STATE;
2760          !!!next-input-character;          !!!next-input-character;
2761    
2762          !!!emit ($self->{current_token}); # DOCTYPE          $self->{ct}->{quirks} = 1;
2763          undef $self->{current_token};          !!!emit ($self->{ct}); # DOCTYPE
2764    
2765          redo A;          redo A;
2766        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2767            !!!cp (193);
2768            !!!parse-error (type => 'unclosed PUBLIC literal');
2769    
2770            $self->{state} = DATA_STATE;
2771            ## reconsume
2772    
2773            $self->{ct}->{quirks} = 1;
2774            !!!emit ($self->{ct}); # DOCTYPE
2775    
2776            redo A;
2777          } else {
2778            !!!cp (194);
2779            $self->{ct}->{pubid} # DOCTYPE
2780                .= chr $self->{nc};
2781            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2782                                  length $self->{ct}->{pubid});
2783    
2784            ## Stay in the state
2785            !!!next-input-character;
2786            redo A;
2787          }
2788        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2789          if ({
2790                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2791                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2792              }->{$self->{nc}}) {
2793            !!!cp (195);
2794            ## Stay in the state
2795            !!!next-input-character;
2796            redo A;
2797          } elsif ($self->{nc} == 0x0022) { # "
2798            !!!cp (196);
2799            $self->{ct}->{sysid} = ''; # DOCTYPE
2800            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2801            !!!next-input-character;
2802            redo A;
2803          } elsif ($self->{nc} == 0x0027) { # '
2804            !!!cp (197);
2805            $self->{ct}->{sysid} = ''; # DOCTYPE
2806            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2807            !!!next-input-character;
2808            redo A;
2809          } elsif ($self->{nc} == 0x003E) { # >
2810            !!!cp (198);
2811            $self->{state} = DATA_STATE;
2812            !!!next-input-character;
2813    
2814            !!!emit ($self->{ct}); # DOCTYPE
2815    
2816            redo A;
2817          } elsif ($self->{nc} == -1) {
2818            !!!cp (199);
2819          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2820          $self->{state} = 'data';  
2821            $self->{state} = DATA_STATE;
2822          ## reconsume          ## reconsume
2823    
2824          !!!emit ($self->{current_token}); # DOCTYPE          $self->{ct}->{quirks} = 1;
2825          undef $self->{current_token};          !!!emit ($self->{ct}); # DOCTYPE
2826    
2827          redo A;          redo A;
2828        } else {        } else {
2829          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (200);
2830          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2831          $self->{state} = 'bogus DOCTYPE';          $self->{ct}->{quirks} = 1;
2832    
2833            $self->{state} = BOGUS_DOCTYPE_STATE;
2834          !!!next-input-character;          !!!next-input-character;
2835          redo A;          redo A;
2836        }        }
2837      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2838        if ($self->{next_input_character} == 0x003E) { # >        if ({
2839          $self->{state} = 'data';              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2840                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2841              }->{$self->{nc}}) {
2842            !!!cp (201);
2843            ## Stay in the state
2844            !!!next-input-character;
2845            redo A;
2846          } elsif ($self->{nc} == 0x0022) { # "
2847            !!!cp (202);
2848            $self->{ct}->{sysid} = ''; # DOCTYPE
2849            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2850            !!!next-input-character;
2851            redo A;
2852          } elsif ($self->{nc} == 0x0027) { # '
2853            !!!cp (203);
2854            $self->{ct}->{sysid} = ''; # DOCTYPE
2855            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2856            !!!next-input-character;
2857            redo A;
2858          } elsif ($self->{nc} == 0x003E) { # >
2859            !!!cp (204);
2860            !!!parse-error (type => 'no SYSTEM literal');
2861            $self->{state} = DATA_STATE;
2862          !!!next-input-character;          !!!next-input-character;
2863    
2864          !!!emit ($self->{current_token}); # DOCTYPE          $self->{ct}->{quirks} = 1;
2865          undef $self->{current_token};          !!!emit ($self->{ct}); # DOCTYPE
2866    
2867          redo A;          redo A;
2868        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2869            !!!cp (205);
2870          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2871          $self->{state} = 'data';  
2872            $self->{state} = DATA_STATE;
2873            ## reconsume
2874    
2875            $self->{ct}->{quirks} = 1;
2876            !!!emit ($self->{ct}); # DOCTYPE
2877    
2878            redo A;
2879          } else {
2880            !!!cp (206);
2881            !!!parse-error (type => 'string after SYSTEM');
2882            $self->{ct}->{quirks} = 1;
2883    
2884            $self->{state} = BOGUS_DOCTYPE_STATE;
2885            !!!next-input-character;
2886            redo A;
2887          }
2888        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2889          if ($self->{nc} == 0x0022) { # "
2890            !!!cp (207);
2891            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2892            !!!next-input-character;
2893            redo A;
2894          } elsif ($self->{nc} == 0x003E) { # >
2895            !!!cp (208);
2896            !!!parse-error (type => 'unclosed SYSTEM literal');
2897    
2898            $self->{state} = DATA_STATE;
2899            !!!next-input-character;
2900    
2901            $self->{ct}->{quirks} = 1;
2902            !!!emit ($self->{ct}); # DOCTYPE
2903    
2904            redo A;
2905          } elsif ($self->{nc} == -1) {
2906            !!!cp (209);
2907            !!!parse-error (type => 'unclosed SYSTEM literal');
2908    
2909            $self->{state} = DATA_STATE;
2910          ## reconsume          ## reconsume
2911    
2912          !!!emit ($self->{current_token}); # DOCTYPE          $self->{ct}->{quirks} = 1;
2913          undef $self->{current_token};          !!!emit ($self->{ct}); # DOCTYPE
2914    
2915          redo A;          redo A;
2916        } else {        } else {
2917            !!!cp (210);
2918            $self->{ct}->{sysid} # DOCTYPE
2919                .= chr $self->{nc};
2920            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2921                                  length $self->{ct}->{sysid});
2922    
2923          ## Stay in the state          ## Stay in the state
2924          !!!next-input-character;          !!!next-input-character;
2925          redo A;          redo A;
2926        }        }
2927      } else {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2928        die "$0: $self->{state}: Unknown state";        if ($self->{nc} == 0x0027) { # '
2929      }          !!!cp (211);
2930    } # A            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2931            !!!next-input-character;
2932            redo A;
2933          } elsif ($self->{nc} == 0x003E) { # >
2934            !!!cp (212);
2935            !!!parse-error (type => 'unclosed SYSTEM literal');
2936    
2937    die "$0: _get_next_token: unexpected case";          $self->{state} = DATA_STATE;
2938  } # _get_next_token          !!!next-input-character;
2939    
2940  sub _tokenize_attempt_to_consume_an_entity ($) {          $self->{ct}->{quirks} = 1;
2941    my $self = shift;          !!!emit ($self->{ct}); # DOCTYPE
     
   if ($self->{next_input_character} == 0x0023) { # #  
     !!!next-input-character;  
     if ($self->{next_input_character} == 0x0078 or # x  
         $self->{next_input_character} == 0x0058) { # X  
       my $num;  
       X: {  
         my $x_char = $self->{next_input_character};  
         !!!next-input-character;  
         if (0x0030 <= $self->{next_input_character} and  
             $self->{next_input_character} <= 0x0039) { # 0..9  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0066) { # a..f  
           ## ISSUE: the spec says U+0078, which is apparently incorrect  
           $num ||= 0;  
           $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;  
         } else {  
           !!!parse-error (type => 'no refc');  
         }  
2942    
2943          ## TODO: check the definition for |a valid Unicode character|.          redo A;
2944          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>        } elsif ($self->{nc} == -1) {
2945          if ($num > 1114111 or $num == 0) {          !!!cp (213);
2946            $num = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => 'unclosed SYSTEM literal');
2947            ## ISSUE: Why this is not an error?  
2948          } elsif (0x80 <= $num and $num <= 0x9F) {          $self->{state} = DATA_STATE;
2949            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          ## reconsume
2950            $num = $c1_entity_char->{$num};  
2951          }          $self->{ct}->{quirks} = 1;
2952            !!!emit ($self->{ct}); # DOCTYPE
2953          return {type => 'character', data => chr $num};  
2954        } # X          redo A;
2955      } elsif (0x0030 <= $self->{next_input_character} and        } else {
2956               $self->{next_input_character} <= 0x0039) { # 0..9          !!!cp (214);
2957        my $code = $self->{next_input_character} - 0x0030;          $self->{ct}->{sysid} # DOCTYPE
2958        !!!next-input-character;              .= chr $self->{nc};
2959            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2960                                  length $self->{ct}->{sysid});
2961    
2962            ## Stay in the state
2963            !!!next-input-character;
2964            redo A;
2965          }
2966        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2967          if ({
2968                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2969                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2970              }->{$self->{nc}}) {
2971            !!!cp (215);
2972            ## Stay in the state
2973            !!!next-input-character;
2974            redo A;
2975          } elsif ($self->{nc} == 0x003E) { # >
2976            !!!cp (216);
2977            $self->{state} = DATA_STATE;
2978            !!!next-input-character;
2979    
2980            !!!emit ($self->{ct}); # DOCTYPE
2981    
2982            redo A;
2983          } elsif ($self->{nc} == -1) {
2984            !!!cp (217);
2985            !!!parse-error (type => 'unclosed DOCTYPE');
2986            $self->{state} = DATA_STATE;
2987            ## reconsume
2988    
2989            $self->{ct}->{quirks} = 1;
2990            !!!emit ($self->{ct}); # DOCTYPE
2991    
2992            redo A;
2993          } else {
2994            !!!cp (218);
2995            !!!parse-error (type => 'string after SYSTEM literal');
2996            #$self->{ct}->{quirks} = 1;
2997    
2998            $self->{state} = BOGUS_DOCTYPE_STATE;
2999            !!!next-input-character;
3000            redo A;
3001          }
3002        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
3003          if ($self->{nc} == 0x003E) { # >
3004            !!!cp (219);
3005            $self->{state} = DATA_STATE;
3006            !!!next-input-character;
3007    
3008            !!!emit ($self->{ct}); # DOCTYPE
3009    
3010            redo A;
3011          } elsif ($self->{nc} == -1) {
3012            !!!cp (220);
3013            !!!parse-error (type => 'unclosed DOCTYPE');
3014            $self->{state} = DATA_STATE;
3015            ## reconsume
3016    
3017            !!!emit ($self->{ct}); # DOCTYPE
3018    
3019            redo A;
3020          } else {
3021            !!!cp (221);
3022            my $s = '';
3023            $self->{read_until}->($s, q[>], 0);
3024    
3025            ## Stay in the state
3026            !!!next-input-character;
3027            redo A;
3028          }
3029        } elsif ($self->{state} == CDATA_SECTION_STATE) {
3030          ## NOTE: "CDATA section state" in the state is jointly implemented
3031          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
3032          ## and |CDATA_SECTION_MSE2_STATE|.
3033                
3034        while (0x0030 <= $self->{next_input_character} and        if ($self->{nc} == 0x005D) { # ]
3035                  $self->{next_input_character} <= 0x0039) { # 0..9          !!!cp (221.1);
3036          $code *= 10;          $self->{state} = CDATA_SECTION_MSE1_STATE;
         $code += $self->{next_input_character} - 0x0030;  
           
3037          !!!next-input-character;          !!!next-input-character;
3038            redo A;
3039          } elsif ($self->{nc} == -1) {
3040            $self->{state} = DATA_STATE;
3041            !!!next-input-character;
3042            if (length $self->{ct}->{data}) { # character
3043              !!!cp (221.2);
3044              !!!emit ($self->{ct}); # character
3045            } else {
3046              !!!cp (221.3);
3047              ## No token to emit. $self->{ct} is discarded.
3048            }        
3049            redo A;
3050          } else {
3051            !!!cp (221.4);
3052            $self->{ct}->{data} .= chr $self->{nc};
3053            $self->{read_until}->($self->{ct}->{data},
3054                                  q<]>,
3055                                  length $self->{ct}->{data});
3056    
3057            ## Stay in the state.
3058            !!!next-input-character;
3059            redo A;
3060        }        }
3061    
3062        if ($self->{next_input_character} == 0x003B) { # ;        ## ISSUE: "text tokens" in spec.
3063        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3064          if ($self->{nc} == 0x005D) { # ]
3065            !!!cp (221.5);
3066            $self->{state} = CDATA_SECTION_MSE2_STATE;
3067            !!!next-input-character;
3068            redo A;
3069          } else {
3070            !!!cp (221.6);
3071            $self->{ct}->{data} .= ']';
3072            $self->{state} = CDATA_SECTION_STATE;
3073            ## Reconsume.
3074            redo A;
3075          }
3076        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3077          if ($self->{nc} == 0x003E) { # >
3078            $self->{state} = DATA_STATE;
3079            !!!next-input-character;
3080            if (length $self->{ct}->{data}) { # character
3081              !!!cp (221.7);
3082              !!!emit ($self->{ct}); # character
3083            } else {
3084              !!!cp (221.8);
3085              ## No token to emit. $self->{ct} is discarded.
3086            }
3087            redo A;
3088          } elsif ($self->{nc} == 0x005D) { # ]
3089            !!!cp (221.9); # character
3090            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3091            ## Stay in the state.
3092            !!!next-input-character;
3093            redo A;
3094          } else {
3095            !!!cp (221.11);
3096            $self->{ct}->{data} .= ']]'; # character
3097            $self->{state} = CDATA_SECTION_STATE;
3098            ## Reconsume.
3099            redo A;
3100          }
3101        } elsif ($self->{state} == ENTITY_STATE) {
3102          if ({
3103            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3104            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3105            $self->{entity_add} => 1,
3106          }->{$self->{nc}}) {
3107            !!!cp (1001);
3108            ## Don't consume
3109            ## No error
3110            ## Return nothing.
3111            #
3112          } elsif ($self->{nc} == 0x0023) { # #
3113            !!!cp (999);
3114            $self->{state} = ENTITY_HASH_STATE;
3115            $self->{s_kwd} = '#';
3116            !!!next-input-character;
3117            redo A;
3118          } elsif ((0x0041 <= $self->{nc} and
3119                    $self->{nc} <= 0x005A) or # A..Z
3120                   (0x0061 <= $self->{nc} and
3121                    $self->{nc} <= 0x007A)) { # a..z
3122            !!!cp (998);
3123            require Whatpm::_NamedEntityList;
3124            $self->{state} = ENTITY_NAME_STATE;
3125            $self->{s_kwd} = chr $self->{nc};
3126            $self->{entity__value} = $self->{s_kwd};
3127            $self->{entity__match} = 0;
3128            !!!next-input-character;
3129            redo A;
3130          } else {
3131            !!!cp (1027);
3132            !!!parse-error (type => 'bare ero');
3133            ## Return nothing.
3134            #
3135          }
3136    
3137          ## NOTE: No character is consumed by the "consume a character
3138          ## reference" algorithm.  In other word, there is an "&" character
3139          ## that does not introduce a character reference, which would be
3140          ## appended to the parent element or the attribute value in later
3141          ## process of the tokenizer.
3142    
3143          if ($self->{prev_state} == DATA_STATE) {
3144            !!!cp (997);
3145            $self->{state} = $self->{prev_state};
3146            ## Reconsume.
3147            !!!emit ({type => CHARACTER_TOKEN, data => '&',
3148                      line => $self->{line_prev},
3149                      column => $self->{column_prev},
3150                     });
3151            redo A;
3152          } else {
3153            !!!cp (996);
3154            $self->{ca}->{value} .= '&';
3155            $self->{state} = $self->{prev_state};
3156            ## Reconsume.
3157            redo A;
3158          }
3159        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3160          if ($self->{nc} == 0x0078 or # x
3161              $self->{nc} == 0x0058) { # X
3162            !!!cp (995);
3163            $self->{state} = HEXREF_X_STATE;
3164            $self->{s_kwd} .= chr $self->{nc};
3165            !!!next-input-character;
3166            redo A;
3167          } elsif (0x0030 <= $self->{nc} and
3168                   $self->{nc} <= 0x0039) { # 0..9
3169            !!!cp (994);
3170            $self->{state} = NCR_NUM_STATE;
3171            $self->{s_kwd} = $self->{nc} - 0x0030;
3172            !!!next-input-character;
3173            redo A;
3174          } else {
3175            !!!parse-error (type => 'bare nero',
3176                            line => $self->{line_prev},
3177                            column => $self->{column_prev} - 1);
3178    
3179            ## NOTE: According to the spec algorithm, nothing is returned,
3180            ## and then "&#" is appended to the parent element or the attribute
3181            ## value in the later processing.
3182    
3183            if ($self->{prev_state} == DATA_STATE) {
3184              !!!cp (1019);
3185              $self->{state} = $self->{prev_state};
3186              ## Reconsume.
3187              !!!emit ({type => CHARACTER_TOKEN,
3188                        data => '&#',
3189                        line => $self->{line_prev},
3190                        column => $self->{column_prev} - 1,
3191                       });
3192              redo A;
3193            } else {
3194              !!!cp (993);
3195              $self->{ca}->{value} .= '&#';
3196              $self->{state} = $self->{prev_state};
3197              ## Reconsume.
3198              redo A;
3199            }
3200          }
3201        } elsif ($self->{state} == NCR_NUM_STATE) {
3202          if (0x0030 <= $self->{nc} and
3203              $self->{nc} <= 0x0039) { # 0..9
3204            !!!cp (1012);
3205            $self->{s_kwd} *= 10;
3206            $self->{s_kwd} += $self->{nc} - 0x0030;
3207            
3208            ## Stay in the state.
3209            !!!next-input-character;
3210            redo A;
3211          } elsif ($self->{nc} == 0x003B) { # ;
3212            !!!cp (1013);
3213          !!!next-input-character;          !!!next-input-character;
3214            #
3215        } else {        } else {
3216            !!!cp (1014);
3217          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3218            ## Reconsume.
3219            #
3220        }        }
3221    
3222        ## TODO: check the definition for |a valid Unicode character|.        my $code = $self->{s_kwd};
3223        if ($code > 1114111 or $code == 0) {        my $l = $self->{line_prev};
3224          $code = 0xFFFD; # REPLACEMENT CHARACTER        my $c = $self->{column_prev};
3225          ## ISSUE: Why this is not an error?        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3226            !!!cp (1015);
3227            !!!parse-error (type => 'invalid character reference',
3228                            text => (sprintf 'U+%04X', $code),
3229                            line => $l, column => $c);
3230            $code = 0xFFFD;
3231          } elsif ($code > 0x10FFFF) {
3232            !!!cp (1016);
3233            !!!parse-error (type => 'invalid character reference',
3234                            text => (sprintf 'U-%08X', $code),
3235                            line => $l, column => $c);
3236            $code = 0xFFFD;
3237          } elsif ($code == 0x000D) {
3238            !!!cp (1017);
3239            !!!parse-error (type => 'CR character reference',
3240                            line => $l, column => $c);
3241            $code = 0x000A;
3242        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3243          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!cp (1018);
3244            !!!parse-error (type => 'C1 character reference',
3245                            text => (sprintf 'U+%04X', $code),
3246                            line => $l, column => $c);
3247          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3248        }        }
3249          
3250        return {type => 'character', data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3251      } else {          !!!cp (992);
3252        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3253        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3254        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3255        return undef;                    line => $l, column => $c,
3256      }                   });
3257    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
             $self->{next_input_character} <= 0x005A) or  
            (0x0061 <= $self->{next_input_character} and  
             $self->{next_input_character} <= 0x007A)) {  
     my $entity_name = chr $self->{next_input_character};  
     !!!next-input-character;  
   
     my $value = $entity_name;  
     my $match;  
   
     while (length $entity_name < 10 and  
            ## NOTE: Some number greater than the maximum length of entity name  
            ((0x0041 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x005A) or  
             (0x0061 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x007A) or  
             (0x0030 <= $self->{next_input_character} and  
              $self->{next_input_character} <= 0x0039))) {  
       $entity_name .= chr $self->{next_input_character};  
       if (defined $entity_char->{$entity_name}) {  
         $value = $entity_char->{$entity_name};  
         $match = 1;  
3258        } else {        } else {
3259          $value .= chr $self->{next_input_character};          !!!cp (991);
3260            $self->{ca}->{value} .= chr $code;
3261            $self->{ca}->{has_reference} = 1;
3262            $self->{state} = $self->{prev_state};
3263            ## Reconsume.
3264            redo A;
3265          }
3266        } elsif ($self->{state} == HEXREF_X_STATE) {
3267          if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3268              (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3269              (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3270            # 0..9, A..F, a..f
3271            !!!cp (990);
3272            $self->{state} = HEXREF_HEX_STATE;
3273            $self->{s_kwd} = 0;
3274            ## Reconsume.
3275            redo A;
3276          } else {
3277            !!!parse-error (type => 'bare hcro',
3278                            line => $self->{line_prev},
3279                            column => $self->{column_prev} - 2);
3280    
3281            ## NOTE: According to the spec algorithm, nothing is returned,
3282            ## and then "&#" followed by "X" or "x" is appended to the parent
3283            ## element or the attribute value in the later processing.
3284    
3285            if ($self->{prev_state} == DATA_STATE) {
3286              !!!cp (1005);
3287              $self->{state} = $self->{prev_state};
3288              ## Reconsume.
3289              !!!emit ({type => CHARACTER_TOKEN,
3290                        data => '&' . $self->{s_kwd},
3291                        line => $self->{line_prev},
3292                        column => $self->{column_prev} - length $self->{s_kwd},
3293                       });
3294              redo A;
3295            } else {
3296              !!!cp (989);
3297              $self->{ca}->{value} .= '&' . $self->{s_kwd};
3298              $self->{state} = $self->{prev_state};
3299              ## Reconsume.
3300              redo A;
3301            }
3302        }        }
3303        !!!next-input-character;      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3304      }        if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3305                # 0..9
3306      if ($match) {          !!!cp (1002);
3307        if ($self->{next_input_character} == 0x003B) { # ;          $self->{s_kwd} *= 0x10;
3308            $self->{s_kwd} += $self->{nc} - 0x0030;
3309            ## Stay in the state.
3310            !!!next-input-character;
3311            redo A;
3312          } elsif (0x0061 <= $self->{nc} and
3313                   $self->{nc} <= 0x0066) { # a..f
3314            !!!cp (1003);
3315            $self->{s_kwd} *= 0x10;
3316            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3317            ## Stay in the state.
3318            !!!next-input-character;
3319            redo A;
3320          } elsif (0x0041 <= $self->{nc} and
3321                   $self->{nc} <= 0x0046) { # A..F
3322            !!!cp (1004);
3323            $self->{s_kwd} *= 0x10;
3324            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3325            ## Stay in the state.
3326          !!!next-input-character;          !!!next-input-character;
3327            redo A;
3328          } elsif ($self->{nc} == 0x003B) { # ;
3329            !!!cp (1006);
3330            !!!next-input-character;
3331            #
3332        } else {        } else {
3333          !!!parse-error (type => 'refc');          !!!cp (1007);
3334            !!!parse-error (type => 'no refc',
3335                            line => $self->{line},
3336                            column => $self->{column});
3337            ## Reconsume.
3338            #
3339          }
3340    
3341          my $code = $self->{s_kwd};
3342          my $l = $self->{line_prev};
3343          my $c = $self->{column_prev};
3344          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3345            !!!cp (1008);
3346            !!!parse-error (type => 'invalid character reference',
3347                            text => (sprintf 'U+%04X', $code),
3348                            line => $l, column => $c);
3349            $code = 0xFFFD;
3350          } elsif ($code > 0x10FFFF) {
3351            !!!cp (1009);
3352            !!!parse-error (type => 'invalid character reference',
3353                            text => (sprintf 'U-%08X', $code),
3354                            line => $l, column => $c);
3355            $code = 0xFFFD;
3356          } elsif ($code == 0x000D) {
3357            !!!cp (1010);
3358            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3359            $code = 0x000A;
3360          } elsif (0x80 <= $code and $code <= 0x9F) {
3361            !!!cp (1011);
3362            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3363            $code = $c1_entity_char->{$code};
3364        }        }
3365    
3366        return {type => 'character', data => $value};        if ($self->{prev_state} == DATA_STATE) {
3367            !!!cp (988);
3368            $self->{state} = $self->{prev_state};
3369            ## Reconsume.
3370            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3371                      line => $l, column => $c,
3372                     });
3373            redo A;
3374          } else {
3375            !!!cp (987);
3376            $self->{ca}->{value} .= chr $code;
3377            $self->{ca}->{has_reference} = 1;
3378            $self->{state} = $self->{prev_state};
3379            ## Reconsume.
3380            redo A;
3381          }
3382        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3383          if (length $self->{s_kwd} < 30 and
3384              ## NOTE: Some number greater than the maximum length of entity name
3385              ((0x0041 <= $self->{nc} and # a
3386                $self->{nc} <= 0x005A) or # x
3387               (0x0061 <= $self->{nc} and # a
3388                $self->{nc} <= 0x007A) or # z
3389               (0x0030 <= $self->{nc} and # 0
3390                $self->{nc} <= 0x0039) or # 9
3391               $self->{nc} == 0x003B)) { # ;
3392            our $EntityChar;
3393            $self->{s_kwd} .= chr $self->{nc};
3394            if (defined $EntityChar->{$self->{s_kwd}}) {
3395              if ($self->{nc} == 0x003B) { # ;
3396                !!!cp (1020);
3397                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3398                $self->{entity__match} = 1;
3399                !!!next-input-character;
3400                #
3401              } else {
3402                !!!cp (1021);
3403                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3404                $self->{entity__match} = -1;
3405                ## Stay in the state.
3406                !!!next-input-character;
3407                redo A;
3408              }
3409            } else {
3410              !!!cp (1022);
3411              $self->{entity__value} .= chr $self->{nc};
3412              $self->{entity__match} *= 2;
3413              ## Stay in the state.
3414              !!!next-input-character;
3415              redo A;
3416            }
3417          }
3418    
3419          my $data;
3420          my $has_ref;
3421          if ($self->{entity__match} > 0) {
3422            !!!cp (1023);
3423            $data = $self->{entity__value};
3424            $has_ref = 1;
3425            #
3426          } elsif ($self->{entity__match} < 0) {
3427            !!!parse-error (type => 'no refc');
3428            if ($self->{prev_state} != DATA_STATE and # in attribute
3429                $self->{entity__match} < -1) {
3430              !!!cp (1024);
3431              $data = '&' . $self->{s_kwd};
3432              #
3433            } else {
3434              !!!cp (1025);
3435              $data = $self->{entity__value};
3436              $has_ref = 1;
3437              #
3438            }
3439          } else {
3440            !!!cp (1026);
3441            !!!parse-error (type => 'bare ero',
3442                            line => $self->{line_prev},
3443                            column => $self->{column_prev} - length $self->{s_kwd});
3444            $data = '&' . $self->{s_kwd};
3445            #
3446          }
3447      
3448          ## NOTE: In these cases, when a character reference is found,
3449          ## it is consumed and a character token is returned, or, otherwise,
3450          ## nothing is consumed and returned, according to the spec algorithm.
3451          ## In this implementation, anything that has been examined by the
3452          ## tokenizer is appended to the parent element or the attribute value
3453          ## as string, either literal string when no character reference or
3454          ## entity-replaced string otherwise, in this stage, since any characters
3455          ## that would not be consumed are appended in the data state or in an
3456          ## appropriate attribute value state anyway.
3457    
3458          if ($self->{prev_state} == DATA_STATE) {
3459            !!!cp (986);
3460            $self->{state} = $self->{prev_state};
3461            ## Reconsume.
3462            !!!emit ({type => CHARACTER_TOKEN,
3463                      data => $data,
3464                      line => $self->{line_prev},
3465                      column => $self->{column_prev} + 1 - length $self->{s_kwd},
3466                     });
3467            redo A;
3468          } else {
3469            !!!cp (985);
3470            $self->{ca}->{value} .= $data;
3471            $self->{ca}->{has_reference} = 1 if $has_ref;
3472            $self->{state} = $self->{prev_state};
3473            ## Reconsume.
3474            redo A;
3475          }
3476      } else {      } else {
3477        !!!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;  
3478      }      }
3479    } else {    } # A  
3480      ## no characters are consumed  
3481      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3482      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3483    
3484  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3485    my $self = shift;    my $self = shift;
# Line 1634  sub _initialize_tree_constructor ($) { Line 3487  sub _initialize_tree_constructor ($) {
3487    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3488    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3489    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3490    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3491      $self->{document}->set_user_data (manakai_source_line => 1);
3492      $self->{document}->set_user_data (manakai_source_column => 1);
3493  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3494    
3495  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1661  sub _construct_tree ($) { Line 3516  sub _construct_tree ($) {
3516        
3517    !!!next-token;    !!!next-token;
3518    
   $self->{insertion_mode} = 'before head';  
3519    undef $self->{form_element};    undef $self->{form_element};
3520    undef $self->{head_element};    undef $self->{head_element};
3521    $self->{open_elements} = [];    $self->{open_elements} = [];
3522    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3523    
3524      ## NOTE: The "initial" insertion mode.
3525    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3526    
3527      ## NOTE: The "before html" insertion mode.
3528    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3529      $self->{insertion_mode} = BEFORE_HEAD_IM;
3530    
3531      ## NOTE: The "before head" insertion mode and so on.
3532    $self->_tree_construction_main;    $self->_tree_construction_main;
3533  } # _construct_tree  } # _construct_tree
3534    
3535  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3536    my $self = shift;    my $self = shift;
3537    B: {  
3538        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3539          if ($token->{error}) {  
3540            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3541            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3542          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3543          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3544            ($token->{name});        ## language.
3545          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3546          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3547          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3548          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3549          return;            defined $token->{sysid}) {
3550        } elsif ({          !!!cp ('t1');
3551                  comment => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3552                  'start tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3553                  'end tag' => 1,          !!!cp ('t2');
3554                  'end-of-file' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3555                 }->{$token->{type}}) {        } elsif (defined $token->{pubid}) {
3556          ## ISSUE: Spec currently left this case undefined.          if ($token->{pubid} eq 'XSLT-compat') {
3557          !!!parse-error (type => 'missing DOCTYPE');            !!!cp ('t1.2');
3558          #$phase = 'root element';            !!!parse-error (type => 'XSLT-compat', token => $token,
3559          ## reprocess                            level => $self->{level}->{should});
3560          #redo B;          } else {
3561          return;            !!!parse-error (type => 'not HTML5', token => $token);
3562        } elsif ($token->{type} eq 'character') {          }
3563          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } else {
3564            $self->{document}->manakai_append_text ($1);          !!!cp ('t3');
3565            ## ISSUE: DOM3 Core does not allow Document > Text          #
3566            unless (length $token->{data}) {        }
3567              ## Stay in the phase        
3568              !!!next-token;        my $doctype = $self->{document}->create_document_type_definition
3569              redo B;          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3570          ## NOTE: Default value for both |public_id| and |system_id| attributes
3571          ## are empty strings, so that we don't set any value in missing cases.
3572          $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3573          $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
3574          ## NOTE: Other DocumentType attributes are null or empty lists.
3575          ## ISSUE: internalSubset = null??
3576          $self->{document}->append_child ($doctype);
3577          
3578          if ($token->{quirks} or $doctype_name ne 'HTML') {
3579            !!!cp ('t4');
3580            $self->{document}->manakai_compat_mode ('quirks');
3581          } elsif (defined $token->{pubid}) {
3582            my $pubid = $token->{pubid};
3583            $pubid =~ tr/a-z/A-z/;
3584            my $prefix = [
3585              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3586              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3587              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3588              "-//IETF//DTD HTML 2.0 LEVEL 1//",
3589              "-//IETF//DTD HTML 2.0 LEVEL 2//",
3590              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3591              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3592              "-//IETF//DTD HTML 2.0 STRICT//",
3593              "-//IETF//DTD HTML 2.0//",
3594              "-//IETF//DTD HTML 2.1E//",
3595              "-//IETF//DTD HTML 3.0//",
3596              "-//IETF//DTD HTML 3.2 FINAL//",
3597              "-//IETF//DTD HTML 3.2//",
3598              "-//IETF//DTD HTML 3//",
3599              "-//IETF//DTD HTML LEVEL 0//",
3600              "-//IETF//DTD HTML LEVEL 1//",
3601              "-//IETF//DTD HTML LEVEL 2//",
3602              "-//IETF//DTD HTML LEVEL 3//",
3603              "-//IETF//DTD HTML STRICT LEVEL 0//",
3604              "-//IETF//DTD HTML STRICT LEVEL 1//",
3605              "-//IETF//DTD HTML STRICT LEVEL 2//",
3606              "-//IETF//DTD HTML STRICT LEVEL 3//",
3607              "-//IETF//DTD HTML STRICT//",
3608              "-//IETF//DTD HTML//",
3609              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3610              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3611              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3612              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3613              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3614              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3615              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3616              "-//NETSCAPE COMM. CORP.//DTD HTML//",
3617              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3618              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3619              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3620              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3621              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3622              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3623              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3624              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3625              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3626              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3627              "-//W3C//DTD HTML 3 1995-03-24//",
3628              "-//W3C//DTD HTML 3.2 DRAFT//",
3629              "-//W3C//DTD HTML 3.2 FINAL//",
3630              "-//W3C//DTD HTML 3.2//",
3631              "-//W3C//DTD HTML 3.2S DRAFT//",
3632              "-//W3C//DTD HTML 4.0 FRAMESET//",
3633              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3634              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3635              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3636              "-//W3C//DTD W3 HTML//",
3637              "-//W3O//DTD W3 HTML 3.0//",
3638              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3639              "-//WEBTECHS//DTD MOZILLA HTML//",
3640            ]; # $prefix
3641            my $match;
3642            for (@$prefix) {
3643              if (substr ($prefix, 0, length $_) eq $_) {
3644                $match = 1;
3645                last;
3646              }
3647            }
3648            if ($match or
3649                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3650                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3651                $pubid eq "HTML") {
3652              !!!cp ('t5');
3653              $self->{document}->manakai_compat_mode ('quirks');
3654            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3655                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3656              if (defined $token->{sysid}) {
3657                !!!cp ('t6');
3658                $self->{document}->manakai_compat_mode ('quirks');
3659              } else {
3660                !!!cp ('t7');
3661                $self->{document}->manakai_compat_mode ('limited quirks');
3662            }            }
3663            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3664                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3665              !!!cp ('t8');
3666              $self->{document}->manakai_compat_mode ('limited quirks');
3667            } else {
3668              !!!cp ('t9');
3669            }
3670          } else {
3671            !!!cp ('t10');
3672          }
3673          if (defined $token->{sysid}) {
3674            my $sysid = $token->{sysid};
3675            $sysid =~ tr/A-Z/a-z/;
3676            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3677              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3678              ## marked as quirks.
3679              $self->{document}->manakai_compat_mode ('quirks');
3680              !!!cp ('t11');
3681            } else {
3682              !!!cp ('t12');
3683          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3684        } else {        } else {
3685          die "$0: $token->{type}: Unknown token";          !!!cp ('t13');
3686        }        }
3687      } # B        
3688          ## Go to the "before html" insertion mode.
3689          !!!next-token;
3690          return;
3691        } elsif ({
3692                  START_TAG_TOKEN, 1,
3693                  END_TAG_TOKEN, 1,
3694                  END_OF_FILE_TOKEN, 1,
3695                 }->{$token->{type}}) {
3696          !!!cp ('t14');
3697          !!!parse-error (type => 'no DOCTYPE', token => $token);
3698          $self->{document}->manakai_compat_mode ('quirks');
3699          ## Go to the "before html" insertion mode.
3700          ## reprocess
3701          !!!ack-later;
3702          return;
3703        } elsif ($token->{type} == CHARACTER_TOKEN) {
3704          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3705            ## Ignore the token
3706    
3707            unless (length $token->{data}) {
3708              !!!cp ('t15');
3709              ## Stay in the insertion mode.
3710              !!!next-token;
3711              redo INITIAL;
3712            } else {
3713              !!!cp ('t16');
3714            }
3715          } else {
3716            !!!cp ('t17');
3717          }
3718    
3719          !!!parse-error (type => 'no DOCTYPE', token => $token);
3720          $self->{document}->manakai_compat_mode ('quirks');
3721          ## Go to the "before html" insertion mode.
3722          ## reprocess
3723          return;
3724        } elsif ($token->{type} == COMMENT_TOKEN) {
3725          !!!cp ('t18');
3726          my $comment = $self->{document}->create_comment ($token->{data});
3727          $self->{document}->append_child ($comment);
3728          
3729          ## Stay in the insertion mode.
3730          !!!next-token;
3731          redo INITIAL;
3732        } else {
3733          die "$0: $token->{type}: Unknown token type";
3734        }
3735      } # INITIAL
3736    
3737      die "$0: _tree_construction_initial: This should be never reached";
3738  } # _tree_construction_initial  } # _tree_construction_initial
3739    
3740  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3741    my $self = shift;    my $self = shift;
3742    
3743      ## NOTE: "before html" insertion mode.
3744        
3745    B: {    B: {
3746        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3747          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3748            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3749          ## Ignore the token          ## Ignore the token
3750          ## Stay in the phase          ## Stay in the insertion mode.
3751          !!!next-token;          !!!next-token;
3752          redo B;          redo B;
3753        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3754            !!!cp ('t20');
3755          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3756          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3757          ## Stay in the phase          ## Stay in the insertion mode.
3758          !!!next-token;          !!!next-token;
3759          redo B;          redo B;
3760        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3761          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3762            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3763            ## ISSUE: DOM3 Core does not allow Document > Text  
3764            unless (length $token->{data}) {            unless (length $token->{data}) {
3765              ## Stay in the phase              !!!cp ('t21');
3766                ## Stay in the insertion mode.
3767              !!!next-token;              !!!next-token;
3768              redo B;              redo B;
3769              } else {
3770                !!!cp ('t22');
3771            }            }
3772            } else {
3773              !!!cp ('t23');
3774          }          }
3775    
3776            $self->{application_cache_selection}->(undef);
3777    
3778          #          #
3779          } elsif ($token->{type} == START_TAG_TOKEN) {
3780            if ($token->{tag_name} eq 'html') {
3781              my $root_element;
3782              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3783              $self->{document}->append_child ($root_element);
3784              push @{$self->{open_elements}},
3785                  [$root_element, $el_category->{html}];
3786    
3787              if ($token->{attributes}->{manifest}) {
3788                !!!cp ('t24');
3789                $self->{application_cache_selection}
3790                    ->($token->{attributes}->{manifest}->{value});
3791                ## ISSUE: Spec is unclear on relative references.
3792                ## According to Hixie (#whatwg 2008-03-19), it should be
3793                ## resolved against the base URI of the document in HTML
3794                ## or xml:base of the element in XHTML.
3795              } else {
3796                !!!cp ('t25');
3797                $self->{application_cache_selection}->(undef);
3798              }
3799    
3800              !!!nack ('t25c');
3801    
3802              !!!next-token;
3803              return; ## Go to the "before head" insertion mode.
3804            } else {
3805              !!!cp ('t25.1');
3806              #
3807            }
3808        } elsif ({        } elsif ({
3809                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3810                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3811                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3812          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3813          #          #
3814        } else {        } else {
3815          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3816        }        }
3817        my $root_element; !!!create-element ($root_element, 'html');  
3818        $self->{document}->append_child ($root_element);      my $root_element;
3819        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3820        #$phase = 'main';      $self->{document}->append_child ($root_element);
3821        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3822        #redo B;  
3823        return;      $self->{application_cache_selection}->(undef);
3824    
3825        ## NOTE: Reprocess the token.
3826        !!!ack-later;
3827        return; ## Go to the "before head" insertion mode.
3828    
3829        ## ISSUE: There is an issue in the spec
3830    } # B    } # B
3831    
3832      die "$0: _tree_construction_root_element: This should never be reached";
3833  } # _tree_construction_root_element  } # _tree_construction_root_element
3834    
3835  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1780  sub _reset_insertion_mode ($) { Line 3844  sub _reset_insertion_mode ($) {
3844            
3845      ## Step 3      ## Step 3
3846      S3: {      S3: {
3847        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3848        if (defined $self->{inner_html_node}) {          $last = 1;
3849          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3850              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3851              $node = $self->{inner_html_node};
3852            } else {
3853              die "_reset_insertion_mode: t27";
3854            }
3855          }
3856          
3857          ## Step 4..14
3858          my $new_mode;
3859          if ($node->[1] & FOREIGN_EL) {
3860            !!!cp ('t28.1');
3861            ## NOTE: Strictly spaking, the line below only applies to MathML and
3862            ## SVG elements.  Currently the HTML syntax supports only MathML and
3863            ## SVG elements as foreigners.
3864            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3865          } elsif ($node->[1] & TABLE_CELL_EL) {
3866            if ($last) {
3867              !!!cp ('t28.2');
3868            #            #
3869          } else {          } else {
3870            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3871              $new_mode = IN_CELL_IM;
3872          }          }
3873          } else {
3874            !!!cp ('t28.4');
3875            $new_mode = {
3876                          select => IN_SELECT_IM,
3877                          ## NOTE: |option| and |optgroup| do not set
3878                          ## insertion mode to "in select" by themselves.
3879                          tr => IN_ROW_IM,
3880                          tbody => IN_TABLE_BODY_IM,
3881                          thead => IN_TABLE_BODY_IM,
3882                          tfoot => IN_TABLE_BODY_IM,
3883                          caption => IN_CAPTION_IM,
3884                          colgroup => IN_COLUMN_GROUP_IM,
3885                          table => IN_TABLE_IM,
3886                          head => IN_BODY_IM, # not in head!
3887                          body => IN_BODY_IM,
3888                          frameset => IN_FRAMESET_IM,
3889                         }->{$node->[0]->manakai_local_name};
3890        }        }
       
       ## 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]};  
3891        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3892                
3893        ## Step 14        ## Step 15
3894        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3895          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3896            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3897              $self->{insertion_mode} = BEFORE_HEAD_IM;
3898          } else {          } else {
3899            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3900              !!!cp ('t30');
3901              $self->{insertion_mode} = AFTER_HEAD_IM;
3902          }          }
3903          return;          return;
3904          } else {
3905            !!!cp ('t31');
3906        }        }
3907                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3908        ## Step 16        ## Step 16
3909          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3910          
3911          ## Step 17
3912        $i--;        $i--;
3913        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3914                
3915        ## Step 17        ## Step 18
3916        redo S3;        redo S3;
3917      } # S3      } # S3
3918    
3919      die "$0: _reset_insertion_mode: This line should never be reached";
3920  } # _reset_insertion_mode  } # _reset_insertion_mode
3921    
3922  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3923    my $self = shift;    my $self = shift;
3924    
   my $phase = 'main';  
   
3925    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3926    
3927    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1851  sub _tree_construction_main ($) { Line 3938  sub _tree_construction_main ($) {
3938      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3939      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3940        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3941            !!!cp ('t32');
3942          return;          return;
3943        }        }
3944      }      }
# Line 1865  sub _tree_construction_main ($) { Line 3953  sub _tree_construction_main ($) {
3953    
3954        ## Step 6        ## Step 6
3955        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3956            !!!cp ('t33_1');
3957          #          #
3958        } else {        } else {
3959          my $in_open_elements;          my $in_open_elements;
3960          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3961            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3962                !!!cp ('t33');
3963              $in_open_elements = 1;              $in_open_elements = 1;
3964              last OE;              last OE;
3965            }            }
3966          }          }
3967          if ($in_open_elements) {          if ($in_open_elements) {
3968              !!!cp ('t34');
3969            #            #
3970          } else {          } else {
3971              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3972              !!!cp ('t35');
3973            redo S4;            redo S4;
3974          }          }
3975        }        }
# Line 1899  sub _tree_construction_main ($) { Line 3992  sub _tree_construction_main ($) {
3992    
3993        ## Step 11        ## Step 11
3994        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3995            !!!cp ('t36');
3996          ## Step 7'          ## Step 7'
3997          $i++;          $i++;
3998          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3999                    
4000          redo S7;          redo S7;
4001        }        }
4002    
4003          !!!cp ('t37');
4004      } # S7      } # S7
4005    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
4006    
4007    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
4008      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
4009        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4010            !!!cp ('t38');
4011          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
4012          return;          return;
4013        }        }
4014      }      }
4015    
4016        !!!cp ('t39');
4017    }; # $clear_up_to_marker    }; # $clear_up_to_marker
4018    
4019    my $style_start_tag = sub {    my $insert;
4020      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
4021      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
4022      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
4023       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
4024        ->append_child ($style_el);      ## Step 1
4025      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
4026                      my $el;
4027        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
4028    
4029        ## Step 2
4030        $insert->($el);
4031    
4032        ## Step 3
4033        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
4034        delete $self->{escape}; # MUST
4035    
4036        ## Step 4
4037      my $text = '';      my $text = '';
4038        !!!nack ('t40.1');
4039      !!!next-token;      !!!next-token;
4040      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
4041          !!!cp ('t40');
4042        $text .= $token->{data};        $text .= $token->{data};
4043        !!!next-token;        !!!next-token;
4044      } # stop if non-character token or tokenizer stops tokenising      }
4045    
4046        ## Step 5
4047      if (length $text) {      if (length $text) {
4048        $style_el->manakai_append_text ($text);        !!!cp ('t41');
4049          my $text = $self->{document}->create_text_node ($text);
4050          $el->append_child ($text);
4051      }      }
4052        
4053      $self->{content_model_flag} = 'PCDATA';      ## Step 6
4054                      $self->{content_model} = PCDATA_CONTENT_MODEL;
4055      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
4056        ## Step 7
4057        if ($token->{type} == END_TAG_TOKEN and
4058            $token->{tag_name} eq $start_tag_name) {
4059          !!!cp ('t42');
4060        ## Ignore the token        ## Ignore the token
4061      } else {      } else {
4062        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
4063        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
4064            !!!cp ('t43');
4065            !!!parse-error (type => 'in CDATA:#eof', token => $token);
4066          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4067            !!!cp ('t44');
4068            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4069          } else {
4070            die "$0: $content_model_flag in parse_rcdata";
4071          }
4072      }      }
4073      !!!next-token;      !!!next-token;
4074    }; # $style_start_tag    }; # $parse_rcdata
4075    
4076    my $script_start_tag = sub {    my $script_start_tag = sub () {
4077      my $script_el;      my $script_el;
4078      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4079      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4080    
4081      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
4082        delete $self->{escape}; # MUST
4083            
4084      my $text = '';      my $text = '';
4085        !!!nack ('t45.1');
4086      !!!next-token;      !!!next-token;
4087      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
4088          !!!cp ('t45');
4089        $text .= $token->{data};        $text .= $token->{data};
4090        !!!next-token;        !!!next-token;
4091      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4092      if (length $text) {      if (length $text) {
4093          !!!cp ('t46');
4094        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4095      }      }
4096                                
4097      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
4098    
4099      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
4100          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4101          !!!cp ('t47');
4102        ## Ignore the token        ## Ignore the token
4103      } else {      } else {
4104        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4105          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4106        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4107        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4108      }      }
4109            
4110      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4111          !!!cp ('t49');
4112        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4113      } else {      } else {
4114          !!!cp ('t50');
4115        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4116        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4117          
4118        (($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);  
4119                
4120        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
4121                
# Line 1991  sub _tree_construction_main ($) { Line 4125  sub _tree_construction_main ($) {
4125      !!!next-token;      !!!next-token;
4126    }; # $script_start_tag    }; # $script_start_tag
4127    
4128      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4129      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4130      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4131    
4132    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4133      my $tag_name = shift;      my $end_tag_token = shift;
4134        my $tag_name = $end_tag_token->{tag_name};
4135    
4136        ## NOTE: The adoption agency algorithm (AAA).
4137    
4138      FET: {      FET: {
4139        ## Step 1        ## Step 1
4140        my $formatting_element;        my $formatting_element;
4141        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4142        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4143          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4144              !!!cp ('t52');
4145              last AFE;
4146            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4147                         eq $tag_name) {
4148              !!!cp ('t51');
4149            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4150            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4151            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4152          }          }
4153        } # AFE        } # AFE
4154        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4155          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4156            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4157          ## Ignore the token          ## Ignore the token
4158          !!!next-token;          !!!next-token;
4159          return;          return;
# Line 2020  sub _tree_construction_main ($) { Line 4165  sub _tree_construction_main ($) {
4165          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4166          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4167            if ($in_scope) {            if ($in_scope) {
4168                !!!cp ('t54');
4169              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4170              last INSCOPE;              last INSCOPE;
4171            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4172              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4173                !!!parse-error (type => 'unmatched end tag',
4174                                text => $token->{tag_name},
4175                                token => $end_tag_token);
4176              ## Ignore the token              ## Ignore the token
4177              !!!next-token;              !!!next-token;
4178              return;              return;
4179            }            }
4180          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4181                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4182            $in_scope = 0;            $in_scope = 0;
4183          }          }
4184        } # INSCOPE        } # INSCOPE
4185        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4186          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4187            !!!parse-error (type => 'unmatched end tag',
4188                            text => $token->{tag_name},
4189                            token => $end_tag_token);
4190          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4191          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4192          return;          return;
4193        }        }
4194        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4195          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4196            !!!parse-error (type => 'not closed',
4197                            text => $self->{open_elements}->[-1]->[0]
4198                                ->manakai_local_name,
4199                            token => $end_tag_token);
4200        }        }
4201                
4202        ## Step 2        ## Step 2
# Line 2050  sub _tree_construction_main ($) { Line 4204  sub _tree_construction_main ($) {
4204        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4205        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4206          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4207          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4208              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4209              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4210               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4211              !!!cp ('t59');
4212            $furthest_block = $node;            $furthest_block = $node;
4213            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4214          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4215              !!!cp ('t60');
4216            last OE;            last OE;
4217          }          }
4218        } # OE        } # OE
4219                
4220        ## Step 3        ## Step 3
4221        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4222            !!!cp ('t61');
4223          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4224          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4225          !!!next-token;          !!!next-token;
# Line 2075  sub _tree_construction_main ($) { Line 4232  sub _tree_construction_main ($) {
4232        ## Step 5        ## Step 5
4233        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4234        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4235            !!!cp ('t62');
4236          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4237        }        }
4238                
# Line 2097  sub _tree_construction_main ($) { Line 4255  sub _tree_construction_main ($) {
4255          S7S2: {          S7S2: {
4256            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4257              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4258                  !!!cp ('t63');
4259                $node_i_in_active = $_;                $node_i_in_active = $_;
4260                last S7S2;                last S7S2;
4261              }              }
# Line 2110  sub _tree_construction_main ($) { Line 4269  sub _tree_construction_main ($) {
4269                    
4270          ## Step 4          ## Step 4
4271          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4272              !!!cp ('t64');
4273            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4274          }          }
4275                    
4276          ## Step 5          ## Step 5
4277          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4278              !!!cp ('t65');
4279            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4280            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4281            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2132  sub _tree_construction_main ($) { Line 4293  sub _tree_construction_main ($) {
4293        } # S7          } # S7  
4294                
4295        ## Step 8        ## Step 8
4296        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4297            my $foster_parent_element;
4298            my $next_sibling;
4299            OE: for (reverse 0..$#{$self->{open_elements}}) {
4300              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4301                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4302                                 if (defined $parent and $parent->node_type == 1) {
4303                                   !!!cp ('t65.1');
4304                                   $foster_parent_element = $parent;
4305                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4306                                 } else {
4307                                   !!!cp ('t65.2');
4308                                   $foster_parent_element
4309                                     = $self->{open_elements}->[$_ - 1]->[0];
4310                                 }
4311                                 last OE;
4312                               }
4313                             } # OE
4314                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4315                               unless defined $foster_parent_element;
4316            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4317            $open_tables->[-1]->[1] = 1; # tainted
4318          } else {
4319            !!!cp ('t65.3');
4320            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4321          }
4322                
4323        ## Step 9        ## Step 9
4324        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2149  sub _tree_construction_main ($) { Line 4335  sub _tree_construction_main ($) {
4335        my $i;        my $i;
4336        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4337          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4338              !!!cp ('t66');
4339            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4340            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4341          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4342              !!!cp ('t67');
4343            $i = $_;            $i = $_;
4344          }          }
4345        } # AFE        } # AFE
# Line 2161  sub _tree_construction_main ($) { Line 4349  sub _tree_construction_main ($) {
4349        undef $i;        undef $i;
4350        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4351          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4352              !!!cp ('t68');
4353            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4354            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4355          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4356              !!!cp ('t69');
4357            $i = $_;            $i = $_;
4358          }          }
4359        } # OE        } # OE
# Line 2174  sub _tree_construction_main ($) { Line 4364  sub _tree_construction_main ($) {
4364      } # FET      } # FET
4365    }; # $formatting_end_tag    }; # $formatting_end_tag
4366    
4367    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4368      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4369    }; # $insert_to_current    }; # $insert_to_current
4370    
4371    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4372                         my $child = shift;      my $child = shift;
4373                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4374                              table => 1, tbody => 1, tfoot => 1,        # MUST
4375                              thead => 1, tr => 1,        my $foster_parent_element;
4376                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4377                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4378                           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') {  
4379                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4380                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4381                                   !!!cp ('t70');
4382                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4383                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4384                               } else {                               } else {
4385                                   !!!cp ('t71');
4386                                 $foster_parent_element                                 $foster_parent_element
4387                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4388                               }                               }
# Line 2204  sub _tree_construction_main ($) { Line 4393  sub _tree_construction_main ($) {
4393                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4394                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4395                             ($child, $next_sibling);                             ($child, $next_sibling);
4396                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4397                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4398                         }        !!!cp ('t72');
4399          $self->{open_elements}->[-1]->[0]->append_child ($child);
4400        }
4401    }; # $insert_to_foster    }; # $insert_to_foster
4402    
4403    my $in_body = sub {    B: while (1) {
4404      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4405      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4406        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4407          $script_start_tag->();        ## Ignore the token
4408          return;        ## Stay in the phase
4409        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4410          $style_start_tag->();        next B;
4411          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4412        } elsif ({               $token->{tag_name} eq 'html') {
4413                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4414                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4415          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4416          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4417          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4418          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4419          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4420            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4421          } else {        } else {
4422            $insert->($el);          !!!cp ('t81');
4423          }        }
4424            
4425          !!!next-token;        !!!cp ('t82');
4426          return;        !!!parse-error (type => 'not first start tag', token => $token);
4427        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4428          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4429          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4430          my $title_el;            !!!cp ('t84');
4431          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4432          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4433            ->append_child ($title_el);               $token->{attributes}->{$attr_name}->{value});
         $self->{content_model_flag} = 'RCDATA';  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             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;  
4434          }          }
4435                    }
4436          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4437                    !!!next-token;
4438          next B;
4439        } elsif ($token->{type} == COMMENT_TOKEN) {
4440          my $comment = $self->{document}->create_comment ($token->{data});
4441          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4442            !!!cp ('t85');
4443            $self->{document}->append_child ($comment);
4444          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4445            !!!cp ('t86');
4446            $self->{open_elements}->[0]->[0]->append_child ($comment);
4447          } else {
4448            !!!cp ('t87');
4449            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4450          }
4451          !!!next-token;
4452          next B;
4453        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4454          if ($token->{type} == CHARACTER_TOKEN) {
4455            !!!cp ('t87.1');
4456            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4457          !!!next-token;          !!!next-token;
4458          return;          next B;
4459        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4460          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4461            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4462            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4463              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4464                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4465              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4466              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4467              $formatting_end_tag->($token->{tag_name});            #
4468                        } elsif ({
4469              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4470                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4471                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4472                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4473                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4474              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4475              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4476                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4477                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4478                  last OE;            !!!cp ('t87.2');
4479                }            !!!parse-error (type => 'not closed',
4480              } # OE                            text => $self->{open_elements}->[-1]->[0]
4481              last AFE;                                ->manakai_local_name,
4482            } elsif ($node->[0] eq '#marker') {                            token => $token);
4483              last AFE;  
4484              pop @{$self->{open_elements}}
4485                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4486    
4487              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4488              ## Reprocess.
4489              next B;
4490            } else {
4491              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4492              my $tag_name = $token->{tag_name};
4493              if ($nsuri eq $SVG_NS) {
4494                $tag_name = {
4495                   altglyph => 'altGlyph',
4496                   altglyphdef => 'altGlyphDef',
4497                   altglyphitem => 'altGlyphItem',
4498                   animatecolor => 'animateColor',
4499                   animatemotion => 'animateMotion',
4500                   animatetransform => 'animateTransform',
4501                   clippath => 'clipPath',
4502                   feblend => 'feBlend',
4503                   fecolormatrix => 'feColorMatrix',
4504                   fecomponenttransfer => 'feComponentTransfer',
4505                   fecomposite => 'feComposite',
4506                   feconvolvematrix => 'feConvolveMatrix',
4507                   fediffuselighting => 'feDiffuseLighting',
4508                   fedisplacementmap => 'feDisplacementMap',
4509                   fedistantlight => 'feDistantLight',
4510                   feflood => 'feFlood',
4511                   fefunca => 'feFuncA',
4512                   fefuncb => 'feFuncB',
4513                   fefuncg => 'feFuncG',
4514                   fefuncr => 'feFuncR',
4515                   fegaussianblur => 'feGaussianBlur',
4516                   feimage => 'feImage',
4517                   femerge => 'feMerge',
4518                   femergenode => 'feMergeNode',
4519                   femorphology => 'feMorphology',
4520                   feoffset => 'feOffset',
4521                   fepointlight => 'fePointLight',
4522                   fespecularlighting => 'feSpecularLighting',
4523                   fespotlight => 'feSpotLight',
4524                   fetile => 'feTile',
4525                   feturbulence => 'feTurbulence',
4526                   foreignobject => 'foreignObject',
4527                   glyphref => 'glyphRef',
4528                   lineargradient => 'linearGradient',
4529                   radialgradient => 'radialGradient',
4530                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4531                   textpath => 'textPath',  
4532                }->{$tag_name} || $tag_name;
4533            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4534    
4535          !!!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];  
4536    
4537          !!!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', ''];  
4538    
4539          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4540          return;  
4541        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4542                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4543          $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';  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
           
         $insert->($el);  
           
         my $text = '';  
         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});  
4544            } else {            } else {
4545              !!!parse-error (type => 'in CDATA:#'.$token->{type});              !!!cp ('t87.4');
4546            }            }
4547            ## ISSUE: And ignore?  
4548              !!!next-token;
4549              next B;
4550          }          }
4551          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4552          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4553        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4554          $reconstruct_active_formatting_elements->($insert_to_current);          #
4555                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4556          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4557                    !!!parse-error (type => 'not closed',
4558          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4559          !!!next-token;                              ->manakai_local_name,
4560          return;                          token => $token);
4561        } elsif ({  
4562                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4563                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4564                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4565                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4566                 }->{$token->{tag_name}}) {          ## Reprocess.
4567          !!!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.  
4568        } else {        } else {
4569          $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;  
4570        }        }
4571      } elsif ($token->{type} eq 'end tag') {      }
4572        if ($token->{tag_name} eq 'body') {  
4573          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4574            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4575            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4576              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4577            }              !!!cp ('t88.2');
4578            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4579            !!!next-token;              #
4580            return;            } else {
4581          } else {              !!!cp ('t88.1');
4582            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              ## Ignore the token.
4583            ## 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;  
4584            }            }
4585          } # INSCOPE            unless (length $token->{data}) {
4586                        !!!cp ('t88');
4587          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4588            !!!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;  
4589            }            }
4590          } # 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]);  
4591          }          }
4592    
4593          undef $self->{form_element};          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4594          !!!next-token;            !!!cp ('t89');
4595          return;            ## As if <head>
4596        } elsif ({            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4597                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4598                 }->{$token->{tag_name}}) {            push @{$self->{open_elements}},
4599          ## 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];  
4600    
4601          ## Step 2            ## Reprocess in the "in head" insertion mode...
4602          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;  
4603    
4604              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4605              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4606            } else {            !!!cp ('t90');
4607              ## Step 3            ## As if </noscript>
4608              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4609                  #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];  
4610                        
4611            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4612            redo S2;            ## As if </head>
4613          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
4614    
4615    B: {            ## Reprocess in the "after head" insertion mode...
4616      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4617        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4618          !!!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]);  
         }  
4619    
4620          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4621          last B;          } else {
4622              !!!cp ('t92');
4623            }
4624    
4625          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4626        } else {          ## As if <body>
4627          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4628            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4629              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4630                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4631                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4632                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4633                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4634                }              !!!cp ('t93');
4635              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4636              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4637              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4638              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4639              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4640              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4641              ## 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);  
4642              !!!next-token;              !!!next-token;
4643              redo B;              next B;
4644            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4645              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4646              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4647              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4648              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4649              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4650              if ($token->{tag_name} eq 'head') {              !!!next-token;
4651                !!!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;  
             }  
4652            } else {            } else {
4653              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4654            }              !!!parse-error (type => 'in head:head',
4655          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4656            if ($token->{type} eq 'character') {              ## Ignore the token
4657              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);  
4658              !!!next-token;              !!!next-token;
4659              redo B;              next B;
4660            } elsif ($token->{type} eq 'start tag') {            }
4661              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4662                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4663                my $title_el;            ## As if <head>
4664                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4665                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4666                  ->append_child ($title_el);            push @{$self->{open_elements}},
4667                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4668    
4669                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4670                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4671                while ($token->{type} eq 'character') {          } else {
4672                  $text .= $token->{data};            !!!cp ('t97');
4673                  !!!next-token;          }
4674                }  
4675                if (length $text) {              if ($token->{tag_name} eq 'base') {
4676                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4677                }                  !!!cp ('t98');
4678                                  ## As if </noscript>
4679                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4680                    !!!parse-error (type => 'in noscript', text => 'base',
4681                                    token => $token);
4682                                
4683                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4684                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4685                } else {                } else {
4686                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4687                }                }
               !!!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);  
4688    
4689                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4690                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4691              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4692                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4693                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4694                !!!next-token;                  push @{$self->{open_elements}},
4695                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}};  
4696                } else {                } else {
4697                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4698                }                }
4699                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4700                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4701                  pop @{$self->{open_elements}} # <head>
4702                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4703                  !!!nack ('t101.1');
4704                !!!next-token;                !!!next-token;
4705                redo B;                next B;
4706              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'link') {
4707                #                ## NOTE: There is a "as if in head" code clone.
4708              } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4709                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t102');
4710                ## Ignore the token                  !!!parse-error (type => 'after head',
4711                !!!next-token;                                  text => $token->{tag_name}, token => $token);
4712                redo B;                  push @{$self->{open_elements}},
4713              }                      [$self->{head_element}, $el_category->{head}];
4714            } else {                } else {
4715              #                  !!!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;  
4716                }                }
4717              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4718                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4719              #                pop @{$self->{open_elements}} # <head>
4720            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4721              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';  
4722                !!!next-token;                !!!next-token;
4723                redo B;                next B;
4724              } elsif ($token->{tag_name} eq 'frameset') {              } elsif ($token->{tag_name} eq 'meta') {
4725                !!!insert-element ('frameset', $token->{attributes});                ## NOTE: There is a "as if in head" code clone.
4726                $self->{insertion_mode} = 'in frameset';                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4727                !!!next-token;                  !!!cp ('t104');
4728                redo B;                  !!!parse-error (type => 'after head',
4729              } elsif ({                                  text => $token->{tag_name}, token => $token);
4730                        base => 1, link => 1, meta => 1,                  push @{$self->{open_elements}},
4731                        script => 1, style => 1, title => 1,                      [$self->{head_element}, $el_category->{head}];
4732                       }->{$token->{tag_name}}) {                } else {
4733                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t105');
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!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;  
4734                }                }
4735              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4736                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
4737    
4738              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
4739              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) {
4740              ## into the current node" while characters might not be                    !!!cp ('t106');
4741              ## result in a new Text node.                    ## NOTE: Whether the encoding is supported or not is handled
4742              $reconstruct_active_formatting_elements->($insert_to_foster);                    ## in the {change_encoding} callback.
4743                                  $self->{change_encoding}
4744              if ({                        ->($self, $token->{attributes}->{charset}->{value},
4745                   table => 1, tbody => 1, tfoot => 1,                           $token);
4746                   thead => 1, tr => 1,                    
4747                  }->{$self->{open_elements}->[-1]->[1]}) {                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4748                # MUST                        ->set_user_data (manakai_has_reference =>
4749                my $foster_parent_element;                                             $token->{attributes}->{charset}
4750                my $next_sibling;                                                 ->{has_reference});
4751                my $prev_sibling;                  } elsif ($token->{attributes}->{content}) {
4752                OE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($token->{attributes}->{content}->{value}
4753                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4754                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*=
4755                    if (defined $parent and $parent->node_type == 1) {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4756                      $foster_parent_element = $parent;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4757                      $next_sibling = $self->{open_elements}->[$_]->[0];                      !!!cp ('t107');
4758                      $prev_sibling = $next_sibling->previous_sibling;                      ## NOTE: Whether the encoding is supported or not is handled
4759                        ## in the {change_encoding} callback.
4760                        $self->{change_encoding}
4761                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4762                               $token);
4763                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4764                            ->set_user_data (manakai_has_reference =>
4765                                                 $token->{attributes}->{content}
4766                                                       ->{has_reference});
4767                    } else {                    } else {
4768                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4769                    }                    }
                   last OE;  
4770                  }                  }
               } # 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});  
4771                } else {                } else {
4772                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4773                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4774                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4775                }                        ->set_user_data (manakai_has_reference =>
4776              } else {                                             $token->{attributes}->{charset}
4777                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4778              }                  }
4779                                if ($token->{attributes}->{content}) {
4780              !!!next-token;                    !!!cp ('t110');
4781              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4782            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4783              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4784              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4785              !!!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}};  
4786                }                }
4787    
4788                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4789                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4790                  !!!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}};  
4791                !!!next-token;                !!!next-token;
4792                redo B;                next B;
4793              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4794                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4795                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4796                       }->{$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]);  
4797                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4798                    !!!parse-error (type => 'in noscript', text => 'title',
4799                                    token => $token);
4800                  
4801                    $self->{insertion_mode} = IN_HEAD_IM;
4802                    ## Reprocess in the "in head" insertion mode...
4803                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4804                    !!!cp ('t112');
4805                    !!!parse-error (type => 'after head',
4806                                    text => $token->{tag_name}, token => $token);
4807                    push @{$self->{open_elements}},
4808                        [$self->{head_element}, $el_category->{head}];
4809                  } else {
4810                    !!!cp ('t113');
4811                }                }
4812    
4813                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4814                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4815                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4816                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4817                redo B;                pop @{$self->{open_elements}} # <head>
4818              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4819                ## NOTE: There are code clones for this "table in table"                next B;
4820                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4821                         $token->{tag_name} eq 'noframes') {
4822                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4823                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4824                my $i;                ## NOTE: There is a "as if in head" code clone.
4825                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4826                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4827                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4828                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4829                    last INSCOPE;                  push @{$self->{open_elements}},
4830                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4831                            table => 1, html => 1,                } else {
4832                           }->{$node->[1]}) {                  !!!cp ('t115');
4833                    last INSCOPE;                }
4834                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4835                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4836                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4837                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4838                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4839                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4840                    !!!cp ('t116');
4841                    ## NOTE: and scripting is disalbed
4842                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4843                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4844                    !!!nack ('t116.1');
4845                  !!!next-token;                  !!!next-token;
4846                  redo B;                  next B;
4847                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4848                    !!!cp ('t117');
4849                    !!!parse-error (type => 'in noscript', text => 'noscript',
4850                                    token => $token);
4851                    ## Ignore the token
4852                    !!!nack ('t117.1');
4853                    !!!next-token;
4854                    next B;
4855                  } else {
4856                    !!!cp ('t118');
4857                    #
4858                }                }
4859                } elsif ($token->{tag_name} eq 'script') {
4860                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4861                    !!!cp ('t119');
4862                    ## As if </noscript>
4863                    pop @{$self->{open_elements}};
4864                    !!!parse-error (type => 'in noscript', text => 'script',
4865                                    token => $token);
4866                                
4867                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4868                if ({                  ## Reprocess in the "in head" insertion mode...
4869                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4870                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4871                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4872                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4873                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4874                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4875                  $token = {type => 'end tag',                } else {
4876                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
4877                  redo B;                }
4878    
4879                  ## NOTE: There is a "as if in head" code clone.
4880                  $script_start_tag->();
4881                  pop @{$self->{open_elements}} # <head>
4882                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4883                  next B;
4884                } elsif ($token->{tag_name} eq 'body' or
4885                         $token->{tag_name} eq 'frameset') {
4886                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4887                    !!!cp ('t122');
4888                    ## As if </noscript>
4889                    pop @{$self->{open_elements}};
4890                    !!!parse-error (type => 'in noscript',
4891                                    text => $token->{tag_name}, token => $token);
4892                    
4893                    ## Reprocess in the "in head" insertion mode...
4894                    ## As if </head>
4895                    pop @{$self->{open_elements}};
4896                    
4897                    ## Reprocess in the "after head" insertion mode...
4898                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4899                    !!!cp ('t124');
4900                    pop @{$self->{open_elements}};
4901                    
4902                    ## Reprocess in the "after head" insertion mode...
4903                  } else {
4904                    !!!cp ('t125');
4905                }                }
4906    
4907                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## "after head" insertion mode
4908                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4909                  if ($token->{tag_name} eq 'body') {
4910                    !!!cp ('t126');
4911                    $self->{insertion_mode} = IN_BODY_IM;
4912                  } elsif ($token->{tag_name} eq 'frameset') {
4913                    !!!cp ('t127');
4914                    $self->{insertion_mode} = IN_FRAMESET_IM;
4915                  } else {
4916                    die "$0: tag name: $self->{tag_name}";
4917                }                }
4918                  !!!nack ('t127.1');
4919                  !!!next-token;
4920                  next B;
4921                } else {
4922                  !!!cp ('t128');
4923                  #
4924                }
4925    
4926                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4927                  !!!cp ('t129');
4928                  ## As if </noscript>
4929                  pop @{$self->{open_elements}};
4930                  !!!parse-error (type => 'in noscript:/',
4931                                  text => $token->{tag_name}, token => $token);
4932                  
4933                  ## Reprocess in the "in head" insertion mode...
4934                  ## As if </head>
4935                  pop @{$self->{open_elements}};
4936    
4937                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
4938                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4939                  !!!cp ('t130');
4940                  ## As if </head>
4941                  pop @{$self->{open_elements}};
4942    
4943                ## reprocess                ## Reprocess in the "after head" insertion mode...
               redo B;  
4944              } else {              } else {
4945                #                !!!cp ('t131');
4946              }              }
4947            } elsif ($token->{type} eq 'end tag') {  
4948              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4949                ## have a table element in table scope              ## As if <body>
4950                my $i;              !!!insert-element ('body',, $token);
4951                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4952                  my $node = $self->{open_elements}->[$_];              ## reprocess
4953                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4954                    $i = $_;              next B;
4955                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4956                  } elsif ({              if ($token->{tag_name} eq 'head') {
4957                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4958                           }->{$node->[1]}) {                  !!!cp ('t132');
4959                    last INSCOPE;                  ## As if <head>
4960                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4961                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4962                unless (defined $i) {                  push @{$self->{open_elements}},
4963                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4964    
4965                    ## Reprocess in the "in head" insertion mode...
4966                    pop @{$self->{open_elements}};
4967                    $self->{insertion_mode} = AFTER_HEAD_IM;
4968                    !!!next-token;
4969                    next B;
4970                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4971                    !!!cp ('t133');
4972                    ## As if </noscript>
4973                    pop @{$self->{open_elements}};
4974                    !!!parse-error (type => 'in noscript:/',
4975                                    text => 'head', token => $token);
4976                    
4977                    ## Reprocess in the "in head" insertion mode...
4978                    pop @{$self->{open_elements}};
4979                    $self->{insertion_mode} = AFTER_HEAD_IM;
4980                    !!!next-token;
4981                    next B;
4982                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4983                    !!!cp ('t134');
4984                    pop @{$self->{open_elements}};
4985                    $self->{insertion_mode} = AFTER_HEAD_IM;
4986                    !!!next-token;
4987                    next B;
4988                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4989                    !!!cp ('t134.1');
4990                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4991                                    token => $token);
4992                  ## Ignore the token                  ## Ignore the token
4993                  !!!next-token;                  !!!next-token;
4994                  redo B;                  next B;
4995                  } else {
4996                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4997                }                }
4998                              } elsif ($token->{tag_name} eq 'noscript') {
4999                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5000                if ({                  !!!cp ('t136');
5001                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
5002                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
5003                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
5004                  !!!back-token;                  next B;
5005                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
5006                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
5007                  redo B;                  !!!cp ('t137');
5008                    !!!parse-error (type => 'unmatched end tag',
5009                                    text => 'noscript', token => $token);
5010                    ## Ignore the token ## ISSUE: An issue in the spec.
5011                    !!!next-token;
5012                    next B;
5013                  } else {
5014                    !!!cp ('t138');
5015                    #
5016                }                }
5017                } elsif ({
5018                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
5019                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
5020                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
5021                      $self->{insertion_mode} == IN_HEAD_IM or
5022                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5023                    !!!cp ('t140');
5024                    !!!parse-error (type => 'unmatched end tag',
5025                                    text => $token->{tag_name}, token => $token);
5026                    ## Ignore the token
5027                    !!!next-token;
5028                    next B;
5029                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5030                    !!!cp ('t140.1');
5031                    !!!parse-error (type => 'unmatched end tag',
5032                                    text => $token->{tag_name}, token => $token);
5033                    ## Ignore the token
5034                    !!!next-token;
5035                    next B;
5036                  } else {
5037                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
5038                }                }
5039                } elsif ($token->{tag_name} eq 'p') {
5040                  !!!cp ('t142');
5041                  !!!parse-error (type => 'unmatched end tag',
5042                                  text => $token->{tag_name}, token => $token);
5043                  ## Ignore the token
5044                  !!!next-token;
5045                  next B;
5046                } elsif ($token->{tag_name} eq 'br') {
5047                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5048                    !!!cp ('t142.2');
5049                    ## (before head) as if <head>, (in head) as if </head>
5050                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5051                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
5052                    $self->{insertion_mode} = AFTER_HEAD_IM;
5053      
5054                    ## Reprocess in the "after head" insertion mode...
5055                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5056                    !!!cp ('t143.2');
5057                    ## As if </head>
5058                    pop @{$self->{open_elements}};
5059                    $self->{insertion_mode} = AFTER_HEAD_IM;
5060      
5061                    ## Reprocess in the "after head" insertion mode...
5062                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5063                    !!!cp ('t143.3');
5064                    ## ISSUE: Two parse errors for <head><noscript></br>
5065                    !!!parse-error (type => 'unmatched end tag',
5066                                    text => 'br', token => $token);
5067                    ## As if </noscript>
5068                    pop @{$self->{open_elements}};
5069                    $self->{insertion_mode} = IN_HEAD_IM;
5070    
5071                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
5072                    ## As if </head>
5073                    pop @{$self->{open_elements}};
5074                    $self->{insertion_mode} = AFTER_HEAD_IM;
5075    
5076                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
5077                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5078                    !!!cp ('t143.4');
5079                    #
5080                  } else {
5081                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
5082                  }
5083    
5084                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5085                  !!!parse-error (type => 'unmatched end tag',
5086                                  text => 'br', token => $token);
5087                  ## Ignore the token
5088                !!!next-token;                !!!next-token;
5089                redo B;                next B;
5090              } elsif ({              } else {
5091                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
5092                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
5093                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5094                ## Ignore the token                ## Ignore the token
5095                !!!next-token;                !!!next-token;
5096                redo B;                next B;
5097                }
5098    
5099                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5100                  !!!cp ('t146');
5101                  ## As if </noscript>
5102                  pop @{$self->{open_elements}};
5103                  !!!parse-error (type => 'in noscript:/',
5104                                  text => $token->{tag_name}, token => $token);
5105                  
5106                  ## Reprocess in the "in head" insertion mode...
5107                  ## As if </head>
5108                  pop @{$self->{open_elements}};
5109    
5110                  ## Reprocess in the "after head" insertion mode...
5111                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5112                  !!!cp ('t147');
5113                  ## As if </head>
5114                  pop @{$self->{open_elements}};
5115    
5116                  ## Reprocess in the "after head" insertion mode...
5117                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5118    ## ISSUE: This case cannot be reached?
5119                  !!!cp ('t148');
5120                  !!!parse-error (type => 'unmatched end tag',
5121                                  text => $token->{tag_name}, token => $token);
5122                  ## Ignore the token ## ISSUE: An issue in the spec.
5123                  !!!next-token;
5124                  next B;
5125              } else {              } else {
5126                #                !!!cp ('t149');
5127              }              }
           } else {  
             #  
           }  
5128    
5129            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
5130            $in_body->($insert_to_foster);              ## As if <body>
5131            redo B;              !!!insert-element ('body',, $token);
5132          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
5133            if ($token->{type} eq 'character') {              ## reprocess
5134              ## NOTE: This is a code clone of "character in body".              next B;
5135          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5136            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5137              !!!cp ('t149.1');
5138    
5139              ## NOTE: As if <head>
5140              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5141              $self->{open_elements}->[-1]->[0]->append_child
5142                  ($self->{head_element});
5143              #push @{$self->{open_elements}},
5144              #    [$self->{head_element}, $el_category->{head}];
5145              #$self->{insertion_mode} = IN_HEAD_IM;
5146              ## NOTE: Reprocess.
5147    
5148              ## NOTE: As if </head>
5149              #pop @{$self->{open_elements}};
5150              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5151              ## NOTE: Reprocess.
5152              
5153              #
5154            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5155              !!!cp ('t149.2');
5156    
5157              ## NOTE: As if </head>
5158              pop @{$self->{open_elements}};
5159              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5160              ## NOTE: Reprocess.
5161    
5162              #
5163            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5164              !!!cp ('t149.3');
5165    
5166              !!!parse-error (type => 'in noscript:#eof', token => $token);
5167    
5168              ## As if </noscript>
5169              pop @{$self->{open_elements}};
5170              #$self->{insertion_mode} = IN_HEAD_IM;
5171              ## NOTE: Reprocess.
5172    
5173              ## NOTE: As if </head>
5174              pop @{$self->{open_elements}};
5175              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5176              ## NOTE: Reprocess.
5177    
5178              #
5179            } else {
5180              !!!cp ('t149.4');
5181              #
5182            }
5183    
5184            ## NOTE: As if <body>
5185            !!!insert-element ('body',, $token);
5186            $self->{insertion_mode} = IN_BODY_IM;
5187            ## NOTE: Reprocess.
5188            next B;
5189          } else {
5190            die "$0: $token->{type}: Unknown token type";
5191          }
5192    
5193              ## ISSUE: An issue in the spec.
5194        } elsif ($self->{insertion_mode} & BODY_IMS) {
5195              if ($token->{type} == CHARACTER_TOKEN) {
5196                !!!cp ('t150');
5197                ## NOTE: There is a code clone of "character in body".
5198              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5199                            
5200              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5201    
5202              !!!next-token;              !!!next-token;
5203              redo B;              next B;
5204            } 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') {  
5205              if ({              if ({
5206                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
5207                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
5208                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5209                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
5210                    ## have an element in table scope
5211                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
5212                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
5213                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
5214                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
5215                  my $node = $self->{open_elements}->[$_];  
5216                  if ($node->[1] eq 'caption') {                      ## Close the cell
5217                    $i = $_;                      !!!back-token; # <x>
5218                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
5219                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
5220                            table => 1, html => 1,                                line => $token->{line},
5221                           }->{$node->[1]}) {                                column => $token->{column}};
5222                    last INSCOPE;                      next B;
5223                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5224                        !!!cp ('t152');
5225                        ## ISSUE: This case can never be reached, maybe.
5226                        last;
5227                      }
5228                  }                  }
5229                } # INSCOPE  
5230                unless (defined $i) {                  !!!cp ('t153');
5231                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
5232                        text => $token->{tag_name}, token => $token);
5233                  ## Ignore the token                  ## Ignore the token
5234                    !!!nack ('t153.1');
5235                  !!!next-token;                  !!!next-token;
5236                  redo B;                  next B;
5237                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5238                                  !!!parse-error (type => 'not closed', text => 'caption',
5239                ## generate implied end tags                                  token => $token);
5240                if ({                  
5241                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
5242                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
5243                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
5244                  !!!back-token; # <?>                  INSCOPE: {
5245                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
5246                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
5247                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
5248                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
5249                  redo B;                        $i = $_;
5250                }                        last INSCOPE;
5251                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5252                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
5253                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
5254                }                      }
5255                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
5256    
5257                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
5258                      !!!parse-error (type => 'start tag not allowed',
5259                                      text => $token->{tag_name}, token => $token);
5260                      ## Ignore the token
5261                      !!!nack ('t157.1');
5262                      !!!next-token;
5263                      next B;
5264                    } # INSCOPE
5265                    
5266                    ## generate implied end tags
5267                    while ($self->{open_elements}->[-1]->[1]
5268                               & END_TAG_OPTIONAL_EL) {
5269                      !!!cp ('t158');
5270                      pop @{$self->{open_elements}};
5271                    }
5272    
5273                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5274                redo B;                    !!!cp ('t159');
5275                      !!!parse-error (type => 'not closed',
5276                                      text => $self->{open_elements}->[-1]->[0]
5277                                          ->manakai_local_name,
5278                                      token => $token);
5279                    } else {
5280                      !!!cp ('t160');
5281                    }
5282                    
5283                    splice @{$self->{open_elements}}, $i;
5284                    
5285                    $clear_up_to_marker->();
5286                    
5287                    $self->{insertion_mode} = IN_TABLE_IM;
5288                    
5289                    ## reprocess
5290                    !!!ack-later;
5291                    next B;
5292                  } else {
5293                    !!!cp ('t161');
5294                    #
5295                  }
5296              } else {              } else {
5297                  !!!cp ('t162');
5298                #                #
5299              }              }
5300            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5301              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5302                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
5303                my $i;                  ## have an element in table scope
5304                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
5305                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5306                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5307                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5308                    last INSCOPE;                      !!!cp ('t163');
5309                  } elsif ({                      $i = $_;
5310                            table => 1, html => 1,                      last INSCOPE;
5311                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5312                    last INSCOPE;                      !!!cp ('t164');
5313                        last INSCOPE;
5314                      }
5315                    } # INSCOPE
5316                      unless (defined $i) {
5317                        !!!cp ('t165');
5318                        !!!parse-error (type => 'unmatched end tag',
5319                                        text => $token->{tag_name},
5320                                        token => $token);
5321                        ## Ignore the token
5322                        !!!next-token;
5323                        next B;
5324                      }
5325                    
5326                    ## generate implied end tags
5327                    while ($self->{open_elements}->[-1]->[1]
5328                               & END_TAG_OPTIONAL_EL) {
5329                      !!!cp ('t166');
5330                      pop @{$self->{open_elements}};
5331                  }                  }
5332                } # INSCOPE  
5333                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5334                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
5335                      !!!cp ('t167');
5336                      !!!parse-error (type => 'not closed',
5337                                      text => $self->{open_elements}->[-1]->[0]
5338                                          ->manakai_local_name,
5339                                      token => $token);
5340                    } else {
5341                      !!!cp ('t168');
5342                    }
5343                    
5344                    splice @{$self->{open_elements}}, $i;
5345                    
5346                    $clear_up_to_marker->();
5347                    
5348                    $self->{insertion_mode} = IN_ROW_IM;
5349                    
5350                    !!!next-token;
5351                    next B;
5352                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5353                    !!!cp ('t169');
5354                    !!!parse-error (type => 'unmatched end tag',
5355                                    text => $token->{tag_name}, token => $token);
5356                  ## Ignore the token                  ## Ignore the token
5357                  !!!next-token;                  !!!next-token;
5358                  redo B;                  next B;
5359                }                } else {
5360                                  !!!cp ('t170');
5361                ## 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;  
5362                }                }
5363                } elsif ($token->{tag_name} eq 'caption') {
5364                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
5365                    ## have a table element in table scope
5366                    my $i;
5367                    INSCOPE: {
5368                      for (reverse 0..$#{$self->{open_elements}}) {
5369                        my $node = $self->{open_elements}->[$_];
5370                        if ($node->[1] & CAPTION_EL) {
5371                          !!!cp ('t171');
5372                          $i = $_;
5373                          last INSCOPE;
5374                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5375                          !!!cp ('t172');
5376                          last;
5377                        }
5378                      }
5379    
5380                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
5381                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
5382                                      text => $token->{tag_name}, token => $token);
5383                      ## Ignore the token
5384                      !!!next-token;
5385                      next B;
5386                    } # INSCOPE
5387                    
5388                    ## generate implied end tags
5389                    while ($self->{open_elements}->[-1]->[1]
5390                               & END_TAG_OPTIONAL_EL) {
5391                      !!!cp ('t174');
5392                      pop @{$self->{open_elements}};
5393                    }
5394                    
5395                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5396                      !!!cp ('t175');
5397                      !!!parse-error (type => 'not closed',
5398                                      text => $self->{open_elements}->[-1]->[0]
5399                                          ->manakai_local_name,
5400                                      token => $token);
5401                    } else {
5402                      !!!cp ('t176');
5403                    }
5404                    
5405                    splice @{$self->{open_elements}}, $i;
5406                    
5407                    $clear_up_to_marker->();
5408                    
5409                    $self->{insertion_mode} = IN_TABLE_IM;
5410                    
5411                    !!!next-token;
5412                    next B;
5413                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5414                    !!!cp ('t177');
5415                    !!!parse-error (type => 'unmatched end tag',
5416                                    text => $token->{tag_name}, token => $token);
5417                    ## Ignore the token
5418                    !!!next-token;
5419                    next B;
5420                  } else {
5421                    !!!cp ('t178');
5422                    #
5423                }                }
5424                } elsif ({
5425                          table => 1, tbody => 1, tfoot => 1,
5426                          thead => 1, tr => 1,
5427                         }->{$token->{tag_name}} and
5428                         $self->{insertion_mode} == IN_CELL_IM) {
5429                  ## have an element in table scope
5430                  my $i;
5431                  my $tn;
5432                  INSCOPE: {
5433                    for (reverse 0..$#{$self->{open_elements}}) {
5434                      my $node = $self->{open_elements}->[$_];
5435                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5436                        !!!cp ('t179');
5437                        $i = $_;
5438    
5439                        ## Close the cell
5440                        !!!back-token; # </x>
5441                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5442                                  line => $token->{line},
5443                                  column => $token->{column}};
5444                        next B;
5445                      } elsif ($node->[1] & TABLE_CELL_EL) {
5446                        !!!cp ('t180');
5447                        $tn = $node->[0]->manakai_local_name;
5448                        ## NOTE: There is exactly one |td| or |th| element
5449                        ## in scope in the stack of open elements by definition.
5450                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5451                        ## ISSUE: Can this be reached?
5452                        !!!cp ('t181');
5453                        last;
5454                      }
5455                    }
5456    
5457                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5458                    !!!parse-error (type => 'unmatched end tag',
5459                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5460                    ## Ignore the token
5461                $self->{insertion_mode} = 'in table';                  !!!next-token;
5462                    next B;
5463                !!!next-token;                } # INSCOPE
5464                redo B;              } elsif ($token->{tag_name} eq 'table' and
5465              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5466                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5467                                  token => $token);
5468    
5469                ## As if </caption>                ## As if </caption>
5470                ## have a table element in table scope                ## have a table element in table scope
5471                my $i;                my $i;
5472                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5473                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5474                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5475                      !!!cp ('t184');
5476                    $i = $_;                    $i = $_;
5477                    last INSCOPE;                    last INSCOPE;
5478                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5479                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5480                    last INSCOPE;                    last INSCOPE;
5481                  }                  }
5482                } # INSCOPE                } # INSCOPE
5483                unless (defined $i) {                unless (defined $i) {
5484                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5485                    !!!parse-error (type => 'unmatched end tag',
5486                                    text => 'caption', token => $token);
5487                  ## Ignore the token                  ## Ignore the token
5488                  !!!next-token;                  !!!next-token;
5489                  redo B;                  next B;
5490                }                }
5491                                
5492                ## generate implied end tags                ## generate implied end tags
5493                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5494                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5495                     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;  
5496                }                }
5497    
5498                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5499                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5500                    !!!parse-error (type => 'not closed',
5501                                    text => $self->{open_elements}->[-1]->[0]
5502                                        ->manakai_local_name,
5503                                    token => $token);
5504                  } else {
5505                    !!!cp ('t189');
5506                }                }
5507    
5508                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5509    
5510                $clear_up_to_marker->();                $clear_up_to_marker->();
5511    
5512                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5513    
5514                ## reprocess                ## reprocess
5515                redo B;                next B;
5516              } elsif ({              } elsif ({
5517                        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,  
5518                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5519                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5520                ## Ignore the token                  !!!cp ('t190');
5521                redo B;                  !!!parse-error (type => 'unmatched end tag',
5522              } 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');  
5523                  ## Ignore the token                  ## Ignore the token
5524                  !!!next-token;                  !!!next-token;
5525                  redo B;                  next B;
5526                } else {                } else {
5527                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5528                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5529                }                }
5530              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5531                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5532                          thead => 1, tr => 1,
5533                         }->{$token->{tag_name}} and
5534                         $self->{insertion_mode} == IN_CAPTION_IM) {
5535                  !!!cp ('t192');
5536                  !!!parse-error (type => 'unmatched end tag',
5537                                  text => $token->{tag_name}, token => $token);
5538                ## Ignore the token                ## Ignore the token
5539                !!!next-token;                !!!next-token;
5540                redo B;                next B;
5541              } else {              } else {
5542                #                !!!cp ('t193');
5543                  #
5544              }              }
5545            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5546              #          for my $entry (@{$self->{open_elements}}) {
5547              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5548                !!!cp ('t75');
5549                !!!parse-error (type => 'in body:#eof', token => $token);
5550                last;
5551            }            }
5552            }
5553    
5554            ## As if </colgroup>          ## Stop parsing.
5555            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5556              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5557              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5558          }
5559    
5560          $insert = $insert_to_current;
5561          #
5562        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5563          if ($token->{type} == CHARACTER_TOKEN) {
5564            if (not $open_tables->[-1]->[1] and # tainted
5565                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5566              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5567                  
5568              unless (length $token->{data}) {
5569                !!!cp ('t194');
5570              !!!next-token;              !!!next-token;
5571              redo B;              next B;
5572            } else {            } else {
5573              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5574            }            }
5575          } 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;  
               }  
             }  
5576    
5577              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5578    
5579              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5580              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5581              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5582              ## result in a new Text node.              ## result in a new Text node.
5583              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5584                
5585              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]}) {  
5586                # MUST                # MUST
5587                my $foster_parent_element;                my $foster_parent_element;
5588                my $next_sibling;                my $next_sibling;
5589                my $prev_sibling;                my $prev_sibling;
5590                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5591                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5592                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5593                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5594                        !!!cp ('t196');
5595                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5596                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5597                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5598                    } else {                    } else {
5599                        !!!cp ('t197');
5600                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5601                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5602                    }                    }
# Line 3777  sub _tree_construction_main ($) { Line 5608  sub _tree_construction_main ($) {
5608                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5609                if (defined $prev_sibling and                if (defined $prev_sibling and
5610                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5611                    !!!cp ('t198');
5612                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5613                } else {                } else {
5614                    !!!cp ('t199');
5615                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5616                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5617                     $next_sibling);                     $next_sibling);
5618                }                }
5619              } else {            $open_tables->[-1]->[1] = 1; # tainted
5620                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5621              !!!cp ('t200');
5622              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5623            }
5624                
5625            !!!next-token;
5626            next B;
5627          } elsif ($token->{type} == START_TAG_TOKEN) {
5628            if ({
5629                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5630                 th => 1, td => 1,
5631                }->{$token->{tag_name}}) {
5632              if ($self->{insertion_mode} == IN_TABLE_IM) {
5633                ## Clear back to table context
5634                while (not ($self->{open_elements}->[-1]->[1]
5635                                & TABLE_SCOPING_EL)) {
5636                  !!!cp ('t201');
5637                  pop @{$self->{open_elements}};
5638              }              }
5639                            
5640              !!!next-token;              !!!insert-element ('tbody',, $token);
5641              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5642            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5643              ## Copied from 'in table'            }
5644              my $comment = $self->{document}->create_comment ($token->{data});            
5645              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5646              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5647              redo B;                !!!cp ('t202');
5648            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5649              if ({              }
5650                   tr => 1,                  
5651                   th => 1, td => 1,              ## Clear back to table body context
5652                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5653                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5654                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5655                  ## ISSUE: Can this case be reached?
5656                  pop @{$self->{open_elements}};
5657                }
5658                    
5659                    $self->{insertion_mode} = IN_ROW_IM;
5660                    if ($token->{tag_name} eq 'tr') {
5661                      !!!cp ('t204');
5662                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5663                      !!!nack ('t204');
5664                      !!!next-token;
5665                      next B;
5666                    } else {
5667                      !!!cp ('t205');
5668                      !!!insert-element ('tr',, $token);
5669                      ## reprocess in the "in row" insertion mode
5670                    }
5671                  } else {
5672                    !!!cp ('t206');
5673                }                }
5674    
5675                ## Clear back to table body context                ## Clear back to table row context
5676                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5677                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5678                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5679                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5680                }                }
5681                                
5682                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5683                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5684                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5685                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5686                } else {                
5687                  !!!insert-element ('tr');                !!!nack ('t207.1');
5688                  ## reprocess                !!!next-token;
5689                }                next B;
               redo B;  
5690              } elsif ({              } elsif ({
5691                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5692                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5693                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5694                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5695                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5696                my $i;                  ## As if </tr>
5697                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5698                  my $node = $self->{open_elements}->[$_];                  my $i;
5699                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5700                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5701                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5702                    $i = $_;                      !!!cp ('t208');
5703                    last INSCOPE;                      $i = $_;
5704                  } elsif ({                      last INSCOPE;
5705                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5706                           }->{$node->[1]}) {                      !!!cp ('t209');
5707                    last INSCOPE;                      last INSCOPE;
5708                      }
5709                    } # INSCOPE
5710                    unless (defined $i) {
5711                      !!!cp ('t210');
5712    ## TODO: This type is wrong.
5713                      !!!parse-error (type => 'unmacthed end tag',
5714                                      text => $token->{tag_name}, token => $token);
5715                      ## Ignore the token
5716                      !!!nack ('t210.1');
5717                      !!!next-token;
5718                      next B;
5719                    }
5720                    
5721                    ## Clear back to table row context
5722                    while (not ($self->{open_elements}->[-1]->[1]
5723                                    & TABLE_ROW_SCOPING_EL)) {
5724                      !!!cp ('t211');
5725                      ## ISSUE: Can this case be reached?
5726                      pop @{$self->{open_elements}};
5727                    }
5728                    
5729                    pop @{$self->{open_elements}}; # tr
5730                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5731                    if ($token->{tag_name} eq 'tr') {
5732                      !!!cp ('t212');
5733                      ## reprocess
5734                      !!!ack-later;
5735                      next B;
5736                    } else {
5737                      !!!cp ('t213');
5738                      ## reprocess in the "in table body" insertion mode...
5739                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5740                }                }
5741    
5742                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5743                while (not {                  ## have an element in table scope
5744                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5745                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5746                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5747                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5748                        !!!cp ('t214');
5749                        $i = $_;
5750                        last INSCOPE;
5751                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5752                        !!!cp ('t215');
5753                        last INSCOPE;
5754                      }
5755                    } # INSCOPE
5756                    unless (defined $i) {
5757                      !!!cp ('t216');
5758    ## TODO: This erorr type is wrong.
5759                      !!!parse-error (type => 'unmatched end tag',
5760                                      text => $token->{tag_name}, token => $token);
5761                      ## Ignore the token
5762                      !!!nack ('t216.1');
5763                      !!!next-token;
5764                      next B;
5765                    }
5766    
5767                    ## Clear back to table body context
5768                    while (not ($self->{open_elements}->[-1]->[1]
5769                                    & TABLE_ROWS_SCOPING_EL)) {
5770                      !!!cp ('t217');
5771                      ## ISSUE: Can this state be reached?
5772                      pop @{$self->{open_elements}};
5773                    }
5774                    
5775                    ## As if <{current node}>
5776                    ## have an element in table scope
5777                    ## true by definition
5778                    
5779                    ## Clear back to table body context
5780                    ## nop by definition
5781                    
5782                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5783                    $self->{insertion_mode} = IN_TABLE_IM;
5784                    ## reprocess in "in table" insertion mode...
5785                  } else {
5786                    !!!cp ('t218');
5787                }                }
5788    
5789                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5790                ## have an element in table scope                  ## Clear back to table context
5791                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5792                                    & TABLE_SCOPING_EL)) {
5793                ## Clear back to table body context                    !!!cp ('t219');
5794                ## nop by definition                    ## ISSUE: Can this state be reached?
5795                      pop @{$self->{open_elements}};
5796                pop @{$self->{open_elements}};                  }
5797                $self->{insertion_mode} = 'in table';                  
5798                ## reprocess                  !!!insert-element ('colgroup',, $token);
5799                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5800                    ## reprocess
5801                    !!!ack-later;
5802                    next B;
5803                  } elsif ({
5804                            caption => 1,
5805                            colgroup => 1,
5806                            tbody => 1, tfoot => 1, thead => 1,
5807                           }->{$token->{tag_name}}) {
5808                    ## Clear back to table context
5809                    while (not ($self->{open_elements}->[-1]->[1]
5810                                    & TABLE_SCOPING_EL)) {
5811                      !!!cp ('t220');
5812                      ## ISSUE: Can this state be reached?
5813                      pop @{$self->{open_elements}};
5814                    }
5815                    
5816                    push @$active_formatting_elements, ['#marker', '']
5817                        if $token->{tag_name} eq 'caption';
5818                    
5819                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5820                    $self->{insertion_mode} = {
5821                                               caption => IN_CAPTION_IM,
5822                                               colgroup => IN_COLUMN_GROUP_IM,
5823                                               tbody => IN_TABLE_BODY_IM,
5824                                               tfoot => IN_TABLE_BODY_IM,
5825                                               thead => IN_TABLE_BODY_IM,
5826                                              }->{$token->{tag_name}};
5827                    !!!next-token;
5828                    !!!nack ('t220.1');
5829                    next B;
5830                  } else {
5831                    die "$0: in table: <>: $token->{tag_name}";
5832                  }
5833              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5834                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5835                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5836                                      ->manakai_local_name,
5837                                  token => $token);
5838    
5839                ## As if </table>                ## As if </table>
5840                ## have a table element in table scope                ## have a table element in table scope
5841                my $i;                my $i;
5842                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5843                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5844                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5845                      !!!cp ('t221');
5846                    $i = $_;                    $i = $_;
5847                    last INSCOPE;                    last INSCOPE;
5848                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5849                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5850                    last INSCOPE;                    last INSCOPE;
5851                  }                  }
5852                } # INSCOPE                } # INSCOPE
5853                unless (defined $i) {                unless (defined $i) {
5854                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5855    ## TODO: The following is wrong, maybe.
5856                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5857                                    token => $token);
5858                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5859                    !!!nack ('t223.1');
5860                  !!!next-token;                  !!!next-token;
5861                  redo B;                  next B;
5862                }                }
5863                                
5864    ## TODO: Followings are removed from the latest spec.
5865                ## generate implied end tags                ## generate implied end tags
5866                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5867                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5868                     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;  
5869                }                }
5870    
5871                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5872                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5873                    ## NOTE: |<table><tr><table>|
5874                    !!!parse-error (type => 'not closed',
5875                                    text => $self->{open_elements}->[-1]->[0]
5876                                        ->manakai_local_name,
5877                                    token => $token);
5878                  } else {
5879                    !!!cp ('t226');
5880                }                }
5881    
5882                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5883                  pop @{$open_tables};
5884    
5885                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5886    
5887                ## reprocess            ## reprocess
5888                redo B;            !!!ack-later;
5889              } else {            next B;
5890                #          } elsif ($token->{tag_name} eq 'style') {
5891              }            if (not $open_tables->[-1]->[1]) { # tainted
5892            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5893              if ({              ## NOTE: This is a "as if in head" code clone.
5894                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5895                  }->{$token->{tag_name}}) {              next B;
5896                ## have an element in table scope            } else {
5897                my $i;              !!!cp ('t227.7');
5898                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5899                  my $node = $self->{open_elements}->[$_];            }
5900                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5901                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5902                    last INSCOPE;              !!!cp ('t227.6');
5903                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5904                            table => 1, html => 1,              $script_start_tag->();
5905                           }->{$node->[1]}) {              next B;
5906                    last INSCOPE;            } else {
5907                  }              !!!cp ('t227.5');
5908                } # INSCOPE              #
5909                unless (defined $i) {            }
5910                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5911                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5912                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5913                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5914                }                if ($type eq 'hidden') {
5915                    !!!cp ('t227.3');
5916                    !!!parse-error (type => 'in table',
5917                                    text => $token->{tag_name}, token => $token);
5918    
5919                ## 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}};  
               }  
5920    
5921                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;  
               }  
5922    
               ## 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]);  
5923                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
5924    
5925                pop @{$self->{open_elements}};                  !!!next-token;
5926                $self->{insertion_mode} = 'in table';                  !!!ack ('t227.2.1');
5927                ## reprocess                  next B;
5928                redo B;                } else {
5929              } elsif ({                  !!!cp ('t227.2');
5930                        body => 1, caption => 1, col => 1, colgroup => 1,                  #
5931                        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;  
5932              } else {              } else {
5933                  !!!cp ('t227.1');
5934                #                #
5935              }              }
5936            } else {            } else {
5937                !!!cp ('t227.4');
5938              #              #
5939            }            }
5940                      } else {
5941            ## As if in table            !!!cp ('t227');
5942            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5943            $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;  
               }  
             }  
5944    
5945              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table', text => $token->{tag_name},
5946                            token => $token);
5947    
5948              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5949              ## ISSUE: Spec says that "whenever a node would be inserted          #
5950              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5951              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5952              $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>  
5953                ## have an element in table scope                ## have an element in table scope
5954                my $i;                my $i;
5955                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5956                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5957                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5958                      !!!cp ('t228');
5959                    $i = $_;                    $i = $_;
5960                    last INSCOPE;                    last INSCOPE;
5961                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5962                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5963                    last INSCOPE;                    last INSCOPE;
5964                  }                  }
5965                } # INSCOPE                } # INSCOPE
5966                unless (defined $i) {                unless (defined $i) {
5967                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5968                    !!!parse-error (type => 'unmatched end tag',
5969                                    text => $token->{tag_name}, token => $token);
5970                  ## Ignore the token                  ## Ignore the token
5971                    !!!nack ('t230.1');
5972                  !!!next-token;                  !!!next-token;
5973                  redo B;                  next B;
5974                  } else {
5975                    !!!cp ('t232');
5976                }                }
5977    
5978                ## Clear back to table row context                ## Clear back to table row context
5979                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5980                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5981                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5982                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5983                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5984                }                }
5985    
5986                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5987                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5988                ## reprocess                !!!next-token;
5989                redo B;                !!!nack ('t231.1');
5990                  next B;
5991              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5992                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5993                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5994                    ## have an element in table scope
5995                ## As if </table>                  my $i;
5996                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5997                my $i;                    my $node = $self->{open_elements}->[$_];
5998                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5999                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
6000                  if ($node->[1] eq 'table') {                      $i = $_;
6001                    $i = $_;                      last INSCOPE;
6002                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6003                  } elsif ({                      !!!cp ('t234');
6004                            table => 1, html => 1,                      last INSCOPE;
6005                           }->{$node->[1]}) {                    }
6006                    last INSCOPE;                  } # INSCOPE
6007                    unless (defined $i) {
6008                      !!!cp ('t235');
6009    ## TODO: The following is wrong.
6010                      !!!parse-error (type => 'unmatched end tag',
6011                                      text => $token->{type}, token => $token);
6012                      ## Ignore the token
6013                      !!!nack ('t236.1');
6014                      !!!next-token;
6015                      next B;
6016                  }                  }
6017                } # INSCOPE                  
6018                unless (defined $i) {                  ## Clear back to table row context
6019                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
6020                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
6021                  !!!next-token;                    !!!cp ('t236');
6022                  redo B;  ## ISSUE: Can this state be reached?
6023                }                    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;  
6024                  }                  }
6025                } # INSCOPE                  
6026                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
6027                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
6028                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
6029                  !!!next-token;                }
6030                  redo B;  
6031                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
6032                    ## have an element in table scope
6033                ## Clear back to table row context                  my $i;
6034                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6035                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
6036                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
6037                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
6038                        $i = $_;
6039                        last INSCOPE;
6040                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
6041                        !!!cp ('t238');
6042                        last INSCOPE;
6043                      }
6044                    } # INSCOPE
6045                    unless (defined $i) {
6046                      !!!cp ('t239');
6047                      !!!parse-error (type => 'unmatched end tag',
6048                                      text => $token->{tag_name}, token => $token);
6049                      ## Ignore the token
6050                      !!!nack ('t239.1');
6051                      !!!next-token;
6052                      next B;
6053                    }
6054                    
6055                    ## Clear back to table body context
6056                    while (not ($self->{open_elements}->[-1]->[1]
6057                                    & TABLE_ROWS_SCOPING_EL)) {
6058                      !!!cp ('t240');
6059                      pop @{$self->{open_elements}};
6060                    }
6061                    
6062                    ## As if <{current node}>
6063                    ## have an element in table scope
6064                    ## true by definition
6065                    
6066                    ## Clear back to table body context
6067                    ## nop by definition
6068                    
6069                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6070                    $self->{insertion_mode} = IN_TABLE_IM;
6071                    ## reprocess in the "in table" insertion mode...
6072                }                }
6073    
6074                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
6075                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
6076                !!!next-token;                ## the code for <table> in the "in table" insertion mode
6077                redo B;                ## is synced with it.
6078              } elsif ($token->{tag_name} eq 'table') {  
6079                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
6080                my $i;                my $i;
6081                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6082                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6083                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
6084                      !!!cp ('t241');
6085                    $i = $_;                    $i = $_;
6086                    last INSCOPE;                    last INSCOPE;
6087                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6088                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
6089                    last INSCOPE;                    last INSCOPE;
6090                  }                  }
6091                } # INSCOPE                } # INSCOPE
6092                unless (defined $i) {                unless (defined $i) {
6093                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
6094                    !!!parse-error (type => 'unmatched end tag',
6095                                    text => $token->{tag_name}, token => $token);
6096                  ## Ignore the token                  ## Ignore the token
6097                    !!!nack ('t243.1');
6098                  !!!next-token;                  !!!next-token;
6099                  redo B;                  next B;
6100                }                }
6101                    
6102                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
6103                while (not {                pop @{$open_tables};
6104                  tr => 1, html => 1,                
6105                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
6106                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
6107                  pop @{$self->{open_elements}};                !!!next-token;
6108                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
6109              } elsif ({              } elsif ({
6110                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6111                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
6112                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
6113                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
6114                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
6115                  my $node = $self->{open_elements}->[$_];                  my $i;
6116                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6117                    $i = $_;                    my $node = $self->{open_elements}->[$_];
6118                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6119                  } elsif ({                      !!!cp ('t247');
6120                            table => 1, html => 1,                      $i = $_;
6121                           }->{$node->[1]}) {                      last INSCOPE;
6122                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6123                        !!!cp ('t248');
6124                        last INSCOPE;
6125                      }
6126                    } # INSCOPE
6127                      unless (defined $i) {
6128                        !!!cp ('t249');
6129                        !!!parse-error (type => 'unmatched end tag',
6130                                        text => $token->{tag_name}, token => $token);
6131                        ## Ignore the token
6132                        !!!nack ('t249.1');
6133                        !!!next-token;
6134                        next B;
6135                      }
6136                    
6137                    ## As if </tr>
6138                    ## have an element in table scope
6139                    my $i;
6140                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6141                      my $node = $self->{open_elements}->[$_];
6142                      if ($node->[1] & TABLE_ROW_EL) {
6143                        !!!cp ('t250');
6144                        $i = $_;
6145                        last INSCOPE;
6146                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
6147                        !!!cp ('t251');
6148                        last INSCOPE;
6149                      }
6150                    } # INSCOPE
6151                      unless (defined $i) {
6152                        !!!cp ('t252');
6153                        !!!parse-error (type => 'unmatched end tag',
6154                                        text => 'tr', token => $token);
6155                        ## Ignore the token
6156                        !!!nack ('t252.1');
6157                        !!!next-token;
6158                        next B;
6159                      }
6160                    
6161                    ## Clear back to table row context
6162                    while (not ($self->{open_elements}->[-1]->[1]
6163                                    & TABLE_ROW_SCOPING_EL)) {
6164                      !!!cp ('t253');
6165    ## ISSUE: Can this case be reached?
6166                      pop @{$self->{open_elements}};
6167                  }                  }
6168                } # INSCOPE                  
6169                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
6170                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
6171                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
6172                }                }
6173    
               ## As if </tr>  
6174                ## have an element in table scope                ## have an element in table scope
6175                my $i;                my $i;
6176                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6177                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6178                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6179                      !!!cp ('t254');
6180                    $i = $_;                    $i = $_;
6181                    last INSCOPE;                    last INSCOPE;
6182                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6183                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6184                    last INSCOPE;                    last INSCOPE;
6185                  }                  }
6186                } # INSCOPE                } # INSCOPE
6187                unless (defined $i) {                unless (defined $i) {
6188                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
6189                    !!!parse-error (type => 'unmatched end tag',
6190                                    text => $token->{tag_name}, token => $token);
6191                  ## Ignore the token                  ## Ignore the token
6192                    !!!nack ('t256.1');
6193                  !!!next-token;                  !!!next-token;
6194                  redo B;                  next B;
6195                }                }
6196    
6197                ## Clear back to table row context                ## Clear back to table body context
6198                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6199                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6200                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6201                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6202                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6203                }                }
6204    
6205                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
6206                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
6207                ## reprocess                !!!nack ('t257.1');
6208                redo B;                !!!next-token;
6209                  next B;
6210              } elsif ({              } elsif ({
6211                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6212                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6213                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6214                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6215                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6216                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6217                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6218                !!!next-token;                            text => $token->{tag_name}, token => $token);
6219                redo B;            ## Ignore the token
6220              } else {            !!!nack ('t258.1');
6221                #             !!!next-token;
6222              }            next B;
6223            } else {          } else {
6224              #            !!!cp ('t259');
6225            }            !!!parse-error (type => 'in table:/',
6226                              text => $token->{tag_name}, token => $token);
6227    
6228            ## As if in table            $insert = $insert_to_foster;
6229            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
6230            $in_body->($insert_to_foster);          }
6231            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6232          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6233            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
6234              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
6235              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
6236                          #
6237              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6238              !!!cp ('t259.2');
6239              #
6240            }
6241    
6242              !!!next-token;          ## Stop parsing
6243              redo B;          last B;
6244            } elsif ($token->{type} eq 'comment') {        } else {
6245              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
6246              my $comment = $self->{document}->create_comment ($token->{data});        }
6247              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6248              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
6249              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6250            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6251              if ({                unless (length $token->{data}) {
6252                   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  
6253                  !!!next-token;                  !!!next-token;
6254                  redo B;                  next B;
6255                }                }
6256                }
6257                ## Close the cell              
6258                !!!back-token; # <?>              !!!cp ('t261');
6259                $token = {type => 'end tag', tag_name => $tn};              #
6260                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
6261              } else {              if ($token->{tag_name} eq 'col') {
6262                  !!!cp ('t262');
6263                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6264                  pop @{$self->{open_elements}};
6265                  !!!ack ('t262.1');
6266                  !!!next-token;
6267                  next B;
6268                } else {
6269                  !!!cp ('t263');
6270                #                #
6271              }              }
6272            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
6273              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
6274                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6275                my $i;                  !!!cp ('t264');
6276                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
6277                  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});  
6278                  ## Ignore the token                  ## Ignore the token
6279                  !!!next-token;                  !!!next-token;
6280                  redo B;                  next B;
6281                }                } else {
6282                                  !!!cp ('t265');
6283                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
6284                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
6285                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
6286                     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]);  
6287                }                }
6288                } elsif ($token->{tag_name} eq 'col') {
6289                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
6290                  !!!parse-error (type => 'unmatched end tag',
6291                $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});  
6292                ## Ignore the token                ## Ignore the token
6293                !!!next-token;                !!!next-token;
6294                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;  
6295              } else {              } else {
6296                #                !!!cp ('t267');
6297                  #
6298              }              }
6299          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6300            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6301                @{$self->{open_elements}} == 1) { # redundant, maybe
6302              !!!cp ('t270.2');
6303              ## Stop parsing.
6304              last B;
6305            } else {
6306              ## NOTE: As if </colgroup>.
6307              !!!cp ('t270.1');
6308              pop @{$self->{open_elements}}; # colgroup
6309              $self->{insertion_mode} = IN_TABLE_IM;
6310              ## Reprocess.
6311              next B;
6312            }
6313          } else {
6314            die "$0: $token->{type}: Unknown token type";
6315          }
6316    
6317              ## As if </colgroup>
6318              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6319                !!!cp ('t269');
6320    ## TODO: Wrong error type?
6321                !!!parse-error (type => 'unmatched end tag',
6322                                text => 'colgroup', token => $token);
6323                ## Ignore the token
6324                !!!nack ('t269.1');
6325                !!!next-token;
6326                next B;
6327            } else {            } else {
6328              #              !!!cp ('t270');
6329                pop @{$self->{open_elements}}; # colgroup
6330                $self->{insertion_mode} = IN_TABLE_IM;
6331                !!!ack-later;
6332                ## reprocess
6333                next B;
6334              }
6335        } elsif ($self->{insertion_mode} & SELECT_IMS) {
6336          if ($token->{type} == CHARACTER_TOKEN) {
6337            !!!cp ('t271');
6338            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6339            !!!next-token;
6340            next B;
6341          } elsif ($token->{type} == START_TAG_TOKEN) {
6342            if ($token->{tag_name} eq 'option') {
6343              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6344                !!!cp ('t272');
6345                ## As if </option>
6346                pop @{$self->{open_elements}};
6347              } else {
6348                !!!cp ('t273');
6349            }            }
             
           $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}};  
               }  
6350    
6351                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6352                !!!next-token;            !!!nack ('t273.1');
6353                redo B;            !!!next-token;
6354              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6355                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6356                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6357                  pop @{$self->{open_elements}};              !!!cp ('t274');
6358                }              ## As if </option>
6359                pop @{$self->{open_elements}};
6360              } else {
6361                !!!cp ('t275');
6362              }
6363    
6364                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6365                  ## As if </optgroup>              !!!cp ('t276');
6366                  pop @{$self->{open_elements}};              ## As if </optgroup>
6367                }              pop @{$self->{open_elements}};
6368              } else {
6369                !!!cp ('t277');
6370              }
6371    
6372                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6373                !!!next-token;            !!!nack ('t277.1');
6374                redo B;            !!!next-token;
6375              } elsif ($token->{tag_name} eq 'select') {            next B;
6376                !!!parse-error (type => 'not closed:select');          } elsif ({
6377                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6378                ## have an element in table scope                   }->{$token->{tag_name}} or
6379                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6380                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6381                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6382                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6383                    $i = $_;                     tr => 1, td => 1, th => 1,
6384                    last INSCOPE;                    }->{$token->{tag_name}})) {
6385                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6386                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6387                           }->{$node->[1]}) {                            token => $token);
6388                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6389                  }            ## as if there were </select> (otherwise).
6390                } # INSCOPE            ## have an element in table scope
6391                unless (defined $i) {            my $i;
6392                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6393                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6394                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6395                  redo B;                !!!cp ('t278');
6396                }                $i = $_;
6397                  last INSCOPE;
6398                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6399                  !!!cp ('t279');
6400                  last INSCOPE;
6401                }
6402              } # INSCOPE
6403              unless (defined $i) {
6404                !!!cp ('t280');
6405                !!!parse-error (type => 'unmatched end tag',
6406                                text => 'select', token => $token);
6407                ## Ignore the token
6408                !!!nack ('t280.1');
6409                !!!next-token;
6410                next B;
6411              }
6412                                
6413                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6414              splice @{$self->{open_elements}}, $i;
6415    
6416                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6417    
6418                !!!next-token;            if ($token->{tag_name} eq 'select') {
6419                redo B;              !!!nack ('t281.2');
6420              } else {              !!!next-token;
6421                #              next B;
6422              } else {
6423                !!!cp ('t281.1');
6424                !!!ack-later;
6425                ## Reprocess the token.
6426                next B;
6427              }
6428            } else {
6429              !!!cp ('t282');
6430              !!!parse-error (type => 'in select',
6431                              text => $token->{tag_name}, token => $token);
6432              ## Ignore the token
6433              !!!nack ('t282.1');
6434              !!!next-token;
6435              next B;
6436            }
6437          } elsif ($token->{type} == END_TAG_TOKEN) {
6438            if ($token->{tag_name} eq 'optgroup') {
6439              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6440                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6441                !!!cp ('t283');
6442                ## As if </option>
6443                splice @{$self->{open_elements}}, -2;
6444              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6445                !!!cp ('t284');
6446                pop @{$self->{open_elements}};
6447              } else {
6448                !!!cp ('t285');
6449                !!!parse-error (type => 'unmatched end tag',
6450                                text => $token->{tag_name}, token => $token);
6451                ## Ignore the token
6452              }
6453              !!!nack ('t285.1');
6454              !!!next-token;
6455              next B;
6456            } elsif ($token->{tag_name} eq 'option') {
6457              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6458                !!!cp ('t286');
6459                pop @{$self->{open_elements}};
6460              } else {
6461                !!!cp ('t287');
6462                !!!parse-error (type => 'unmatched end tag',
6463                                text => $token->{tag_name}, token => $token);
6464                ## Ignore the token
6465              }
6466              !!!nack ('t287.1');
6467              !!!next-token;
6468              next B;
6469            } elsif ($token->{tag_name} eq 'select') {
6470              ## have an element in table scope
6471              my $i;
6472              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6473                my $node = $self->{open_elements}->[$_];
6474                if ($node->[1] & SELECT_EL) {
6475                  !!!cp ('t288');
6476                  $i = $_;
6477                  last INSCOPE;
6478                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6479                  !!!cp ('t289');
6480                  last INSCOPE;
6481              }              }
6482            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6483              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6484                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6485                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6486                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6487                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6488                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6489                  pop @{$self->{open_elements}};              !!!next-token;
6490                } else {              next B;
6491                  !!!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;  
               }  
6492                                
6493                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6494              splice @{$self->{open_elements}}, $i;
6495    
6496                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6497    
6498                !!!next-token;            !!!nack ('t291.1');
6499                redo B;            !!!next-token;
6500              } elsif ({            next B;
6501                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6502                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6503                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6504                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6505                                   }->{$token->{tag_name}}) {
6506                ## have an element in table scope  ## TODO: The following is wrong?
6507                my $i;            !!!parse-error (type => 'unmatched end tag',
6508                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;  
               }  
6509                                
6510                ## As if </select>            ## have an element in table scope
6511                ## have an element in table scope            my $i;
6512                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6513                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6514                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6515                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6516                    $i = $_;                $i = $_;
6517                    last INSCOPE;                last INSCOPE;
6518                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6519                            table => 1, html => 1,                !!!cp ('t293');
6520                           }->{$node->[1]}) {                last INSCOPE;
6521                    last INSCOPE;              }
6522                  }            } # INSCOPE
6523                } # INSCOPE            unless (defined $i) {
6524                unless (defined $i) {              !!!cp ('t294');
6525                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6526                  ## Ignore the </select> token              !!!nack ('t294.1');
6527                  !!!next-token; ## TODO: ok?              !!!next-token;
6528                  redo B;              next B;
6529                }            }
6530                                
6531                splice @{$self->{open_elements}}, $i;            ## As if </select>
6532              ## have an element in table scope
6533                $self->_reset_insertion_mode;            undef $i;
6534              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6535                ## reprocess              my $node = $self->{open_elements}->[$_];
6536                redo B;              if ($node->[1] & SELECT_EL) {
6537              } else {                !!!cp ('t295');
6538                #                $i = $_;
6539                  last INSCOPE;
6540                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6541    ## ISSUE: Can this state be reached?
6542                  !!!cp ('t296');
6543                  last INSCOPE;
6544              }              }
6545            } else {            } # INSCOPE
6546              #            unless (defined $i) {
6547                !!!cp ('t297');
6548    ## TODO: The following error type is correct?
6549                !!!parse-error (type => 'unmatched end tag',
6550                                text => 'select', token => $token);
6551                ## Ignore the </select> token
6552                !!!nack ('t297.1');
6553                !!!next-token; ## TODO: ok?
6554                next B;
6555            }            }
6556                  
6557              !!!cp ('t298');
6558              splice @{$self->{open_elements}}, $i;
6559    
6560              $self->_reset_insertion_mode;
6561    
6562            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6563              ## reprocess
6564              next B;
6565            } else {
6566              !!!cp ('t299');
6567              !!!parse-error (type => 'in select:/',
6568                              text => $token->{tag_name}, token => $token);
6569            ## Ignore the token            ## Ignore the token
6570              !!!nack ('t299.3');
6571            !!!next-token;            !!!next-token;
6572            redo B;            next B;
6573          } elsif ($self->{insertion_mode} eq 'after body') {          }
6574            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6575              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6576                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6577                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6578                            !!!parse-error (type => 'in body:#eof', token => $token);
6579                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6580              !!!cp ('t299.2');
6581            }
6582    
6583                unless (length $token->{data}) {          ## Stop parsing.
6584                  !!!next-token;          last B;
6585                  redo B;        } else {
6586                }          die "$0: $token->{type}: Unknown token type";
6587              }        }
6588                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6589              #        if ($token->{type} == CHARACTER_TOKEN) {
6590              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6591            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6592              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6593              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6594                  
6595              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6596              
6597              unless (length $token->{data}) {
6598                !!!cp ('t300');
6599              !!!next-token;              !!!next-token;
6600              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});  
6601            }            }
6602            }
6603            
6604            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6605              !!!cp ('t301');
6606              !!!parse-error (type => 'after html:#text', token => $token);
6607    
6608            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6609            ## reprocess          } else {
6610            redo B;            !!!cp ('t302');
6611          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6612            if ($token->{type} eq 'character') {          
6613              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6614                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6615    
6616                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6617                  !!!next-token;          ## reprocess
6618                  redo B;          next B;
6619                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6620              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6621              !!!cp ('t303');
6622              !!!parse-error (type => 'after html',
6623                              text => $token->{tag_name}, token => $token);
6624              
6625              ## Reprocess in the "after body" insertion mode.
6626            } else {
6627              !!!cp ('t304');
6628            }
6629    
6630              #          ## "after body" insertion mode
6631            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6632              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6633              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6634            $self->{insertion_mode} = IN_BODY_IM;
6635            !!!ack-later;
6636            ## reprocess
6637            next B;
6638          } elsif ($token->{type} == END_TAG_TOKEN) {
6639            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6640              !!!cp ('t305');
6641              !!!parse-error (type => 'after html:/',
6642                              text => $token->{tag_name}, token => $token);
6643              
6644              $self->{insertion_mode} = AFTER_BODY_IM;
6645              ## Reprocess in the "after body" insertion mode.
6646            } else {
6647              !!!cp ('t306');
6648            }
6649    
6650            ## "after body" insertion mode
6651            if ($token->{tag_name} eq 'html') {
6652              if (defined $self->{inner_html_node}) {
6653                !!!cp ('t307');
6654                !!!parse-error (type => 'unmatched end tag',
6655                                text => 'html', token => $token);
6656                ## Ignore the token
6657              !!!next-token;              !!!next-token;
6658              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 {  
               #  
             }  
6659            } else {            } else {
6660              #              !!!cp ('t308');
6661                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6662                !!!next-token;
6663                next B;
6664              }
6665            } else {
6666              !!!cp ('t309');
6667              !!!parse-error (type => 'after body:/',
6668                              text => $token->{tag_name}, token => $token);
6669    
6670              $self->{insertion_mode} = IN_BODY_IM;
6671              ## reprocess
6672              next B;
6673            }
6674          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6675            !!!cp ('t309.2');
6676            ## Stop parsing
6677            last B;
6678          } else {
6679            die "$0: $token->{type}: Unknown token type";
6680          }
6681        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6682          if ($token->{type} == CHARACTER_TOKEN) {
6683            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6684              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6685              
6686              unless (length $token->{data}) {
6687                !!!cp ('t310');
6688                !!!next-token;
6689                next B;
6690              }
6691            }
6692            
6693            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6694              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6695                !!!cp ('t311');
6696                !!!parse-error (type => 'in frameset:#text', token => $token);
6697              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6698                !!!cp ('t312');
6699                !!!parse-error (type => 'after frameset:#text', token => $token);
6700              } else { # "after after frameset"
6701                !!!cp ('t313');
6702                !!!parse-error (type => 'after html:#text', token => $token);
6703            }            }
6704                        
6705            if (defined $token->{tag_name}) {            ## Ignore the token.
6706              !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if (length $token->{data}) {
6707                !!!cp ('t314');
6708                ## reprocess the rest of characters
6709            } else {            } else {
6710              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
6711                !!!next-token;
6712              }
6713              next B;
6714            }
6715            
6716            die qq[$0: Character "$token->{data}"];
6717          } elsif ($token->{type} == START_TAG_TOKEN) {
6718            if ($token->{tag_name} eq 'frameset' and
6719                $self->{insertion_mode} == IN_FRAMESET_IM) {
6720              !!!cp ('t318');
6721              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6722              !!!nack ('t318.1');
6723              !!!next-token;
6724              next B;
6725            } elsif ($token->{tag_name} eq 'frame' and
6726                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6727              !!!cp ('t319');
6728              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6729              pop @{$self->{open_elements}};
6730              !!!ack ('t319.1');
6731              !!!next-token;
6732              next B;
6733            } elsif ($token->{tag_name} eq 'noframes') {
6734              !!!cp ('t320');
6735              ## NOTE: As if in head.
6736              $parse_rcdata->(CDATA_CONTENT_MODEL);
6737              next B;
6738    
6739              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6740              ## has no parse error.
6741            } else {
6742              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6743                !!!cp ('t321');
6744                !!!parse-error (type => 'in frameset',
6745                                text => $token->{tag_name}, token => $token);
6746              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6747                !!!cp ('t322');
6748                !!!parse-error (type => 'after frameset',
6749                                text => $token->{tag_name}, token => $token);
6750              } else { # "after after frameset"
6751                !!!cp ('t322.2');
6752                !!!parse-error (type => 'after after frameset',
6753                                text => $token->{tag_name}, token => $token);
6754            }            }
6755            ## Ignore the token            ## Ignore the token
6756              !!!nack ('t322.1');
6757            !!!next-token;            !!!next-token;
6758            redo B;            next B;
6759          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6760            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6761              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'frameset' and
6762                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{insertion_mode} == IN_FRAMESET_IM) {
6763              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6764                  @{$self->{open_elements}} == 1) {
6765                !!!cp ('t325');
6766                !!!parse-error (type => 'unmatched end tag',
6767                                text => $token->{tag_name}, token => $token);
6768                ## Ignore the token
6769                !!!next-token;
6770              } else {
6771                !!!cp ('t326');
6772                pop @{$self->{open_elements}};
6773                !!!next-token;
6774              }
6775    
6776                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
6777                  !!!next-token;                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6778                  redo B;              !!!cp ('t327');
6779                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6780              } else {
6781                !!!cp ('t328');
6782              }
6783              next B;
6784            } elsif ($token->{tag_name} eq 'html' and
6785                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6786              !!!cp ('t329');
6787              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6788              !!!next-token;
6789              next B;
6790            } else {
6791              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6792                !!!cp ('t330');
6793                !!!parse-error (type => 'in frameset:/',
6794                                text => $token->{tag_name}, token => $token);
6795              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6796                !!!cp ('t330.1');
6797                !!!parse-error (type => 'after frameset:/',
6798                                text => $token->{tag_name}, token => $token);
6799              } else { # "after after html"
6800                !!!cp ('t331');
6801                !!!parse-error (type => 'after after frameset:/',
6802                                text => $token->{tag_name}, token => $token);
6803              }
6804              ## Ignore the token
6805              !!!next-token;
6806              next B;
6807            }
6808          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6809            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6810                    @{$self->{open_elements}} == 1) { # redundant, maybe
6811              !!!cp ('t331.1');
6812              !!!parse-error (type => 'in body:#eof', token => $token);
6813            } else {
6814              !!!cp ('t331.2');
6815            }
6816            
6817            ## Stop parsing
6818            last B;
6819          } else {
6820            die "$0: $token->{type}: Unknown token type";
6821          }
6822    
6823          ## ISSUE: An issue in spec here
6824        } else {
6825          die "$0: $self->{insertion_mode}: Unknown insertion mode";
6826        }
6827    
6828        ## "in body" insertion mode
6829        if ($token->{type} == START_TAG_TOKEN) {
6830          if ($token->{tag_name} eq 'script') {
6831            !!!cp ('t332');
6832            ## NOTE: This is an "as if in head" code clone
6833            $script_start_tag->();
6834            next B;
6835          } elsif ($token->{tag_name} eq 'style') {
6836            !!!cp ('t333');
6837            ## NOTE: This is an "as if in head" code clone
6838            $parse_rcdata->(CDATA_CONTENT_MODEL);
6839            next B;
6840          } elsif ({
6841                    base => 1, link => 1,
6842                   }->{$token->{tag_name}}) {
6843            !!!cp ('t334');
6844            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6845            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6846            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6847            !!!ack ('t334.1');
6848            !!!next-token;
6849            next B;
6850          } elsif ($token->{tag_name} eq 'meta') {
6851            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6852            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6853            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6854    
6855            unless ($self->{confident}) {
6856              if ($token->{attributes}->{charset}) {
6857                !!!cp ('t335');
6858                ## NOTE: Whether the encoding is supported or not is handled
6859                ## in the {change_encoding} callback.
6860                $self->{change_encoding}
6861                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6862                
6863                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6864                    ->set_user_data (manakai_has_reference =>
6865                                         $token->{attributes}->{charset}
6866                                             ->{has_reference});
6867              } elsif ($token->{attributes}->{content}) {
6868                if ($token->{attributes}->{content}->{value}
6869                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6870                        [\x09-\x0D\x20]*=
6871                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6872                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6873                  !!!cp ('t336');
6874                  ## NOTE: Whether the encoding is supported or not is handled
6875                  ## in the {change_encoding} callback.
6876                  $self->{change_encoding}
6877                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6878                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6879                      ->set_user_data (manakai_has_reference =>
6880                                           $token->{attributes}->{content}
6881                                                 ->{has_reference});
6882              }              }
6883              }
6884            } else {
6885              if ($token->{attributes}->{charset}) {
6886                !!!cp ('t337');
6887                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6888                    ->set_user_data (manakai_has_reference =>
6889                                         $token->{attributes}->{charset}
6890                                             ->{has_reference});
6891              }
6892              if ($token->{attributes}->{content}) {
6893                !!!cp ('t338');
6894                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6895                    ->set_user_data (manakai_has_reference =>
6896                                         $token->{attributes}->{content}
6897                                             ->{has_reference});
6898              }
6899            }
6900    
6901              #          !!!ack ('t338.1');
6902            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6903              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6904              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6905              !!!next-token;          !!!cp ('t341');
6906              redo B;          ## NOTE: This is an "as if in head" code clone
6907            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6908              if ($token->{tag_name} eq 'noframes') {          next B;
6909                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6910                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6911              } else {                
6912                #          if (@{$self->{open_elements}} == 1 or
6913                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6914              !!!cp ('t342');
6915              ## Ignore the token
6916            } else {
6917              my $body_el = $self->{open_elements}->[1]->[0];
6918              for my $attr_name (keys %{$token->{attributes}}) {
6919                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6920                  !!!cp ('t343');
6921                  $body_el->set_attribute_ns
6922                    (undef, [undef, $attr_name],
6923                     $token->{attributes}->{$attr_name}->{value});
6924              }              }
6925            } elsif ($token->{type} eq 'end tag') {            }
6926              if ($token->{tag_name} eq 'html') {          }
6927                $phase = 'trailing end';          !!!nack ('t343.1');
6928            !!!next-token;
6929            next B;
6930          } elsif ({
6931                    address => 1, blockquote => 1, center => 1, dir => 1,
6932                    div => 1, dl => 1, fieldset => 1,
6933                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6934                    menu => 1, ol => 1, p => 1, ul => 1,
6935                    pre => 1, listing => 1,
6936                    form => 1,
6937                    table => 1,
6938                    hr => 1,
6939                   }->{$token->{tag_name}}) {
6940            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6941              !!!cp ('t350');
6942              !!!parse-error (type => 'in form:form', token => $token);
6943              ## Ignore the token
6944              !!!nack ('t350.1');
6945              !!!next-token;
6946              next B;
6947            }
6948    
6949            ## has a p element in scope
6950            INSCOPE: for (reverse @{$self->{open_elements}}) {
6951              if ($_->[1] & P_EL) {
6952                !!!cp ('t344');
6953                !!!back-token; # <form>
6954                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6955                          line => $token->{line}, column => $token->{column}};
6956                next B;
6957              } elsif ($_->[1] & SCOPING_EL) {
6958                !!!cp ('t345');
6959                last INSCOPE;
6960              }
6961            } # INSCOPE
6962              
6963            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6964            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6965              !!!nack ('t346.1');
6966              !!!next-token;
6967              if ($token->{type} == CHARACTER_TOKEN) {
6968                $token->{data} =~ s/^\x0A//;
6969                unless (length $token->{data}) {
6970                  !!!cp ('t346');
6971                !!!next-token;                !!!next-token;
               redo B;  
6972              } else {              } else {
6973                #                !!!cp ('t349');
6974              }              }
6975            } else {            } else {
6976              #              !!!cp ('t348');
6977            }            }
6978            } elsif ($token->{tag_name} eq 'form') {
6979              !!!cp ('t347.1');
6980              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6981    
6982              !!!nack ('t347.2');
6983              !!!next-token;
6984            } elsif ($token->{tag_name} eq 'table') {
6985              !!!cp ('t382');
6986              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6987                        
6988            if (defined $token->{tag_name}) {            $self->{insertion_mode} = IN_TABLE_IM;
6989              !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
6990              !!!nack ('t382.1');
6991              !!!next-token;
6992            } elsif ($token->{tag_name} eq 'hr') {
6993              !!!cp ('t386');
6994              pop @{$self->{open_elements}};
6995            
6996              !!!nack ('t386.1');
6997              !!!next-token;
6998            } else {
6999              !!!nack ('t347.1');
7000              !!!next-token;
7001            }
7002            next B;
7003          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
7004            ## has a p element in scope
7005            INSCOPE: for (reverse @{$self->{open_elements}}) {
7006              if ($_->[1] & P_EL) {
7007                !!!cp ('t353');
7008                !!!back-token; # <x>
7009                $token = {type => END_TAG_TOKEN, tag_name => 'p',
7010                          line => $token->{line}, column => $token->{column}};
7011                next B;
7012              } elsif ($_->[1] & SCOPING_EL) {
7013                !!!cp ('t354');
7014                last INSCOPE;
7015              }
7016            } # INSCOPE
7017              
7018            ## Step 1
7019            my $i = -1;
7020            my $node = $self->{open_elements}->[$i];
7021            my $li_or_dtdd = {li => {li => 1},
7022                              dt => {dt => 1, dd => 1},
7023                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
7024            LI: {
7025              ## Step 2
7026              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
7027                if ($i != -1) {
7028                  !!!cp ('t355');
7029                  !!!parse-error (type => 'not closed',
7030                                  text => $self->{open_elements}->[-1]->[0]
7031                                      ->manakai_local_name,
7032                                  token => $token);
7033                } else {
7034                  !!!cp ('t356');
7035                }
7036                splice @{$self->{open_elements}}, $i;
7037                last LI;
7038            } else {            } else {
7039              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
7040              }
7041              
7042              ## Step 3
7043              if (not ($node->[1] & FORMATTING_EL) and
7044                  #not $phrasing_category->{$node->[1]} and
7045                  ($node->[1] & SPECIAL_EL or
7046                   $node->[1] & SCOPING_EL) and
7047                  not ($node->[1] & ADDRESS_EL) and
7048                  not ($node->[1] & DIV_EL)) {
7049                !!!cp ('t358');
7050                last LI;
7051              }
7052              
7053              !!!cp ('t359');
7054              ## Step 4
7055              $i--;
7056              $node = $self->{open_elements}->[$i];
7057              redo LI;
7058            } # LI
7059              
7060            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7061            !!!nack ('t359.1');
7062            !!!next-token;
7063            next B;
7064          } elsif ($token->{tag_name} eq 'plaintext') {
7065            ## has a p element in scope
7066            INSCOPE: for (reverse @{$self->{open_elements}}) {
7067              if ($_->[1] & P_EL) {
7068                !!!cp ('t367');
7069                !!!back-token; # <plaintext>
7070                $token = {type => END_TAG_TOKEN, tag_name => 'p',
7071                          line => $token->{line}, column => $token->{column}};
7072                next B;
7073              } elsif ($_->[1] & SCOPING_EL) {
7074                !!!cp ('t368');
7075                last INSCOPE;
7076              }
7077            } # INSCOPE
7078              
7079            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7080              
7081            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7082              
7083            !!!nack ('t368.1');
7084            !!!next-token;
7085            next B;
7086          } elsif ($token->{tag_name} eq 'a') {
7087            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7088              my $node = $active_formatting_elements->[$i];
7089              if ($node->[1] & A_EL) {
7090                !!!cp ('t371');
7091                !!!parse-error (type => 'in a:a', token => $token);
7092                
7093                !!!back-token; # <a>
7094                $token = {type => END_TAG_TOKEN, tag_name => 'a',
7095                          line => $token->{line}, column => $token->{column}};
7096                $formatting_end_tag->($token);
7097                
7098                AFE2: for (reverse 0..$#$active_formatting_elements) {
7099                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7100                    !!!cp ('t372');
7101                    splice @$active_formatting_elements, $_, 1;
7102                    last AFE2;
7103                  }
7104                } # AFE2
7105                OE: for (reverse 0..$#{$self->{open_elements}}) {
7106                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7107                    !!!cp ('t373');
7108                    splice @{$self->{open_elements}}, $_, 1;
7109                    last OE;
7110                  }
7111                } # OE
7112                last AFE;
7113              } elsif ($node->[0] eq '#marker') {
7114                !!!cp ('t374');
7115                last AFE;
7116            }            }
7117            } # AFE
7118              
7119            $reconstruct_active_formatting_elements->($insert_to_current);
7120    
7121            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7122            push @$active_formatting_elements, $self->{open_elements}->[-1];
7123    
7124            !!!nack ('t374.1');
7125            !!!next-token;
7126            next B;
7127          } elsif ($token->{tag_name} eq 'nobr') {
7128            $reconstruct_active_formatting_elements->($insert_to_current);
7129    
7130            ## has a |nobr| element in scope
7131            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7132              my $node = $self->{open_elements}->[$_];
7133              if ($node->[1] & NOBR_EL) {
7134                !!!cp ('t376');
7135                !!!parse-error (type => 'in nobr:nobr', token => $token);
7136                !!!back-token; # <nobr>
7137                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7138                          line => $token->{line}, column => $token->{column}};
7139                next B;
7140              } elsif ($node->[1] & SCOPING_EL) {
7141                !!!cp ('t377');
7142                last INSCOPE;
7143              }
7144            } # INSCOPE
7145            
7146            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7147            push @$active_formatting_elements, $self->{open_elements}->[-1];
7148            
7149            !!!nack ('t377.1');
7150            !!!next-token;
7151            next B;
7152          } elsif ($token->{tag_name} eq 'button') {
7153            ## has a button element in scope
7154            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7155              my $node = $self->{open_elements}->[$_];
7156              if ($node->[1] & BUTTON_EL) {
7157                !!!cp ('t378');
7158                !!!parse-error (type => 'in button:button', token => $token);
7159                !!!back-token; # <button>
7160                $token = {type => END_TAG_TOKEN, tag_name => 'button',
7161                          line => $token->{line}, column => $token->{column}};
7162                next B;
7163              } elsif ($node->[1] & SCOPING_EL) {
7164                !!!cp ('t379');
7165                last INSCOPE;
7166              }
7167            } # INSCOPE
7168              
7169            $reconstruct_active_formatting_elements->($insert_to_current);
7170              
7171            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7172    
7173            ## TODO: associate with $self->{form_element} if defined
7174    
7175            push @$active_formatting_elements, ['#marker', ''];
7176    
7177            !!!nack ('t379.1');
7178            !!!next-token;
7179            next B;
7180          } elsif ({
7181                    xmp => 1,
7182                    iframe => 1,
7183                    noembed => 1,
7184                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7185                    noscript => 0, ## TODO: 1 if scripting is enabled
7186                   }->{$token->{tag_name}}) {
7187            if ($token->{tag_name} eq 'xmp') {
7188              !!!cp ('t381');
7189              $reconstruct_active_formatting_elements->($insert_to_current);
7190            } else {
7191              !!!cp ('t399');
7192            }
7193            ## NOTE: There is an "as if in body" code clone.
7194            $parse_rcdata->(CDATA_CONTENT_MODEL);
7195            next B;
7196          } elsif ($token->{tag_name} eq 'isindex') {
7197            !!!parse-error (type => 'isindex', token => $token);
7198            
7199            if (defined $self->{form_element}) {
7200              !!!cp ('t389');
7201            ## Ignore the token            ## Ignore the token
7202              !!!nack ('t389'); ## NOTE: Not acknowledged.
7203            !!!next-token;            !!!next-token;
7204            redo B;            next B;
7205            } else {
7206              !!!ack ('t391.1');
7207    
7208            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
7209              my $form_attrs;
7210              $form_attrs->{action} = $at->{action} if $at->{action};
7211              my $prompt_attr = $at->{prompt};
7212              $at->{name} = {name => 'name', value => 'isindex'};
7213              delete $at->{action};
7214              delete $at->{prompt};
7215              my @tokens = (
7216                            {type => START_TAG_TOKEN, tag_name => 'form',
7217                             attributes => $form_attrs,
7218                             line => $token->{line}, column => $token->{column}},
7219                            {type => START_TAG_TOKEN, tag_name => 'hr',
7220                             line => $token->{line}, column => $token->{column}},
7221                            {type => START_TAG_TOKEN, tag_name => 'p',
7222                             line => $token->{line}, column => $token->{column}},
7223                            {type => START_TAG_TOKEN, tag_name => 'label',
7224                             line => $token->{line}, column => $token->{column}},
7225                           );
7226              if ($prompt_attr) {
7227                !!!cp ('t390');
7228                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7229                               #line => $token->{line}, column => $token->{column},
7230                              };
7231              } else {
7232                !!!cp ('t391');
7233                push @tokens, {type => CHARACTER_TOKEN,
7234                               data => 'This is a searchable index. Insert your search keywords here: ',
7235                               #line => $token->{line}, column => $token->{column},
7236                              }; # SHOULD
7237                ## TODO: make this configurable
7238              }
7239              push @tokens,
7240                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7241                             line => $token->{line}, column => $token->{column}},
7242                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7243                            {type => END_TAG_TOKEN, tag_name => 'label',
7244                             line => $token->{line}, column => $token->{column}},
7245                            {type => END_TAG_TOKEN, tag_name => 'p',
7246                             line => $token->{line}, column => $token->{column}},
7247                            {type => START_TAG_TOKEN, tag_name => 'hr',
7248                             line => $token->{line}, column => $token->{column}},
7249                            {type => END_TAG_TOKEN, tag_name => 'form',
7250                             line => $token->{line}, column => $token->{column}};
7251              !!!back-token (@tokens);
7252              !!!next-token;
7253              next B;
7254            }
7255          } elsif ($token->{tag_name} eq 'textarea') {
7256            my $tag_name = $token->{tag_name};
7257            my $el;
7258            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7259            
7260            ## TODO: $self->{form_element} if defined
7261            $self->{content_model} = RCDATA_CONTENT_MODEL;
7262            delete $self->{escape}; # MUST
7263            
7264            $insert->($el);
7265            
7266            my $text = '';
7267            !!!nack ('t392.1');
7268            !!!next-token;
7269            if ($token->{type} == CHARACTER_TOKEN) {
7270              $token->{data} =~ s/^\x0A//;
7271              unless (length $token->{data}) {
7272                !!!cp ('t392');
7273                !!!next-token;
7274              } else {
7275                !!!cp ('t393');
7276              }
7277          } else {          } else {
7278            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
7279            }
7280            while ($token->{type} == CHARACTER_TOKEN) {
7281              !!!cp ('t395');
7282              $text .= $token->{data};
7283              !!!next-token;
7284            }
7285            if (length $text) {
7286              !!!cp ('t396');
7287              $el->manakai_append_text ($text);
7288            }
7289            
7290            $self->{content_model} = PCDATA_CONTENT_MODEL;
7291            
7292            if ($token->{type} == END_TAG_TOKEN and
7293                $token->{tag_name} eq $tag_name) {
7294              !!!cp ('t397');
7295              ## Ignore the token
7296            } else {
7297              !!!cp ('t398');
7298              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7299          }          }
       }  
     } 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  
7300          !!!next-token;          !!!next-token;
7301          redo B;          next B;
7302        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
7303          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
7304          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
7305            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7306              my $node = $self->{open_elements}->[$_];
7307              if ($node->[1] & RUBY_EL) {
7308                !!!cp ('t398.1');
7309                ## generate implied end tags
7310                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7311                  !!!cp ('t398.2');
7312                  pop @{$self->{open_elements}};
7313                }
7314                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7315                  !!!cp ('t398.3');
7316                  !!!parse-error (type => 'not closed',
7317                                  text => $self->{open_elements}->[-1]->[0]
7318                                      ->manakai_local_name,
7319                                  token => $token);
7320                  pop @{$self->{open_elements}}
7321                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7322                }
7323                last INSCOPE;
7324              } elsif ($node->[1] & SCOPING_EL) {
7325                !!!cp ('t398.4');
7326                last INSCOPE;
7327              }
7328            } # INSCOPE
7329    
7330            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7331    
7332            !!!nack ('t398.5');
7333          !!!next-token;          !!!next-token;
7334          redo B;          redo B;
7335        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
7336          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
7337            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
7338            ## As if in the main phase.  
7339            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7340            ## just before the phase has been changed to the trailing  
7341            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7342            $reconstruct_active_formatting_elements->($insert_to_current)  
7343              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
7344            
7345            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7346            
7347            if ($self->{self_closing}) {
7348              pop @{$self->{open_elements}};
7349              !!!ack ('t398.1');
7350            } else {
7351              !!!cp ('t398.2');
7352              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7353              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7354              ## mode, "in body" (not "in foreign content") secondary insertion
7355              ## mode, maybe.
7356            }
7357    
7358            !!!next-token;
7359            next B;
7360          } elsif ({
7361                    caption => 1, col => 1, colgroup => 1, frame => 1,
7362                    frameset => 1, head => 1, option => 1, optgroup => 1,
7363                    tbody => 1, td => 1, tfoot => 1, th => 1,
7364                    thead => 1, tr => 1,
7365                   }->{$token->{tag_name}}) {
7366            !!!cp ('t401');
7367            !!!parse-error (type => 'in body',
7368                            text => $token->{tag_name}, token => $token);
7369            ## Ignore the token
7370            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7371            !!!next-token;
7372            next B;
7373            
7374            ## ISSUE: An issue on HTML5 new elements in the spec.
7375          } else {
7376            if ($token->{tag_name} eq 'image') {
7377              !!!cp ('t384');
7378              !!!parse-error (type => 'image', token => $token);
7379              $token->{tag_name} = 'img';
7380            } else {
7381              !!!cp ('t385');
7382            }
7383    
7384            ## NOTE: There is an "as if <br>" code clone.
7385            $reconstruct_active_formatting_elements->($insert_to_current);
7386            
7387            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7388    
7389            if ({
7390                 applet => 1, marquee => 1, object => 1,
7391                }->{$token->{tag_name}}) {
7392              !!!cp ('t380');
7393              push @$active_formatting_elements, ['#marker', ''];
7394              !!!nack ('t380.1');
7395            } elsif ({
7396                      b => 1, big => 1, em => 1, font => 1, i => 1,
7397                      s => 1, small => 1, strile => 1,
7398                      strong => 1, tt => 1, u => 1,
7399                     }->{$token->{tag_name}}) {
7400              !!!cp ('t375');
7401              push @$active_formatting_elements, $self->{open_elements}->[-1];
7402              !!!nack ('t375.1');
7403            } elsif ($token->{tag_name} eq 'input') {
7404              !!!cp ('t388');
7405              ## TODO: associate with $self->{form_element} if defined
7406              pop @{$self->{open_elements}};
7407              !!!ack ('t388.2');
7408            } elsif ({
7409                      area => 1, basefont => 1, bgsound => 1, br => 1,
7410                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7411                      #image => 1,
7412                     }->{$token->{tag_name}}) {
7413              !!!cp ('t388.1');
7414              pop @{$self->{open_elements}};
7415              !!!ack ('t388.3');
7416            } elsif ($token->{tag_name} eq 'select') {
7417              ## TODO: associate with $self->{form_element} if defined
7418            
7419              if ($self->{insertion_mode} & TABLE_IMS or
7420                  $self->{insertion_mode} & BODY_TABLE_IMS or
7421                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7422                !!!cp ('t400.1');
7423                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7424              } else {
7425                !!!cp ('t400.2');
7426                $self->{insertion_mode} = IN_SELECT_IM;
7427              }
7428              !!!nack ('t400.3');
7429            } else {
7430              !!!nack ('t402');
7431            }
7432            
7433            !!!next-token;
7434            next B;
7435          }
7436        } elsif ($token->{type} == END_TAG_TOKEN) {
7437          if ($token->{tag_name} eq 'body') {
7438            ## has a |body| element in scope
7439            my $i;
7440            INSCOPE: {
7441              for (reverse @{$self->{open_elements}}) {
7442                if ($_->[1] & BODY_EL) {
7443                  !!!cp ('t405');
7444                  $i = $_;
7445                  last INSCOPE;
7446                } elsif ($_->[1] & SCOPING_EL) {
7447                  !!!cp ('t405.1');
7448                  last;
7449                }
7450              }
7451    
7452              !!!parse-error (type => 'start tag not allowed',
7453                              text => $token->{tag_name}, token => $token);
7454              ## NOTE: Ignore the token.
7455              !!!next-token;
7456              next B;
7457            } # INSCOPE
7458    
7459            for (@{$self->{open_elements}}) {
7460              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7461                !!!cp ('t403');
7462                !!!parse-error (type => 'not closed',
7463                                text => $_->[0]->manakai_local_name,
7464                                token => $token);
7465                last;
7466              } else {
7467                !!!cp ('t404');
7468              }
7469            }
7470    
7471            $self->{insertion_mode} = AFTER_BODY_IM;
7472            !!!next-token;
7473            next B;
7474          } elsif ($token->{tag_name} eq 'html') {
7475            ## TODO: Update this code.  It seems that the code below is not
7476            ## up-to-date, though it has same effect as speced.
7477            if (@{$self->{open_elements}} > 1 and
7478                $self->{open_elements}->[1]->[1] & BODY_EL) {
7479              ## ISSUE: There is an issue in the spec.
7480              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7481                !!!cp ('t406');
7482                !!!parse-error (type => 'not closed',
7483                                text => $self->{open_elements}->[1]->[0]
7484                                    ->manakai_local_name,
7485                                token => $token);
7486              } else {
7487                !!!cp ('t407');
7488              }
7489              $self->{insertion_mode} = AFTER_BODY_IM;
7490              ## reprocess
7491              next B;
7492            } else {
7493              !!!cp ('t408');
7494              !!!parse-error (type => 'unmatched end tag',
7495                              text => $token->{tag_name}, token => $token);
7496              ## Ignore the token
7497              !!!next-token;
7498              next B;
7499            }
7500          } elsif ({
7501                    address => 1, blockquote => 1, center => 1, dir => 1,
7502                    div => 1, dl => 1, fieldset => 1, listing => 1,
7503                    menu => 1, ol => 1, pre => 1, ul => 1,
7504                    dd => 1, dt => 1, li => 1,
7505                    applet => 1, button => 1, marquee => 1, object => 1,
7506                   }->{$token->{tag_name}}) {
7507            ## has an element in scope
7508            my $i;
7509            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7510              my $node = $self->{open_elements}->[$_];
7511              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7512                !!!cp ('t410');
7513                $i = $_;
7514                last INSCOPE;
7515              } elsif ($node->[1] & SCOPING_EL) {
7516                !!!cp ('t411');
7517                last INSCOPE;
7518              }
7519            } # INSCOPE
7520    
7521            unless (defined $i) { # has an element in scope
7522              !!!cp ('t413');
7523              !!!parse-error (type => 'unmatched end tag',
7524                              text => $token->{tag_name}, token => $token);
7525              ## NOTE: Ignore the token.
7526            } else {
7527              ## Step 1. generate implied end tags
7528              while ({
7529                      ## END_TAG_OPTIONAL_EL
7530                      dd => ($token->{tag_name} ne 'dd'),
7531                      dt => ($token->{tag_name} ne 'dt'),
7532                      li => ($token->{tag_name} ne 'li'),
7533                      p => 1,
7534                      rt => 1,
7535                      rp => 1,
7536                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7537                !!!cp ('t409');
7538                pop @{$self->{open_elements}};
7539              }
7540    
7541              ## Step 2.
7542              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7543                      ne $token->{tag_name}) {
7544                !!!cp ('t412');
7545                !!!parse-error (type => 'not closed',
7546                                text => $self->{open_elements}->[-1]->[0]
7547                                    ->manakai_local_name,
7548                                token => $token);
7549              } else {
7550                !!!cp ('t414');
7551              }
7552    
7553              ## Step 3.
7554              splice @{$self->{open_elements}}, $i;
7555    
7556              ## Step 4.
7557              $clear_up_to_marker->()
7558                  if {
7559                    applet => 1, button => 1, marquee => 1, object => 1,
7560                  }->{$token->{tag_name}};
7561            }
7562            !!!next-token;
7563            next B;
7564          } elsif ($token->{tag_name} eq 'form') {
7565            undef $self->{form_element};
7566    
7567            ## has an element in scope
7568            my $i;
7569            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7570              my $node = $self->{open_elements}->[$_];
7571              if ($node->[1] & FORM_EL) {
7572                !!!cp ('t418');
7573                $i = $_;
7574                last INSCOPE;
7575              } elsif ($node->[1] & SCOPING_EL) {
7576                !!!cp ('t419');
7577                last INSCOPE;
7578              }
7579            } # INSCOPE
7580    
7581            unless (defined $i) { # has an element in scope
7582              !!!cp ('t421');
7583              !!!parse-error (type => 'unmatched end tag',
7584                              text => $token->{tag_name}, token => $token);
7585              ## NOTE: Ignore the token.
7586            } else {
7587              ## Step 1. generate implied end tags
7588              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7589                !!!cp ('t417');
7590                pop @{$self->{open_elements}};
7591              }
7592                        
7593            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7594              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7595                      ne $token->{tag_name}) {
7596                !!!cp ('t417.1');
7597                !!!parse-error (type => 'not closed',
7598                                text => $self->{open_elements}->[-1]->[0]
7599                                    ->manakai_local_name,
7600                                token => $token);
7601              } else {
7602                !!!cp ('t420');
7603              }  
7604                        
7605            unless (length $token->{data}) {            ## Step 3.
7606              !!!next-token;            splice @{$self->{open_elements}}, $i;
7607              redo B;          }
7608    
7609            !!!next-token;
7610            next B;
7611          } elsif ({
7612                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7613                   }->{$token->{tag_name}}) {
7614            ## has an element in scope
7615            my $i;
7616            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7617              my $node = $self->{open_elements}->[$_];
7618              if ($node->[1] & HEADING_EL) {
7619                !!!cp ('t423');
7620                $i = $_;
7621                last INSCOPE;
7622              } elsif ($node->[1] & SCOPING_EL) {
7623                !!!cp ('t424');
7624                last INSCOPE;
7625            }            }
7626            } # INSCOPE
7627    
7628            unless (defined $i) { # has an element in scope
7629              !!!cp ('t425.1');
7630              !!!parse-error (type => 'unmatched end tag',
7631                              text => $token->{tag_name}, token => $token);
7632              ## NOTE: Ignore the token.
7633            } else {
7634              ## Step 1. generate implied end tags
7635              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7636                !!!cp ('t422');
7637                pop @{$self->{open_elements}};
7638              }
7639              
7640              ## Step 2.
7641              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7642                      ne $token->{tag_name}) {
7643                !!!cp ('t425');
7644                !!!parse-error (type => 'unmatched end tag',
7645                                text => $token->{tag_name}, token => $token);
7646              } else {
7647                !!!cp ('t426');
7648              }
7649    
7650              ## Step 3.
7651              splice @{$self->{open_elements}}, $i;
7652          }          }
7653            
7654            !!!next-token;
7655            next B;
7656          } elsif ($token->{tag_name} eq 'p') {
7657            ## has an element in scope
7658            my $i;
7659            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7660              my $node = $self->{open_elements}->[$_];
7661              if ($node->[1] & P_EL) {
7662                !!!cp ('t410.1');
7663                $i = $_;
7664                last INSCOPE;
7665              } elsif ($node->[1] & SCOPING_EL) {
7666                !!!cp ('t411.1');
7667                last INSCOPE;
7668              }
7669            } # INSCOPE
7670    
7671          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7672          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7673          ## reprocess                    ne $token->{tag_name}) {
7674          redo B;              !!!cp ('t412.1');
7675        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7676                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7677          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7678          $phase = 'main';                              token => $token);
7679          ## reprocess            } else {
7680          redo B;              !!!cp ('t414.1');
7681        } elsif ($token->{type} eq 'end-of-file') {            }
7682          ## Stop parsing  
7683          last B;            splice @{$self->{open_elements}}, $i;
7684            } else {
7685              !!!cp ('t413.1');
7686              !!!parse-error (type => 'unmatched end tag',
7687                              text => $token->{tag_name}, token => $token);
7688    
7689              !!!cp ('t415.1');
7690              ## As if <p>, then reprocess the current token
7691              my $el;
7692              !!!create-element ($el, $HTML_NS, 'p',, $token);
7693              $insert->($el);
7694              ## NOTE: Not inserted into |$self->{open_elements}|.
7695            }
7696    
7697            !!!next-token;
7698            next B;
7699          } elsif ({
7700                    a => 1,
7701                    b => 1, big => 1, em => 1, font => 1, i => 1,
7702                    nobr => 1, s => 1, small => 1, strile => 1,
7703                    strong => 1, tt => 1, u => 1,
7704                   }->{$token->{tag_name}}) {
7705            !!!cp ('t427');
7706            $formatting_end_tag->($token);
7707            next B;
7708          } elsif ($token->{tag_name} eq 'br') {
7709            !!!cp ('t428');
7710            !!!parse-error (type => 'unmatched end tag',
7711                            text => 'br', token => $token);
7712    
7713            ## As if <br>
7714            $reconstruct_active_formatting_elements->($insert_to_current);
7715            
7716            my $el;
7717            !!!create-element ($el, $HTML_NS, 'br',, $token);
7718            $insert->($el);
7719            
7720            ## Ignore the token.
7721            !!!next-token;
7722            next B;
7723          } elsif ({
7724                    caption => 1, col => 1, colgroup => 1, frame => 1,
7725                    frameset => 1, head => 1, option => 1, optgroup => 1,
7726                    tbody => 1, td => 1, tfoot => 1, th => 1,
7727                    thead => 1, tr => 1,
7728                    area => 1, basefont => 1, bgsound => 1,
7729                    embed => 1, hr => 1, iframe => 1, image => 1,
7730                    img => 1, input => 1, isindex => 1, noembed => 1,
7731                    noframes => 1, param => 1, select => 1, spacer => 1,
7732                    table => 1, textarea => 1, wbr => 1,
7733                    noscript => 0, ## TODO: if scripting is enabled
7734                   }->{$token->{tag_name}}) {
7735            !!!cp ('t429');
7736            !!!parse-error (type => 'unmatched end tag',
7737                            text => $token->{tag_name}, token => $token);
7738            ## Ignore the token
7739            !!!next-token;
7740            next B;
7741            
7742            ## ISSUE: Issue on HTML5 new elements in spec
7743            
7744        } else {        } else {
7745          die "$0: $token->{type}: Unknown token";          ## Step 1
7746            my $node_i = -1;
7747            my $node = $self->{open_elements}->[$node_i];
7748    
7749            ## Step 2
7750            S2: {
7751              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7752                ## Step 1
7753                ## generate implied end tags
7754                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7755                  !!!cp ('t430');
7756                  ## NOTE: |<ruby><rt></ruby>|.
7757                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7758                  ## which seems wrong.
7759                  pop @{$self->{open_elements}};
7760                  $node_i++;
7761                }
7762            
7763                ## Step 2
7764                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7765                        ne $token->{tag_name}) {
7766                  !!!cp ('t431');
7767                  ## NOTE: <x><y></x>
7768                  !!!parse-error (type => 'not closed',
7769                                  text => $self->{open_elements}->[-1]->[0]
7770                                      ->manakai_local_name,
7771                                  token => $token);
7772                } else {
7773                  !!!cp ('t432');
7774                }
7775                
7776                ## Step 3
7777                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7778    
7779                !!!next-token;
7780                last S2;
7781              } else {
7782                ## Step 3
7783                if (not ($node->[1] & FORMATTING_EL) and
7784                    #not $phrasing_category->{$node->[1]} and
7785                    ($node->[1] & SPECIAL_EL or
7786                     $node->[1] & SCOPING_EL)) {
7787                  !!!cp ('t433');
7788                  !!!parse-error (type => 'unmatched end tag',
7789                                  text => $token->{tag_name}, token => $token);
7790                  ## Ignore the token
7791                  !!!next-token;
7792                  last S2;
7793                }
7794    
7795                !!!cp ('t434');
7796              }
7797              
7798              ## Step 4
7799              $node_i--;
7800              $node = $self->{open_elements}->[$node_i];
7801              
7802              ## Step 5;
7803              redo S2;
7804            } # S2
7805            next B;
7806        }        }
7807      }      }
7808        next B;
7809      } continue { # B
7810        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7811          ## NOTE: The code below is executed in cases where it does not have
7812          ## to be, but it it is harmless even in those cases.
7813          ## has an element in scope
7814          INSCOPE: {
7815            for (reverse 0..$#{$self->{open_elements}}) {
7816              my $node = $self->{open_elements}->[$_];
7817              if ($node->[1] & FOREIGN_EL) {
7818                last INSCOPE;
7819              } elsif ($node->[1] & SCOPING_EL) {
7820                last;
7821              }
7822            }
7823            
7824            ## NOTE: No foreign element in scope.
7825            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7826          } # INSCOPE
7827        }
7828    } # B    } # B
7829    
7830    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4868  sub _tree_construction_main ($) { Line 7832  sub _tree_construction_main ($) {
7832    ## TODO: script stuffs    ## TODO: script stuffs
7833  } # _tree_construct_main  } # _tree_construct_main
7834    
7835  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7836    my $class = shift;    my $class = shift;
7837    my $node = shift;    my $node = shift;
7838    my $s = \$_[0];    #my $s = \$_[0];
7839    my $onerror = $_[1];    my $onerror = $_[1];
7840      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7841    
7842      ## ISSUE: Should {confident} be true?
7843    
7844    my $nt = $node->node_type;    my $nt = $node->node_type;
7845    if ($nt == 9) {    if ($nt == 9) {
# Line 4889  sub set_inner_html ($$$) { Line 7856  sub set_inner_html ($$$) {
7856      }      }
7857    
7858      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7859      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7860    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7861      ## TODO: If non-html element      ## TODO: If non-html element
7862    
7863      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7864    
7865    ## TODO: Support for $get_wrapper
7866    
7867      ## Step 1 # MUST      ## Step 1 # MUST
7868      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7869      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7870        $doc->manakai_is_html (1);
7871      my $p = $class->new;      my $p = $class->new;
7872      $p->{document} = $doc;      $p->{document} = $doc;
7873    
7874      ## Step 9 # MUST      ## Step 8 # MUST
7875      my $i = 0;      my $i = 0;
7876      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7877      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7878      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7879        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7880        $input = $get_wrapper->($input);
7881        $p->{set_nc} = sub {
7882        my $self = shift;        my $self = shift;
7883        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7884        $self->{next_input_character} = ord substr $$s, $i++, 1;        my $char = '';
7885        $column++;        if (defined $self->{next_nc}) {
7886            $char = $self->{next_nc};
7887        if ($self->{next_input_character} == 0x000A) { # LF          delete $self->{next_nc};
7888          $line++;          $self->{nc} = ord $char;
7889          $column = 0;        } else {
7890        } elsif ($self->{next_input_character} == 0x000D) { # CR          $self->{char_buffer} = '';
7891          if ($i >= length $$s) {          $self->{char_buffer_pos} = 0;
7892            #          
7893            my $count = $input->manakai_read_until
7894                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7895                 $self->{char_buffer_pos});
7896            if ($count) {
7897              $self->{line_prev} = $self->{line};
7898              $self->{column_prev} = $self->{column};
7899              $self->{column}++;
7900              $self->{nc}
7901                  = ord substr ($self->{char_buffer},
7902                                $self->{char_buffer_pos}++, 1);
7903              return;
7904            }
7905            
7906            if ($input->read ($char, 1)) {
7907              $self->{nc} = ord $char;
7908          } else {          } else {
7909            my $next_char = ord substr $$s, $i++, 1;            $self->{nc} = -1;
7910            if ($next_char == 0x000A) { # LF            return;
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
7911          }          }
7912          $self->{next_input_character} = 0x000A; # LF # MUST        }
7913          $line++;  
7914          $column = 0;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7915        } elsif ($self->{next_input_character} > 0x10FFFF) {        $p->{column}++;
7916          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
7917        } elsif ($self->{next_input_character} == 0x0000) { # NULL        if ($self->{nc} == 0x000A) { # LF
7918          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $p->{line}++;
7919            $p->{column} = 0;
7920            !!!cp ('i1');
7921          } elsif ($self->{nc} == 0x000D) { # CR
7922    ## TODO: support for abort/streaming
7923            my $next = '';
7924            if ($input->read ($next, 1) and $next ne "\x0A") {
7925              $self->{next_nc} = $next;
7926            }
7927            $self->{nc} = 0x000A; # LF # MUST
7928            $p->{line}++;
7929            $p->{column} = 0;
7930            !!!cp ('i2');
7931          } elsif ($self->{nc} == 0x0000) { # NULL
7932            !!!cp ('i4');
7933            !!!parse-error (type => 'NULL');
7934            $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7935        }        }
7936      };      };
7937        
7938        $p->{read_until} = sub {
7939          #my ($scalar, $specials_range, $offset) = @_;
7940          return 0 if defined $p->{next_nc};
7941    
7942          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7943          my $offset = $_[2] || 0;
7944          
7945          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7946            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7947            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7948              substr ($_[0], $offset)
7949                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7950              my $count = $+[0] - $-[0];
7951              if ($count) {
7952                $p->{column} += $count;
7953                $p->{char_buffer_pos} += $count;
7954                $p->{line_prev} = $p->{line};
7955                $p->{column_prev} = $p->{column} - 1;
7956                $p->{nc} = -1;
7957              }
7958              return $count;
7959            } else {
7960              return 0;
7961            }
7962          } else {
7963            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7964            if ($count) {
7965              $p->{column} += $count;
7966              $p->{column_prev} += $count;
7967              $p->{nc} = -1;
7968            }
7969            return $count;
7970          }
7971        }; # $p->{read_until}
7972    
7973      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7974        my (%opt) = @_;        my (%opt) = @_;
7975        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7976          my $column = $opt{column};
7977          if (defined $opt{token} and defined $opt{token}->{line}) {
7978            $line = $opt{token}->{line};
7979            $column = $opt{token}->{column};
7980          }
7981          warn "Parse error ($opt{type}) at line $line column $column\n";
7982      };      };
7983      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7984        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7985      };      };
7986            
7987        my $char_onerror = sub {
7988          my (undef, $type, %opt) = @_;
7989          $ponerror->(layer => 'encode',
7990                      line => $p->{line}, column => $p->{column} + 1,
7991                      %opt, type => $type);
7992        }; # $char_onerror
7993        $input->onerror ($char_onerror);
7994    
7995      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7996      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7997    
7998      ## Step 2      ## Step 2
7999      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
8000      $p->{content_model_flag} = {      $p->{content_model} = {
8001        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
8002        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
8003        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
8004        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
8005        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
8006        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
8007        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
8008        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
8009        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
8010        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
8011      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
8012         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
8013            unless defined $p->{content_model};
8014            ## ISSUE: What is "the name of the element"? local name?
8015    
8016      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
8017          ## TODO: Foreign element OK?
8018    
8019      ## Step 4      ## Step 3
8020      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
8021        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
8022    
8023      ## Step 5 # MUST      ## Step 4 # MUST
8024      $doc->append_child ($root);      $doc->append_child ($root);
8025    
8026      ## Step 6 # MUST      ## Step 5 # MUST
8027      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
8028    
8029      undef $p->{head_element};      undef $p->{head_element};
8030    
8031      ## Step 7 # MUST      ## Step 6 # MUST
8032      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
8033    
8034      ## Step 8 # MUST      ## Step 7 # MUST
8035      my $anode = $node;      my $anode = $node;
8036      AN: while (defined $anode) {      AN: while (defined $anode) {
8037        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
8038          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
8039          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
8040            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
8041                !!!cp ('i5');
8042              $p->{form_element} = $anode;              $p->{form_element} = $anode;
8043              last AN;              last AN;
8044            }            }
# Line 4994  sub set_inner_html ($$$) { Line 8047  sub set_inner_html ($$$) {
8047        $anode = $anode->parent_node;        $anode = $anode->parent_node;
8048      } # AN      } # AN
8049            
8050      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
8051      {      {
8052        my $self = $p;        my $self = $p;
8053        !!!next-token;        !!!next-token;
8054      }      }
8055      $p->_tree_construction_main;      $p->_tree_construction_main;
8056    
8057      ## Step 11 # MUST      ## Step 10 # MUST
8058      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
8059      for (@cn) {      for (@cn) {
8060        $node->remove_child ($_);        $node->remove_child ($_);
8061      }      }
8062      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
8063    
8064      ## Step 12 # MUST      ## Step 11 # MUST
8065      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
8066      for (@cn) {      for (@cn) {
8067          $this_doc->adopt_node ($_);
8068        $node->append_child ($_);        $node->append_child ($_);
8069      }      }
8070      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
8071    
8072      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8073    
8074        delete $p->{parse_error}; # delete loop
8075    } else {    } else {
8076      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";
8077    }    }
# Line 5024  sub set_inner_html ($$$) { Line 8079  sub set_inner_html ($$$) {
8079    
8080  } # tree construction stage  } # tree construction stage
8081    
8082  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
8083    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  
8084    
8085  1;  1;
8086  # $Date$  # $Date$

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.185

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24