/[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.9 by wakaba, Sat Jun 23 02:41:51 2007 UTC revision 1.162 by wakaba, Thu Sep 11 09:12:27 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## This is an early version of an HTML parser.  ## ISSUE:
7    ## var doc = implementation.createDocument (null, null, null);
8    ## doc.write ('');
9    ## alert (doc.compatMode);
10    
11    require IO::Handle;
12    
13    my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15    my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    
20    sub A_EL () { 0b1 }
21    sub ADDRESS_EL () { 0b10 }
22    sub BODY_EL () { 0b100 }
23    sub BUTTON_EL () { 0b1000 }
24    sub CAPTION_EL () { 0b10000 }
25    sub DD_EL () { 0b100000 }
26    sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233  my $permitted_slash_tag_name = {  my $svg_attr_name = {
234    base => 1,    attributename => 'attributeName',
235    link => 1,    attributetype => 'attributeType',
236    meta => 1,    basefrequency => 'baseFrequency',
237    hr => 1,    baseprofile => 'baseProfile',
238    br => 1,    calcmode => 'calcMode',
239    img=> 1,    clippathunits => 'clipPathUnits',
240    embed => 1,    contentscripttype => 'contentScriptType',
241    param => 1,    contentstyletype => 'contentStyleType',
242    area => 1,    diffuseconstant => 'diffuseConstant',
243    col => 1,    edgemode => 'edgeMode',
244    input => 1,    externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298  my $entity_char = {  my $foreign_attr_xname = {
299    AElig => "\x{00C6}",    'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300    Aacute => "\x{00C1}",    'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301    Acirc => "\x{00C2}",    'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302    Agrave => "\x{00C0}",    'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303    Alpha => "\x{0391}",    'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304    Aring => "\x{00C5}",    'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305    Atilde => "\x{00C3}",    'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306    Auml => "\x{00C4}",    'xml:base' => [$XML_NS, ['xml', 'base']],
307    Beta => "\x{0392}",    'xml:lang' => [$XML_NS, ['xml', 'lang']],
308    Ccedil => "\x{00C7}",    'xml:space' => [$XML_NS, ['xml', 'space']],
309    Chi => "\x{03A7}",    'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310    Dagger => "\x{2021}",    'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    Delta => "\x{0394}",  };
312    ETH => "\x{00D0}",  
313    Eacute => "\x{00C9}",  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 }; # $entity_char  
314    
 ## TODO: Ensure that this table match to <http://html5.org/tools/web-apps-tracker?from=868&to=869>.  
 ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>  
315  my $c1_entity_char = {  my $c1_entity_char = {
316       128, 8364,    0x80 => 0x20AC,
317       129, 65533,    0x81 => 0xFFFD,
318       130, 8218,    0x82 => 0x201A,
319       131, 402,    0x83 => 0x0192,
320       132, 8222,    0x84 => 0x201E,
321       133, 8230,    0x85 => 0x2026,
322       134, 8224,    0x86 => 0x2020,
323       135, 8225,    0x87 => 0x2021,
324       136, 710,    0x88 => 0x02C6,
325       137, 8240,    0x89 => 0x2030,
326       138, 352,    0x8A => 0x0160,
327       139, 8249,    0x8B => 0x2039,
328       140, 338,    0x8C => 0x0152,
329       141, 65533,    0x8D => 0xFFFD,
330       142, 381,    0x8E => 0x017D,
331       143, 65533,    0x8F => 0xFFFD,
332       144, 65533,    0x90 => 0xFFFD,
333       145, 8216,    0x91 => 0x2018,
334       146, 8217,    0x92 => 0x2019,
335       147, 8220,    0x93 => 0x201C,
336       148, 8221,    0x94 => 0x201D,
337       149, 8226,    0x95 => 0x2022,
338       150, 8211,    0x96 => 0x2013,
339       151, 8212,    0x97 => 0x2014,
340       152, 732,    0x98 => 0x02DC,
341       153, 8482,    0x99 => 0x2122,
342       154, 353,    0x9A => 0x0161,
343       155, 8250,    0x9B => 0x203A,
344       156, 339,    0x9C => 0x0153,
345       157, 65533,    0x9D => 0xFFFD,
346       158, 382,    0x9E => 0x017E,
347       159, 376,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
350  my $special_category = {  sub parse_byte_string ($$$$;$) {
351    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
352    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
353    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
356    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
357    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$$) {
358    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $self = ref $_[0] ? shift : shift->new;
360    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $charset_name = shift;
361  };    my $byte_stream = $_[0];
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
362    
363  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
364    my $self = shift->new;      my (%opt) = @_;
365    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
366      };
367      $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373      ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403        }
404    
405        ## Step 2
406        my $byte_buffer = '';
407        for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419          $self->{confident} = 1;
420          last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        }
436    
437        ## Step 4
438        ## TODO: <meta charset>
439    
440        ## Step 5
441        ## TODO: from history
442    
443        ## Step 6
444        require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559      }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574      my @args = @_; shift @args; # $s
575      my $return;
576      try {
577        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578      } catch Whatpm::HTML::RestartParser with {
579        ## NOTE: Invoked after {change_encoding}.
580    
581        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599        $self->{confident} = 1;
600    
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605      };
606      return $return;
607    } # parse_byte_stream
608    
609    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
611    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
612    ## because the core part of our HTML parser expects a string of character,
613    ## not a string of bytes or code units or anything which might contain a BOM.
614    ## Therefore, any parser interface that accepts a string of bytes,
615    ## such as |parse_byte_string| in this module, must ensure that it does
616    ## strip the BOM and never strip any ZWNBSP.
617    
618    sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631    sub parse_char_stream ($$$;$) {
632      my $self = ref $_[0] ? shift : shift->new;
633      my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635      @{$self->{document}->child_nodes} = ();
636    
637    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
638    
639      $self->{confident} = 1 unless exists $self->{confident};
640      $self->{document}->input_encoding ($self->{input_encoding})
641          if defined $self->{input_encoding};
642    
643    my $i = 0;    my $i = 0;
644    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
645    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
646    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
647      my $self = shift;      my $self = shift;
648      $self->{next_input_character} = -1 and return if $i >= length $$s;  
649      $self->{next_input_character} = ord substr $$s, $i++, 1;      pop @{$self->{prev_char}};
650      $column++;      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652        my $char;
653        if (defined $self->{next_next_char}) {
654          $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662        ($self->{line_prev}, $self->{column_prev})
663            = ($self->{line}, $self->{column});
664        $self->{column}++;
665            
666      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667        $line++;        !!!cp ('j1');
668        $column = 0;        $self->{line}++;
669      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
670        if ($i >= length $$s) {      } elsif ($self->{next_char} == 0x000D) { # CR
671          #        !!!cp ('j2');
672          my $next = $input->getc;
673          if (defined $next and $next ne "\x0A") {
674            $self->{next_next_char} = $next;
675          }
676          $self->{next_char} = 0x000A; # LF # MUST
677          $self->{line}++;
678          $self->{column} = 0;
679        } elsif ($self->{next_char} > 0x10FFFF) {
680          !!!cp ('j3');
681          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
682        } elsif ($self->{next_char} == 0x0000) { # NULL
683          !!!cp ('j4');
684          !!!parse-error (type => 'NULL');
685          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
686        } elsif ($self->{next_char} <= 0x0008 or
687                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
688                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
689                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
690                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
691                 {
692                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
693                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
694                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
695                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
696                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
697                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
698                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
699                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
700                  0x10FFFE => 1, 0x10FFFF => 1,
701                 }->{$self->{next_char}}) {
702          !!!cp ('j5');
703          if ($self->{next_char} < 0x10000) {
704            !!!parse-error (type => 'control char',
705                            text => (sprintf 'U+%04X', $self->{next_char}));
706        } else {        } else {
707          my $next_char = ord substr $$s, $i++, 1;          !!!parse-error (type => 'control char',
708          if ($next_char == 0x000A) { # LF                          text => (sprintf 'U-%08X', $self->{next_char}));
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
709        }        }
       $self->{next_input_character} = 0x000A; # LF # MUST  
       $line++;  
       $column = 0;  
     } elsif ($self->{next_input_character} > 0x10FFFF) {  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_input_character} == 0x0000) { # NULL  
       !!!parse-error (type => 'NULL');  
 ## TODO: test  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
710      }      }
711    };    };
712      $self->{prev_char} = [-1, -1, -1];
713      $self->{next_char} = -1;
714    
715    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
716      my (%opt) = @_;      my (%opt) = @_;
717      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
718        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
719        warn "Parse error ($opt{type}) at line $line column $column\n";
720    };    };
721    $self->{parse_error} = sub {    $self->{parse_error} = sub {
722      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
723    };    };
724    
725    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 394  sub parse_string ($$$;$) { Line 727  sub parse_string ($$$;$) {
727    $self->_construct_tree;    $self->_construct_tree;
728    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
729    
730      delete $self->{parse_error}; # remove loop
731    
732    return $self->{document};    return $self->{document};
733  } # parse_string  } # parse_char_stream
734    
735  sub new ($) {  sub new ($) {
736    my $class = shift;    my $class = shift;
737    my $self = bless {}, $class;    my $self = bless {
738    $self->{set_next_input_character} = sub {      level => {must => 'm',
739      $self->{next_input_character} = -1;                should => 's',
740                  warn => 'w',
741                  info => 'i',
742                  uncertain => 'u'},
743      }, $class;
744      $self->{set_next_char} = sub {
745        $self->{next_char} = -1;
746    };    };
747    $self->{parse_error} = sub {    $self->{parse_error} = sub {
748      #      #
749    };    };
750      $self->{change_encoding} = sub {
751        # if ($_[0] is a supported encoding) {
752        #   run "change the encoding" algorithm;
753        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
754        # }
755      };
756      $self->{application_cache_selection} = sub {
757        #
758      };
759    return $self;    return $self;
760  } # new  } # new
761    
762    sub CM_ENTITY () { 0b001 } # & markup in data
763    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
764    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
765    
766    sub PLAINTEXT_CONTENT_MODEL () { 0 }
767    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
768    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
769    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
770    
771    sub DATA_STATE () { 0 }
772    sub ENTITY_DATA_STATE () { 1 }
773    sub TAG_OPEN_STATE () { 2 }
774    sub CLOSE_TAG_OPEN_STATE () { 3 }
775    sub TAG_NAME_STATE () { 4 }
776    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
777    sub ATTRIBUTE_NAME_STATE () { 6 }
778    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
779    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
780    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
781    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
782    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
783    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
784    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
785    sub COMMENT_START_STATE () { 14 }
786    sub COMMENT_START_DASH_STATE () { 15 }
787    sub COMMENT_STATE () { 16 }
788    sub COMMENT_END_STATE () { 17 }
789    sub COMMENT_END_DASH_STATE () { 18 }
790    sub BOGUS_COMMENT_STATE () { 19 }
791    sub DOCTYPE_STATE () { 20 }
792    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
793    sub DOCTYPE_NAME_STATE () { 22 }
794    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
795    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
796    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
797    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
798    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
799    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
800    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
801    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
802    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
803    sub BOGUS_DOCTYPE_STATE () { 32 }
804    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805    sub SELF_CLOSING_START_TAG_STATE () { 34 }
806    sub CDATA_BLOCK_STATE () { 35 }
807    
808    sub DOCTYPE_TOKEN () { 1 }
809    sub COMMENT_TOKEN () { 2 }
810    sub START_TAG_TOKEN () { 3 }
811    sub END_TAG_TOKEN () { 4 }
812    sub END_OF_FILE_TOKEN () { 5 }
813    sub CHARACTER_TOKEN () { 6 }
814    
815    sub AFTER_HTML_IMS () { 0b100 }
816    sub HEAD_IMS ()       { 0b1000 }
817    sub BODY_IMS ()       { 0b10000 }
818    sub BODY_TABLE_IMS () { 0b100000 }
819    sub TABLE_IMS ()      { 0b1000000 }
820    sub ROW_IMS ()        { 0b10000000 }
821    sub BODY_AFTER_IMS () { 0b100000000 }
822    sub FRAME_IMS ()      { 0b1000000000 }
823    sub SELECT_IMS ()     { 0b10000000000 }
824    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
825        ## NOTE: "in foreign content" insertion mode is special; it is combined
826        ## with the secondary insertion mode.  In this parser, they are stored
827        ## together in the bit-or'ed form.
828    
829    ## NOTE: "initial" and "before html" insertion modes have no constants.
830    
831    ## NOTE: "after after body" insertion mode.
832    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
833    
834    ## NOTE: "after after frameset" insertion mode.
835    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
836    
837    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
838    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
839    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
840    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
841    sub IN_BODY_IM () { BODY_IMS }
842    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
843    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
844    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
845    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
846    sub IN_TABLE_IM () { TABLE_IMS }
847    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
848    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
849    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
850    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
851    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
852    sub IN_COLUMN_GROUP_IM () { 0b10 }
853    
854  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
855    
856  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
857    my $self = shift;    my $self = shift;
858    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
859    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
860    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
861    undef $self->{current_attribute};    undef $self->{current_attribute};
862    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
863    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
864      delete $self->{self_closing};
865    $self->{char} = [];    $self->{char} = [];
866    # $self->{next_input_character}    # $self->{next_char}
867    !!!next-input-character;    !!!next-input-character;
868    $self->{token} = [];    $self->{token} = [];
869      # $self->{escape}
870  } # _initialize_tokenizer  } # _initialize_tokenizer
871    
872  ## A token has:  ## A token has:
873  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
874  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
875  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
876      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
877  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
878  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
879  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
880    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
881  ## Macros  ##        ->{name}
882  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
883  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
884  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
885    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
886    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
887    ##     while the token is pushed back to the stack.
888    
889  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
890    
# Line 447  sub _initialize_tokenizer ($) { Line 894  sub _initialize_tokenizer ($) {
894  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
895  ## and removed from the list.  ## and removed from the list.
896    
897  ## ISSUE: <http://html5.org/tools/web-apps-tracker?from=874&to=876>  ## NOTE: HTML5 "Writing HTML documents" section, applied to
898    ## documents and not to user agents and conformance checkers,
899    ## contains some requirements that are not detected by the
900    ## parsing algorithm:
901    ## - Some requirements on character encoding declarations. ## TODO
902    ## - "Elements MUST NOT contain content that their content model disallows."
903    ##   ... Some are parse error, some are not (will be reported by c.c.).
904    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
905    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
906    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
907    
908    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
909    ## be detected by the HTML5 parsing algorithm:
910    ## - Text,
911    
912  sub _get_next_token ($) {  sub _get_next_token ($) {
913    my $self = shift;    my $self = shift;
914    
915      if ($self->{self_closing}) {
916        !!!parse-error (type => 'nestc', token => $self->{current_token});
917        ## NOTE: The |self_closing| flag is only set by start tag token.
918        ## In addition, when a start tag token is emitted, it is always set to
919        ## |current_token|.
920        delete $self->{self_closing};
921      }
922    
923    if (@{$self->{token}}) {    if (@{$self->{token}}) {
924        $self->{self_closing} = $self->{token}->[0]->{self_closing};
925      return shift @{$self->{token}};      return shift @{$self->{token}};
926    }    }
927    
928    A: {    A: {
929      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
930        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
931          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
932              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
933            $self->{state} = 'entity data';            !!!cp (1);
934              $self->{state} = ENTITY_DATA_STATE;
935            !!!next-input-character;            !!!next-input-character;
936            redo A;            redo A;
937          } else {          } else {
938              !!!cp (2);
939            #            #
940          }          }
941        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
942          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
943            $self->{state} = 'tag open';            unless ($self->{escape}) {
944                if ($self->{prev_char}->[0] == 0x002D and # -
945                    $self->{prev_char}->[1] == 0x0021 and # !
946                    $self->{prev_char}->[2] == 0x003C) { # <
947                  !!!cp (3);
948                  $self->{escape} = 1;
949                } else {
950                  !!!cp (4);
951                }
952              } else {
953                !!!cp (5);
954              }
955            }
956            
957            #
958          } elsif ($self->{next_char} == 0x003C) { # <
959            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
960                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
961                 not $self->{escape})) {
962              !!!cp (6);
963              $self->{state} = TAG_OPEN_STATE;
964            !!!next-input-character;            !!!next-input-character;
965            redo A;            redo A;
966          } else {          } else {
967              !!!cp (7);
968            #            #
969          }          }
970        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
971          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
972                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
973              if ($self->{prev_char}->[0] == 0x002D and # -
974                  $self->{prev_char}->[1] == 0x002D) { # -
975                !!!cp (8);
976                delete $self->{escape};
977              } else {
978                !!!cp (9);
979              }
980            } else {
981              !!!cp (10);
982            }
983            
984            #
985          } elsif ($self->{next_char} == -1) {
986            !!!cp (11);
987            !!!emit ({type => END_OF_FILE_TOKEN,
988                      line => $self->{line}, column => $self->{column}});
989          last A; ## TODO: ok?          last A; ## TODO: ok?
990          } else {
991            !!!cp (12);
992        }        }
993        # Anything else        # Anything else
994        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
995                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
996                       line => $self->{line}, column => $self->{column},
997                      };
998        ## Stay in the data state        ## Stay in the data state
999        !!!next-input-character;        !!!next-input-character;
1000    
1001        !!!emit ($token);        !!!emit ($token);
1002    
1003        redo A;        redo A;
1004      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
1005        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
1006    
1007          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
1008                
1009        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
1010    
1011        $self->{state} = 'data';        $self->{state} = DATA_STATE;
1012        # next-input-character is already done        # next-input-character is already done
1013    
1014        unless (defined $token) {        unless (defined $token) {
1015          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
1016            !!!emit ({type => CHARACTER_TOKEN, data => '&',
1017                      line => $l, column => $c,
1018                     });
1019        } else {        } else {
1020            !!!cp (14);
1021          !!!emit ($token);          !!!emit ($token);
1022        }        }
1023    
1024        redo A;        redo A;
1025      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1026        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1027            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
1028          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
1029            !!!next-input-character;            !!!next-input-character;
1030            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1031            redo A;            redo A;
1032          } else {          } else {
1033              !!!cp (16);
1034            ## reconsume            ## reconsume
1035            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1036    
1037            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1038                        line => $self->{line_prev},
1039                        column => $self->{column_prev},
1040                       });
1041    
1042            redo A;            redo A;
1043          }          }
1044        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1045          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1046            $self->{state} = 'markup declaration open';            !!!cp (17);
1047              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1048            !!!next-input-character;            !!!next-input-character;
1049            redo A;            redo A;
1050          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1051            $self->{state} = 'close tag open';            !!!cp (18);
1052              $self->{state} = CLOSE_TAG_OPEN_STATE;
1053            !!!next-input-character;            !!!next-input-character;
1054            redo A;            redo A;
1055          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1056                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1057              !!!cp (19);
1058            $self->{current_token}            $self->{current_token}
1059              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1060                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1061            $self->{state} = 'tag name';                 line => $self->{line_prev},
1062                   column => $self->{column_prev}};
1063              $self->{state} = TAG_NAME_STATE;
1064            !!!next-input-character;            !!!next-input-character;
1065            redo A;            redo A;
1066          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1067                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1068            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1069                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1070            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1071                                        line => $self->{line_prev},
1072                                        column => $self->{column_prev}};
1073              $self->{state} = TAG_NAME_STATE;
1074            !!!next-input-character;            !!!next-input-character;
1075            redo A;            redo A;
1076          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1077            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1078            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1079                              line => $self->{line_prev},
1080                              column => $self->{column_prev});
1081              $self->{state} = DATA_STATE;
1082            !!!next-input-character;            !!!next-input-character;
1083    
1084            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1085                        line => $self->{line_prev},
1086                        column => $self->{column_prev},
1087                       });
1088    
1089            redo A;            redo A;
1090          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1091            !!!parse-error (type => 'pio');            !!!cp (22);
1092            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1093            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1094                              column => $self->{column_prev});
1095              $self->{state} = BOGUS_COMMENT_STATE;
1096              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1097                                        line => $self->{line_prev},
1098                                        column => $self->{column_prev},
1099                                       };
1100              ## $self->{next_char} is intentionally left as is
1101            redo A;            redo A;
1102          } else {          } else {
1103            !!!parse-error (type => 'bare stago');            !!!cp (23);
1104            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1105                              line => $self->{line_prev},
1106                              column => $self->{column_prev});
1107              $self->{state} = DATA_STATE;
1108            ## reconsume            ## reconsume
1109    
1110            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1111                        line => $self->{line_prev},
1112                        column => $self->{column_prev},
1113                       });
1114    
1115            redo A;            redo A;
1116          }          }
1117        } else {        } else {
1118          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1119        }        }
1120      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1121        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1122            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1123          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
1124          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
1125            push @next_char, $self->{next_input_character};            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1126            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            my @next_char;
1127            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1128            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              push @next_char, $self->{next_char};
1129              !!!next-input-character;              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1130              next TAGNAME;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1131            } else {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1132              !!!parse-error (type => 'unmatched end tag');                !!!cp (24);
1133              $self->{next_input_character} = shift @next_char; # reconsume                !!!next-input-character;
1134                  next TAGNAME;
1135                } else {
1136                  !!!cp (25);
1137                  $self->{next_char} = shift @next_char; # reconsume
1138                  !!!back-next-input-character (@next_char);
1139                  $self->{state} = DATA_STATE;
1140    
1141                  !!!emit ({type => CHARACTER_TOKEN, data => '</',
1142                            line => $l, column => $c,
1143                           });
1144      
1145                  redo A;
1146                }
1147              }
1148              push @next_char, $self->{next_char};
1149          
1150              unless ($self->{next_char} == 0x0009 or # HT
1151                      $self->{next_char} == 0x000A or # LF
1152                      $self->{next_char} == 0x000B or # VT
1153                      $self->{next_char} == 0x000C or # FF
1154                      $self->{next_char} == 0x0020 or # SP
1155                      $self->{next_char} == 0x003E or # >
1156                      $self->{next_char} == 0x002F or # /
1157                      $self->{next_char} == -1) {
1158                !!!cp (26);
1159                $self->{next_char} = shift @next_char; # reconsume
1160              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1161              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1162                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1163              !!!emit ({type => 'character', data => '</'});                        line => $l, column => $c,
1164                         });
1165              redo A;              redo A;
1166              } else {
1167                !!!cp (27);
1168                $self->{next_char} = shift @next_char;
1169                !!!back-next-input-character (@next_char);
1170                # and consume...
1171            }            }
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error (type => 'unmatched end tag');  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
           redo A;  
1172          } else {          } else {
1173            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1174            !!!back-next-input-character (@next_char);            !!!cp (28);
1175            # and consume...            # next-input-character is already done
1176              $self->{state} = DATA_STATE;
1177              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1178                        line => $l, column => $c,
1179                       });
1180              redo A;
1181          }          }
1182        }        }
1183                
1184        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1185            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1186          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1187                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1188          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1189                   tag_name => chr ($self->{next_char} + 0x0020),
1190                   line => $l, column => $c};
1191            $self->{state} = TAG_NAME_STATE;
1192            !!!next-input-character;
1193            redo A;
1194          } elsif (0x0061 <= $self->{next_char} and
1195                   $self->{next_char} <= 0x007A) { # a..z
1196            !!!cp (30);
1197            $self->{current_token} = {type => END_TAG_TOKEN,
1198                                      tag_name => chr ($self->{next_char}),
1199                                      line => $l, column => $c};
1200            $self->{state} = TAG_NAME_STATE;
1201            !!!next-input-character;
1202            redo A;
1203          } elsif ($self->{next_char} == 0x003E) { # >
1204            !!!cp (31);
1205            !!!parse-error (type => 'empty end tag',
1206                            line => $self->{line_prev}, ## "<" in "</>"
1207                            column => $self->{column_prev} - 1);
1208            $self->{state} = DATA_STATE;
1209          !!!next-input-character;          !!!next-input-character;
1210          redo A;          redo A;
1211        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
1212                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (32);
         $self->{current_token} = {type => 'end tag',  
                           tag_name => chr ($self->{next_input_character})};  
         $self->{state} = 'tag name';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003E) { # >  
         !!!parse-error (type => 'empty end tag');  
         $self->{state} = 'data';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1213          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1214          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1215          # reconsume          # reconsume
1216    
1217          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1218                      line => $l, column => $c,
1219                     });
1220    
1221          redo A;          redo A;
1222        } else {        } else {
1223            !!!cp (33);
1224          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1225          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1226          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1227          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1228        }                                    column => $self->{column_prev} - 1,
1229      } elsif ($self->{state} eq 'tag name') {                                   };
1230        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1231            $self->{next_input_character} == 0x000A or # LF          redo A;
1232            $self->{next_input_character} == 0x000B or # VT        }
1233            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1234            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1235          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1236          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1237          redo A;            $self->{next_char} == 0x000C or # FF
1238        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1239          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1240            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1241            !!!next-input-character;
1242            redo A;
1243          } elsif ($self->{next_char} == 0x003E) { # >
1244            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1245              !!!cp (35);
1246            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1247          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1248            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1249            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1250              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1251            }            #  !!! cp (36);
1252              #  !!! parse-error (type => 'end tag attribute');
1253              #} else {
1254                !!!cp (37);
1255              #}
1256          } else {          } else {
1257            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1258          }          }
1259          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1260          !!!next-input-character;          !!!next-input-character;
1261    
1262          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1263    
1264          redo A;          redo A;
1265        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1266                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1267          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1268            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1269            # start tag or end tag            # start tag or end tag
1270          ## Stay in this state          ## Stay in this state
1271          !!!next-input-character;          !!!next-input-character;
1272          redo A;          redo A;
1273        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1274          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1275          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1276              !!!cp (39);
1277            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1278          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1279            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1280            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1281              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1282            }            #  !!! cp (40);
1283              #  !!! parse-error (type => 'end tag attribute');
1284              #} else {
1285                !!!cp (41);
1286              #}
1287          } else {          } else {
1288            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1289          }          }
1290          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1291          # reconsume          # reconsume
1292    
1293          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1294    
1295          redo A;          redo A;
1296        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1297            !!!cp (42);
1298            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1299          !!!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  
1300          redo A;          redo A;
1301        } else {        } else {
1302          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1303            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1304            # start tag or end tag            # start tag or end tag
1305          ## Stay in the state          ## Stay in the state
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        }        }
1309      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1310        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1311            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1312            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1313            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1314            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1315            !!!cp (45);
1316          ## Stay in the state          ## Stay in the state
1317          !!!next-input-character;          !!!next-input-character;
1318          redo A;          redo A;
1319        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1320          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1321              !!!cp (46);
1322            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1323          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1324            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1325            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1326                !!!cp (47);
1327              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1328              } else {
1329                !!!cp (48);
1330            }            }
1331          } else {          } else {
1332            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1333          }          }
1334          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1335          !!!next-input-character;          !!!next-input-character;
1336    
1337          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1338    
1339          redo A;          redo A;
1340        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1341                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1342          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1343                                value => ''};          $self->{current_attribute}
1344          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1345                   value => '',
1346                   line => $self->{line}, column => $self->{column}};
1347            $self->{state} = ATTRIBUTE_NAME_STATE;
1348            !!!next-input-character;
1349            redo A;
1350          } elsif ($self->{next_char} == 0x002F) { # /
1351            !!!cp (50);
1352            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1353          !!!next-input-character;          !!!next-input-character;
1354          redo A;          redo A;
1355        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
1356          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1357          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1358              !!!cp (52);
1359            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1360          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1361            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1362            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1363                !!!cp (53);
1364              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1365              } else {
1366                !!!cp (54);
1367            }            }
1368          } else {          } else {
1369            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1370          }          }
1371          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1372          # reconsume          # reconsume
1373    
1374          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1375    
1376          redo A;          redo A;
1377        } else {        } else {
1378          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1379                                value => ''};               0x0022 => 1, # "
1380          $self->{state} = 'attribute name';               0x0027 => 1, # '
1381                 0x003D => 1, # =
1382                }->{$self->{next_char}}) {
1383              !!!cp (55);
1384              !!!parse-error (type => 'bad attribute name');
1385            } else {
1386              !!!cp (56);
1387            }
1388            $self->{current_attribute}
1389                = {name => chr ($self->{next_char}),
1390                   value => '',
1391                   line => $self->{line}, column => $self->{column}};
1392            $self->{state} = ATTRIBUTE_NAME_STATE;
1393          !!!next-input-character;          !!!next-input-character;
1394          redo A;          redo A;
1395        }        }
1396      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1397        my $before_leave = sub {        my $before_leave = sub {
1398          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1399              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1400            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1401              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1402            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1403          } else {          } else {
1404              !!!cp (58);
1405            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1406              = $self->{current_attribute};              = $self->{current_attribute};
1407          }          }
1408        }; # $before_leave        }; # $before_leave
1409    
1410        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1411            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1412            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1413            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1414            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1415            !!!cp (59);
1416          $before_leave->();          $before_leave->();
1417          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1418          !!!next-input-character;          !!!next-input-character;
1419          redo A;          redo A;
1420        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1421            !!!cp (60);
1422          $before_leave->();          $before_leave->();
1423          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1424          !!!next-input-character;          !!!next-input-character;
1425          redo A;          redo A;
1426        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1427          $before_leave->();          $before_leave->();
1428          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1429              !!!cp (61);
1430            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1431          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1432            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1433              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1434            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1435              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1436            }            }
1437          } else {          } else {
1438            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1439          }          }
1440          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1441          !!!next-input-character;          !!!next-input-character;
1442    
1443          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1444    
1445          redo A;          redo A;
1446        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1447                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1448          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1449            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1450          ## Stay in the state          ## Stay in the state
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1454            !!!cp (64);
1455          $before_leave->();          $before_leave->();
1456            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1457          !!!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  
1458          redo A;          redo A;
1459        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1460          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1461          $before_leave->();          $before_leave->();
1462          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1463              !!!cp (66);
1464            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1465          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1466            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1467            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1468                !!!cp (67);
1469              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1470              } else {
1471                ## NOTE: This state should never be reached.
1472                !!!cp (68);
1473            }            }
1474          } else {          } else {
1475            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1476          }          }
1477          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1478          # reconsume          # reconsume
1479    
1480          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1481    
1482          redo A;          redo A;
1483        } else {        } else {
1484          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1485                $self->{next_char} == 0x0027) { # '
1486              !!!cp (69);
1487              !!!parse-error (type => 'bad attribute name');
1488            } else {
1489              !!!cp (70);
1490            }
1491            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1492          ## Stay in the state          ## Stay in the state
1493          !!!next-input-character;          !!!next-input-character;
1494          redo A;          redo A;
1495        }        }
1496      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1497        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1498            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1499            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1500            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1501            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1502            !!!cp (71);
1503          ## Stay in the state          ## Stay in the state
1504          !!!next-input-character;          !!!next-input-character;
1505          redo A;          redo A;
1506        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1507          $self->{state} = 'before attribute value';          !!!cp (72);
1508            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1509          !!!next-input-character;          !!!next-input-character;
1510          redo A;          redo A;
1511        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1512          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1513              !!!cp (73);
1514            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1515          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1516            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1517            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1518                !!!cp (74);
1519              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1520              } else {
1521                ## NOTE: This state should never be reached.
1522                !!!cp (75);
1523            }            }
1524          } else {          } else {
1525            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1526          }          }
1527          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1528          !!!next-input-character;          !!!next-input-character;
1529    
1530          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1531    
1532          redo A;          redo A;
1533        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1534                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1535          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1536                                value => ''};          $self->{current_attribute}
1537          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1538                   value => '',
1539                   line => $self->{line}, column => $self->{column}};
1540            $self->{state} = ATTRIBUTE_NAME_STATE;
1541            !!!next-input-character;
1542            redo A;
1543          } elsif ($self->{next_char} == 0x002F) { # /
1544            !!!cp (77);
1545            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1546          !!!next-input-character;          !!!next-input-character;
1547          redo A;          redo A;
1548        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == 0x003C or # <  
                $self->{next_input_character} == -1) {  
1549          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1550          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1551              !!!cp (79);
1552            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1553          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1554            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1555            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1556                !!!cp (80);
1557              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1558              } else {
1559                ## NOTE: This state should never be reached.
1560                !!!cp (81);
1561            }            }
1562          } else {          } else {
1563            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1564          }          }
1565          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1566          # reconsume          # reconsume
1567    
1568          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1569    
1570          redo A;          redo A;
1571        } else {        } else {
1572          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1573                                value => ''};              $self->{next_char} == 0x0027) { # '
1574          $self->{state} = 'attribute name';            !!!cp (78);
1575              !!!parse-error (type => 'bad attribute name');
1576            } else {
1577              !!!cp (82);
1578            }
1579            $self->{current_attribute}
1580                = {name => chr ($self->{next_char}),
1581                   value => '',
1582                   line => $self->{line}, column => $self->{column}};
1583            $self->{state} = ATTRIBUTE_NAME_STATE;
1584          !!!next-input-character;          !!!next-input-character;
1585          redo A;                  redo A;        
1586        }        }
1587      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1588        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1589            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1590            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1591            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1592            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1593            !!!cp (83);
1594          ## Stay in the state          ## Stay in the state
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1598          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1599            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1600          !!!next-input-character;          !!!next-input-character;
1601          redo A;          redo A;
1602        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1603          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1604            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1605          ## reconsume          ## reconsume
1606          redo A;          redo A;
1607        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1608          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1609            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1610          !!!next-input-character;          !!!next-input-character;
1611          redo A;          redo A;
1612        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1613          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1614            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1615              !!!cp (87);
1616            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1617          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1618            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1619            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1620                !!!cp (88);
1621              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1622              } else {
1623                ## NOTE: This state should never be reached.
1624                !!!cp (89);
1625            }            }
1626          } else {          } else {
1627            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1628          }          }
1629          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1630          !!!next-input-character;          !!!next-input-character;
1631    
1632          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1633    
1634          redo A;          redo A;
1635        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1636          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1637          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1638              !!!cp (90);
1639            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1640          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1641            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1642            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1643                !!!cp (91);
1644              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1645              } else {
1646                ## NOTE: This state should never be reached.
1647                !!!cp (92);
1648            }            }
1649          } else {          } else {
1650            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1651          }          }
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          ## reconsume          ## reconsume
1654    
1655          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1656    
1657          redo A;          redo A;
1658        } else {        } else {
1659          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1660          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1661              !!!parse-error (type => 'bad attribute value');
1662            } else {
1663              !!!cp (94);
1664            }
1665            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1666            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1667          !!!next-input-character;          !!!next-input-character;
1668          redo A;          redo A;
1669        }        }
1670      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1671        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1672          $self->{state} = 'before attribute name';          !!!cp (95);
1673            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1677          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1678          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1679            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1680          !!!next-input-character;          !!!next-input-character;
1681          redo A;          redo A;
1682        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1683          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1684          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1685              !!!cp (97);
1686            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1687          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1688            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1689            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1690                !!!cp (98);
1691              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1692              } else {
1693                ## NOTE: This state should never be reached.
1694                !!!cp (99);
1695            }            }
1696          } else {          } else {
1697            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1698          }          }
1699          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1700          ## reconsume          ## reconsume
1701    
1702          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1703    
1704          redo A;          redo A;
1705        } else {        } else {
1706          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1707            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1708          ## Stay in the state          ## Stay in the state
1709          !!!next-input-character;          !!!next-input-character;
1710          redo A;          redo A;
1711        }        }
1712      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1713        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1714          $self->{state} = 'before attribute name';          !!!cp (101);
1715            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1716          !!!next-input-character;          !!!next-input-character;
1717          redo A;          redo A;
1718        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1719          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1720          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1721            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1725          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1726          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1727              !!!cp (103);
1728            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1729          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1730            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1731            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1732                !!!cp (104);
1733              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1734              } else {
1735                ## NOTE: This state should never be reached.
1736                !!!cp (105);
1737            }            }
1738          } else {          } else {
1739            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1740          }          }
1741          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1742          ## reconsume          ## reconsume
1743    
1744          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1745    
1746          redo A;          redo A;
1747        } else {        } else {
1748          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1749            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1750          ## Stay in the state          ## Stay in the state
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        }        }
1754      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1755        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1756            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1757            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1758            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1759            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1760          $self->{state} = 'before attribute name';          !!!cp (107);
1761          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1762          redo A;          !!!next-input-character;
1763        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1764          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1765          $self->{state} = 'entity in attribute value';          !!!cp (108);
1766          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1767          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1768        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1769          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1770          } elsif ($self->{next_char} == 0x003E) { # >
1771            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1772              !!!cp (109);
1773            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1774          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1775            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1776            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1777                !!!cp (110);
1778              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1779              } else {
1780                ## NOTE: This state should never be reached.
1781                !!!cp (111);
1782            }            }
1783          } else {          } else {
1784            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1785          }          }
1786          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1787          !!!next-input-character;          !!!next-input-character;
1788    
1789          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1790    
1791          redo A;          redo A;
1792        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1793          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1794          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1795              !!!cp (112);
1796            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1797          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1798            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1799            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1800                !!!cp (113);
1801              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1802              } else {
1803                ## NOTE: This state should never be reached.
1804                !!!cp (114);
1805            }            }
1806          } else {          } else {
1807            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1808          }          }
1809          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1810          ## reconsume          ## reconsume
1811    
1812          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1813    
1814          redo A;          redo A;
1815        } else {        } else {
1816          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1817                 0x0022 => 1, # "
1818                 0x0027 => 1, # '
1819                 0x003D => 1, # =
1820                }->{$self->{next_char}}) {
1821              !!!cp (115);
1822              !!!parse-error (type => 'bad attribute value');
1823            } else {
1824              !!!cp (116);
1825            }
1826            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1827          ## Stay in the state          ## Stay in the state
1828          !!!next-input-character;          !!!next-input-character;
1829          redo A;          redo A;
1830        }        }
1831      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1832        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1833              (1,
1834               $self->{last_attribute_value_state}
1835                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1836               $self->{last_attribute_value_state}
1837                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1838               -1);
1839    
1840        unless (defined $token) {        unless (defined $token) {
1841            !!!cp (117);
1842          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1843        } else {        } else {
1844            !!!cp (118);
1845          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1846            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1847          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1848        }        }
1849    
1850        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1851        # next-input-character is already done        # next-input-character is already done
1852        redo A;        redo A;
1853      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1854          if ($self->{next_char} == 0x0009 or # HT
1855              $self->{next_char} == 0x000A or # LF
1856              $self->{next_char} == 0x000B or # VT
1857              $self->{next_char} == 0x000C or # FF
1858              $self->{next_char} == 0x0020) { # SP
1859            !!!cp (118);
1860            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1861            !!!next-input-character;
1862            redo A;
1863          } elsif ($self->{next_char} == 0x003E) { # >
1864            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1865              !!!cp (119);
1866              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1867            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1868              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1869              if ($self->{current_token}->{attributes}) {
1870                !!!cp (120);
1871                !!!parse-error (type => 'end tag attribute');
1872              } else {
1873                ## NOTE: This state should never be reached.
1874                !!!cp (121);
1875              }
1876            } else {
1877              die "$0: $self->{current_token}->{type}: Unknown token type";
1878            }
1879            $self->{state} = DATA_STATE;
1880            !!!next-input-character;
1881    
1882            !!!emit ($self->{current_token}); # start tag or end tag
1883    
1884            redo A;
1885          } elsif ($self->{next_char} == 0x002F) { # /
1886            !!!cp (122);
1887            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1888            !!!next-input-character;
1889            redo A;
1890          } elsif ($self->{next_char} == -1) {
1891            !!!parse-error (type => 'unclosed tag');
1892            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1893              !!!cp (122.3);
1894              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1895            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1896              if ($self->{current_token}->{attributes}) {
1897                !!!cp (122.1);
1898                !!!parse-error (type => 'end tag attribute');
1899              } else {
1900                ## NOTE: This state should never be reached.
1901                !!!cp (122.2);
1902              }
1903            } else {
1904              die "$0: $self->{current_token}->{type}: Unknown token type";
1905            }
1906            $self->{state} = DATA_STATE;
1907            ## Reconsume.
1908            !!!emit ($self->{current_token}); # start tag or end tag
1909            redo A;
1910          } else {
1911            !!!cp ('124.1');
1912            !!!parse-error (type => 'no space between attributes');
1913            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1914            ## reconsume
1915            redo A;
1916          }
1917        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1918          if ($self->{next_char} == 0x003E) { # >
1919            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1920              !!!cp ('124.2');
1921              !!!parse-error (type => 'nestc', token => $self->{current_token});
1922              ## TODO: Different type than slash in start tag
1923              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1924              if ($self->{current_token}->{attributes}) {
1925                !!!cp ('124.4');
1926                !!!parse-error (type => 'end tag attribute');
1927              } else {
1928                !!!cp ('124.5');
1929              }
1930              ## TODO: Test |<title></title/>|
1931            } else {
1932              !!!cp ('124.3');
1933              $self->{self_closing} = 1;
1934            }
1935    
1936            $self->{state} = DATA_STATE;
1937            !!!next-input-character;
1938    
1939            !!!emit ($self->{current_token}); # start tag or end tag
1940    
1941            redo A;
1942          } elsif ($self->{next_char} == -1) {
1943            !!!parse-error (type => 'unclosed tag');
1944            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1945              !!!cp (124.7);
1946              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1947            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1948              if ($self->{current_token}->{attributes}) {
1949                !!!cp (124.5);
1950                !!!parse-error (type => 'end tag attribute');
1951              } else {
1952                ## NOTE: This state should never be reached.
1953                !!!cp (124.6);
1954              }
1955            } else {
1956              die "$0: $self->{current_token}->{type}: Unknown token type";
1957            }
1958            $self->{state} = DATA_STATE;
1959            ## Reconsume.
1960            !!!emit ($self->{current_token}); # start tag or end tag
1961            redo A;
1962          } else {
1963            !!!cp ('124.4');
1964            !!!parse-error (type => 'nestc');
1965            ## TODO: This error type is wrong.
1966            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1967            ## Reconsume.
1968            redo A;
1969          }
1970        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1971        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1972                
1973        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1974          #my $token = {type => COMMENT_TOKEN, data => ''};
1975    
1976        BC: {        BC: {
1977          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1978            $self->{state} = 'data';            !!!cp (124);
1979              $self->{state} = DATA_STATE;
1980            !!!next-input-character;            !!!next-input-character;
1981    
1982            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1983    
1984            redo A;            redo A;
1985          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1986            $self->{state} = 'data';            !!!cp (125);
1987              $self->{state} = DATA_STATE;
1988            ## reconsume            ## reconsume
1989    
1990            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1991    
1992            redo A;            redo A;
1993          } else {          } else {
1994            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1995              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1996            !!!next-input-character;            !!!next-input-character;
1997            redo BC;            redo BC;
1998          }          }
1999        } # BC        } # BC
2000      } elsif ($self->{state} eq 'markup declaration open') {  
2001          die "$0: _get_next_token: unexpected case [BC]";
2002        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2003        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2004    
2005          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
2006    
2007        my @next_char;        my @next_char;
2008        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
2009                
2010        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2011          !!!next-input-character;          !!!next-input-character;
2012          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2013          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
2014            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
2015            $self->{state} = 'comment';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2016                                        line => $l, column => $c,
2017                                       };
2018              $self->{state} = COMMENT_START_STATE;
2019            !!!next-input-character;            !!!next-input-character;
2020            redo A;            redo A;
2021            } else {
2022              !!!cp (128);
2023          }          }
2024        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2025                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2026          !!!next-input-character;          !!!next-input-character;
2027          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2028          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
2029              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
2030            !!!next-input-character;            !!!next-input-character;
2031            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
2032            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
2033                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
2034              !!!next-input-character;              !!!next-input-character;
2035              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
2036              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2037                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2038                !!!next-input-character;                !!!next-input-character;
2039                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
2040                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
2041                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
2042                  !!!next-input-character;                  !!!next-input-character;
2043                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
2044                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
2045                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
2046                    !!!next-input-character;                    !!!next-input-character;
2047                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
2048                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
2049                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
2050                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
2051                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
2052                        $self->{state} = DOCTYPE_STATE;
2053                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2054                                                  quirks => 1,
2055                                                  line => $l, column => $c,
2056                                                 };
2057                      !!!next-input-character;                      !!!next-input-character;
2058                      redo A;                      redo A;
2059                      } else {
2060                        !!!cp (130);
2061                    }                    }
2062                    } else {
2063                      !!!cp (131);
2064                  }                  }
2065                  } else {
2066                    !!!cp (132);
2067                }                }
2068                } else {
2069                  !!!cp (133);
2070              }              }
2071              } else {
2072                !!!cp (134);
2073            }            }
2074            } else {
2075              !!!cp (135);
2076          }          }
2077          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2078                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2079                   $self->{next_char} == 0x005B) { # [
2080            !!!next-input-character;
2081            push @next_char, $self->{next_char};
2082            if ($self->{next_char} == 0x0043) { # C
2083              !!!next-input-character;
2084              push @next_char, $self->{next_char};
2085              if ($self->{next_char} == 0x0044) { # D
2086                !!!next-input-character;
2087                push @next_char, $self->{next_char};
2088                if ($self->{next_char} == 0x0041) { # A
2089                  !!!next-input-character;
2090                  push @next_char, $self->{next_char};
2091                  if ($self->{next_char} == 0x0054) { # T
2092                    !!!next-input-character;
2093                    push @next_char, $self->{next_char};
2094                    if ($self->{next_char} == 0x0041) { # A
2095                      !!!next-input-character;
2096                      push @next_char, $self->{next_char};
2097                      if ($self->{next_char} == 0x005B) { # [
2098                        !!!cp (135.1);
2099                        $self->{state} = CDATA_BLOCK_STATE;
2100                        !!!next-input-character;
2101                        redo A;
2102                      } else {
2103                        !!!cp (135.2);
2104                      }
2105                    } else {
2106                      !!!cp (135.3);
2107                    }
2108                  } else {
2109                    !!!cp (135.4);                
2110                  }
2111                } else {
2112                  !!!cp (135.5);
2113                }
2114              } else {
2115                !!!cp (135.6);
2116              }
2117            } else {
2118              !!!cp (135.7);
2119            }
2120          } else {
2121            !!!cp (136);
2122        }        }
2123    
2124        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
2125        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2126        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2127        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2128          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2129                                    line => $l, column => $c,
2130                                   };
2131        redo A;        redo A;
2132                
2133        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2134        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
2135      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2136        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2137          $self->{state} = 'comment dash';          !!!cp (137);
2138            $self->{state} = COMMENT_START_DASH_STATE;
2139          !!!next-input-character;          !!!next-input-character;
2140          redo A;          redo A;
2141        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2142            !!!cp (138);
2143            !!!parse-error (type => 'bogus comment');
2144            $self->{state} = DATA_STATE;
2145            !!!next-input-character;
2146    
2147            !!!emit ($self->{current_token}); # comment
2148    
2149            redo A;
2150          } elsif ($self->{next_char} == -1) {
2151            !!!cp (139);
2152            !!!parse-error (type => 'unclosed comment');
2153            $self->{state} = DATA_STATE;
2154            ## reconsume
2155    
2156            !!!emit ($self->{current_token}); # comment
2157    
2158            redo A;
2159          } else {
2160            !!!cp (140);
2161            $self->{current_token}->{data} # comment
2162                .= chr ($self->{next_char});
2163            $self->{state} = COMMENT_STATE;
2164            !!!next-input-character;
2165            redo A;
2166          }
2167        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2168          if ($self->{next_char} == 0x002D) { # -
2169            !!!cp (141);
2170            $self->{state} = COMMENT_END_STATE;
2171            !!!next-input-character;
2172            redo A;
2173          } elsif ($self->{next_char} == 0x003E) { # >
2174            !!!cp (142);
2175            !!!parse-error (type => 'bogus comment');
2176            $self->{state} = DATA_STATE;
2177            !!!next-input-character;
2178    
2179            !!!emit ($self->{current_token}); # comment
2180    
2181            redo A;
2182          } elsif ($self->{next_char} == -1) {
2183            !!!cp (143);
2184          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2185          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2186          ## reconsume          ## reconsume
2187    
2188          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2189    
2190          redo A;          redo A;
2191        } else {        } else {
2192          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (144);
2193            $self->{current_token}->{data} # comment
2194                .= '-' . chr ($self->{next_char});
2195            $self->{state} = COMMENT_STATE;
2196            !!!next-input-character;
2197            redo A;
2198          }
2199        } elsif ($self->{state} == COMMENT_STATE) {
2200          if ($self->{next_char} == 0x002D) { # -
2201            !!!cp (145);
2202            $self->{state} = COMMENT_END_DASH_STATE;
2203            !!!next-input-character;
2204            redo A;
2205          } elsif ($self->{next_char} == -1) {
2206            !!!cp (146);
2207            !!!parse-error (type => 'unclosed comment');
2208            $self->{state} = DATA_STATE;
2209            ## reconsume
2210    
2211            !!!emit ($self->{current_token}); # comment
2212    
2213            redo A;
2214          } else {
2215            !!!cp (147);
2216            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2217          ## Stay in the state          ## Stay in the state
2218          !!!next-input-character;          !!!next-input-character;
2219          redo A;          redo A;
2220        }        }
2221      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2222        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2223          $self->{state} = 'comment end';          !!!cp (148);
2224            $self->{state} = COMMENT_END_STATE;
2225          !!!next-input-character;          !!!next-input-character;
2226          redo A;          redo A;
2227        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2228            !!!cp (149);
2229          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2230          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2231          ## reconsume          ## reconsume
2232    
2233          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2234    
2235          redo A;          redo A;
2236        } else {        } else {
2237          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2238          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2239            $self->{state} = COMMENT_STATE;
2240          !!!next-input-character;          !!!next-input-character;
2241          redo A;          redo A;
2242        }        }
2243      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2244        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2245          $self->{state} = 'data';          !!!cp (151);
2246            $self->{state} = DATA_STATE;
2247          !!!next-input-character;          !!!next-input-character;
2248    
2249          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2250    
2251          redo A;          redo A;
2252        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2253          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2254            !!!parse-error (type => 'dash in comment',
2255                            line => $self->{line_prev},
2256                            column => $self->{column_prev});
2257          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2258          ## Stay in the state          ## Stay in the state
2259          !!!next-input-character;          !!!next-input-character;
2260          redo A;          redo A;
2261        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2262            !!!cp (153);
2263          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2264          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2265          ## reconsume          ## reconsume
2266    
2267          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2268    
2269          redo A;          redo A;
2270        } else {        } else {
2271          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2272          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2273          $self->{state} = 'comment';                          line => $self->{line_prev},
2274                            column => $self->{column_prev});
2275            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2276            $self->{state} = COMMENT_STATE;
2277          !!!next-input-character;          !!!next-input-character;
2278          redo A;          redo A;
2279        }        }
2280      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2281        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2282            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2283            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2284            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2285            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2286          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2287            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2288          !!!next-input-character;          !!!next-input-character;
2289          redo A;          redo A;
2290        } else {        } else {
2291            !!!cp (156);
2292          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2293          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2294          ## reconsume          ## reconsume
2295          redo A;          redo A;
2296        }        }
2297      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2298        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2299            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2300            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2301            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2302            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2303            !!!cp (157);
2304          ## Stay in the state          ## Stay in the state
2305          !!!next-input-character;          !!!next-input-character;
2306          redo A;          redo A;
2307        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2308                 $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) { # >  
2309          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2310          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2311          !!!next-input-character;          !!!next-input-character;
2312    
2313          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2314    
2315          redo A;          redo A;
2316        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2317            !!!cp (159);
2318          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2319          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2320          ## reconsume          ## reconsume
2321    
2322          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2323    
2324          redo A;          redo A;
2325        } else {        } else {
2326          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
2327                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name} = chr $self->{next_char};
2328                            error => 1};          delete $self->{current_token}->{quirks};
2329  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2330          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2331          !!!next-input-character;          !!!next-input-character;
2332          redo A;          redo A;
2333        }        }
2334      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2335        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
2336            $self->{next_input_character} == 0x000A or # LF        if ($self->{next_char} == 0x0009 or # HT
2337            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000A or # LF
2338            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000B or # VT
2339            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000C or # FF
2340          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{next_char} == 0x0020) { # SP
2341          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2342            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2343          !!!next-input-character;          !!!next-input-character;
2344          redo A;          redo A;
2345        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2346          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
2347          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2348          !!!next-input-character;          !!!next-input-character;
2349    
2350          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2351    
2352          redo A;          redo A;
2353        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2354                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
2355          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2356          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2357            ## reconsume
2358    
2359            $self->{current_token}->{quirks} = 1;
2360            !!!emit ($self->{current_token}); # DOCTYPE
2361    
2362            redo A;
2363          } else {
2364            !!!cp (164);
2365            $self->{current_token}->{name}
2366              .= chr ($self->{next_char}); # DOCTYPE
2367            ## Stay in the state
2368            !!!next-input-character;
2369            redo A;
2370          }
2371        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2372          if ($self->{next_char} == 0x0009 or # HT
2373              $self->{next_char} == 0x000A or # LF
2374              $self->{next_char} == 0x000B or # VT
2375              $self->{next_char} == 0x000C or # FF
2376              $self->{next_char} == 0x0020) { # SP
2377            !!!cp (165);
2378          ## Stay in the state          ## Stay in the state
2379          !!!next-input-character;          !!!next-input-character;
2380          redo A;          redo A;
2381        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2382            !!!cp (166);
2383            $self->{state} = DATA_STATE;
2384            !!!next-input-character;
2385    
2386            !!!emit ($self->{current_token}); # DOCTYPE
2387    
2388            redo A;
2389          } elsif ($self->{next_char} == -1) {
2390            !!!cp (167);
2391          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2392          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2393          ## reconsume          ## reconsume
2394    
2395          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2396          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2397    
2398          redo A;          redo A;
2399          } elsif ($self->{next_char} == 0x0050 or # P
2400                   $self->{next_char} == 0x0070) { # p
2401            !!!next-input-character;
2402            if ($self->{next_char} == 0x0055 or # U
2403                $self->{next_char} == 0x0075) { # u
2404              !!!next-input-character;
2405              if ($self->{next_char} == 0x0042 or # B
2406                  $self->{next_char} == 0x0062) { # b
2407                !!!next-input-character;
2408                if ($self->{next_char} == 0x004C or # L
2409                    $self->{next_char} == 0x006C) { # l
2410                  !!!next-input-character;
2411                  if ($self->{next_char} == 0x0049 or # I
2412                      $self->{next_char} == 0x0069) { # i
2413                    !!!next-input-character;
2414                    if ($self->{next_char} == 0x0043 or # C
2415                        $self->{next_char} == 0x0063) { # c
2416                      !!!cp (168);
2417                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2418                      !!!next-input-character;
2419                      redo A;
2420                    } else {
2421                      !!!cp (169);
2422                    }
2423                  } else {
2424                    !!!cp (170);
2425                  }
2426                } else {
2427                  !!!cp (171);
2428                }
2429              } else {
2430                !!!cp (172);
2431              }
2432            } else {
2433              !!!cp (173);
2434            }
2435    
2436            #
2437          } elsif ($self->{next_char} == 0x0053 or # S
2438                   $self->{next_char} == 0x0073) { # s
2439            !!!next-input-character;
2440            if ($self->{next_char} == 0x0059 or # Y
2441                $self->{next_char} == 0x0079) { # y
2442              !!!next-input-character;
2443              if ($self->{next_char} == 0x0053 or # S
2444                  $self->{next_char} == 0x0073) { # s
2445                !!!next-input-character;
2446                if ($self->{next_char} == 0x0054 or # T
2447                    $self->{next_char} == 0x0074) { # t
2448                  !!!next-input-character;
2449                  if ($self->{next_char} == 0x0045 or # E
2450                      $self->{next_char} == 0x0065) { # e
2451                    !!!next-input-character;
2452                    if ($self->{next_char} == 0x004D or # M
2453                        $self->{next_char} == 0x006D) { # m
2454                      !!!cp (174);
2455                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2456                      !!!next-input-character;
2457                      redo A;
2458                    } else {
2459                      !!!cp (175);
2460                    }
2461                  } else {
2462                    !!!cp (176);
2463                  }
2464                } else {
2465                  !!!cp (177);
2466                }
2467              } else {
2468                !!!cp (178);
2469              }
2470            } else {
2471              !!!cp (179);
2472            }
2473    
2474            #
2475        } else {        } else {
2476          $self->{current_token}->{name}          !!!cp (180);
2477            .= chr ($self->{next_input_character}); # DOCTYPE          !!!next-input-character;
2478          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          #
2479          }
2480    
2481          !!!parse-error (type => 'string after DOCTYPE name');
2482          $self->{current_token}->{quirks} = 1;
2483    
2484          $self->{state} = BOGUS_DOCTYPE_STATE;
2485          # next-input-character is already done
2486          redo A;
2487        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2488          if ({
2489                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2490                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2491              }->{$self->{next_char}}) {
2492            !!!cp (181);
2493          ## Stay in the state          ## Stay in the state
2494          !!!next-input-character;          !!!next-input-character;
2495          redo A;          redo A;
2496          } elsif ($self->{next_char} eq 0x0022) { # "
2497            !!!cp (182);
2498            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2499            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2500            !!!next-input-character;
2501            redo A;
2502          } elsif ($self->{next_char} eq 0x0027) { # '
2503            !!!cp (183);
2504            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2505            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2506            !!!next-input-character;
2507            redo A;
2508          } elsif ($self->{next_char} eq 0x003E) { # >
2509            !!!cp (184);
2510            !!!parse-error (type => 'no PUBLIC literal');
2511    
2512            $self->{state} = DATA_STATE;
2513            !!!next-input-character;
2514    
2515            $self->{current_token}->{quirks} = 1;
2516            !!!emit ($self->{current_token}); # DOCTYPE
2517    
2518            redo A;
2519          } elsif ($self->{next_char} == -1) {
2520            !!!cp (185);
2521            !!!parse-error (type => 'unclosed DOCTYPE');
2522    
2523            $self->{state} = DATA_STATE;
2524            ## reconsume
2525    
2526            $self->{current_token}->{quirks} = 1;
2527            !!!emit ($self->{current_token}); # DOCTYPE
2528    
2529            redo A;
2530          } else {
2531            !!!cp (186);
2532            !!!parse-error (type => 'string after PUBLIC');
2533            $self->{current_token}->{quirks} = 1;
2534    
2535            $self->{state} = BOGUS_DOCTYPE_STATE;
2536            !!!next-input-character;
2537            redo A;
2538        }        }
2539      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2540        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0022) { # "
2541            $self->{next_input_character} == 0x000A or # LF          !!!cp (187);
2542            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2543            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2544            $self->{next_input_character} == 0x0020) { # SP          redo A;
2545          } elsif ($self->{next_char} == 0x003E) { # >
2546            !!!cp (188);
2547            !!!parse-error (type => 'unclosed PUBLIC literal');
2548    
2549            $self->{state} = DATA_STATE;
2550            !!!next-input-character;
2551    
2552            $self->{current_token}->{quirks} = 1;
2553            !!!emit ($self->{current_token}); # DOCTYPE
2554    
2555            redo A;
2556          } elsif ($self->{next_char} == -1) {
2557            !!!cp (189);
2558            !!!parse-error (type => 'unclosed PUBLIC literal');
2559    
2560            $self->{state} = DATA_STATE;
2561            ## reconsume
2562    
2563            $self->{current_token}->{quirks} = 1;
2564            !!!emit ($self->{current_token}); # DOCTYPE
2565    
2566            redo A;
2567          } else {
2568            !!!cp (190);
2569            $self->{current_token}->{public_identifier} # DOCTYPE
2570                .= chr $self->{next_char};
2571          ## Stay in the state          ## Stay in the state
2572          !!!next-input-character;          !!!next-input-character;
2573          redo A;          redo A;
2574        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2575          $self->{state} = 'data';      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2576          if ($self->{next_char} == 0x0027) { # '
2577            !!!cp (191);
2578            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2579            !!!next-input-character;
2580            redo A;
2581          } elsif ($self->{next_char} == 0x003E) { # >
2582            !!!cp (192);
2583            !!!parse-error (type => 'unclosed PUBLIC literal');
2584    
2585            $self->{state} = DATA_STATE;
2586          !!!next-input-character;          !!!next-input-character;
2587    
2588            $self->{current_token}->{quirks} = 1;
2589          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2590    
2591          redo A;          redo A;
2592        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2593            !!!cp (193);
2594            !!!parse-error (type => 'unclosed PUBLIC literal');
2595    
2596            $self->{state} = DATA_STATE;
2597            ## reconsume
2598    
2599            $self->{current_token}->{quirks} = 1;
2600            !!!emit ($self->{current_token}); # DOCTYPE
2601    
2602            redo A;
2603          } else {
2604            !!!cp (194);
2605            $self->{current_token}->{public_identifier} # DOCTYPE
2606                .= chr $self->{next_char};
2607            ## Stay in the state
2608            !!!next-input-character;
2609            redo A;
2610          }
2611        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2612          if ({
2613                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2614                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2615              }->{$self->{next_char}}) {
2616            !!!cp (195);
2617            ## Stay in the state
2618            !!!next-input-character;
2619            redo A;
2620          } elsif ($self->{next_char} == 0x0022) { # "
2621            !!!cp (196);
2622            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2623            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2624            !!!next-input-character;
2625            redo A;
2626          } elsif ($self->{next_char} == 0x0027) { # '
2627            !!!cp (197);
2628            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2629            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2630            !!!next-input-character;
2631            redo A;
2632          } elsif ($self->{next_char} == 0x003E) { # >
2633            !!!cp (198);
2634            $self->{state} = DATA_STATE;
2635            !!!next-input-character;
2636    
2637            !!!emit ($self->{current_token}); # DOCTYPE
2638    
2639            redo A;
2640          } elsif ($self->{next_char} == -1) {
2641            !!!cp (199);
2642          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2643          $self->{state} = 'data';  
2644            $self->{state} = DATA_STATE;
2645          ## reconsume          ## reconsume
2646    
2647            $self->{current_token}->{quirks} = 1;
2648          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2649    
2650          redo A;          redo A;
2651        } else {        } else {
2652          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (200);
2653          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2654          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2655    
2656            $self->{state} = BOGUS_DOCTYPE_STATE;
2657          !!!next-input-character;          !!!next-input-character;
2658          redo A;          redo A;
2659        }        }
2660      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2661        if ($self->{next_input_character} == 0x003E) { # >        if ({
2662          $self->{state} = 'data';              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2663                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2664              }->{$self->{next_char}}) {
2665            !!!cp (201);
2666            ## Stay in the state
2667            !!!next-input-character;
2668            redo A;
2669          } elsif ($self->{next_char} == 0x0022) { # "
2670            !!!cp (202);
2671            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2672            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2673            !!!next-input-character;
2674            redo A;
2675          } elsif ($self->{next_char} == 0x0027) { # '
2676            !!!cp (203);
2677            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2678            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2679            !!!next-input-character;
2680            redo A;
2681          } elsif ($self->{next_char} == 0x003E) { # >
2682            !!!cp (204);
2683            !!!parse-error (type => 'no SYSTEM literal');
2684            $self->{state} = DATA_STATE;
2685          !!!next-input-character;          !!!next-input-character;
2686    
2687            $self->{current_token}->{quirks} = 1;
2688          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2689    
2690          redo A;          redo A;
2691        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2692            !!!cp (205);
2693          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2694          $self->{state} = 'data';  
2695            $self->{state} = DATA_STATE;
2696          ## reconsume          ## reconsume
2697    
2698            $self->{current_token}->{quirks} = 1;
2699          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2700    
2701          redo A;          redo A;
2702        } else {        } else {
2703            !!!cp (206);
2704            !!!parse-error (type => 'string after SYSTEM');
2705            $self->{current_token}->{quirks} = 1;
2706    
2707            $self->{state} = BOGUS_DOCTYPE_STATE;
2708            !!!next-input-character;
2709            redo A;
2710          }
2711        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2712          if ($self->{next_char} == 0x0022) { # "
2713            !!!cp (207);
2714            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2715            !!!next-input-character;
2716            redo A;
2717          } elsif ($self->{next_char} == 0x003E) { # >
2718            !!!cp (208);
2719            !!!parse-error (type => 'unclosed SYSTEM literal');
2720    
2721            $self->{state} = DATA_STATE;
2722            !!!next-input-character;
2723    
2724            $self->{current_token}->{quirks} = 1;
2725            !!!emit ($self->{current_token}); # DOCTYPE
2726    
2727            redo A;
2728          } elsif ($self->{next_char} == -1) {
2729            !!!cp (209);
2730            !!!parse-error (type => 'unclosed SYSTEM literal');
2731    
2732            $self->{state} = DATA_STATE;
2733            ## reconsume
2734    
2735            $self->{current_token}->{quirks} = 1;
2736            !!!emit ($self->{current_token}); # DOCTYPE
2737    
2738            redo A;
2739          } else {
2740            !!!cp (210);
2741            $self->{current_token}->{system_identifier} # DOCTYPE
2742                .= chr $self->{next_char};
2743            ## Stay in the state
2744            !!!next-input-character;
2745            redo A;
2746          }
2747        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2748          if ($self->{next_char} == 0x0027) { # '
2749            !!!cp (211);
2750            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2751            !!!next-input-character;
2752            redo A;
2753          } elsif ($self->{next_char} == 0x003E) { # >
2754            !!!cp (212);
2755            !!!parse-error (type => 'unclosed SYSTEM literal');
2756    
2757            $self->{state} = DATA_STATE;
2758            !!!next-input-character;
2759    
2760            $self->{current_token}->{quirks} = 1;
2761            !!!emit ($self->{current_token}); # DOCTYPE
2762    
2763            redo A;
2764          } elsif ($self->{next_char} == -1) {
2765            !!!cp (213);
2766            !!!parse-error (type => 'unclosed SYSTEM literal');
2767    
2768            $self->{state} = DATA_STATE;
2769            ## reconsume
2770    
2771            $self->{current_token}->{quirks} = 1;
2772            !!!emit ($self->{current_token}); # DOCTYPE
2773    
2774            redo A;
2775          } else {
2776            !!!cp (214);
2777            $self->{current_token}->{system_identifier} # DOCTYPE
2778                .= chr $self->{next_char};
2779            ## Stay in the state
2780            !!!next-input-character;
2781            redo A;
2782          }
2783        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2784          if ({
2785                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2786                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2787              }->{$self->{next_char}}) {
2788            !!!cp (215);
2789            ## Stay in the state
2790            !!!next-input-character;
2791            redo A;
2792          } elsif ($self->{next_char} == 0x003E) { # >
2793            !!!cp (216);
2794            $self->{state} = DATA_STATE;
2795            !!!next-input-character;
2796    
2797            !!!emit ($self->{current_token}); # DOCTYPE
2798    
2799            redo A;
2800          } elsif ($self->{next_char} == -1) {
2801            !!!cp (217);
2802            !!!parse-error (type => 'unclosed DOCTYPE');
2803            $self->{state} = DATA_STATE;
2804            ## reconsume
2805    
2806            $self->{current_token}->{quirks} = 1;
2807            !!!emit ($self->{current_token}); # DOCTYPE
2808    
2809            redo A;
2810          } else {
2811            !!!cp (218);
2812            !!!parse-error (type => 'string after SYSTEM literal');
2813            #$self->{current_token}->{quirks} = 1;
2814    
2815            $self->{state} = BOGUS_DOCTYPE_STATE;
2816            !!!next-input-character;
2817            redo A;
2818          }
2819        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2820          if ($self->{next_char} == 0x003E) { # >
2821            !!!cp (219);
2822            $self->{state} = DATA_STATE;
2823            !!!next-input-character;
2824    
2825            !!!emit ($self->{current_token}); # DOCTYPE
2826    
2827            redo A;
2828          } elsif ($self->{next_char} == -1) {
2829            !!!cp (220);
2830            !!!parse-error (type => 'unclosed DOCTYPE');
2831            $self->{state} = DATA_STATE;
2832            ## reconsume
2833    
2834            !!!emit ($self->{current_token}); # DOCTYPE
2835    
2836            redo A;
2837          } else {
2838            !!!cp (221);
2839          ## Stay in the state          ## Stay in the state
2840          !!!next-input-character;          !!!next-input-character;
2841          redo A;          redo A;
2842        }        }
2843        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2844          my $s = '';
2845          
2846          my ($l, $c) = ($self->{line}, $self->{column});
2847    
2848          CS: while ($self->{next_char} != -1) {
2849            if ($self->{next_char} == 0x005D) { # ]
2850              !!!next-input-character;
2851              if ($self->{next_char} == 0x005D) { # ]
2852                !!!next-input-character;
2853                MDC: {
2854                  if ($self->{next_char} == 0x003E) { # >
2855                    !!!cp (221.1);
2856                    !!!next-input-character;
2857                    last CS;
2858                  } elsif ($self->{next_char} == 0x005D) { # ]
2859                    !!!cp (221.2);
2860                    $s .= ']';
2861                    !!!next-input-character;
2862                    redo MDC;
2863                  } else {
2864                    !!!cp (221.3);
2865                    $s .= ']]';
2866                    #
2867                  }
2868                } # MDC
2869              } else {
2870                !!!cp (221.4);
2871                $s .= ']';
2872                #
2873              }
2874            } else {
2875              !!!cp (221.5);
2876              #
2877            }
2878            $s .= chr $self->{next_char};
2879            !!!next-input-character;
2880          } # CS
2881    
2882          $self->{state} = DATA_STATE;
2883          ## next-input-character done or EOF, which is reconsumed.
2884    
2885          if (length $s) {
2886            !!!cp (221.6);
2887            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2888                      line => $l, column => $c});
2889          } else {
2890            !!!cp (221.7);
2891          }
2892    
2893          redo A;
2894    
2895          ## ISSUE: "text tokens" in spec.
2896          ## TODO: Streaming support
2897      } else {      } else {
2898        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2899      }      }
# Line 1495  sub _get_next_token ($) { Line 2902  sub _get_next_token ($) {
2902    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2903  } # _get_next_token  } # _get_next_token
2904    
2905  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2906    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2907      
2908    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2909    
2910      if ({
2911           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2912           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2913           $additional => 1,
2914          }->{$self->{next_char}}) {
2915        !!!cp (1001);
2916        ## Don't consume
2917        ## No error
2918        return undef;
2919      } elsif ($self->{next_char} == 0x0023) { # #
2920      !!!next-input-character;      !!!next-input-character;
2921      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2922          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2923        my $num;        my $code;
2924        X: {        X: {
2925          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2926          !!!next-input-character;          !!!next-input-character;
2927          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2928              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2929            $num ||= 0;            !!!cp (1002);
2930            $num *= 0x10;            $code ||= 0;
2931            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
2932              $code += $self->{next_char} - 0x0030;
2933            redo X;            redo X;
2934          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2935                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2936            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
2937            $num ||= 0;            $code ||= 0;
2938            $num *= 0x10;            $code *= 0x10;
2939            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2940            redo X;            redo X;
2941          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2942                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2943            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
2944            $num ||= 0;            $code ||= 0;
2945            $num *= 0x10;            $code *= 0x10;
2946            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2947            redo X;            redo X;
2948          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2949            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2950            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2951            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2952              $self->{next_char} = 0x0023; # #
2953            return undef;            return undef;
2954          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2955              !!!cp (1006);
2956            !!!next-input-character;            !!!next-input-character;
2957          } else {          } else {
2958            !!!parse-error (type => 'no refc');            !!!cp (1007);
2959          }            !!!parse-error (type => 'no refc', line => $l, column => $c);
   
         ## TODO: check the definition for |a valid Unicode character|.  
         ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>  
         if ($num > 1114111 or $num == 0) {  
           $num = 0xFFFD; # REPLACEMENT CHARACTER  
           ## ISSUE: Why this is not an error?  
         } elsif (0x80 <= $num and $num <= 0x9F) {  
           !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);  
           $num = $c1_entity_char->{$num};  
2960          }          }
2961    
2962          return {type => 'character', data => chr $num};          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2963              !!!cp (1008);
2964              !!!parse-error (type => 'invalid character reference',
2965                              text => (sprintf 'U+%04X', $code),
2966                              line => $l, column => $c);
2967              $code = 0xFFFD;
2968            } elsif ($code > 0x10FFFF) {
2969              !!!cp (1009);
2970              !!!parse-error (type => 'invalid character reference',
2971                              text => (sprintf 'U-%08X', $code),
2972                              line => $l, column => $c);
2973              $code = 0xFFFD;
2974            } elsif ($code == 0x000D) {
2975              !!!cp (1010);
2976              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2977              $code = 0x000A;
2978            } elsif (0x80 <= $code and $code <= 0x9F) {
2979              !!!cp (1011);
2980              !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2981              $code = $c1_entity_char->{$code};
2982            }
2983    
2984            return {type => CHARACTER_TOKEN, data => chr $code,
2985                    has_reference => 1,
2986                    line => $l, column => $c,
2987                   };
2988        } # X        } # X
2989      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2990               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2991        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2992        !!!next-input-character;        !!!next-input-character;
2993                
2994        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2995                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2996            !!!cp (1012);
2997          $code *= 10;          $code *= 10;
2998          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2999                    
3000          !!!next-input-character;          !!!next-input-character;
3001        }        }
3002    
3003        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
3004            !!!cp (1013);
3005          !!!next-input-character;          !!!next-input-character;
3006        } else {        } else {
3007          !!!parse-error (type => 'no refc');          !!!cp (1014);
3008            !!!parse-error (type => 'no refc', line => $l, column => $c);
3009        }        }
3010    
3011        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3012        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
3013          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => 'invalid character reference',
3014          ## ISSUE: Why this is not an error?                          text => (sprintf 'U+%04X', $code),
3015                            line => $l, column => $c);
3016            $code = 0xFFFD;
3017          } elsif ($code > 0x10FFFF) {
3018            !!!cp (1016);
3019            !!!parse-error (type => 'invalid character reference',
3020                            text => (sprintf 'U-%08X', $code),
3021                            line => $l, column => $c);
3022            $code = 0xFFFD;
3023          } elsif ($code == 0x000D) {
3024            !!!cp (1017);
3025            !!!parse-error (type => 'CR character reference',
3026                            line => $l, column => $c);
3027            $code = 0x000A;
3028        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3029          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!cp (1018);
3030            !!!parse-error (type => 'C1 character reference',
3031                            text => (sprintf 'U+%04X', $code),
3032                            line => $l, column => $c);
3033          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3034        }        }
3035                
3036        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3037                  line => $l, column => $c,
3038                 };
3039      } else {      } else {
3040        !!!parse-error (type => 'bare nero');        !!!cp (1019);
3041        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3042        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
3043          $self->{next_char} = 0x0023; # #
3044        return undef;        return undef;
3045      }      }
3046    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
3047              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
3048             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
3049              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
3050      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
3051      !!!next-input-character;      !!!next-input-character;
3052    
3053      my $value = $entity_name;      my $value = $entity_name;
3054      my $match;      my $match = 0;
3055        require Whatpm::_NamedEntityList;
3056        our $EntityChar;
3057    
3058      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3059             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3060             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
3061               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
3062              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
3063               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
3064              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
3065               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
3066        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
3067        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
3068          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
3069          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
3070              !!!cp (1020);
3071              $value = $EntityChar->{$entity_name};
3072              $match = 1;
3073              !!!next-input-character;
3074              last;
3075            } else {
3076              !!!cp (1021);
3077              $value = $EntityChar->{$entity_name};
3078              $match = -1;
3079              !!!next-input-character;
3080            }
3081        } else {        } else {
3082          $value .= chr $self->{next_input_character};          !!!cp (1022);
3083            $value .= chr $self->{next_char};
3084            $match *= 2;
3085            !!!next-input-character;
3086        }        }
       !!!next-input-character;  
3087      }      }
3088            
3089      if ($match) {      if ($match > 0) {
3090        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
3091          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3092                  line => $l, column => $c,
3093                 };
3094        } elsif ($match < 0) {
3095          !!!parse-error (type => 'no refc', line => $l, column => $c);
3096          if ($in_attr and $match < -1) {
3097            !!!cp (1024);
3098            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3099                    line => $l, column => $c,
3100                   };
3101        } else {        } else {
3102          !!!parse-error (type => 'refc');          !!!cp (1025);
3103            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3104                    line => $l, column => $c,
3105                   };
3106        }        }
   
       return {type => 'character', data => $value};  
3107      } else {      } else {
3108        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3109        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3110        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
3111        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
3112                  line => $l, column => $c,
3113                 };
3114      }      }
3115    } else {    } else {
3116        !!!cp (1027);
3117      ## no characters are consumed      ## no characters are consumed
3118      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3119      return undef;      return undef;
3120    }    }
3121  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1639  sub _initialize_tree_constructor ($) { Line 3126  sub _initialize_tree_constructor ($) {
3126    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3127    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3128    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3129    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3130      $self->{document}->set_user_data (manakai_source_line => 1);
3131      $self->{document}->set_user_data (manakai_source_column => 1);
3132  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3133    
3134  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1666  sub _construct_tree ($) { Line 3155  sub _construct_tree ($) {
3155        
3156    !!!next-token;    !!!next-token;
3157    
   $self->{insertion_mode} = 'before head';  
3158    undef $self->{form_element};    undef $self->{form_element};
3159    undef $self->{head_element};    undef $self->{head_element};
3160    $self->{open_elements} = [];    $self->{open_elements} = [];
3161    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3162    
3163      ## NOTE: The "initial" insertion mode.
3164    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3165    
3166      ## NOTE: The "before html" insertion mode.
3167    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3168      $self->{insertion_mode} = BEFORE_HEAD_IM;
3169    
3170      ## NOTE: The "before head" insertion mode and so on.
3171    $self->_tree_construction_main;    $self->_tree_construction_main;
3172  } # _construct_tree  } # _construct_tree
3173    
3174  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3175    my $self = shift;    my $self = shift;
3176    B: {  
3177        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3178          if ($token->{error}) {  
3179            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3180            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3181          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3182          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3183            ($token->{name});        ## language.
3184          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3185          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3186          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3187          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3188          return;            defined $token->{system_identifier}) {
3189        } elsif ({          !!!cp ('t1');
3190                  comment => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3191                  'start tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3192                  'end tag' => 1,          !!!cp ('t2');
3193                  'end-of-file' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3194                 }->{$token->{type}}) {        } elsif (defined $token->{public_identifier}) {
3195          ## ISSUE: Spec currently left this case undefined.          if ($token->{public_identifier} eq 'XSLT-compat') {
3196          !!!parse-error (type => 'missing DOCTYPE');            !!!cp ('t1.2');
3197          #$phase = 'root element';            !!!parse-error (type => 'XSLT-compat', token => $token,
3198          ## reprocess                            level => $self->{level}->{should});
3199          #redo B;          } else {
3200          return;            !!!parse-error (type => 'not HTML5', token => $token);
3201        } elsif ($token->{type} eq 'character') {          }
3202          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } else {
3203            $self->{document}->manakai_append_text ($1);          !!!cp ('t3');
3204            ## ISSUE: DOM3 Core does not allow Document > Text          #
3205            unless (length $token->{data}) {        }
3206              ## Stay in the phase        
3207              !!!next-token;        my $doctype = $self->{document}->create_document_type_definition
3208              redo B;          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3209          ## NOTE: Default value for both |public_id| and |system_id| attributes
3210          ## are empty strings, so that we don't set any value in missing cases.
3211          $doctype->public_id ($token->{public_identifier})
3212              if defined $token->{public_identifier};
3213          $doctype->system_id ($token->{system_identifier})
3214              if defined $token->{system_identifier};
3215          ## NOTE: Other DocumentType attributes are null or empty lists.
3216          ## ISSUE: internalSubset = null??
3217          $self->{document}->append_child ($doctype);
3218          
3219          if ($token->{quirks} or $doctype_name ne 'HTML') {
3220            !!!cp ('t4');
3221            $self->{document}->manakai_compat_mode ('quirks');
3222          } elsif (defined $token->{public_identifier}) {
3223            my $pubid = $token->{public_identifier};
3224            $pubid =~ tr/a-z/A-z/;
3225            my $prefix = [
3226              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3227              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3228              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3229              "-//IETF//DTD HTML 2.0 LEVEL 1//",
3230              "-//IETF//DTD HTML 2.0 LEVEL 2//",
3231              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3232              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3233              "-//IETF//DTD HTML 2.0 STRICT//",
3234              "-//IETF//DTD HTML 2.0//",
3235              "-//IETF//DTD HTML 2.1E//",
3236              "-//IETF//DTD HTML 3.0//",
3237              "-//IETF//DTD HTML 3.2 FINAL//",
3238              "-//IETF//DTD HTML 3.2//",
3239              "-//IETF//DTD HTML 3//",
3240              "-//IETF//DTD HTML LEVEL 0//",
3241              "-//IETF//DTD HTML LEVEL 1//",
3242              "-//IETF//DTD HTML LEVEL 2//",
3243              "-//IETF//DTD HTML LEVEL 3//",
3244              "-//IETF//DTD HTML STRICT LEVEL 0//",
3245              "-//IETF//DTD HTML STRICT LEVEL 1//",
3246              "-//IETF//DTD HTML STRICT LEVEL 2//",
3247              "-//IETF//DTD HTML STRICT LEVEL 3//",
3248              "-//IETF//DTD HTML STRICT//",
3249              "-//IETF//DTD HTML//",
3250              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3251              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3252              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3253              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3254              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3255              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3256              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3257              "-//NETSCAPE COMM. CORP.//DTD HTML//",
3258              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3259              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3260              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3261              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3262              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3263              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3264              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3265              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3266              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3267              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3268              "-//W3C//DTD HTML 3 1995-03-24//",
3269              "-//W3C//DTD HTML 3.2 DRAFT//",
3270              "-//W3C//DTD HTML 3.2 FINAL//",
3271              "-//W3C//DTD HTML 3.2//",
3272              "-//W3C//DTD HTML 3.2S DRAFT//",
3273              "-//W3C//DTD HTML 4.0 FRAMESET//",
3274              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3275              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3276              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3277              "-//W3C//DTD W3 HTML//",
3278              "-//W3O//DTD W3 HTML 3.0//",
3279              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3280              "-//WEBTECHS//DTD MOZILLA HTML//",
3281            ]; # $prefix
3282            my $match;
3283            for (@$prefix) {
3284              if (substr ($prefix, 0, length $_) eq $_) {
3285                $match = 1;
3286                last;
3287              }
3288            }
3289            if ($match or
3290                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3291                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3292                $pubid eq "HTML") {
3293              !!!cp ('t5');
3294              $self->{document}->manakai_compat_mode ('quirks');
3295            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3296                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3297              if (defined $token->{system_identifier}) {
3298                !!!cp ('t6');
3299                $self->{document}->manakai_compat_mode ('quirks');
3300              } else {
3301                !!!cp ('t7');
3302                $self->{document}->manakai_compat_mode ('limited quirks');
3303            }            }
3304            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3305                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3306              !!!cp ('t8');
3307              $self->{document}->manakai_compat_mode ('limited quirks');
3308            } else {
3309              !!!cp ('t9');
3310          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3311        } else {        } else {
3312          die "$0: $token->{type}: Unknown token";          !!!cp ('t10');
3313        }        }
3314      } # B        if (defined $token->{system_identifier}) {
3315            my $sysid = $token->{system_identifier};
3316            $sysid =~ tr/A-Z/a-z/;
3317            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3318              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3319              ## marked as quirks.
3320              $self->{document}->manakai_compat_mode ('quirks');
3321              !!!cp ('t11');
3322            } else {
3323              !!!cp ('t12');
3324            }
3325          } else {
3326            !!!cp ('t13');
3327          }
3328          
3329          ## Go to the "before html" insertion mode.
3330          !!!next-token;
3331          return;
3332        } elsif ({
3333                  START_TAG_TOKEN, 1,
3334                  END_TAG_TOKEN, 1,
3335                  END_OF_FILE_TOKEN, 1,
3336                 }->{$token->{type}}) {
3337          !!!cp ('t14');
3338          !!!parse-error (type => 'no DOCTYPE', token => $token);
3339          $self->{document}->manakai_compat_mode ('quirks');
3340          ## Go to the "before html" insertion mode.
3341          ## reprocess
3342          !!!ack-later;
3343          return;
3344        } elsif ($token->{type} == CHARACTER_TOKEN) {
3345          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3346            ## Ignore the token
3347    
3348            unless (length $token->{data}) {
3349              !!!cp ('t15');
3350              ## Stay in the insertion mode.
3351              !!!next-token;
3352              redo INITIAL;
3353            } else {
3354              !!!cp ('t16');
3355            }
3356          } else {
3357            !!!cp ('t17');
3358          }
3359    
3360          !!!parse-error (type => 'no DOCTYPE', token => $token);
3361          $self->{document}->manakai_compat_mode ('quirks');
3362          ## Go to the "before html" insertion mode.
3363          ## reprocess
3364          return;
3365        } elsif ($token->{type} == COMMENT_TOKEN) {
3366          !!!cp ('t18');
3367          my $comment = $self->{document}->create_comment ($token->{data});
3368          $self->{document}->append_child ($comment);
3369          
3370          ## Stay in the insertion mode.
3371          !!!next-token;
3372          redo INITIAL;
3373        } else {
3374          die "$0: $token->{type}: Unknown token type";
3375        }
3376      } # INITIAL
3377    
3378      die "$0: _tree_construction_initial: This should be never reached";
3379  } # _tree_construction_initial  } # _tree_construction_initial
3380    
3381  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3382    my $self = shift;    my $self = shift;
3383    
3384      ## NOTE: "before html" insertion mode.
3385        
3386    B: {    B: {
3387        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3388          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3389            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3390          ## Ignore the token          ## Ignore the token
3391          ## Stay in the phase          ## Stay in the insertion mode.
3392          !!!next-token;          !!!next-token;
3393          redo B;          redo B;
3394        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3395            !!!cp ('t20');
3396          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3397          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3398          ## Stay in the phase          ## Stay in the insertion mode.
3399          !!!next-token;          !!!next-token;
3400          redo B;          redo B;
3401        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3402          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3403            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3404            ## ISSUE: DOM3 Core does not allow Document > Text  
3405            unless (length $token->{data}) {            unless (length $token->{data}) {
3406              ## Stay in the phase              !!!cp ('t21');
3407                ## Stay in the insertion mode.
3408              !!!next-token;              !!!next-token;
3409              redo B;              redo B;
3410              } else {
3411                !!!cp ('t22');
3412            }            }
3413            } else {
3414              !!!cp ('t23');
3415          }          }
3416    
3417            $self->{application_cache_selection}->(undef);
3418    
3419          #          #
3420          } elsif ($token->{type} == START_TAG_TOKEN) {
3421            if ($token->{tag_name} eq 'html') {
3422              my $root_element;
3423              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3424              $self->{document}->append_child ($root_element);
3425              push @{$self->{open_elements}},
3426                  [$root_element, $el_category->{html}];
3427    
3428              if ($token->{attributes}->{manifest}) {
3429                !!!cp ('t24');
3430                $self->{application_cache_selection}
3431                    ->($token->{attributes}->{manifest}->{value});
3432                ## ISSUE: Spec is unclear on relative references.
3433                ## According to Hixie (#whatwg 2008-03-19), it should be
3434                ## resolved against the base URI of the document in HTML
3435                ## or xml:base of the element in XHTML.
3436              } else {
3437                !!!cp ('t25');
3438                $self->{application_cache_selection}->(undef);
3439              }
3440    
3441              !!!nack ('t25c');
3442    
3443              !!!next-token;
3444              return; ## Go to the "before head" insertion mode.
3445            } else {
3446              !!!cp ('t25.1');
3447              #
3448            }
3449        } elsif ({        } elsif ({
3450                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3451                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3452                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3453          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3454          #          #
3455        } else {        } else {
3456          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3457        }        }
3458        my $root_element; !!!create-element ($root_element, 'html');  
3459        $self->{document}->append_child ($root_element);      my $root_element;
3460        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3461        #$phase = 'main';      $self->{document}->append_child ($root_element);
3462        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3463        #redo B;  
3464        return;      $self->{application_cache_selection}->(undef);
3465    
3466        ## NOTE: Reprocess the token.
3467        !!!ack-later;
3468        return; ## Go to the "before head" insertion mode.
3469    
3470        ## ISSUE: There is an issue in the spec
3471    } # B    } # B
3472    
3473      die "$0: _tree_construction_root_element: This should never be reached";
3474  } # _tree_construction_root_element  } # _tree_construction_root_element
3475    
3476  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1785  sub _reset_insertion_mode ($) { Line 3485  sub _reset_insertion_mode ($) {
3485            
3486      ## Step 3      ## Step 3
3487      S3: {      S3: {
3488        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3489        if (defined $self->{inner_html_node}) {          $last = 1;
3490          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3491              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3492              $node = $self->{inner_html_node};
3493            } else {
3494              die "_reset_insertion_mode: t27";
3495            }
3496          }
3497          
3498          ## Step 4..14
3499          my $new_mode;
3500          if ($node->[1] & FOREIGN_EL) {
3501            !!!cp ('t28.1');
3502            ## NOTE: Strictly spaking, the line below only applies to MathML and
3503            ## SVG elements.  Currently the HTML syntax supports only MathML and
3504            ## SVG elements as foreigners.
3505            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3506          } elsif ($node->[1] & TABLE_CELL_EL) {
3507            if ($last) {
3508              !!!cp ('t28.2');
3509            #            #
3510          } else {          } else {
3511            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3512              $new_mode = IN_CELL_IM;
3513          }          }
3514          } else {
3515            !!!cp ('t28.4');
3516            $new_mode = {
3517                          select => IN_SELECT_IM,
3518                          ## NOTE: |option| and |optgroup| do not set
3519                          ## insertion mode to "in select" by themselves.
3520                          tr => IN_ROW_IM,
3521                          tbody => IN_TABLE_BODY_IM,
3522                          thead => IN_TABLE_BODY_IM,
3523                          tfoot => IN_TABLE_BODY_IM,
3524                          caption => IN_CAPTION_IM,
3525                          colgroup => IN_COLUMN_GROUP_IM,
3526                          table => IN_TABLE_IM,
3527                          head => IN_BODY_IM, # not in head!
3528                          body => IN_BODY_IM,
3529                          frameset => IN_FRAMESET_IM,
3530                         }->{$node->[0]->manakai_local_name};
3531        }        }
       
       ## 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]};  
3532        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3533                
3534        ## Step 14        ## Step 15
3535        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3536          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3537            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3538              $self->{insertion_mode} = BEFORE_HEAD_IM;
3539          } else {          } else {
3540            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3541              !!!cp ('t30');
3542              $self->{insertion_mode} = AFTER_HEAD_IM;
3543          }          }
3544          return;          return;
3545          } else {
3546            !!!cp ('t31');
3547        }        }
3548                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3549        ## Step 16        ## Step 16
3550          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3551          
3552          ## Step 17
3553        $i--;        $i--;
3554        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3555                
3556        ## Step 17        ## Step 18
3557        redo S3;        redo S3;
3558      } # S3      } # S3
3559    
3560      die "$0: _reset_insertion_mode: This line should never be reached";
3561  } # _reset_insertion_mode  } # _reset_insertion_mode
3562    
3563  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3564    my $self = shift;    my $self = shift;
3565    
   my $phase = 'main';  
   
3566    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3567    
3568    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1856  sub _tree_construction_main ($) { Line 3579  sub _tree_construction_main ($) {
3579      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3580      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3581        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3582            !!!cp ('t32');
3583          return;          return;
3584        }        }
3585      }      }
# Line 1870  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594    
3595        ## Step 6        ## Step 6
3596        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3597            !!!cp ('t33_1');
3598          #          #
3599        } else {        } else {
3600          my $in_open_elements;          my $in_open_elements;
3601          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3602            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3603                !!!cp ('t33');
3604              $in_open_elements = 1;              $in_open_elements = 1;
3605              last OE;              last OE;
3606            }            }
3607          }          }
3608          if ($in_open_elements) {          if ($in_open_elements) {
3609              !!!cp ('t34');
3610            #            #
3611          } else {          } else {
3612              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3613              !!!cp ('t35');
3614            redo S4;            redo S4;
3615          }          }
3616        }        }
# Line 1904  sub _tree_construction_main ($) { Line 3633  sub _tree_construction_main ($) {
3633    
3634        ## Step 11        ## Step 11
3635        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3636            !!!cp ('t36');
3637          ## Step 7'          ## Step 7'
3638          $i++;          $i++;
3639          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3640                    
3641          redo S7;          redo S7;
3642        }        }
3643    
3644          !!!cp ('t37');
3645      } # S7      } # S7
3646    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3647    
3648    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3649      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3650        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3651            !!!cp ('t38');
3652          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3653          return;          return;
3654        }        }
3655      }      }
3656    
3657        !!!cp ('t39');
3658    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3659    
3660    my $style_start_tag = sub {    my $insert;
3661      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
3662      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3663      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3664       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3665        ->append_child ($style_el);      ## Step 1
3666      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3667                      my $el;
3668        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3669    
3670        ## Step 2
3671        $insert->($el);
3672    
3673        ## Step 3
3674        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3675        delete $self->{escape}; # MUST
3676    
3677        ## Step 4
3678      my $text = '';      my $text = '';
3679        !!!nack ('t40.1');
3680      !!!next-token;      !!!next-token;
3681      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3682          !!!cp ('t40');
3683        $text .= $token->{data};        $text .= $token->{data};
3684        !!!next-token;        !!!next-token;
3685      } # stop if non-character token or tokenizer stops tokenising      }
3686    
3687        ## Step 5
3688      if (length $text) {      if (length $text) {
3689        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3690          my $text = $self->{document}->create_text_node ($text);
3691          $el->append_child ($text);
3692      }      }
3693        
3694      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3695                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3696      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3697        ## Step 7
3698        if ($token->{type} == END_TAG_TOKEN and
3699            $token->{tag_name} eq $start_tag_name) {
3700          !!!cp ('t42');
3701        ## Ignore the token        ## Ignore the token
3702      } else {      } else {
3703        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3704        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3705            !!!cp ('t43');
3706            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3707          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3708            !!!cp ('t44');
3709            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3710          } else {
3711            die "$0: $content_model_flag in parse_rcdata";
3712          }
3713      }      }
3714      !!!next-token;      !!!next-token;
3715    }; # $style_start_tag    }; # $parse_rcdata
3716    
3717    my $script_start_tag = sub {    my $script_start_tag = sub () {
3718      my $script_el;      my $script_el;
3719      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3720      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3721    
3722      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3723        delete $self->{escape}; # MUST
3724            
3725      my $text = '';      my $text = '';
3726        !!!nack ('t45.1');
3727      !!!next-token;      !!!next-token;
3728      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3729          !!!cp ('t45');
3730        $text .= $token->{data};        $text .= $token->{data};
3731        !!!next-token;        !!!next-token;
3732      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3733      if (length $text) {      if (length $text) {
3734          !!!cp ('t46');
3735        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3736      }      }
3737                                
3738      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3739    
3740      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3741          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3742          !!!cp ('t47');
3743        ## Ignore the token        ## Ignore the token
3744      } else {      } else {
3745        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3746          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3747        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3748        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3749      }      }
3750            
3751      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3752          !!!cp ('t49');
3753        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3754      } else {      } else {
3755          !!!cp ('t50');
3756        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3757        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3758          
3759        (($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);  
3760                
3761        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3762                
# Line 1996  sub _tree_construction_main ($) { Line 3766  sub _tree_construction_main ($) {
3766      !!!next-token;      !!!next-token;
3767    }; # $script_start_tag    }; # $script_start_tag
3768    
3769      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3770      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3771      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3772    
3773    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3774      my $tag_name = shift;      my $end_tag_token = shift;
3775        my $tag_name = $end_tag_token->{tag_name};
3776    
3777        ## NOTE: The adoption agency algorithm (AAA).
3778    
3779      FET: {      FET: {
3780        ## Step 1        ## Step 1
3781        my $formatting_element;        my $formatting_element;
3782        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3783        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3784          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3785              !!!cp ('t52');
3786              last AFE;
3787            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3788                         eq $tag_name) {
3789              !!!cp ('t51');
3790            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3791            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3792            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3793          }          }
3794        } # AFE        } # AFE
3795        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3796          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3797            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3798          ## Ignore the token          ## Ignore the token
3799          !!!next-token;          !!!next-token;
3800          return;          return;
# Line 2025  sub _tree_construction_main ($) { Line 3806  sub _tree_construction_main ($) {
3806          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3807          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3808            if ($in_scope) {            if ($in_scope) {
3809                !!!cp ('t54');
3810              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3811              last INSCOPE;              last INSCOPE;
3812            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3813              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3814                !!!parse-error (type => 'unmatched end tag',
3815                                text => $token->{tag_name},
3816                                token => $end_tag_token);
3817              ## Ignore the token              ## Ignore the token
3818              !!!next-token;              !!!next-token;
3819              return;              return;
3820            }            }
3821          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3822                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3823            $in_scope = 0;            $in_scope = 0;
3824          }          }
3825        } # INSCOPE        } # INSCOPE
3826        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3827          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3828            !!!parse-error (type => 'unmatched end tag',
3829                            text => $token->{tag_name},
3830                            token => $end_tag_token);
3831          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3832          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3833          return;          return;
3834        }        }
3835        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3836          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3837            !!!parse-error (type => 'not closed',
3838                            text => $self->{open_elements}->[-1]->[0]
3839                                ->manakai_local_name,
3840                            token => $end_tag_token);
3841        }        }
3842                
3843        ## Step 2        ## Step 2
# Line 2055  sub _tree_construction_main ($) { Line 3845  sub _tree_construction_main ($) {
3845        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3846        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3847          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3848          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3849              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3850              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3851               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3852              !!!cp ('t59');
3853            $furthest_block = $node;            $furthest_block = $node;
3854            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3855          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3856              !!!cp ('t60');
3857            last OE;            last OE;
3858          }          }
3859        } # OE        } # OE
3860                
3861        ## Step 3        ## Step 3
3862        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3863            !!!cp ('t61');
3864          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3865          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3866          !!!next-token;          !!!next-token;
# Line 2080  sub _tree_construction_main ($) { Line 3873  sub _tree_construction_main ($) {
3873        ## Step 5        ## Step 5
3874        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3875        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3876            !!!cp ('t62');
3877          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3878        }        }
3879                
# Line 2102  sub _tree_construction_main ($) { Line 3896  sub _tree_construction_main ($) {
3896          S7S2: {          S7S2: {
3897            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3898              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3899                  !!!cp ('t63');
3900                $node_i_in_active = $_;                $node_i_in_active = $_;
3901                last S7S2;                last S7S2;
3902              }              }
# Line 2115  sub _tree_construction_main ($) { Line 3910  sub _tree_construction_main ($) {
3910                    
3911          ## Step 4          ## Step 4
3912          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3913              !!!cp ('t64');
3914            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3915          }          }
3916                    
3917          ## Step 5          ## Step 5
3918          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3919              !!!cp ('t65');
3920            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3921            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3922            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2137  sub _tree_construction_main ($) { Line 3934  sub _tree_construction_main ($) {
3934        } # S7          } # S7  
3935                
3936        ## Step 8        ## Step 8
3937        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3938            my $foster_parent_element;
3939            my $next_sibling;
3940            OE: for (reverse 0..$#{$self->{open_elements}}) {
3941              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3942                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3943                                 if (defined $parent and $parent->node_type == 1) {
3944                                   !!!cp ('t65.1');
3945                                   $foster_parent_element = $parent;
3946                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3947                                 } else {
3948                                   !!!cp ('t65.2');
3949                                   $foster_parent_element
3950                                     = $self->{open_elements}->[$_ - 1]->[0];
3951                                 }
3952                                 last OE;
3953                               }
3954                             } # OE
3955                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3956                               unless defined $foster_parent_element;
3957            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3958            $open_tables->[-1]->[1] = 1; # tainted
3959          } else {
3960            !!!cp ('t65.3');
3961            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3962          }
3963                
3964        ## Step 9        ## Step 9
3965        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2154  sub _tree_construction_main ($) { Line 3976  sub _tree_construction_main ($) {
3976        my $i;        my $i;
3977        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3978          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3979              !!!cp ('t66');
3980            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3981            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3982          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3983              !!!cp ('t67');
3984            $i = $_;            $i = $_;
3985          }          }
3986        } # AFE        } # AFE
# Line 2166  sub _tree_construction_main ($) { Line 3990  sub _tree_construction_main ($) {
3990        undef $i;        undef $i;
3991        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3992          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3993              !!!cp ('t68');
3994            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3995            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3996          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3997              !!!cp ('t69');
3998            $i = $_;            $i = $_;
3999          }          }
4000        } # OE        } # OE
# Line 2179  sub _tree_construction_main ($) { Line 4005  sub _tree_construction_main ($) {
4005      } # FET      } # FET
4006    }; # $formatting_end_tag    }; # $formatting_end_tag
4007    
4008    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4009      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4010    }; # $insert_to_current    }; # $insert_to_current
4011    
4012    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4013                         my $child = shift;      my $child = shift;
4014                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4015                              table => 1, tbody => 1, tfoot => 1,        # MUST
4016                              thead => 1, tr => 1,        my $foster_parent_element;
4017                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4018                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4019                           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') {  
4020                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4021                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4022                                   !!!cp ('t70');
4023                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4024                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4025                               } else {                               } else {
4026                                   !!!cp ('t71');
4027                                 $foster_parent_element                                 $foster_parent_element
4028                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4029                               }                               }
# Line 2209  sub _tree_construction_main ($) { Line 4034  sub _tree_construction_main ($) {
4034                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4035                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4036                             ($child, $next_sibling);                             ($child, $next_sibling);
4037                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4038                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4039                         }        !!!cp ('t72');
4040          $self->{open_elements}->[-1]->[0]->append_child ($child);
4041        }
4042    }; # $insert_to_foster    }; # $insert_to_foster
4043    
4044    my $in_body = sub {    B: while (1) {
4045      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4046      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4047        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4048          $script_start_tag->();        ## Ignore the token
4049          return;        ## Stay in the phase
4050        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4051          $style_start_tag->();        next B;
4052          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4053        } elsif ({               $token->{tag_name} eq 'html') {
4054                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4055                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4056          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4057          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4058          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4059          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4060          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4061            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4062          } else {        } else {
4063            $insert->($el);          !!!cp ('t81');
4064          }        }
4065            
4066          !!!next-token;        !!!cp ('t82');
4067          return;        !!!parse-error (type => 'not first start tag', token => $token);
4068        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4069          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4070          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4071          my $title_el;            !!!cp ('t84');
4072          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4073          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4074            ->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;  
4075          }          }
4076                    }
4077          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4078                    !!!next-token;
4079          next B;
4080        } elsif ($token->{type} == COMMENT_TOKEN) {
4081          my $comment = $self->{document}->create_comment ($token->{data});
4082          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4083            !!!cp ('t85');
4084            $self->{document}->append_child ($comment);
4085          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4086            !!!cp ('t86');
4087            $self->{open_elements}->[0]->[0]->append_child ($comment);
4088          } else {
4089            !!!cp ('t87');
4090            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4091          }
4092          !!!next-token;
4093          next B;
4094        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4095          if ($token->{type} == CHARACTER_TOKEN) {
4096            !!!cp ('t87.1');
4097            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4098          !!!next-token;          !!!next-token;
4099          return;          next B;
4100        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4101          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4102            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4103            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4104              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4105                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4106              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4107              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4108              $formatting_end_tag->($token->{tag_name});            #
4109                        } elsif ({
4110              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4111                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4112                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4113                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4114                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4115              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4116              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4117                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4118                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4119                  last OE;            !!!cp ('t87.2');
4120                }            !!!parse-error (type => 'not closed',
4121              } # OE                            text => $self->{open_elements}->[-1]->[0]
4122              last AFE;                                ->manakai_local_name,
4123            } elsif ($node->[0] eq '#marker') {                            token => $token);
4124              last AFE;  
4125              pop @{$self->{open_elements}}
4126                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4127    
4128              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4129              ## Reprocess.
4130              next B;
4131            } else {
4132              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4133              my $tag_name = $token->{tag_name};
4134              if ($nsuri eq $SVG_NS) {
4135                $tag_name = {
4136                   altglyph => 'altGlyph',
4137                   altglyphdef => 'altGlyphDef',
4138                   altglyphitem => 'altGlyphItem',
4139                   animatecolor => 'animateColor',
4140                   animatemotion => 'animateMotion',
4141                   animatetransform => 'animateTransform',
4142                   clippath => 'clipPath',
4143                   feblend => 'feBlend',
4144                   fecolormatrix => 'feColorMatrix',
4145                   fecomponenttransfer => 'feComponentTransfer',
4146                   fecomposite => 'feComposite',
4147                   feconvolvematrix => 'feConvolveMatrix',
4148                   fediffuselighting => 'feDiffuseLighting',
4149                   fedisplacementmap => 'feDisplacementMap',
4150                   fedistantlight => 'feDistantLight',
4151                   feflood => 'feFlood',
4152                   fefunca => 'feFuncA',
4153                   fefuncb => 'feFuncB',
4154                   fefuncg => 'feFuncG',
4155                   fefuncr => 'feFuncR',
4156                   fegaussianblur => 'feGaussianBlur',
4157                   feimage => 'feImage',
4158                   femerge => 'feMerge',
4159                   femergenode => 'feMergeNode',
4160                   femorphology => 'feMorphology',
4161                   feoffset => 'feOffset',
4162                   fepointlight => 'fePointLight',
4163                   fespecularlighting => 'feSpecularLighting',
4164                   fespotlight => 'feSpotLight',
4165                   fetile => 'feTile',
4166                   feturbulence => 'feTurbulence',
4167                   foreignobject => 'foreignObject',
4168                   glyphref => 'glyphRef',
4169                   lineargradient => 'linearGradient',
4170                   radialgradient => 'radialGradient',
4171                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4172                   textpath => 'textPath',  
4173                }->{$tag_name} || $tag_name;
4174            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4175    
4176          !!!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];  
4177    
4178          !!!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', ''];  
4179    
4180          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4181          return;  
4182        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4183                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4184          $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') { ## TODO: This is incorrect maybe  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=866&to=867>  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
4185            } else {            } else {
4186              !!!parse-error (type => 'in RCDATA:#'.$token->{type});              !!!cp ('t87.4');
4187            }            }
4188            ## ISSUE: And ignore?  
4189              !!!next-token;
4190              next B;
4191          }          }
4192          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4193          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4194        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4195          $reconstruct_active_formatting_elements->($insert_to_current);          #
4196                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4197          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4198                    !!!parse-error (type => 'not closed',
4199          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4200          !!!next-token;                              ->manakai_local_name,
4201          return;                          token => $token);
4202        } elsif ({  
4203                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4204                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4205                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4206                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4207                 }->{$token->{tag_name}}) {          ## Reprocess.
4208          !!!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.  
4209        } else {        } else {
4210          $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;  
4211        }        }
4212      } elsif ($token->{type} eq 'end tag') {      }
4213        if ($token->{tag_name} eq 'body') {  
4214          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4215            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4216            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4217              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4218            }              !!!cp ('t88.2');
4219            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4220            !!!next-token;            } else {
4221            return;              !!!cp ('t88.1');
4222          } else {              ## Ignore the token.
4223            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
4224            ## Ignore the token              next B;
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 form => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4225            }            }
4226          } # INSCOPE            unless (length $token->{data}) {
4227                        !!!cp ('t88');
4228          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4229            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              next B;
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         undef $self->{form_element} if $token->{tag_name} eq 'form';  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4230            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4231          }          }
           
         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];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4232    
4233              !!!next-token;          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4234              last S2;            !!!cp ('t89');
4235            } else {            ## As if <head>
4236              ## Step 3            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4237              if (not $formatting_category->{$node->[1]} and            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4238                  #not $phrasing_category->{$node->[1]} and            push @{$self->{open_elements}},
4239                  ($special_category->{$node->[1]} or                [$self->{head_element}, $el_category->{head}];
4240                   $scoping_category->{$node->[1]})) {  
4241                !!!parse-error (type => 'not closed:'.$node->[1]);            ## Reprocess in the "in head" insertion mode...
4242                ## Ignore the token            pop @{$self->{open_elements}};
4243                !!!next-token;  
4244                last S2;            ## Reprocess in the "after head" insertion mode...
4245              }          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4246            }            !!!cp ('t90');
4247              ## As if </noscript>
4248              pop @{$self->{open_elements}};
4249              !!!parse-error (type => 'in noscript:#text', token => $token);
4250                        
4251            ## Step 4            ## Reprocess in the "in head" insertion mode...
4252            $node_i--;            ## As if </head>
4253            $node = $self->{open_elements}->[$node_i];            pop @{$self->{open_elements}};
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
4254    
4255    B: {            ## Reprocess in the "after head" insertion mode...
4256      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4257        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4258          !!!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]);  
         }  
4259    
4260          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4261          last B;          } else {
4262              !!!cp ('t92');
4263            }
4264    
4265          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4266        } else {          ## As if <body>
4267          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4268            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4269              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4270                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4271                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4272                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4273                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4274                }              !!!cp ('t93');
4275              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4276              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4277              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4278              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4279              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4280              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4281              ## 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);  
4282              !!!next-token;              !!!next-token;
4283              redo B;              next B;
4284            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4285              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4286              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4287              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4288              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4289              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4290              if ($token->{tag_name} eq 'head') {              !!!next-token;
4291                !!!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;  
             }  
4292            } else {            } else {
4293              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4294            }              !!!parse-error (type => 'in head:head',
4295          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4296            if ($token->{type} eq 'character') {              ## Ignore the token
4297              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);  
4298              !!!next-token;              !!!next-token;
4299              redo B;              next B;
4300            } elsif ($token->{type} eq 'start tag') {            }
4301              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4302                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4303                my $title_el;            ## As if <head>
4304                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4305                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4306                  ->append_child ($title_el);            push @{$self->{open_elements}},
4307                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4308    
4309                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4310                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4311                while ($token->{type} eq 'character') {          } else {
4312                  $text .= $token->{data};            !!!cp ('t97');
4313                  !!!next-token;          }
4314                }  
4315                if (length $text) {              if ($token->{tag_name} eq 'base') {
4316                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4317                }                  !!!cp ('t98');
4318                                  ## As if </noscript>
4319                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4320                    !!!parse-error (type => 'in noscript', text => 'base',
4321                                    token => $token);
4322                                
4323                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4324                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4325                } else {                } else {
4326                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4327                }                }
               !!!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);  
4328    
4329                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4330                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4331              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4332                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4333                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4334                !!!next-token;                  push @{$self->{open_elements}},
4335                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}};  
4336                } else {                } else {
4337                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4338                }                }
4339                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4340                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4341                  pop @{$self->{open_elements}} # <head>
4342                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4343                  !!!nack ('t101.1');
4344                !!!next-token;                !!!next-token;
4345                redo B;                next B;
4346              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'link') {
4347                #                ## NOTE: There is a "as if in head" code clone.
4348              } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4349                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t102');
4350                ## Ignore the token                  !!!parse-error (type => 'after head',
4351                !!!next-token;                                  text => $token->{tag_name}, token => $token);
4352                redo B;                  push @{$self->{open_elements}},
4353              }                      [$self->{head_element}, $el_category->{head}];
4354            } else {                } else {
4355              #                  !!!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;  
4356                }                }
4357              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4358                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4359              #                pop @{$self->{open_elements}} # <head>
4360            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4361              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';  
4362                !!!next-token;                !!!next-token;
4363                redo B;                next B;
4364              } elsif ($token->{tag_name} eq 'frameset') {              } elsif ($token->{tag_name} eq 'meta') {
4365                !!!insert-element ('frameset', $token->{attributes});                ## NOTE: There is a "as if in head" code clone.
4366                $self->{insertion_mode} = 'in frameset';                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4367                !!!next-token;                  !!!cp ('t104');
4368                redo B;                  !!!parse-error (type => 'after head',
4369              } elsif ({                                  text => $token->{tag_name}, token => $token);
4370                        base => 1, link => 1, meta => 1,                  push @{$self->{open_elements}},
4371                        script => 1, style => 1, title => 1,                      [$self->{head_element}, $el_category->{head}];
4372                       }->{$token->{tag_name}}) {                } else {
4373                !!!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;  
4374                }                }
4375              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4376                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4377    
4378              !!!parse-error (type => 'in table:#character');                unless ($self->{confident}) {
4379                    if ($token->{attributes}->{charset}) {
4380              ## As if in body, but insert into foster parent element                    !!!cp ('t106');
4381              ## ISSUE: Spec says that "whenever a node would be inserted                    ## NOTE: Whether the encoding is supported or not is handled
4382              ## into the current node" while characters might not be                    ## in the {change_encoding} callback.
4383              ## result in a new Text node.                    $self->{change_encoding}
4384              $reconstruct_active_formatting_elements->($insert_to_foster);                        ->($self, $token->{attributes}->{charset}->{value},
4385                                         $token);
4386              if ({                    
4387                   table => 1, tbody => 1, tfoot => 1,                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4388                   thead => 1, tr => 1,                        ->set_user_data (manakai_has_reference =>
4389                  }->{$self->{open_elements}->[-1]->[1]}) {                                             $token->{attributes}->{charset}
4390                # MUST                                                 ->{has_reference});
4391                my $foster_parent_element;                  } elsif ($token->{attributes}->{content}) {
4392                my $next_sibling;                    if ($token->{attributes}->{content}->{value}
4393                my $prev_sibling;                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4394                OE: for (reverse 0..$#{$self->{open_elements}}) {                            [\x09-\x0D\x20]*=
4395                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4396                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4397                    if (defined $parent and $parent->node_type == 1) {                      !!!cp ('t107');
4398                      $foster_parent_element = $parent;                      ## NOTE: Whether the encoding is supported or not is handled
4399                      $next_sibling = $self->{open_elements}->[$_]->[0];                      ## in the {change_encoding} callback.
4400                      $prev_sibling = $next_sibling->previous_sibling;                      $self->{change_encoding}
4401                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4402                               $token);
4403                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4404                            ->set_user_data (manakai_has_reference =>
4405                                                 $token->{attributes}->{content}
4406                                                       ->{has_reference});
4407                    } else {                    } else {
4408                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4409                    }                    }
                   last OE;  
4410                  }                  }
               } # 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});  
4411                } else {                } else {
4412                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4413                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4414                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4415                }                        ->set_user_data (manakai_has_reference =>
4416              } else {                                             $token->{attributes}->{charset}
4417                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4418              }                  }
4419                                if ($token->{attributes}->{content}) {
4420              !!!next-token;                    !!!cp ('t110');
4421              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4422            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4423              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4424              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4425              !!!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}};  
4426                }                }
4427    
4428                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4429                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4430                  !!!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}};  
4431                !!!next-token;                !!!next-token;
4432                redo B;                next B;
4433              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4434                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4435                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4436                       }->{$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]);  
4437                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4438                    !!!parse-error (type => 'in noscript', text => 'title',
4439                                    token => $token);
4440                  
4441                    $self->{insertion_mode} = IN_HEAD_IM;
4442                    ## Reprocess in the "in head" insertion mode...
4443                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4444                    !!!cp ('t112');
4445                    !!!parse-error (type => 'after head',
4446                                    text => $token->{tag_name}, token => $token);
4447                    push @{$self->{open_elements}},
4448                        [$self->{head_element}, $el_category->{head}];
4449                  } else {
4450                    !!!cp ('t113');
4451                }                }
4452    
4453                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4454                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4455                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4456                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4457                redo B;                pop @{$self->{open_elements}} # <head>
4458              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4459                ## NOTE: There are code clones for this "table in table"                next B;
4460                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4461                         $token->{tag_name} eq 'noframes') {
4462                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4463                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4464                my $i;                ## NOTE: There is a "as if in head" code clone.
4465                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4466                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4467                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4468                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4469                    last INSCOPE;                  push @{$self->{open_elements}},
4470                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4471                            table => 1, html => 1,                } else {
4472                           }->{$node->[1]}) {                  !!!cp ('t115');
4473                    last INSCOPE;                }
4474                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4475                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4476                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4477                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4478                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4479                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4480                    !!!cp ('t116');
4481                    ## NOTE: and scripting is disalbed
4482                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4483                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4484                    !!!nack ('t116.1');
4485                  !!!next-token;                  !!!next-token;
4486                  redo B;                  next B;
4487                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4488                    !!!cp ('t117');
4489                    !!!parse-error (type => 'in noscript', text => 'noscript',
4490                                    token => $token);
4491                    ## Ignore the token
4492                    !!!nack ('t117.1');
4493                    !!!next-token;
4494                    next B;
4495                  } else {
4496                    !!!cp ('t118');
4497                    #
4498                }                }
4499                } elsif ($token->{tag_name} eq 'script') {
4500                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4501                    !!!cp ('t119');
4502                    ## As if </noscript>
4503                    pop @{$self->{open_elements}};
4504                    !!!parse-error (type => 'in noscript', text => 'script',
4505                                    token => $token);
4506                                
4507                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4508                if ({                  ## Reprocess in the "in head" insertion mode...
4509                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4510                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4511                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4512                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4513                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4514                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4515                  $token = {type => 'end tag',                } else {
4516                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
                 redo B;  
4517                }                }
4518    
4519                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4520                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4521                  pop @{$self->{open_elements}} # <head>
4522                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4523                  next B;
4524                } elsif ($token->{tag_name} eq 'body' or
4525                         $token->{tag_name} eq 'frameset') {
4526                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4527                    !!!cp ('t122');
4528                    ## As if </noscript>
4529                    pop @{$self->{open_elements}};
4530                    !!!parse-error (type => 'in noscript',
4531                                    text => $token->{tag_name}, token => $token);
4532                    
4533                    ## Reprocess in the "in head" insertion mode...
4534                    ## As if </head>
4535                    pop @{$self->{open_elements}};
4536                    
4537                    ## Reprocess in the "after head" insertion mode...
4538                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4539                    !!!cp ('t124');
4540                    pop @{$self->{open_elements}};
4541                    
4542                    ## Reprocess in the "after head" insertion mode...
4543                  } else {
4544                    !!!cp ('t125');
4545                }                }
4546    
4547                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4548                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4549                  if ($token->{tag_name} eq 'body') {
4550                    !!!cp ('t126');
4551                    $self->{insertion_mode} = IN_BODY_IM;
4552                  } elsif ($token->{tag_name} eq 'frameset') {
4553                    !!!cp ('t127');
4554                    $self->{insertion_mode} = IN_FRAMESET_IM;
4555                  } else {
4556                    die "$0: tag name: $self->{tag_name}";
4557                  }
4558                  !!!nack ('t127.1');
4559                  !!!next-token;
4560                  next B;
4561                } else {
4562                  !!!cp ('t128');
4563                  #
4564                }
4565    
4566                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4567                  !!!cp ('t129');
4568                  ## As if </noscript>
4569                  pop @{$self->{open_elements}};
4570                  !!!parse-error (type => 'in noscript:/',
4571                                  text => $token->{tag_name}, token => $token);
4572                  
4573                  ## Reprocess in the "in head" insertion mode...
4574                  ## As if </head>
4575                  pop @{$self->{open_elements}};
4576    
4577                ## reprocess                ## Reprocess in the "after head" insertion mode...
4578                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4579                  !!!cp ('t130');
4580                  ## As if </head>
4581                  pop @{$self->{open_elements}};
4582    
4583                  ## Reprocess in the "after head" insertion mode...
4584              } else {              } else {
4585                #                !!!cp ('t131');
4586              }              }
4587            } elsif ($token->{type} eq 'end tag') {  
4588              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4589                ## have a table element in table scope              ## As if <body>
4590                my $i;              !!!insert-element ('body',, $token);
4591                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4592                  my $node = $self->{open_elements}->[$_];              ## reprocess
4593                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4594                    $i = $_;              next B;
4595                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4596                  } elsif ({              if ($token->{tag_name} eq 'head') {
4597                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4598                           }->{$node->[1]}) {                  !!!cp ('t132');
4599                    last INSCOPE;                  ## As if <head>
4600                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4601                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4602                unless (defined $i) {                  push @{$self->{open_elements}},
4603                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4604    
4605                    ## Reprocess in the "in head" insertion mode...
4606                    pop @{$self->{open_elements}};
4607                    $self->{insertion_mode} = AFTER_HEAD_IM;
4608                    !!!next-token;
4609                    next B;
4610                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4611                    !!!cp ('t133');
4612                    ## As if </noscript>
4613                    pop @{$self->{open_elements}};
4614                    !!!parse-error (type => 'in noscript:/',
4615                                    text => 'head', token => $token);
4616                    
4617                    ## Reprocess in the "in head" insertion mode...
4618                    pop @{$self->{open_elements}};
4619                    $self->{insertion_mode} = AFTER_HEAD_IM;
4620                    !!!next-token;
4621                    next B;
4622                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4623                    !!!cp ('t134');
4624                    pop @{$self->{open_elements}};
4625                    $self->{insertion_mode} = AFTER_HEAD_IM;
4626                    !!!next-token;
4627                    next B;
4628                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4629                    !!!cp ('t134.1');
4630                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4631                                    token => $token);
4632                  ## Ignore the token                  ## Ignore the token
4633                  !!!next-token;                  !!!next-token;
4634                  redo B;                  next B;
4635                  } else {
4636                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4637                }                }
4638                              } elsif ($token->{tag_name} eq 'noscript') {
4639                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4640                if ({                  !!!cp ('t136');
4641                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4642                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4643                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4644                  !!!back-token;                  next B;
4645                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4646                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4647                  redo B;                  !!!cp ('t137');
4648                    !!!parse-error (type => 'unmatched end tag',
4649                                    text => 'noscript', token => $token);
4650                    ## Ignore the token ## ISSUE: An issue in the spec.
4651                    !!!next-token;
4652                    next B;
4653                  } else {
4654                    !!!cp ('t138');
4655                    #
4656                }                }
4657                } elsif ({
4658                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4659                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4660                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4661                      $self->{insertion_mode} == IN_HEAD_IM or
4662                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4663                    !!!cp ('t140');
4664                    !!!parse-error (type => 'unmatched end tag',
4665                                    text => $token->{tag_name}, token => $token);
4666                    ## Ignore the token
4667                    !!!next-token;
4668                    next B;
4669                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4670                    !!!cp ('t140.1');
4671                    !!!parse-error (type => 'unmatched end tag',
4672                                    text => $token->{tag_name}, token => $token);
4673                    ## Ignore the token
4674                    !!!next-token;
4675                    next B;
4676                  } else {
4677                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4678                }                }
4679                } elsif ($token->{tag_name} eq 'p') {
4680                  !!!cp ('t142');
4681                  !!!parse-error (type => 'unmatched end tag',
4682                                  text => $token->{tag_name}, token => $token);
4683                  ## Ignore the token
4684                  !!!next-token;
4685                  next B;
4686                } elsif ($token->{tag_name} eq 'br') {
4687                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4688                    !!!cp ('t142.2');
4689                    ## (before head) as if <head>, (in head) as if </head>
4690                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4691                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4692                    $self->{insertion_mode} = AFTER_HEAD_IM;
4693      
4694                    ## Reprocess in the "after head" insertion mode...
4695                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4696                    !!!cp ('t143.2');
4697                    ## As if </head>
4698                    pop @{$self->{open_elements}};
4699                    $self->{insertion_mode} = AFTER_HEAD_IM;
4700      
4701                    ## Reprocess in the "after head" insertion mode...
4702                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4703                    !!!cp ('t143.3');
4704                    ## ISSUE: Two parse errors for <head><noscript></br>
4705                    !!!parse-error (type => 'unmatched end tag',
4706                                    text => 'br', token => $token);
4707                    ## As if </noscript>
4708                    pop @{$self->{open_elements}};
4709                    $self->{insertion_mode} = IN_HEAD_IM;
4710    
4711                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
4712                    ## As if </head>
4713                    pop @{$self->{open_elements}};
4714                    $self->{insertion_mode} = AFTER_HEAD_IM;
4715    
4716                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
4717                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4718                    !!!cp ('t143.4');
4719                    #
4720                  } else {
4721                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4722                  }
4723    
4724                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4725                  !!!parse-error (type => 'unmatched end tag',
4726                                  text => 'br', token => $token);
4727                  ## Ignore the token
4728                !!!next-token;                !!!next-token;
4729                redo B;                next B;
4730              } elsif ({              } else {
4731                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
4732                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
4733                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4734                ## Ignore the token                ## Ignore the token
4735                !!!next-token;                !!!next-token;
4736                redo B;                next B;
4737                }
4738    
4739                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4740                  !!!cp ('t146');
4741                  ## As if </noscript>
4742                  pop @{$self->{open_elements}};
4743                  !!!parse-error (type => 'in noscript:/',
4744                                  text => $token->{tag_name}, token => $token);
4745                  
4746                  ## Reprocess in the "in head" insertion mode...
4747                  ## As if </head>
4748                  pop @{$self->{open_elements}};
4749    
4750                  ## Reprocess in the "after head" insertion mode...
4751                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4752                  !!!cp ('t147');
4753                  ## As if </head>
4754                  pop @{$self->{open_elements}};
4755    
4756                  ## Reprocess in the "after head" insertion mode...
4757                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4758    ## ISSUE: This case cannot be reached?
4759                  !!!cp ('t148');
4760                  !!!parse-error (type => 'unmatched end tag',
4761                                  text => $token->{tag_name}, token => $token);
4762                  ## Ignore the token ## ISSUE: An issue in the spec.
4763                  !!!next-token;
4764                  next B;
4765              } else {              } else {
4766                #                !!!cp ('t149');
4767              }              }
           } else {  
             #  
           }  
4768    
4769            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4770            $in_body->($insert_to_foster);              ## As if <body>
4771            redo B;              !!!insert-element ('body',, $token);
4772          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4773            if ($token->{type} eq 'character') {              ## reprocess
4774              ## NOTE: This is a code clone of "character in body".              next B;
4775          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4776            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4777              !!!cp ('t149.1');
4778    
4779              ## NOTE: As if <head>
4780              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4781              $self->{open_elements}->[-1]->[0]->append_child
4782                  ($self->{head_element});
4783              #push @{$self->{open_elements}},
4784              #    [$self->{head_element}, $el_category->{head}];
4785              #$self->{insertion_mode} = IN_HEAD_IM;
4786              ## NOTE: Reprocess.
4787    
4788              ## NOTE: As if </head>
4789              #pop @{$self->{open_elements}};
4790              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4791              ## NOTE: Reprocess.
4792              
4793              #
4794            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4795              !!!cp ('t149.2');
4796    
4797              ## NOTE: As if </head>
4798              pop @{$self->{open_elements}};
4799              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4800              ## NOTE: Reprocess.
4801    
4802              #
4803            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4804              !!!cp ('t149.3');
4805    
4806              !!!parse-error (type => 'in noscript:#eof', token => $token);
4807    
4808              ## As if </noscript>
4809              pop @{$self->{open_elements}};
4810              #$self->{insertion_mode} = IN_HEAD_IM;
4811              ## NOTE: Reprocess.
4812    
4813              ## NOTE: As if </head>
4814              pop @{$self->{open_elements}};
4815              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4816              ## NOTE: Reprocess.
4817    
4818              #
4819            } else {
4820              !!!cp ('t149.4');
4821              #
4822            }
4823    
4824            ## NOTE: As if <body>
4825            !!!insert-element ('body',, $token);
4826            $self->{insertion_mode} = IN_BODY_IM;
4827            ## NOTE: Reprocess.
4828            next B;
4829          } else {
4830            die "$0: $token->{type}: Unknown token type";
4831          }
4832    
4833              ## ISSUE: An issue in the spec.
4834        } elsif ($self->{insertion_mode} & BODY_IMS) {
4835              if ($token->{type} == CHARACTER_TOKEN) {
4836                !!!cp ('t150');
4837                ## NOTE: There is a code clone of "character in body".
4838              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4839                            
4840              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4841    
4842              !!!next-token;              !!!next-token;
4843              redo B;              next B;
4844            } 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') {  
4845              if ({              if ({
4846                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4847                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4848                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4849                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4850                    ## have an element in table scope
4851                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4852                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4853                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4854                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4855                  my $node = $self->{open_elements}->[$_];  
4856                  if ($node->[1] eq 'caption') {                      ## Close the cell
4857                    $i = $_;                      !!!back-token; # <x>
4858                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4859                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4860                            table => 1, html => 1,                                line => $token->{line},
4861                           }->{$node->[1]}) {                                column => $token->{column}};
4862                    last INSCOPE;                      next B;
4863                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4864                        !!!cp ('t152');
4865                        ## ISSUE: This case can never be reached, maybe.
4866                        last;
4867                      }
4868                  }                  }
4869                } # INSCOPE  
4870                unless (defined $i) {                  !!!cp ('t153');
4871                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4872                        text => $token->{tag_name}, token => $token);
4873                  ## Ignore the token                  ## Ignore the token
4874                    !!!nack ('t153.1');
4875                  !!!next-token;                  !!!next-token;
4876                  redo B;                  next B;
4877                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4878                                  !!!parse-error (type => 'not closed', text => 'caption',
4879                ## generate implied end tags                                  token => $token);
4880                if ({                  
4881                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
4882                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
4883                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
4884                  !!!back-token; # <?>                  INSCOPE: {
4885                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
4886                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
4887                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
4888                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
4889                  redo B;                        $i = $_;
4890                }                        last INSCOPE;
4891                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4892                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
4893                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
4894                }                      }
4895                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4896    
4897                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4898                      !!!parse-error (type => 'start tag not allowed',
4899                                      text => $token->{tag_name}, token => $token);
4900                      ## Ignore the token
4901                      !!!nack ('t157.1');
4902                      !!!next-token;
4903                      next B;
4904                    } # INSCOPE
4905                    
4906                    ## generate implied end tags
4907                    while ($self->{open_elements}->[-1]->[1]
4908                               & END_TAG_OPTIONAL_EL) {
4909                      !!!cp ('t158');
4910                      pop @{$self->{open_elements}};
4911                    }
4912    
4913                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4914                redo B;                    !!!cp ('t159');
4915                      !!!parse-error (type => 'not closed',
4916                                      text => $self->{open_elements}->[-1]->[0]
4917                                          ->manakai_local_name,
4918                                      token => $token);
4919                    } else {
4920                      !!!cp ('t160');
4921                    }
4922                    
4923                    splice @{$self->{open_elements}}, $i;
4924                    
4925                    $clear_up_to_marker->();
4926                    
4927                    $self->{insertion_mode} = IN_TABLE_IM;
4928                    
4929                    ## reprocess
4930                    !!!ack-later;
4931                    next B;
4932                  } else {
4933                    !!!cp ('t161');
4934                    #
4935                  }
4936              } else {              } else {
4937                  !!!cp ('t162');
4938                #                #
4939              }              }
4940            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4941              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4942                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4943                my $i;                  ## have an element in table scope
4944                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4945                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4946                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4947                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4948                    last INSCOPE;                      !!!cp ('t163');
4949                  } elsif ({                      $i = $_;
4950                            table => 1, html => 1,                      last INSCOPE;
4951                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4952                    last INSCOPE;                      !!!cp ('t164');
4953                        last INSCOPE;
4954                      }
4955                    } # INSCOPE
4956                      unless (defined $i) {
4957                        !!!cp ('t165');
4958                        !!!parse-error (type => 'unmatched end tag',
4959                                        text => $token->{tag_name},
4960                                        token => $token);
4961                        ## Ignore the token
4962                        !!!next-token;
4963                        next B;
4964                      }
4965                    
4966                    ## generate implied end tags
4967                    while ($self->{open_elements}->[-1]->[1]
4968                               & END_TAG_OPTIONAL_EL) {
4969                      !!!cp ('t166');
4970                      pop @{$self->{open_elements}};
4971                  }                  }
4972                } # INSCOPE  
4973                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4974                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4975                      !!!cp ('t167');
4976                      !!!parse-error (type => 'not closed',
4977                                      text => $self->{open_elements}->[-1]->[0]
4978                                          ->manakai_local_name,
4979                                      token => $token);
4980                    } else {
4981                      !!!cp ('t168');
4982                    }
4983                    
4984                    splice @{$self->{open_elements}}, $i;
4985                    
4986                    $clear_up_to_marker->();
4987                    
4988                    $self->{insertion_mode} = IN_ROW_IM;
4989                    
4990                    !!!next-token;
4991                    next B;
4992                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4993                    !!!cp ('t169');
4994                    !!!parse-error (type => 'unmatched end tag',
4995                                    text => $token->{tag_name}, token => $token);
4996                  ## Ignore the token                  ## Ignore the token
4997                  !!!next-token;                  !!!next-token;
4998                  redo B;                  next B;
4999                }                } else {
5000                                  !!!cp ('t170');
5001                ## 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;  
5002                }                }
5003                } elsif ($token->{tag_name} eq 'caption') {
5004                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
5005                    ## have a table element in table scope
5006                    my $i;
5007                    INSCOPE: {
5008                      for (reverse 0..$#{$self->{open_elements}}) {
5009                        my $node = $self->{open_elements}->[$_];
5010                        if ($node->[1] & CAPTION_EL) {
5011                          !!!cp ('t171');
5012                          $i = $_;
5013                          last INSCOPE;
5014                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5015                          !!!cp ('t172');
5016                          last;
5017                        }
5018                      }
5019    
5020                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
5021                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
5022                                      text => $token->{tag_name}, token => $token);
5023                      ## Ignore the token
5024                      !!!next-token;
5025                      next B;
5026                    } # INSCOPE
5027                    
5028                    ## generate implied end tags
5029                    while ($self->{open_elements}->[-1]->[1]
5030                               & END_TAG_OPTIONAL_EL) {
5031                      !!!cp ('t174');
5032                      pop @{$self->{open_elements}};
5033                    }
5034                    
5035                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5036                      !!!cp ('t175');
5037                      !!!parse-error (type => 'not closed',
5038                                      text => $self->{open_elements}->[-1]->[0]
5039                                          ->manakai_local_name,
5040                                      token => $token);
5041                    } else {
5042                      !!!cp ('t176');
5043                    }
5044                    
5045                    splice @{$self->{open_elements}}, $i;
5046                    
5047                    $clear_up_to_marker->();
5048                    
5049                    $self->{insertion_mode} = IN_TABLE_IM;
5050                    
5051                    !!!next-token;
5052                    next B;
5053                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5054                    !!!cp ('t177');
5055                    !!!parse-error (type => 'unmatched end tag',
5056                                    text => $token->{tag_name}, token => $token);
5057                    ## Ignore the token
5058                    !!!next-token;
5059                    next B;
5060                  } else {
5061                    !!!cp ('t178');
5062                    #
5063                }                }
5064                } elsif ({
5065                          table => 1, tbody => 1, tfoot => 1,
5066                          thead => 1, tr => 1,
5067                         }->{$token->{tag_name}} and
5068                         $self->{insertion_mode} == IN_CELL_IM) {
5069                  ## have an element in table scope
5070                  my $i;
5071                  my $tn;
5072                  INSCOPE: {
5073                    for (reverse 0..$#{$self->{open_elements}}) {
5074                      my $node = $self->{open_elements}->[$_];
5075                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5076                        !!!cp ('t179');
5077                        $i = $_;
5078    
5079                        ## Close the cell
5080                        !!!back-token; # </x>
5081                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5082                                  line => $token->{line},
5083                                  column => $token->{column}};
5084                        next B;
5085                      } elsif ($node->[1] & TABLE_CELL_EL) {
5086                        !!!cp ('t180');
5087                        $tn = $node->[0]->manakai_local_name;
5088                        ## NOTE: There is exactly one |td| or |th| element
5089                        ## in scope in the stack of open elements by definition.
5090                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5091                        ## ISSUE: Can this be reached?
5092                        !!!cp ('t181');
5093                        last;
5094                      }
5095                    }
5096    
5097                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5098                    !!!parse-error (type => 'unmatched end tag',
5099                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5100                    ## Ignore the token
5101                $self->{insertion_mode} = 'in table';                  !!!next-token;
5102                    next B;
5103                !!!next-token;                } # INSCOPE
5104                redo B;              } elsif ($token->{tag_name} eq 'table' and
5105              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5106                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5107                                  token => $token);
5108    
5109                ## As if </caption>                ## As if </caption>
5110                ## have a table element in table scope                ## have a table element in table scope
5111                my $i;                my $i;
5112                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5113                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5114                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5115                      !!!cp ('t184');
5116                    $i = $_;                    $i = $_;
5117                    last INSCOPE;                    last INSCOPE;
5118                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5119                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5120                    last INSCOPE;                    last INSCOPE;
5121                  }                  }
5122                } # INSCOPE                } # INSCOPE
5123                unless (defined $i) {                unless (defined $i) {
5124                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5125                    !!!parse-error (type => 'unmatched end tag',
5126                                    text => 'caption', token => $token);
5127                  ## Ignore the token                  ## Ignore the token
5128                  !!!next-token;                  !!!next-token;
5129                  redo B;                  next B;
5130                }                }
5131                                
5132                ## generate implied end tags                ## generate implied end tags
5133                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5134                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5135                     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;  
5136                }                }
5137    
5138                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5139                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5140                    !!!parse-error (type => 'not closed',
5141                                    text => $self->{open_elements}->[-1]->[0]
5142                                        ->manakai_local_name,
5143                                    token => $token);
5144                  } else {
5145                    !!!cp ('t189');
5146                }                }
5147    
5148                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5149    
5150                $clear_up_to_marker->();                $clear_up_to_marker->();
5151    
5152                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5153    
5154                ## reprocess                ## reprocess
5155                redo B;                next B;
5156              } elsif ({              } elsif ({
5157                        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,  
5158                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5159                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5160                ## Ignore the token                  !!!cp ('t190');
5161                redo B;                  !!!parse-error (type => 'unmatched end tag',
5162              } 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');  
5163                  ## Ignore the token                  ## Ignore the token
5164                  !!!next-token;                  !!!next-token;
5165                  redo B;                  next B;
5166                } else {                } else {
5167                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5168                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5169                }                }
5170              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5171                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5172                          thead => 1, tr => 1,
5173                         }->{$token->{tag_name}} and
5174                         $self->{insertion_mode} == IN_CAPTION_IM) {
5175                  !!!cp ('t192');
5176                  !!!parse-error (type => 'unmatched end tag',
5177                                  text => $token->{tag_name}, token => $token);
5178                ## Ignore the token                ## Ignore the token
5179                !!!next-token;                !!!next-token;
5180                redo B;                next B;
5181              } else {              } else {
5182                #                !!!cp ('t193');
5183                  #
5184              }              }
5185            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5186              #          for my $entry (@{$self->{open_elements}}) {
5187              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5188                !!!cp ('t75');
5189                !!!parse-error (type => 'in body:#eof', token => $token);
5190                last;
5191            }            }
5192            }
5193    
5194            ## As if </colgroup>          ## Stop parsing.
5195            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5196              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5197              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5198          }
5199    
5200          $insert = $insert_to_current;
5201          #
5202        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5203          if ($token->{type} == CHARACTER_TOKEN) {
5204            if (not $open_tables->[-1]->[1] and # tainted
5205                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5206              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5207                  
5208              unless (length $token->{data}) {
5209                !!!cp ('t194');
5210              !!!next-token;              !!!next-token;
5211              redo B;              next B;
5212            } else {            } else {
5213              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5214            }            }
5215          } 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;  
               }  
             }  
5216    
5217              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5218    
5219              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5220              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5221              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5222              ## result in a new Text node.              ## result in a new Text node.
5223              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5224                
5225              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]}) {  
5226                # MUST                # MUST
5227                my $foster_parent_element;                my $foster_parent_element;
5228                my $next_sibling;                my $next_sibling;
5229                my $prev_sibling;                my $prev_sibling;
5230                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5231                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5232                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5233                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5234                        !!!cp ('t196');
5235                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5236                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5237                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5238                    } else {                    } else {
5239                        !!!cp ('t197');
5240                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5241                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5242                    }                    }
# Line 3752  sub _tree_construction_main ($) { Line 5248  sub _tree_construction_main ($) {
5248                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5249                if (defined $prev_sibling and                if (defined $prev_sibling and
5250                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5251                    !!!cp ('t198');
5252                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5253                } else {                } else {
5254                    !!!cp ('t199');
5255                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5256                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5257                     $next_sibling);                     $next_sibling);
5258                }                }
5259              } else {            $open_tables->[-1]->[1] = 1; # tainted
5260                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5261              !!!cp ('t200');
5262              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5263            }
5264                
5265            !!!next-token;
5266            next B;
5267          } elsif ($token->{type} == START_TAG_TOKEN) {
5268            if ({
5269                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5270                 th => 1, td => 1,
5271                }->{$token->{tag_name}}) {
5272              if ($self->{insertion_mode} == IN_TABLE_IM) {
5273                ## Clear back to table context
5274                while (not ($self->{open_elements}->[-1]->[1]
5275                                & TABLE_SCOPING_EL)) {
5276                  !!!cp ('t201');
5277                  pop @{$self->{open_elements}};
5278              }              }
5279                            
5280              !!!next-token;              !!!insert-element ('tbody',, $token);
5281              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5282            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5283              ## Copied from 'in table'            }
5284              my $comment = $self->{document}->create_comment ($token->{data});            
5285              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5286              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5287              redo B;                !!!cp ('t202');
5288            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5289              if ({              }
5290                   tr => 1,                  
5291                   th => 1, td => 1,              ## Clear back to table body context
5292                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5293                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5294                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5295                  ## ISSUE: Can this case be reached?
5296                  pop @{$self->{open_elements}};
5297                }
5298                    
5299                    $self->{insertion_mode} = IN_ROW_IM;
5300                    if ($token->{tag_name} eq 'tr') {
5301                      !!!cp ('t204');
5302                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5303                      !!!nack ('t204');
5304                      !!!next-token;
5305                      next B;
5306                    } else {
5307                      !!!cp ('t205');
5308                      !!!insert-element ('tr',, $token);
5309                      ## reprocess in the "in row" insertion mode
5310                    }
5311                  } else {
5312                    !!!cp ('t206');
5313                }                }
5314    
5315                ## Clear back to table body context                ## Clear back to table row context
5316                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5317                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5318                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5319                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5320                }                }
5321                                
5322                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5323                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5324                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5325                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5326                } else {                
5327                  !!!insert-element ('tr');                !!!nack ('t207.1');
5328                  ## reprocess                !!!next-token;
5329                }                next B;
               redo B;  
5330              } elsif ({              } elsif ({
5331                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5332                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5333                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5334                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5335                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5336                my $i;                  ## As if </tr>
5337                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5338                  my $node = $self->{open_elements}->[$_];                  my $i;
5339                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5340                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5341                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5342                    $i = $_;                      !!!cp ('t208');
5343                    last INSCOPE;                      $i = $_;
5344                  } elsif ({                      last INSCOPE;
5345                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5346                           }->{$node->[1]}) {                      !!!cp ('t209');
5347                    last INSCOPE;                      last INSCOPE;
5348                      }
5349                    } # INSCOPE
5350                    unless (defined $i) {
5351                      !!!cp ('t210');
5352    ## TODO: This type is wrong.
5353                      !!!parse-error (type => 'unmacthed end tag',
5354                                      text => $token->{tag_name}, token => $token);
5355                      ## Ignore the token
5356                      !!!nack ('t210.1');
5357                      !!!next-token;
5358                      next B;
5359                    }
5360                    
5361                    ## Clear back to table row context
5362                    while (not ($self->{open_elements}->[-1]->[1]
5363                                    & TABLE_ROW_SCOPING_EL)) {
5364                      !!!cp ('t211');
5365                      ## ISSUE: Can this case be reached?
5366                      pop @{$self->{open_elements}};
5367                    }
5368                    
5369                    pop @{$self->{open_elements}}; # tr
5370                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5371                    if ($token->{tag_name} eq 'tr') {
5372                      !!!cp ('t212');
5373                      ## reprocess
5374                      !!!ack-later;
5375                      next B;
5376                    } else {
5377                      !!!cp ('t213');
5378                      ## reprocess in the "in table body" insertion mode...
5379                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5380                }                }
5381    
5382                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5383                while (not {                  ## have an element in table scope
5384                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5385                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5386                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5387                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5388                        !!!cp ('t214');
5389                        $i = $_;
5390                        last INSCOPE;
5391                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5392                        !!!cp ('t215');
5393                        last INSCOPE;
5394                      }
5395                    } # INSCOPE
5396                    unless (defined $i) {
5397                      !!!cp ('t216');
5398    ## TODO: This erorr type is wrong.
5399                      !!!parse-error (type => 'unmatched end tag',
5400                                      text => $token->{tag_name}, token => $token);
5401                      ## Ignore the token
5402                      !!!nack ('t216.1');
5403                      !!!next-token;
5404                      next B;
5405                    }
5406    
5407                    ## Clear back to table body context
5408                    while (not ($self->{open_elements}->[-1]->[1]
5409                                    & TABLE_ROWS_SCOPING_EL)) {
5410                      !!!cp ('t217');
5411                      ## ISSUE: Can this state be reached?
5412                      pop @{$self->{open_elements}};
5413                    }
5414                    
5415                    ## As if <{current node}>
5416                    ## have an element in table scope
5417                    ## true by definition
5418                    
5419                    ## Clear back to table body context
5420                    ## nop by definition
5421                    
5422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5423                    $self->{insertion_mode} = IN_TABLE_IM;
5424                    ## reprocess in "in table" insertion mode...
5425                  } else {
5426                    !!!cp ('t218');
5427                }                }
5428    
5429                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5430                ## have an element in table scope                  ## Clear back to table context
5431                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5432                                    & TABLE_SCOPING_EL)) {
5433                ## Clear back to table body context                    !!!cp ('t219');
5434                ## nop by definition                    ## ISSUE: Can this state be reached?
5435                      pop @{$self->{open_elements}};
5436                pop @{$self->{open_elements}};                  }
5437                $self->{insertion_mode} = 'in table';                  
5438                ## reprocess                  !!!insert-element ('colgroup',, $token);
5439                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5440                    ## reprocess
5441                    !!!ack-later;
5442                    next B;
5443                  } elsif ({
5444                            caption => 1,
5445                            colgroup => 1,
5446                            tbody => 1, tfoot => 1, thead => 1,
5447                           }->{$token->{tag_name}}) {
5448                    ## Clear back to table context
5449                    while (not ($self->{open_elements}->[-1]->[1]
5450                                    & TABLE_SCOPING_EL)) {
5451                      !!!cp ('t220');
5452                      ## ISSUE: Can this state be reached?
5453                      pop @{$self->{open_elements}};
5454                    }
5455                    
5456                    push @$active_formatting_elements, ['#marker', '']
5457                        if $token->{tag_name} eq 'caption';
5458                    
5459                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5460                    $self->{insertion_mode} = {
5461                                               caption => IN_CAPTION_IM,
5462                                               colgroup => IN_COLUMN_GROUP_IM,
5463                                               tbody => IN_TABLE_BODY_IM,
5464                                               tfoot => IN_TABLE_BODY_IM,
5465                                               thead => IN_TABLE_BODY_IM,
5466                                              }->{$token->{tag_name}};
5467                    !!!next-token;
5468                    !!!nack ('t220.1');
5469                    next B;
5470                  } else {
5471                    die "$0: in table: <>: $token->{tag_name}";
5472                  }
5473              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5474                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5475                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5476                                      ->manakai_local_name,
5477                                  token => $token);
5478    
5479                ## As if </table>                ## As if </table>
5480                ## have a table element in table scope                ## have a table element in table scope
5481                my $i;                my $i;
5482                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5483                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5484                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5485                      !!!cp ('t221');
5486                    $i = $_;                    $i = $_;
5487                    last INSCOPE;                    last INSCOPE;
5488                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5489                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5490                    last INSCOPE;                    last INSCOPE;
5491                  }                  }
5492                } # INSCOPE                } # INSCOPE
5493                unless (defined $i) {                unless (defined $i) {
5494                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5495    ## TODO: The following is wrong, maybe.
5496                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5497                                    token => $token);
5498                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5499                    !!!nack ('t223.1');
5500                  !!!next-token;                  !!!next-token;
5501                  redo B;                  next B;
5502                }                }
5503                                
5504    ## TODO: Followings are removed from the latest spec.
5505                ## generate implied end tags                ## generate implied end tags
5506                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5507                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5508                     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;  
5509                }                }
5510    
5511                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5512                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5513                    ## NOTE: |<table><tr><table>|
5514                    !!!parse-error (type => 'not closed',
5515                                    text => $self->{open_elements}->[-1]->[0]
5516                                        ->manakai_local_name,
5517                                    token => $token);
5518                  } else {
5519                    !!!cp ('t226');
5520                }                }
5521    
5522                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5523                  pop @{$open_tables};
5524    
5525                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5526    
5527                ## reprocess            ## reprocess
5528                redo B;            !!!ack-later;
5529              } else {            next B;
5530                #          } elsif ($token->{tag_name} eq 'style') {
5531              }            if (not $open_tables->[-1]->[1]) { # tainted
5532            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5533              if ({              ## NOTE: This is a "as if in head" code clone.
5534                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5535                  }->{$token->{tag_name}}) {              next B;
5536                ## have an element in table scope            } else {
5537                my $i;              !!!cp ('t227.7');
5538                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5539                  my $node = $self->{open_elements}->[$_];            }
5540                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5541                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5542                    last INSCOPE;              !!!cp ('t227.6');
5543                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5544                            table => 1, html => 1,              $script_start_tag->();
5545                           }->{$node->[1]}) {              next B;
5546                    last INSCOPE;            } else {
5547                  }              !!!cp ('t227.5');
5548                } # INSCOPE              #
5549                unless (defined $i) {            }
5550                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5551                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5552                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5553                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5554                }                if ($type eq 'hidden') {
5555                    !!!cp ('t227.3');
5556                    !!!parse-error (type => 'in table',
5557                                    text => $token->{tag_name}, token => $token);
5558    
5559                ## 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}};  
               }  
5560    
5561                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;  
               }  
5562    
               ## 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]);  
5563                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
5564    
5565                ## Clear back to table body context                  !!!next-token;
5566                ## nop by definition                  !!!ack ('t227.2.1');
5567                    next B;
5568                pop @{$self->{open_elements}};                } else {
5569                $self->{insertion_mode} = 'in table';                  !!!cp ('t227.2');
5570                ## reprocess                  #
5571                redo B;                }
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
5572              } else {              } else {
5573                  !!!cp ('t227.1');
5574                #                #
5575              }              }
5576            } else {            } else {
5577                !!!cp ('t227.4');
5578              #              #
5579            }            }
5580                      } else {
5581            ## As if in table            !!!cp ('t227');
5582            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5583            $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;  
               }  
             }  
5584    
5585              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table', text => $token->{tag_name},
5586                            token => $token);
5587    
5588              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5589              ## ISSUE: Spec says that "whenever a node would be inserted          #
5590              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5591              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5592              $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>  
5593                ## have an element in table scope                ## have an element in table scope
5594                my $i;                my $i;
5595                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5596                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5597                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5598                      !!!cp ('t228');
5599                    $i = $_;                    $i = $_;
5600                    last INSCOPE;                    last INSCOPE;
5601                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5602                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5603                    last INSCOPE;                    last INSCOPE;
5604                  }                  }
5605                } # INSCOPE                } # INSCOPE
5606                unless (defined $i) {                unless (defined $i) {
5607                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5608                    !!!parse-error (type => 'unmatched end tag',
5609                                    text => $token->{tag_name}, token => $token);
5610                  ## Ignore the token                  ## Ignore the token
5611                    !!!nack ('t230.1');
5612                  !!!next-token;                  !!!next-token;
5613                  redo B;                  next B;
5614                  } else {
5615                    !!!cp ('t232');
5616                }                }
5617    
5618                ## Clear back to table row context                ## Clear back to table row context
5619                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5620                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5621                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5622                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5623                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5624                }                }
5625    
5626                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5627                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5628                ## reprocess                !!!next-token;
5629                redo B;                !!!nack ('t231.1');
5630                  next B;
5631              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5632                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5633                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5634                    ## have an element in table scope
5635                ## As if </table>                  my $i;
5636                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5637                my $i;                    my $node = $self->{open_elements}->[$_];
5638                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5639                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5640                  if ($node->[1] eq 'table') {                      $i = $_;
5641                    $i = $_;                      last INSCOPE;
5642                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5643                  } elsif ({                      !!!cp ('t234');
5644                            table => 1, html => 1,                      last INSCOPE;
5645                           }->{$node->[1]}) {                    }
5646                    last INSCOPE;                  } # INSCOPE
5647                    unless (defined $i) {
5648                      !!!cp ('t235');
5649    ## TODO: The following is wrong.
5650                      !!!parse-error (type => 'unmatched end tag',
5651                                      text => $token->{type}, token => $token);
5652                      ## Ignore the token
5653                      !!!nack ('t236.1');
5654                      !!!next-token;
5655                      next B;
5656                  }                  }
5657                } # INSCOPE                  
5658                unless (defined $i) {                  ## Clear back to table row context
5659                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5660                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5661                  !!!next-token;                    !!!cp ('t236');
5662                  redo B;  ## ISSUE: Can this state be reached?
5663                }                    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;  
5664                  }                  }
5665                } # INSCOPE                  
5666                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5667                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5668                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5669                  !!!next-token;                }
5670                  redo B;  
5671                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5672                    ## have an element in table scope
5673                ## Clear back to table row context                  my $i;
5674                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5675                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5676                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5677                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5678                        $i = $_;
5679                        last INSCOPE;
5680                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5681                        !!!cp ('t238');
5682                        last INSCOPE;
5683                      }
5684                    } # INSCOPE
5685                    unless (defined $i) {
5686                      !!!cp ('t239');
5687                      !!!parse-error (type => 'unmatched end tag',
5688                                      text => $token->{tag_name}, token => $token);
5689                      ## Ignore the token
5690                      !!!nack ('t239.1');
5691                      !!!next-token;
5692                      next B;
5693                    }
5694                    
5695                    ## Clear back to table body context
5696                    while (not ($self->{open_elements}->[-1]->[1]
5697                                    & TABLE_ROWS_SCOPING_EL)) {
5698                      !!!cp ('t240');
5699                      pop @{$self->{open_elements}};
5700                    }
5701                    
5702                    ## As if <{current node}>
5703                    ## have an element in table scope
5704                    ## true by definition
5705                    
5706                    ## Clear back to table body context
5707                    ## nop by definition
5708                    
5709                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5710                    $self->{insertion_mode} = IN_TABLE_IM;
5711                    ## reprocess in the "in table" insertion mode...
5712                }                }
5713    
5714                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5715                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5716                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5717                redo B;                ## is synced with it.
5718              } elsif ($token->{tag_name} eq 'table') {  
5719                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5720                my $i;                my $i;
5721                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5722                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5723                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5724                      !!!cp ('t241');
5725                    $i = $_;                    $i = $_;
5726                    last INSCOPE;                    last INSCOPE;
5727                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5728                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5729                    last INSCOPE;                    last INSCOPE;
5730                  }                  }
5731                } # INSCOPE                } # INSCOPE
5732                unless (defined $i) {                unless (defined $i) {
5733                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5734                    !!!parse-error (type => 'unmatched end tag',
5735                                    text => $token->{tag_name}, token => $token);
5736                  ## Ignore the token                  ## Ignore the token
5737                    !!!nack ('t243.1');
5738                  !!!next-token;                  !!!next-token;
5739                  redo B;                  next B;
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
5740                }                }
5741                    
5742                pop @{$self->{open_elements}}; # tr                splice @{$self->{open_elements}}, $i;
5743                $self->{insertion_mode} = 'in table body';                pop @{$open_tables};
5744                ## reprocess                
5745                redo B;                $self->_reset_insertion_mode;
5746                  
5747                  !!!next-token;
5748                  next B;
5749              } elsif ({              } elsif ({
5750                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5751                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5752                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5753                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5754                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5755                  my $node = $self->{open_elements}->[$_];                  my $i;
5756                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5757                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5758                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5759                  } elsif ({                      !!!cp ('t247');
5760                            table => 1, html => 1,                      $i = $_;
5761                           }->{$node->[1]}) {                      last INSCOPE;
5762                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5763                        !!!cp ('t248');
5764                        last INSCOPE;
5765                      }
5766                    } # INSCOPE
5767                      unless (defined $i) {
5768                        !!!cp ('t249');
5769                        !!!parse-error (type => 'unmatched end tag',
5770                                        text => $token->{tag_name}, token => $token);
5771                        ## Ignore the token
5772                        !!!nack ('t249.1');
5773                        !!!next-token;
5774                        next B;
5775                      }
5776                    
5777                    ## As if </tr>
5778                    ## have an element in table scope
5779                    my $i;
5780                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5781                      my $node = $self->{open_elements}->[$_];
5782                      if ($node->[1] & TABLE_ROW_EL) {
5783                        !!!cp ('t250');
5784                        $i = $_;
5785                        last INSCOPE;
5786                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5787                        !!!cp ('t251');
5788                        last INSCOPE;
5789                      }
5790                    } # INSCOPE
5791                      unless (defined $i) {
5792                        !!!cp ('t252');
5793                        !!!parse-error (type => 'unmatched end tag',
5794                                        text => 'tr', token => $token);
5795                        ## Ignore the token
5796                        !!!nack ('t252.1');
5797                        !!!next-token;
5798                        next B;
5799                      }
5800                    
5801                    ## Clear back to table row context
5802                    while (not ($self->{open_elements}->[-1]->[1]
5803                                    & TABLE_ROW_SCOPING_EL)) {
5804                      !!!cp ('t253');
5805    ## ISSUE: Can this case be reached?
5806                      pop @{$self->{open_elements}};
5807                  }                  }
5808                } # INSCOPE                  
5809                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5810                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5811                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5812                }                }
5813    
               ## As if </tr>  
5814                ## have an element in table scope                ## have an element in table scope
5815                my $i;                my $i;
5816                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5817                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5818                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5819                      !!!cp ('t254');
5820                    $i = $_;                    $i = $_;
5821                    last INSCOPE;                    last INSCOPE;
5822                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5823                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5824                    last INSCOPE;                    last INSCOPE;
5825                  }                  }
5826                } # INSCOPE                } # INSCOPE
5827                unless (defined $i) {                unless (defined $i) {
5828                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5829                    !!!parse-error (type => 'unmatched end tag',
5830                                    text => $token->{tag_name}, token => $token);
5831                  ## Ignore the token                  ## Ignore the token
5832                    !!!nack ('t256.1');
5833                  !!!next-token;                  !!!next-token;
5834                  redo B;                  next B;
5835                }                }
5836    
5837                ## Clear back to table row context                ## Clear back to table body context
5838                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5839                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5840                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5841                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5842                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5843                }                }
5844    
5845                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5846                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5847                ## reprocess                !!!nack ('t257.1');
5848                redo B;                !!!next-token;
5849                  next B;
5850              } elsif ({              } elsif ({
5851                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5852                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5853                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5854                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5855                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5856                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5857                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
5858                !!!next-token;                            text => $token->{tag_name}, token => $token);
5859                redo B;            ## Ignore the token
5860              } else {            !!!nack ('t258.1');
5861                #             !!!next-token;
5862              }            next B;
5863            } else {          } else {
5864              #            !!!cp ('t259');
5865            }            !!!parse-error (type => 'in table:/',
5866                              text => $token->{tag_name}, token => $token);
5867    
5868            ## As if in table            $insert = $insert_to_foster;
5869            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5870            $in_body->($insert_to_foster);          }
5871            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5872          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5873            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5874              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5875              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5876                          #
5877              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5878              !!!cp ('t259.2');
5879              #
5880            }
5881    
5882              !!!next-token;          ## Stop parsing
5883              redo B;          last B;
5884            } elsif ($token->{type} eq 'comment') {        } else {
5885              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
5886              my $comment = $self->{document}->create_comment ($token->{data});        }
5887              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5888              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
5889              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5890            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5891              if ({                unless (length $token->{data}) {
5892                   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  
5893                  !!!next-token;                  !!!next-token;
5894                  redo B;                  next B;
5895                }                }
5896                }
5897                ## Close the cell              
5898                !!!back-token; # <?>              !!!cp ('t261');
5899                $token = {type => 'end tag', tag_name => $tn};              #
5900                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5901              } else {              if ($token->{tag_name} eq 'col') {
5902                  !!!cp ('t262');
5903                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5904                  pop @{$self->{open_elements}};
5905                  !!!ack ('t262.1');
5906                  !!!next-token;
5907                  next B;
5908                } else {
5909                  !!!cp ('t263');
5910                #                #
5911              }              }
5912            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5913              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5914                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5915                my $i;                  !!!cp ('t264');
5916                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
5917                  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});  
5918                  ## Ignore the token                  ## Ignore the token
5919                  !!!next-token;                  !!!next-token;
5920                  redo B;                  next B;
5921                }                } else {
5922                                  !!!cp ('t265');
5923                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5924                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5925                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5926                     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]);  
5927                }                }
5928                } elsif ($token->{tag_name} eq 'col') {
5929                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5930                  !!!parse-error (type => 'unmatched end tag',
5931                $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});  
5932                ## Ignore the token                ## Ignore the token
5933                !!!next-token;                !!!next-token;
5934                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;  
5935              } else {              } else {
5936                #                !!!cp ('t267');
5937                  #
5938              }              }
5939          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5940            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5941                @{$self->{open_elements}} == 1) { # redundant, maybe
5942              !!!cp ('t270.2');
5943              ## Stop parsing.
5944              last B;
5945            } else {
5946              ## NOTE: As if </colgroup>.
5947              !!!cp ('t270.1');
5948              pop @{$self->{open_elements}}; # colgroup
5949              $self->{insertion_mode} = IN_TABLE_IM;
5950              ## Reprocess.
5951              next B;
5952            }
5953          } else {
5954            die "$0: $token->{type}: Unknown token type";
5955          }
5956    
5957              ## As if </colgroup>
5958              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5959                !!!cp ('t269');
5960    ## TODO: Wrong error type?
5961                !!!parse-error (type => 'unmatched end tag',
5962                                text => 'colgroup', token => $token);
5963                ## Ignore the token
5964                !!!nack ('t269.1');
5965                !!!next-token;
5966                next B;
5967            } else {            } else {
5968              #              !!!cp ('t270');
5969                pop @{$self->{open_elements}}; # colgroup
5970                $self->{insertion_mode} = IN_TABLE_IM;
5971                !!!ack-later;
5972                ## reprocess
5973                next B;
5974              }
5975        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5976          if ($token->{type} == CHARACTER_TOKEN) {
5977            !!!cp ('t271');
5978            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5979            !!!next-token;
5980            next B;
5981          } elsif ($token->{type} == START_TAG_TOKEN) {
5982            if ($token->{tag_name} eq 'option') {
5983              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5984                !!!cp ('t272');
5985                ## As if </option>
5986                pop @{$self->{open_elements}};
5987              } else {
5988                !!!cp ('t273');
5989            }            }
             
           $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}};  
               }  
5990    
5991                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5992                !!!next-token;            !!!nack ('t273.1');
5993                redo B;            !!!next-token;
5994              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5995                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5996                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5997                  pop @{$self->{open_elements}};              !!!cp ('t274');
5998                }              ## As if </option>
5999                pop @{$self->{open_elements}};
6000              } else {
6001                !!!cp ('t275');
6002              }
6003    
6004                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6005                  ## As if </optgroup>              !!!cp ('t276');
6006                  pop @{$self->{open_elements}};              ## As if </optgroup>
6007                }              pop @{$self->{open_elements}};
6008              } else {
6009                !!!cp ('t277');
6010              }
6011    
6012                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6013                !!!next-token;            !!!nack ('t277.1');
6014                redo B;            !!!next-token;
6015              } elsif ($token->{tag_name} eq 'select') {            next B;
6016                !!!parse-error (type => 'not closed:select');          } elsif ({
6017                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6018                ## have an element in table scope                   }->{$token->{tag_name}} or
6019                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6020                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6021                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6022                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6023                    $i = $_;                     tr => 1, td => 1, th => 1,
6024                    last INSCOPE;                    }->{$token->{tag_name}})) {
6025                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6026                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6027                           }->{$node->[1]}) {                            token => $token);
6028                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6029                  }            ## as if there were </select> (otherwise).
6030                } # INSCOPE            ## have an element in table scope
6031                unless (defined $i) {            my $i;
6032                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6033                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6034                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6035                  redo B;                !!!cp ('t278');
6036                }                $i = $_;
6037                  last INSCOPE;
6038                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6039                  !!!cp ('t279');
6040                  last INSCOPE;
6041                }
6042              } # INSCOPE
6043              unless (defined $i) {
6044                !!!cp ('t280');
6045                !!!parse-error (type => 'unmatched end tag',
6046                                text => 'select', token => $token);
6047                ## Ignore the token
6048                !!!nack ('t280.1');
6049                !!!next-token;
6050                next B;
6051              }
6052                                
6053                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6054              splice @{$self->{open_elements}}, $i;
6055    
6056                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6057    
6058                !!!next-token;            if ($token->{tag_name} eq 'select') {
6059                redo B;              !!!nack ('t281.2');
6060              } else {              !!!next-token;
6061                #              next B;
6062              } else {
6063                !!!cp ('t281.1');
6064                !!!ack-later;
6065                ## Reprocess the token.
6066                next B;
6067              }
6068            } else {
6069              !!!cp ('t282');
6070              !!!parse-error (type => 'in select',
6071                              text => $token->{tag_name}, token => $token);
6072              ## Ignore the token
6073              !!!nack ('t282.1');
6074              !!!next-token;
6075              next B;
6076            }
6077          } elsif ($token->{type} == END_TAG_TOKEN) {
6078            if ($token->{tag_name} eq 'optgroup') {
6079              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6080                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6081                !!!cp ('t283');
6082                ## As if </option>
6083                splice @{$self->{open_elements}}, -2;
6084              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6085                !!!cp ('t284');
6086                pop @{$self->{open_elements}};
6087              } else {
6088                !!!cp ('t285');
6089                !!!parse-error (type => 'unmatched end tag',
6090                                text => $token->{tag_name}, token => $token);
6091                ## Ignore the token
6092              }
6093              !!!nack ('t285.1');
6094              !!!next-token;
6095              next B;
6096            } elsif ($token->{tag_name} eq 'option') {
6097              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6098                !!!cp ('t286');
6099                pop @{$self->{open_elements}};
6100              } else {
6101                !!!cp ('t287');
6102                !!!parse-error (type => 'unmatched end tag',
6103                                text => $token->{tag_name}, token => $token);
6104                ## Ignore the token
6105              }
6106              !!!nack ('t287.1');
6107              !!!next-token;
6108              next B;
6109            } elsif ($token->{tag_name} eq 'select') {
6110              ## have an element in table scope
6111              my $i;
6112              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6113                my $node = $self->{open_elements}->[$_];
6114                if ($node->[1] & SELECT_EL) {
6115                  !!!cp ('t288');
6116                  $i = $_;
6117                  last INSCOPE;
6118                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6119                  !!!cp ('t289');
6120                  last INSCOPE;
6121              }              }
6122            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6123              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6124                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6125                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6126                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6127                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6128                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6129                  pop @{$self->{open_elements}};              !!!next-token;
6130                } else {              next B;
6131                  !!!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;  
               }  
6132                                
6133                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6134              splice @{$self->{open_elements}}, $i;
6135    
6136                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6137    
6138                !!!next-token;            !!!nack ('t291.1');
6139                redo B;            !!!next-token;
6140              } elsif ({            next B;
6141                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6142                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6143                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6144                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6145                                   }->{$token->{tag_name}}) {
6146                ## have an element in table scope  ## TODO: The following is wrong?
6147                my $i;            !!!parse-error (type => 'unmatched end tag',
6148                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;  
               }  
6149                                
6150                ## As if </select>            ## have an element in table scope
6151                ## have an element in table scope            my $i;
6152                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6153                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6154                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6155                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6156                    $i = $_;                $i = $_;
6157                    last INSCOPE;                last INSCOPE;
6158                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6159                            table => 1, html => 1,                !!!cp ('t293');
6160                           }->{$node->[1]}) {                last INSCOPE;
6161                    last INSCOPE;              }
6162                  }            } # INSCOPE
6163                } # INSCOPE            unless (defined $i) {
6164                unless (defined $i) {              !!!cp ('t294');
6165                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6166                  ## Ignore the </select> token              !!!nack ('t294.1');
6167                  !!!next-token; ## TODO: ok?              !!!next-token;
6168                  redo B;              next B;
6169                }            }
6170                                
6171                splice @{$self->{open_elements}}, $i;            ## As if </select>
6172              ## have an element in table scope
6173                $self->_reset_insertion_mode;            undef $i;
6174              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6175                ## reprocess              my $node = $self->{open_elements}->[$_];
6176                redo B;              if ($node->[1] & SELECT_EL) {
6177              } else {                !!!cp ('t295');
6178                #                $i = $_;
6179                  last INSCOPE;
6180                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6181    ## ISSUE: Can this state be reached?
6182                  !!!cp ('t296');
6183                  last INSCOPE;
6184              }              }
6185            } else {            } # INSCOPE
6186              #            unless (defined $i) {
6187                !!!cp ('t297');
6188    ## TODO: The following error type is correct?
6189                !!!parse-error (type => 'unmatched end tag',
6190                                text => 'select', token => $token);
6191                ## Ignore the </select> token
6192                !!!nack ('t297.1');
6193                !!!next-token; ## TODO: ok?
6194                next B;
6195            }            }
6196                  
6197              !!!cp ('t298');
6198              splice @{$self->{open_elements}}, $i;
6199    
6200            !!!parse-error (type => 'in select:'.$token->{tag_name});            $self->_reset_insertion_mode;
6201    
6202              !!!ack-later;
6203              ## reprocess
6204              next B;
6205            } else {
6206              !!!cp ('t299');
6207              !!!parse-error (type => 'in select:/',
6208                              text => $token->{tag_name}, token => $token);
6209            ## Ignore the token            ## Ignore the token
6210              !!!nack ('t299.3');
6211            !!!next-token;            !!!next-token;
6212            redo B;            next B;
6213          } elsif ($self->{insertion_mode} eq 'after body') {          }
6214            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6215              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6216                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6217                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6218                            !!!parse-error (type => 'in body:#eof', token => $token);
6219                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6220              !!!cp ('t299.2');
6221            }
6222    
6223                unless (length $token->{data}) {          ## Stop parsing.
6224                  !!!next-token;          last B;
6225                  redo B;        } else {
6226                }          die "$0: $token->{type}: Unknown token type";
6227              }        }
6228                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6229              #        if ($token->{type} == CHARACTER_TOKEN) {
6230              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6231            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6232              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6233              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6234                  
6235              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6236              
6237              unless (length $token->{data}) {
6238                !!!cp ('t300');
6239              !!!next-token;              !!!next-token;
6240              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});  
6241            }            }
6242            }
6243            
6244            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6245              !!!cp ('t301');
6246              !!!parse-error (type => 'after html:#text', token => $token);
6247    
6248            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6249            ## reprocess          } else {
6250            redo B;            !!!cp ('t302');
6251          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6252            if ($token->{type} eq 'character') {          
6253              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6254                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6255    
6256                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6257                  !!!next-token;          ## reprocess
6258                  redo B;          next B;
6259                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6260              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6261              !!!cp ('t303');
6262              !!!parse-error (type => 'after html',
6263                              text => $token->{tag_name}, token => $token);
6264              
6265              ## Reprocess in the "after body" insertion mode.
6266            } else {
6267              !!!cp ('t304');
6268            }
6269    
6270              #          ## "after body" insertion mode
6271            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6272              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6273              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6274            $self->{insertion_mode} = IN_BODY_IM;
6275            !!!ack-later;
6276            ## reprocess
6277            next B;
6278          } elsif ($token->{type} == END_TAG_TOKEN) {
6279            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6280              !!!cp ('t305');
6281              !!!parse-error (type => 'after html:/',
6282                              text => $token->{tag_name}, token => $token);
6283              
6284              $self->{insertion_mode} = AFTER_BODY_IM;
6285              ## Reprocess in the "after body" insertion mode.
6286            } else {
6287              !!!cp ('t306');
6288            }
6289    
6290            ## "after body" insertion mode
6291            if ($token->{tag_name} eq 'html') {
6292              if (defined $self->{inner_html_node}) {
6293                !!!cp ('t307');
6294                !!!parse-error (type => 'unmatched end tag',
6295                                text => 'html', token => $token);
6296                ## Ignore the token
6297              !!!next-token;              !!!next-token;
6298              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 {  
               #  
             }  
6299            } else {            } else {
6300              #              !!!cp ('t308');
6301                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6302                !!!next-token;
6303                next B;
6304              }
6305            } else {
6306              !!!cp ('t309');
6307              !!!parse-error (type => 'after body:/',
6308                              text => $token->{tag_name}, token => $token);
6309    
6310              $self->{insertion_mode} = IN_BODY_IM;
6311              ## reprocess
6312              next B;
6313            }
6314          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6315            !!!cp ('t309.2');
6316            ## Stop parsing
6317            last B;
6318          } else {
6319            die "$0: $token->{type}: Unknown token type";
6320          }
6321        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6322          if ($token->{type} == CHARACTER_TOKEN) {
6323            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6324              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6325              
6326              unless (length $token->{data}) {
6327                !!!cp ('t310');
6328                !!!next-token;
6329                next B;
6330              }
6331            }
6332            
6333            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6334              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6335                !!!cp ('t311');
6336                !!!parse-error (type => 'in frameset:#text', token => $token);
6337              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6338                !!!cp ('t312');
6339                !!!parse-error (type => 'after frameset:#text', token => $token);
6340              } else { # "after after frameset"
6341                !!!cp ('t313');
6342                !!!parse-error (type => 'after html:#text', token => $token);
6343            }            }
6344                        
6345            if (defined $token->{tag_name}) {            ## Ignore the token.
6346              !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if (length $token->{data}) {
6347                !!!cp ('t314');
6348                ## reprocess the rest of characters
6349            } else {            } else {
6350              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
6351                !!!next-token;
6352              }
6353              next B;
6354            }
6355            
6356            die qq[$0: Character "$token->{data}"];
6357          } elsif ($token->{type} == START_TAG_TOKEN) {
6358            if ($token->{tag_name} eq 'frameset' and
6359                $self->{insertion_mode} == IN_FRAMESET_IM) {
6360              !!!cp ('t318');
6361              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6362              !!!nack ('t318.1');
6363              !!!next-token;
6364              next B;
6365            } elsif ($token->{tag_name} eq 'frame' and
6366                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6367              !!!cp ('t319');
6368              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6369              pop @{$self->{open_elements}};
6370              !!!ack ('t319.1');
6371              !!!next-token;
6372              next B;
6373            } elsif ($token->{tag_name} eq 'noframes') {
6374              !!!cp ('t320');
6375              ## NOTE: As if in head.
6376              $parse_rcdata->(CDATA_CONTENT_MODEL);
6377              next B;
6378    
6379              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6380              ## has no parse error.
6381            } else {
6382              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6383                !!!cp ('t321');
6384                !!!parse-error (type => 'in frameset',
6385                                text => $token->{tag_name}, token => $token);
6386              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6387                !!!cp ('t322');
6388                !!!parse-error (type => 'after frameset',
6389                                text => $token->{tag_name}, token => $token);
6390              } else { # "after after frameset"
6391                !!!cp ('t322.2');
6392                !!!parse-error (type => 'after after frameset',
6393                                text => $token->{tag_name}, token => $token);
6394            }            }
6395            ## Ignore the token            ## Ignore the token
6396              !!!nack ('t322.1');
6397            !!!next-token;            !!!next-token;
6398            redo B;            next B;
6399          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6400            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6401              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'frameset' and
6402                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{insertion_mode} == IN_FRAMESET_IM) {
6403              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6404                  @{$self->{open_elements}} == 1) {
6405                !!!cp ('t325');
6406                !!!parse-error (type => 'unmatched end tag',
6407                                text => $token->{tag_name}, token => $token);
6408                ## Ignore the token
6409                !!!next-token;
6410              } else {
6411                !!!cp ('t326');
6412                pop @{$self->{open_elements}};
6413                !!!next-token;
6414              }
6415    
6416                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
6417                  !!!next-token;                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6418                  redo B;              !!!cp ('t327');
6419                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6420              } else {
6421                !!!cp ('t328');
6422              }
6423              next B;
6424            } elsif ($token->{tag_name} eq 'html' and
6425                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6426              !!!cp ('t329');
6427              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6428              !!!next-token;
6429              next B;
6430            } else {
6431              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6432                !!!cp ('t330');
6433                !!!parse-error (type => 'in frameset:/',
6434                                text => $token->{tag_name}, token => $token);
6435              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6436                !!!cp ('t330.1');
6437                !!!parse-error (type => 'after frameset:/',
6438                                text => $token->{tag_name}, token => $token);
6439              } else { # "after after html"
6440                !!!cp ('t331');
6441                !!!parse-error (type => 'after after frameset:/',
6442                                text => $token->{tag_name}, token => $token);
6443              }
6444              ## Ignore the token
6445              !!!next-token;
6446              next B;
6447            }
6448          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6449            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6450                    @{$self->{open_elements}} == 1) { # redundant, maybe
6451              !!!cp ('t331.1');
6452              !!!parse-error (type => 'in body:#eof', token => $token);
6453            } else {
6454              !!!cp ('t331.2');
6455            }
6456            
6457            ## Stop parsing
6458            last B;
6459          } else {
6460            die "$0: $token->{type}: Unknown token type";
6461          }
6462    
6463          ## ISSUE: An issue in spec here
6464        } else {
6465          die "$0: $self->{insertion_mode}: Unknown insertion mode";
6466        }
6467    
6468        ## "in body" insertion mode
6469        if ($token->{type} == START_TAG_TOKEN) {
6470          if ($token->{tag_name} eq 'script') {
6471            !!!cp ('t332');
6472            ## NOTE: This is an "as if in head" code clone
6473            $script_start_tag->();
6474            next B;
6475          } elsif ($token->{tag_name} eq 'style') {
6476            !!!cp ('t333');
6477            ## NOTE: This is an "as if in head" code clone
6478            $parse_rcdata->(CDATA_CONTENT_MODEL);
6479            next B;
6480          } elsif ({
6481                    base => 1, link => 1,
6482                   }->{$token->{tag_name}}) {
6483            !!!cp ('t334');
6484            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6485            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6486            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6487            !!!ack ('t334.1');
6488            !!!next-token;
6489            next B;
6490          } elsif ($token->{tag_name} eq 'meta') {
6491            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6492            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6493            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6494    
6495            unless ($self->{confident}) {
6496              if ($token->{attributes}->{charset}) {
6497                !!!cp ('t335');
6498                ## NOTE: Whether the encoding is supported or not is handled
6499                ## in the {change_encoding} callback.
6500                $self->{change_encoding}
6501                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6502                
6503                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6504                    ->set_user_data (manakai_has_reference =>
6505                                         $token->{attributes}->{charset}
6506                                             ->{has_reference});
6507              } elsif ($token->{attributes}->{content}) {
6508                if ($token->{attributes}->{content}->{value}
6509                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6510                        [\x09-\x0D\x20]*=
6511                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6512                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6513                  !!!cp ('t336');
6514                  ## NOTE: Whether the encoding is supported or not is handled
6515                  ## in the {change_encoding} callback.
6516                  $self->{change_encoding}
6517                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6518                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6519                      ->set_user_data (manakai_has_reference =>
6520                                           $token->{attributes}->{content}
6521                                                 ->{has_reference});
6522              }              }
6523              }
6524            } else {
6525              if ($token->{attributes}->{charset}) {
6526                !!!cp ('t337');
6527                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6528                    ->set_user_data (manakai_has_reference =>
6529                                         $token->{attributes}->{charset}
6530                                             ->{has_reference});
6531              }
6532              if ($token->{attributes}->{content}) {
6533                !!!cp ('t338');
6534                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6535                    ->set_user_data (manakai_has_reference =>
6536                                         $token->{attributes}->{content}
6537                                             ->{has_reference});
6538              }
6539            }
6540    
6541              #          !!!ack ('t338.1');
6542            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6543              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6544              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6545              !!!next-token;          !!!cp ('t341');
6546              redo B;          ## NOTE: This is an "as if in head" code clone
6547            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6548              if ($token->{tag_name} eq 'noframes') {          next B;
6549                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6550                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6551              } else {                
6552                #          if (@{$self->{open_elements}} == 1 or
6553                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6554              !!!cp ('t342');
6555              ## Ignore the token
6556            } else {
6557              my $body_el = $self->{open_elements}->[1]->[0];
6558              for my $attr_name (keys %{$token->{attributes}}) {
6559                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6560                  !!!cp ('t343');
6561                  $body_el->set_attribute_ns
6562                    (undef, [undef, $attr_name],
6563                     $token->{attributes}->{$attr_name}->{value});
6564              }              }
6565            } elsif ($token->{type} eq 'end tag') {            }
6566              if ($token->{tag_name} eq 'html') {          }
6567                $phase = 'trailing end';          !!!nack ('t343.1');
6568            !!!next-token;
6569            next B;
6570          } elsif ({
6571                    address => 1, blockquote => 1, center => 1, dir => 1,
6572                    div => 1, dl => 1, fieldset => 1,
6573                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6574                    menu => 1, ol => 1, p => 1, ul => 1,
6575                    pre => 1, listing => 1,
6576                    form => 1,
6577                    table => 1,
6578                    hr => 1,
6579                   }->{$token->{tag_name}}) {
6580            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6581              !!!cp ('t350');
6582              !!!parse-error (type => 'in form:form', token => $token);
6583              ## Ignore the token
6584              !!!nack ('t350.1');
6585              !!!next-token;
6586              next B;
6587            }
6588    
6589            ## has a p element in scope
6590            INSCOPE: for (reverse @{$self->{open_elements}}) {
6591              if ($_->[1] & P_EL) {
6592                !!!cp ('t344');
6593                !!!back-token; # <form>
6594                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6595                          line => $token->{line}, column => $token->{column}};
6596                next B;
6597              } elsif ($_->[1] & SCOPING_EL) {
6598                !!!cp ('t345');
6599                last INSCOPE;
6600              }
6601            } # INSCOPE
6602              
6603            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6604            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6605              !!!nack ('t346.1');
6606              !!!next-token;
6607              if ($token->{type} == CHARACTER_TOKEN) {
6608                $token->{data} =~ s/^\x0A//;
6609                unless (length $token->{data}) {
6610                  !!!cp ('t346');
6611                !!!next-token;                !!!next-token;
               redo B;  
6612              } else {              } else {
6613                #                !!!cp ('t349');
6614              }              }
6615            } else {            } else {
6616              #              !!!cp ('t348');
6617            }            }
6618            } elsif ($token->{tag_name} eq 'form') {
6619              !!!cp ('t347.1');
6620              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6621    
6622              !!!nack ('t347.2');
6623              !!!next-token;
6624            } elsif ($token->{tag_name} eq 'table') {
6625              !!!cp ('t382');
6626              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6627                        
6628            if (defined $token->{tag_name}) {            $self->{insertion_mode} = IN_TABLE_IM;
6629              !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
6630              !!!nack ('t382.1');
6631              !!!next-token;
6632            } elsif ($token->{tag_name} eq 'hr') {
6633              !!!cp ('t386');
6634              pop @{$self->{open_elements}};
6635            
6636              !!!nack ('t386.1');
6637              !!!next-token;
6638            } else {
6639              !!!nack ('t347.1');
6640              !!!next-token;
6641            }
6642            next B;
6643          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6644            ## has a p element in scope
6645            INSCOPE: for (reverse @{$self->{open_elements}}) {
6646              if ($_->[1] & P_EL) {
6647                !!!cp ('t353');
6648                !!!back-token; # <x>
6649                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6650                          line => $token->{line}, column => $token->{column}};
6651                next B;
6652              } elsif ($_->[1] & SCOPING_EL) {
6653                !!!cp ('t354');
6654                last INSCOPE;
6655              }
6656            } # INSCOPE
6657              
6658            ## Step 1
6659            my $i = -1;
6660            my $node = $self->{open_elements}->[$i];
6661            my $li_or_dtdd = {li => {li => 1},
6662                              dt => {dt => 1, dd => 1},
6663                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6664            LI: {
6665              ## Step 2
6666              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6667                if ($i != -1) {
6668                  !!!cp ('t355');
6669                  !!!parse-error (type => 'not closed',
6670                                  text => $self->{open_elements}->[-1]->[0]
6671                                      ->manakai_local_name,
6672                                  token => $token);
6673                } else {
6674                  !!!cp ('t356');
6675                }
6676                splice @{$self->{open_elements}}, $i;
6677                last LI;
6678            } else {            } else {
6679              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6680              }
6681              
6682              ## Step 3
6683              if (not ($node->[1] & FORMATTING_EL) and
6684                  #not $phrasing_category->{$node->[1]} and
6685                  ($node->[1] & SPECIAL_EL or
6686                   $node->[1] & SCOPING_EL) and
6687                  not ($node->[1] & ADDRESS_EL) and
6688                  not ($node->[1] & DIV_EL)) {
6689                !!!cp ('t358');
6690                last LI;
6691              }
6692              
6693              !!!cp ('t359');
6694              ## Step 4
6695              $i--;
6696              $node = $self->{open_elements}->[$i];
6697              redo LI;
6698            } # LI
6699              
6700            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6701            !!!nack ('t359.1');
6702            !!!next-token;
6703            next B;
6704          } elsif ($token->{tag_name} eq 'plaintext') {
6705            ## has a p element in scope
6706            INSCOPE: for (reverse @{$self->{open_elements}}) {
6707              if ($_->[1] & P_EL) {
6708                !!!cp ('t367');
6709                !!!back-token; # <plaintext>
6710                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6711                          line => $token->{line}, column => $token->{column}};
6712                next B;
6713              } elsif ($_->[1] & SCOPING_EL) {
6714                !!!cp ('t368');
6715                last INSCOPE;
6716              }
6717            } # INSCOPE
6718              
6719            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6720              
6721            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6722              
6723            !!!nack ('t368.1');
6724            !!!next-token;
6725            next B;
6726          } elsif ($token->{tag_name} eq 'a') {
6727            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6728              my $node = $active_formatting_elements->[$i];
6729              if ($node->[1] & A_EL) {
6730                !!!cp ('t371');
6731                !!!parse-error (type => 'in a:a', token => $token);
6732                
6733                !!!back-token; # <a>
6734                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6735                          line => $token->{line}, column => $token->{column}};
6736                $formatting_end_tag->($token);
6737                
6738                AFE2: for (reverse 0..$#$active_formatting_elements) {
6739                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6740                    !!!cp ('t372');
6741                    splice @$active_formatting_elements, $_, 1;
6742                    last AFE2;
6743                  }
6744                } # AFE2
6745                OE: for (reverse 0..$#{$self->{open_elements}}) {
6746                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6747                    !!!cp ('t373');
6748                    splice @{$self->{open_elements}}, $_, 1;
6749                    last OE;
6750                  }
6751                } # OE
6752                last AFE;
6753              } elsif ($node->[0] eq '#marker') {
6754                !!!cp ('t374');
6755                last AFE;
6756            }            }
6757            } # AFE
6758              
6759            $reconstruct_active_formatting_elements->($insert_to_current);
6760    
6761            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6762            push @$active_formatting_elements, $self->{open_elements}->[-1];
6763    
6764            !!!nack ('t374.1');
6765            !!!next-token;
6766            next B;
6767          } elsif ($token->{tag_name} eq 'nobr') {
6768            $reconstruct_active_formatting_elements->($insert_to_current);
6769    
6770            ## has a |nobr| element in scope
6771            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6772              my $node = $self->{open_elements}->[$_];
6773              if ($node->[1] & NOBR_EL) {
6774                !!!cp ('t376');
6775                !!!parse-error (type => 'in nobr:nobr', token => $token);
6776                !!!back-token; # <nobr>
6777                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6778                          line => $token->{line}, column => $token->{column}};
6779                next B;
6780              } elsif ($node->[1] & SCOPING_EL) {
6781                !!!cp ('t377');
6782                last INSCOPE;
6783              }
6784            } # INSCOPE
6785            
6786            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6787            push @$active_formatting_elements, $self->{open_elements}->[-1];
6788            
6789            !!!nack ('t377.1');
6790            !!!next-token;
6791            next B;
6792          } elsif ($token->{tag_name} eq 'button') {
6793            ## has a button element in scope
6794            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6795              my $node = $self->{open_elements}->[$_];
6796              if ($node->[1] & BUTTON_EL) {
6797                !!!cp ('t378');
6798                !!!parse-error (type => 'in button:button', token => $token);
6799                !!!back-token; # <button>
6800                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6801                          line => $token->{line}, column => $token->{column}};
6802                next B;
6803              } elsif ($node->[1] & SCOPING_EL) {
6804                !!!cp ('t379');
6805                last INSCOPE;
6806              }
6807            } # INSCOPE
6808              
6809            $reconstruct_active_formatting_elements->($insert_to_current);
6810              
6811            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6812    
6813            ## TODO: associate with $self->{form_element} if defined
6814    
6815            push @$active_formatting_elements, ['#marker', ''];
6816    
6817            !!!nack ('t379.1');
6818            !!!next-token;
6819            next B;
6820          } elsif ({
6821                    xmp => 1,
6822                    iframe => 1,
6823                    noembed => 1,
6824                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6825                    noscript => 0, ## TODO: 1 if scripting is enabled
6826                   }->{$token->{tag_name}}) {
6827            if ($token->{tag_name} eq 'xmp') {
6828              !!!cp ('t381');
6829              $reconstruct_active_formatting_elements->($insert_to_current);
6830            } else {
6831              !!!cp ('t399');
6832            }
6833            ## NOTE: There is an "as if in body" code clone.
6834            $parse_rcdata->(CDATA_CONTENT_MODEL);
6835            next B;
6836          } elsif ($token->{tag_name} eq 'isindex') {
6837            !!!parse-error (type => 'isindex', token => $token);
6838            
6839            if (defined $self->{form_element}) {
6840              !!!cp ('t389');
6841            ## Ignore the token            ## Ignore the token
6842              !!!nack ('t389'); ## NOTE: Not acknowledged.
6843            !!!next-token;            !!!next-token;
6844            redo B;            next B;
6845            } else {
6846              !!!ack ('t391.1');
6847    
6848            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
6849              my $form_attrs;
6850              $form_attrs->{action} = $at->{action} if $at->{action};
6851              my $prompt_attr = $at->{prompt};
6852              $at->{name} = {name => 'name', value => 'isindex'};
6853              delete $at->{action};
6854              delete $at->{prompt};
6855              my @tokens = (
6856                            {type => START_TAG_TOKEN, tag_name => 'form',
6857                             attributes => $form_attrs,
6858                             line => $token->{line}, column => $token->{column}},
6859                            {type => START_TAG_TOKEN, tag_name => 'hr',
6860                             line => $token->{line}, column => $token->{column}},
6861                            {type => START_TAG_TOKEN, tag_name => 'p',
6862                             line => $token->{line}, column => $token->{column}},
6863                            {type => START_TAG_TOKEN, tag_name => 'label',
6864                             line => $token->{line}, column => $token->{column}},
6865                           );
6866              if ($prompt_attr) {
6867                !!!cp ('t390');
6868                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6869                               #line => $token->{line}, column => $token->{column},
6870                              };
6871              } else {
6872                !!!cp ('t391');
6873                push @tokens, {type => CHARACTER_TOKEN,
6874                               data => 'This is a searchable index. Insert your search keywords here: ',
6875                               #line => $token->{line}, column => $token->{column},
6876                              }; # SHOULD
6877                ## TODO: make this configurable
6878              }
6879              push @tokens,
6880                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6881                             line => $token->{line}, column => $token->{column}},
6882                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6883                            {type => END_TAG_TOKEN, tag_name => 'label',
6884                             line => $token->{line}, column => $token->{column}},
6885                            {type => END_TAG_TOKEN, tag_name => 'p',
6886                             line => $token->{line}, column => $token->{column}},
6887                            {type => START_TAG_TOKEN, tag_name => 'hr',
6888                             line => $token->{line}, column => $token->{column}},
6889                            {type => END_TAG_TOKEN, tag_name => 'form',
6890                             line => $token->{line}, column => $token->{column}};
6891              !!!back-token (@tokens);
6892              !!!next-token;
6893              next B;
6894            }
6895          } elsif ($token->{tag_name} eq 'textarea') {
6896            my $tag_name = $token->{tag_name};
6897            my $el;
6898            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6899            
6900            ## TODO: $self->{form_element} if defined
6901            $self->{content_model} = RCDATA_CONTENT_MODEL;
6902            delete $self->{escape}; # MUST
6903            
6904            $insert->($el);
6905            
6906            my $text = '';
6907            !!!nack ('t392.1');
6908            !!!next-token;
6909            if ($token->{type} == CHARACTER_TOKEN) {
6910              $token->{data} =~ s/^\x0A//;
6911              unless (length $token->{data}) {
6912                !!!cp ('t392');
6913                !!!next-token;
6914              } else {
6915                !!!cp ('t393');
6916              }
6917          } else {          } else {
6918            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
6919            }
6920            while ($token->{type} == CHARACTER_TOKEN) {
6921              !!!cp ('t395');
6922              $text .= $token->{data};
6923              !!!next-token;
6924            }
6925            if (length $text) {
6926              !!!cp ('t396');
6927              $el->manakai_append_text ($text);
6928            }
6929            
6930            $self->{content_model} = PCDATA_CONTENT_MODEL;
6931            
6932            if ($token->{type} == END_TAG_TOKEN and
6933                $token->{tag_name} eq $tag_name) {
6934              !!!cp ('t397');
6935              ## Ignore the token
6936            } else {
6937              !!!cp ('t398');
6938              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6939          }          }
       }  
     } 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  
6940          !!!next-token;          !!!next-token;
6941          redo B;          next B;
6942        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
6943          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
6944          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
6945            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6946              my $node = $self->{open_elements}->[$_];
6947              if ($node->[1] & RUBY_EL) {
6948                !!!cp ('t398.1');
6949                ## generate implied end tags
6950                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6951                  !!!cp ('t398.2');
6952                  pop @{$self->{open_elements}};
6953                }
6954                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6955                  !!!cp ('t398.3');
6956                  !!!parse-error (type => 'not closed',
6957                                  text => $self->{open_elements}->[-1]->[0]
6958                                      ->manakai_local_name,
6959                                  token => $token);
6960                  pop @{$self->{open_elements}}
6961                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6962                }
6963                last INSCOPE;
6964              } elsif ($node->[1] & SCOPING_EL) {
6965                !!!cp ('t398.4');
6966                last INSCOPE;
6967              }
6968            } # INSCOPE
6969    
6970            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6971    
6972            !!!nack ('t398.5');
6973          !!!next-token;          !!!next-token;
6974          redo B;          redo B;
6975        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
6976          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
6977            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
6978            ## As if in the main phase.  
6979            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6980            ## just before the phase has been changed to the trailing  
6981            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6982            $reconstruct_active_formatting_elements->($insert_to_current)  
6983              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
6984            
6985            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6986            
6987            if ($self->{self_closing}) {
6988              pop @{$self->{open_elements}};
6989              !!!ack ('t398.1');
6990            } else {
6991              !!!cp ('t398.2');
6992              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6993              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6994              ## mode, "in body" (not "in foreign content") secondary insertion
6995              ## mode, maybe.
6996            }
6997    
6998            !!!next-token;
6999            next B;
7000          } elsif ({
7001                    caption => 1, col => 1, colgroup => 1, frame => 1,
7002                    frameset => 1, head => 1, option => 1, optgroup => 1,
7003                    tbody => 1, td => 1, tfoot => 1, th => 1,
7004                    thead => 1, tr => 1,
7005                   }->{$token->{tag_name}}) {
7006            !!!cp ('t401');
7007            !!!parse-error (type => 'in body',
7008                            text => $token->{tag_name}, token => $token);
7009            ## Ignore the token
7010            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7011            !!!next-token;
7012            next B;
7013            
7014            ## ISSUE: An issue on HTML5 new elements in the spec.
7015          } else {
7016            if ($token->{tag_name} eq 'image') {
7017              !!!cp ('t384');
7018              !!!parse-error (type => 'image', token => $token);
7019              $token->{tag_name} = 'img';
7020            } else {
7021              !!!cp ('t385');
7022            }
7023    
7024            ## NOTE: There is an "as if <br>" code clone.
7025            $reconstruct_active_formatting_elements->($insert_to_current);
7026            
7027            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7028    
7029            if ({
7030                 applet => 1, marquee => 1, object => 1,
7031                }->{$token->{tag_name}}) {
7032              !!!cp ('t380');
7033              push @$active_formatting_elements, ['#marker', ''];
7034              !!!nack ('t380.1');
7035            } elsif ({
7036                      b => 1, big => 1, em => 1, font => 1, i => 1,
7037                      s => 1, small => 1, strile => 1,
7038                      strong => 1, tt => 1, u => 1,
7039                     }->{$token->{tag_name}}) {
7040              !!!cp ('t375');
7041              push @$active_formatting_elements, $self->{open_elements}->[-1];
7042              !!!nack ('t375.1');
7043            } elsif ($token->{tag_name} eq 'input') {
7044              !!!cp ('t388');
7045              ## TODO: associate with $self->{form_element} if defined
7046              pop @{$self->{open_elements}};
7047              !!!ack ('t388.2');
7048            } elsif ({
7049                      area => 1, basefont => 1, bgsound => 1, br => 1,
7050                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7051                      #image => 1,
7052                     }->{$token->{tag_name}}) {
7053              !!!cp ('t388.1');
7054              pop @{$self->{open_elements}};
7055              !!!ack ('t388.3');
7056            } elsif ($token->{tag_name} eq 'select') {
7057              ## TODO: associate with $self->{form_element} if defined
7058            
7059              if ($self->{insertion_mode} & TABLE_IMS or
7060                  $self->{insertion_mode} & BODY_TABLE_IMS or
7061                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7062                !!!cp ('t400.1');
7063                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7064              } else {
7065                !!!cp ('t400.2');
7066                $self->{insertion_mode} = IN_SELECT_IM;
7067              }
7068              !!!nack ('t400.3');
7069            } else {
7070              !!!nack ('t402');
7071            }
7072            
7073            !!!next-token;
7074            next B;
7075          }
7076        } elsif ($token->{type} == END_TAG_TOKEN) {
7077          if ($token->{tag_name} eq 'body') {
7078            ## has a |body| element in scope
7079            my $i;
7080            INSCOPE: {
7081              for (reverse @{$self->{open_elements}}) {
7082                if ($_->[1] & BODY_EL) {
7083                  !!!cp ('t405');
7084                  $i = $_;
7085                  last INSCOPE;
7086                } elsif ($_->[1] & SCOPING_EL) {
7087                  !!!cp ('t405.1');
7088                  last;
7089                }
7090              }
7091    
7092              !!!parse-error (type => 'start tag not allowed',
7093                              text => $token->{tag_name}, token => $token);
7094              ## NOTE: Ignore the token.
7095              !!!next-token;
7096              next B;
7097            } # INSCOPE
7098    
7099            for (@{$self->{open_elements}}) {
7100              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7101                !!!cp ('t403');
7102                !!!parse-error (type => 'not closed',
7103                                text => $_->[0]->manakai_local_name,
7104                                token => $token);
7105                last;
7106              } else {
7107                !!!cp ('t404');
7108              }
7109            }
7110    
7111            $self->{insertion_mode} = AFTER_BODY_IM;
7112            !!!next-token;
7113            next B;
7114          } elsif ($token->{tag_name} eq 'html') {
7115            ## TODO: Update this code.  It seems that the code below is not
7116            ## up-to-date, though it has same effect as speced.
7117            if (@{$self->{open_elements}} > 1 and
7118                $self->{open_elements}->[1]->[1] & BODY_EL) {
7119              ## ISSUE: There is an issue in the spec.
7120              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7121                !!!cp ('t406');
7122                !!!parse-error (type => 'not closed',
7123                                text => $self->{open_elements}->[1]->[0]
7124                                    ->manakai_local_name,
7125                                token => $token);
7126              } else {
7127                !!!cp ('t407');
7128              }
7129              $self->{insertion_mode} = AFTER_BODY_IM;
7130              ## reprocess
7131              next B;
7132            } else {
7133              !!!cp ('t408');
7134              !!!parse-error (type => 'unmatched end tag',
7135                              text => $token->{tag_name}, token => $token);
7136              ## Ignore the token
7137              !!!next-token;
7138              next B;
7139            }
7140          } elsif ({
7141                    address => 1, blockquote => 1, center => 1, dir => 1,
7142                    div => 1, dl => 1, fieldset => 1, listing => 1,
7143                    menu => 1, ol => 1, pre => 1, ul => 1,
7144                    dd => 1, dt => 1, li => 1,
7145                    applet => 1, button => 1, marquee => 1, object => 1,
7146                   }->{$token->{tag_name}}) {
7147            ## has an element in scope
7148            my $i;
7149            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7150              my $node = $self->{open_elements}->[$_];
7151              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7152                !!!cp ('t410');
7153                $i = $_;
7154                last INSCOPE;
7155              } elsif ($node->[1] & SCOPING_EL) {
7156                !!!cp ('t411');
7157                last INSCOPE;
7158              }
7159            } # INSCOPE
7160    
7161            unless (defined $i) { # has an element in scope
7162              !!!cp ('t413');
7163              !!!parse-error (type => 'unmatched end tag',
7164                              text => $token->{tag_name}, token => $token);
7165              ## NOTE: Ignore the token.
7166            } else {
7167              ## Step 1. generate implied end tags
7168              while ({
7169                      ## END_TAG_OPTIONAL_EL
7170                      dd => ($token->{tag_name} ne 'dd'),
7171                      dt => ($token->{tag_name} ne 'dt'),
7172                      li => ($token->{tag_name} ne 'li'),
7173                      p => 1,
7174                      rt => 1,
7175                      rp => 1,
7176                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7177                !!!cp ('t409');
7178                pop @{$self->{open_elements}};
7179              }
7180    
7181              ## Step 2.
7182              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7183                      ne $token->{tag_name}) {
7184                !!!cp ('t412');
7185                !!!parse-error (type => 'not closed',
7186                                text => $self->{open_elements}->[-1]->[0]
7187                                    ->manakai_local_name,
7188                                token => $token);
7189              } else {
7190                !!!cp ('t414');
7191              }
7192    
7193              ## Step 3.
7194              splice @{$self->{open_elements}}, $i;
7195    
7196              ## Step 4.
7197              $clear_up_to_marker->()
7198                  if {
7199                    applet => 1, button => 1, marquee => 1, object => 1,
7200                  }->{$token->{tag_name}};
7201            }
7202            !!!next-token;
7203            next B;
7204          } elsif ($token->{tag_name} eq 'form') {
7205            undef $self->{form_element};
7206    
7207            ## has an element in scope
7208            my $i;
7209            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7210              my $node = $self->{open_elements}->[$_];
7211              if ($node->[1] & FORM_EL) {
7212                !!!cp ('t418');
7213                $i = $_;
7214                last INSCOPE;
7215              } elsif ($node->[1] & SCOPING_EL) {
7216                !!!cp ('t419');
7217                last INSCOPE;
7218              }
7219            } # INSCOPE
7220    
7221            unless (defined $i) { # has an element in scope
7222              !!!cp ('t421');
7223              !!!parse-error (type => 'unmatched end tag',
7224                              text => $token->{tag_name}, token => $token);
7225              ## NOTE: Ignore the token.
7226            } else {
7227              ## Step 1. generate implied end tags
7228              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7229                !!!cp ('t417');
7230                pop @{$self->{open_elements}};
7231              }
7232                        
7233            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7234              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7235                      ne $token->{tag_name}) {
7236                !!!cp ('t417.1');
7237                !!!parse-error (type => 'not closed',
7238                                text => $self->{open_elements}->[-1]->[0]
7239                                    ->manakai_local_name,
7240                                token => $token);
7241              } else {
7242                !!!cp ('t420');
7243              }  
7244                        
7245            unless (length $token->{data}) {            ## Step 3.
7246              !!!next-token;            splice @{$self->{open_elements}}, $i;
7247              redo B;          }
7248    
7249            !!!next-token;
7250            next B;
7251          } elsif ({
7252                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7253                   }->{$token->{tag_name}}) {
7254            ## has an element in scope
7255            my $i;
7256            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7257              my $node = $self->{open_elements}->[$_];
7258              if ($node->[1] & HEADING_EL) {
7259                !!!cp ('t423');
7260                $i = $_;
7261                last INSCOPE;
7262              } elsif ($node->[1] & SCOPING_EL) {
7263                !!!cp ('t424');
7264                last INSCOPE;
7265            }            }
7266            } # INSCOPE
7267    
7268            unless (defined $i) { # has an element in scope
7269              !!!cp ('t425.1');
7270              !!!parse-error (type => 'unmatched end tag',
7271                              text => $token->{tag_name}, token => $token);
7272              ## NOTE: Ignore the token.
7273            } else {
7274              ## Step 1. generate implied end tags
7275              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7276                !!!cp ('t422');
7277                pop @{$self->{open_elements}};
7278              }
7279              
7280              ## Step 2.
7281              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7282                      ne $token->{tag_name}) {
7283                !!!cp ('t425');
7284                !!!parse-error (type => 'unmatched end tag',
7285                                text => $token->{tag_name}, token => $token);
7286              } else {
7287                !!!cp ('t426');
7288              }
7289    
7290              ## Step 3.
7291              splice @{$self->{open_elements}}, $i;
7292          }          }
7293            
7294            !!!next-token;
7295            next B;
7296          } elsif ($token->{tag_name} eq 'p') {
7297            ## has an element in scope
7298            my $i;
7299            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7300              my $node = $self->{open_elements}->[$_];
7301              if ($node->[1] & P_EL) {
7302                !!!cp ('t410.1');
7303                $i = $_;
7304                last INSCOPE;
7305              } elsif ($node->[1] & SCOPING_EL) {
7306                !!!cp ('t411.1');
7307                last INSCOPE;
7308              }
7309            } # INSCOPE
7310    
7311          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7312          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7313          ## reprocess                    ne $token->{tag_name}) {
7314          redo B;              !!!cp ('t412.1');
7315        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7316                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7317          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7318          $phase = 'main';                              token => $token);
7319          ## reprocess            } else {
7320          redo B;              !!!cp ('t414.1');
7321        } elsif ($token->{type} eq 'end-of-file') {            }
7322          ## Stop parsing  
7323          last B;            splice @{$self->{open_elements}}, $i;
7324            } else {
7325              !!!cp ('t413.1');
7326              !!!parse-error (type => 'unmatched end tag',
7327                              text => $token->{tag_name}, token => $token);
7328    
7329              !!!cp ('t415.1');
7330              ## As if <p>, then reprocess the current token
7331              my $el;
7332              !!!create-element ($el, $HTML_NS, 'p',, $token);
7333              $insert->($el);
7334              ## NOTE: Not inserted into |$self->{open_elements}|.
7335            }
7336    
7337            !!!next-token;
7338            next B;
7339          } elsif ({
7340                    a => 1,
7341                    b => 1, big => 1, em => 1, font => 1, i => 1,
7342                    nobr => 1, s => 1, small => 1, strile => 1,
7343                    strong => 1, tt => 1, u => 1,
7344                   }->{$token->{tag_name}}) {
7345            !!!cp ('t427');
7346            $formatting_end_tag->($token);
7347            next B;
7348          } elsif ($token->{tag_name} eq 'br') {
7349            !!!cp ('t428');
7350            !!!parse-error (type => 'unmatched end tag',
7351                            text => 'br', token => $token);
7352    
7353            ## As if <br>
7354            $reconstruct_active_formatting_elements->($insert_to_current);
7355            
7356            my $el;
7357            !!!create-element ($el, $HTML_NS, 'br',, $token);
7358            $insert->($el);
7359            
7360            ## Ignore the token.
7361            !!!next-token;
7362            next B;
7363          } elsif ({
7364                    caption => 1, col => 1, colgroup => 1, frame => 1,
7365                    frameset => 1, head => 1, option => 1, optgroup => 1,
7366                    tbody => 1, td => 1, tfoot => 1, th => 1,
7367                    thead => 1, tr => 1,
7368                    area => 1, basefont => 1, bgsound => 1,
7369                    embed => 1, hr => 1, iframe => 1, image => 1,
7370                    img => 1, input => 1, isindex => 1, noembed => 1,
7371                    noframes => 1, param => 1, select => 1, spacer => 1,
7372                    table => 1, textarea => 1, wbr => 1,
7373                    noscript => 0, ## TODO: if scripting is enabled
7374                   }->{$token->{tag_name}}) {
7375            !!!cp ('t429');
7376            !!!parse-error (type => 'unmatched end tag',
7377                            text => $token->{tag_name}, token => $token);
7378            ## Ignore the token
7379            !!!next-token;
7380            next B;
7381            
7382            ## ISSUE: Issue on HTML5 new elements in spec
7383            
7384        } else {        } else {
7385          die "$0: $token->{type}: Unknown token";          ## Step 1
7386            my $node_i = -1;
7387            my $node = $self->{open_elements}->[$node_i];
7388    
7389            ## Step 2
7390            S2: {
7391              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7392                ## Step 1
7393                ## generate implied end tags
7394                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7395                  !!!cp ('t430');
7396                  ## NOTE: |<ruby><rt></ruby>|.
7397                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7398                  ## which seems wrong.
7399                  pop @{$self->{open_elements}};
7400                  $node_i++;
7401                }
7402            
7403                ## Step 2
7404                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7405                        ne $token->{tag_name}) {
7406                  !!!cp ('t431');
7407                  ## NOTE: <x><y></x>
7408                  !!!parse-error (type => 'not closed',
7409                                  text => $self->{open_elements}->[-1]->[0]
7410                                      ->manakai_local_name,
7411                                  token => $token);
7412                } else {
7413                  !!!cp ('t432');
7414                }
7415                
7416                ## Step 3
7417                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7418    
7419                !!!next-token;
7420                last S2;
7421              } else {
7422                ## Step 3
7423                if (not ($node->[1] & FORMATTING_EL) and
7424                    #not $phrasing_category->{$node->[1]} and
7425                    ($node->[1] & SPECIAL_EL or
7426                     $node->[1] & SCOPING_EL)) {
7427                  !!!cp ('t433');
7428                  !!!parse-error (type => 'unmatched end tag',
7429                                  text => $token->{tag_name}, token => $token);
7430                  ## Ignore the token
7431                  !!!next-token;
7432                  last S2;
7433                }
7434    
7435                !!!cp ('t434');
7436              }
7437              
7438              ## Step 4
7439              $node_i--;
7440              $node = $self->{open_elements}->[$node_i];
7441              
7442              ## Step 5;
7443              redo S2;
7444            } # S2
7445            next B;
7446        }        }
7447      }      }
7448        next B;
7449      } continue { # B
7450        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7451          ## NOTE: The code below is executed in cases where it does not have
7452          ## to be, but it it is harmless even in those cases.
7453          ## has an element in scope
7454          INSCOPE: {
7455            for (reverse 0..$#{$self->{open_elements}}) {
7456              my $node = $self->{open_elements}->[$_];
7457              if ($node->[1] & FOREIGN_EL) {
7458                last INSCOPE;
7459              } elsif ($node->[1] & SCOPING_EL) {
7460                last;
7461              }
7462            }
7463            
7464            ## NOTE: No foreign element in scope.
7465            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7466          } # INSCOPE
7467        }
7468    } # B    } # B
7469    
7470    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4843  sub _tree_construction_main ($) { Line 7472  sub _tree_construction_main ($) {
7472    ## TODO: script stuffs    ## TODO: script stuffs
7473  } # _tree_construct_main  } # _tree_construct_main
7474    
7475  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7476    my $class = shift;    my $class = shift;
7477    my $node = shift;    my $node = shift;
7478    my $s = \$_[0];    my $s = \$_[0];
7479    my $onerror = $_[1];    my $onerror = $_[1];
7480      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7481    
7482      ## ISSUE: Should {confident} be true?
7483    
7484    my $nt = $node->node_type;    my $nt = $node->node_type;
7485    if ($nt == 9) {    if ($nt == 9) {
# Line 4864  sub set_inner_html ($$$) { Line 7496  sub set_inner_html ($$$) {
7496      }      }
7497    
7498      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7499      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7500    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7501      ## TODO: If non-html element      ## TODO: If non-html element
7502    
7503      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7504    
7505    ## TODO: Support for $get_wrapper
7506    
7507      ## Step 1 # MUST      ## Step 1 # MUST
7508      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7509      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7510        $doc->manakai_is_html (1);
7511      my $p = $class->new;      my $p = $class->new;
7512      $p->{document} = $doc;      $p->{document} = $doc;
7513    
7514      ## Step 9 # MUST      ## Step 8 # MUST
7515      my $i = 0;      my $i = 0;
7516      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7517      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7518      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7519        my $self = shift;        my $self = shift;
7520        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7521        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7522        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7523    
7524        if ($self->{next_input_character} == 0x000A) { # LF        $self->{next_char} = -1 and return if $i >= length $$s;
7525          $line++;        $self->{next_char} = ord substr $$s, $i++, 1;
7526          $column = 0;  
7527        } elsif ($self->{next_input_character} == 0x000D) { # CR        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7528          if ($i >= length $$s) {        $p->{column}++;
7529            #  
7530          if ($self->{next_char} == 0x000A) { # LF
7531            $p->{line}++;
7532            $p->{column} = 0;
7533            !!!cp ('i1');
7534          } elsif ($self->{next_char} == 0x000D) { # CR
7535            $i++ if substr ($$s, $i, 1) eq "\x0A";
7536            $self->{next_char} = 0x000A; # LF # MUST
7537            $p->{line}++;
7538            $p->{column} = 0;
7539            !!!cp ('i2');
7540          } elsif ($self->{next_char} > 0x10FFFF) {
7541            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7542            !!!cp ('i3');
7543          } elsif ($self->{next_char} == 0x0000) { # NULL
7544            !!!cp ('i4');
7545            !!!parse-error (type => 'NULL');
7546            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7547          } elsif ($self->{next_char} <= 0x0008 or
7548                   (0x000E <= $self->{next_char} and
7549                    $self->{next_char} <= 0x001F) or
7550                   (0x007F <= $self->{next_char} and
7551                    $self->{next_char} <= 0x009F) or
7552                   (0xD800 <= $self->{next_char} and
7553                    $self->{next_char} <= 0xDFFF) or
7554                   (0xFDD0 <= $self->{next_char} and
7555                    $self->{next_char} <= 0xFDDF) or
7556                   {
7557                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7558                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7559                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7560                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7561                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7562                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7563                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7564                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7565                    0x10FFFE => 1, 0x10FFFF => 1,
7566                   }->{$self->{next_char}}) {
7567            !!!cp ('i4.1');
7568            if ($self->{next_char} < 0x10000) {
7569              !!!parse-error (type => 'control char',
7570                              text => (sprintf 'U+%04X', $self->{next_char}));
7571          } else {          } else {
7572            my $next_char = ord substr $$s, $i++, 1;            !!!parse-error (type => 'control char',
7573            if ($next_char == 0x000A) { # LF                            text => (sprintf 'U-%08X', $self->{next_char}));
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
7574          }          }
         $self->{next_input_character} = 0x000A; # LF # MUST  
         $line++;  
         $column = 0;  
       } elsif ($self->{next_input_character} > 0x10FFFF) {  
         $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
       } elsif ($self->{next_input_character} == 0x0000) { # NULL  
         $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
7575        }        }
7576      };      };
7577        $p->{prev_char} = [-1, -1, -1];
7578        $p->{next_char} = -1;
7579            
7580      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7581        my (%opt) = @_;        my (%opt) = @_;
7582        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7583          my $column = $opt{column};
7584          if (defined $opt{token} and defined $opt{token}->{line}) {
7585            $line = $opt{token}->{line};
7586            $column = $opt{token}->{column};
7587          }
7588          warn "Parse error ($opt{type}) at line $line column $column\n";
7589      };      };
7590      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7591        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7592      };      };
7593            
7594      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7595      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7596    
7597      ## Step 2      ## Step 2
7598      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7599      $p->{content_model_flag} = {      $p->{content_model} = {
7600        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7601        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7602        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7603        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7604        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7605        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7606        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7607        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7608        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7609        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7610      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7611         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7612            unless defined $p->{content_model};
7613            ## ISSUE: What is "the name of the element"? local name?
7614    
7615      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7616          ## TODO: Foreign element OK?
7617    
7618      ## Step 4      ## Step 3
7619      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7620        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7621    
7622      ## Step 5 # MUST      ## Step 4 # MUST
7623      $doc->append_child ($root);      $doc->append_child ($root);
7624    
7625      ## Step 6 # MUST      ## Step 5 # MUST
7626      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7627    
7628      undef $p->{head_element};      undef $p->{head_element};
7629    
7630      ## Step 7 # MUST      ## Step 6 # MUST
7631      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7632    
7633      ## Step 8 # MUST      ## Step 7 # MUST
7634      my $anode = $node;      my $anode = $node;
7635      AN: while (defined $anode) {      AN: while (defined $anode) {
7636        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7637          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7638          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7639            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7640                !!!cp ('i5');
7641              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7642              last AN;              last AN;
7643            }            }
# Line 4969  sub set_inner_html ($$$) { Line 7646  sub set_inner_html ($$$) {
7646        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7647      } # AN      } # AN
7648            
7649      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7650      {      {
7651        my $self = $p;        my $self = $p;
7652        !!!next-token;        !!!next-token;
7653      }      }
7654      $p->_tree_construction_main;      $p->_tree_construction_main;
7655    
7656      ## Step 11 # MUST      ## Step 10 # MUST
7657      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7658      for (@cn) {      for (@cn) {
7659        $node->remove_child ($_);        $node->remove_child ($_);
7660      }      }
7661      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7662    
7663      ## Step 12 # MUST      ## Step 11 # MUST
7664      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7665      for (@cn) {      for (@cn) {
7666          $this_doc->adopt_node ($_);
7667        $node->append_child ($_);        $node->append_child ($_);
7668      }      }
7669      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7670    
7671      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7672    
7673        delete $p->{parse_error}; # delete loop
7674    } else {    } else {
7675      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";
7676    }    }
# Line 4999  sub set_inner_html ($$$) { Line 7678  sub set_inner_html ($$$) {
7678    
7679  } # tree construction stage  } # tree construction stage
7680    
7681  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7682    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  
7683    
7684  1;  1;
7685  # $Date$  # $Date$

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.162

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24