/[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.6 by wakaba, Sat May 26 08:12:34 2007 UTC revision 1.166 by wakaba, Sat Sep 13 08:21:35 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    
 ## <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  
       $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 391  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_SECTION_STATE () { 35 }
807    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
808    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
809    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
810    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
811    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
812    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
813    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
814    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
815    
816    sub DOCTYPE_TOKEN () { 1 }
817    sub COMMENT_TOKEN () { 2 }
818    sub START_TAG_TOKEN () { 3 }
819    sub END_TAG_TOKEN () { 4 }
820    sub END_OF_FILE_TOKEN () { 5 }
821    sub CHARACTER_TOKEN () { 6 }
822    
823    sub AFTER_HTML_IMS () { 0b100 }
824    sub HEAD_IMS ()       { 0b1000 }
825    sub BODY_IMS ()       { 0b10000 }
826    sub BODY_TABLE_IMS () { 0b100000 }
827    sub TABLE_IMS ()      { 0b1000000 }
828    sub ROW_IMS ()        { 0b10000000 }
829    sub BODY_AFTER_IMS () { 0b100000000 }
830    sub FRAME_IMS ()      { 0b1000000000 }
831    sub SELECT_IMS ()     { 0b10000000000 }
832    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
833        ## NOTE: "in foreign content" insertion mode is special; it is combined
834        ## with the secondary insertion mode.  In this parser, they are stored
835        ## together in the bit-or'ed form.
836    
837    ## NOTE: "initial" and "before html" insertion modes have no constants.
838    
839    ## NOTE: "after after body" insertion mode.
840    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
841    
842    ## NOTE: "after after frameset" insertion mode.
843    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
844    
845    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
846    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
847    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
848    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
849    sub IN_BODY_IM () { BODY_IMS }
850    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
851    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
852    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
853    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
854    sub IN_TABLE_IM () { TABLE_IMS }
855    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
856    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
857    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
858    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
859    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
860    sub IN_COLUMN_GROUP_IM () { 0b10 }
861    
862  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
863    
864  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
865    my $self = shift;    my $self = shift;
866    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
867    $self->{content_model_flag} = 'PCDATA'; # be    #$self->{state_keyword}; # initialized when used
868    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
869      undef $self->{current_token};
870    undef $self->{current_attribute};    undef $self->{current_attribute};
871    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
872    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
873      delete $self->{self_closing};
874    $self->{char} = [];    $self->{char} = [];
875    # $self->{next_input_character}    # $self->{next_char}
876    !!!next-input-character;    !!!next-input-character;
877    $self->{token} = [];    $self->{token} = [];
878      # $self->{escape}
879  } # _initialize_tokenizer  } # _initialize_tokenizer
880    
881  ## A token has:  ## A token has:
882  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
883  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
884  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
885      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
886  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
887  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
888  ##   ->{data} (comment, character)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
889    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
890  ## Macros  ##        ->{name}
891  ##   Macros MUST be preceded by three EXCLAMATION MARKs.  ##        ->{value}
892  ##   emit ($token)  ##        ->{has_reference} == 1 or 0
893  ##     Emits the specified token.  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
894    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
895    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
896    ##     while the token is pushed back to the stack.
897    
898  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
899    
# Line 444  sub _initialize_tokenizer ($) { Line 903  sub _initialize_tokenizer ($) {
903  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
904  ## and removed from the list.  ## and removed from the list.
905    
906    ## NOTE: HTML5 "Writing HTML documents" section, applied to
907    ## documents and not to user agents and conformance checkers,
908    ## contains some requirements that are not detected by the
909    ## parsing algorithm:
910    ## - Some requirements on character encoding declarations. ## TODO
911    ## - "Elements MUST NOT contain content that their content model disallows."
912    ##   ... Some are parse error, some are not (will be reported by c.c.).
913    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
914    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
915    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
916    
917    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
918    ## be detected by the HTML5 parsing algorithm:
919    ## - Text,
920    
921  sub _get_next_token ($) {  sub _get_next_token ($) {
922    my $self = shift;    my $self = shift;
923    
924      if ($self->{self_closing}) {
925        !!!parse-error (type => 'nestc', token => $self->{current_token});
926        ## NOTE: The |self_closing| flag is only set by start tag token.
927        ## In addition, when a start tag token is emitted, it is always set to
928        ## |current_token|.
929        delete $self->{self_closing};
930      }
931    
932    if (@{$self->{token}}) {    if (@{$self->{token}}) {
933        $self->{self_closing} = $self->{token}->[0]->{self_closing};
934      return shift @{$self->{token}};      return shift @{$self->{token}};
935    }    }
936    
937    A: {    A: {
938      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
939        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
940          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
941              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
942            $self->{state} = 'entity data';            !!!cp (1);
943              $self->{state} = ENTITY_DATA_STATE;
944            !!!next-input-character;            !!!next-input-character;
945            redo A;            redo A;
946          } else {          } else {
947              !!!cp (2);
948            #            #
949          }          }
950        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x002D) { # -
951          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
952            $self->{state} = 'tag open';            unless ($self->{escape}) {
953                if ($self->{prev_char}->[0] == 0x002D and # -
954                    $self->{prev_char}->[1] == 0x0021 and # !
955                    $self->{prev_char}->[2] == 0x003C) { # <
956                  !!!cp (3);
957                  $self->{escape} = 1;
958                } else {
959                  !!!cp (4);
960                }
961              } else {
962                !!!cp (5);
963              }
964            }
965            
966            #
967          } elsif ($self->{next_char} == 0x003C) { # <
968            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
969                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
970                 not $self->{escape})) {
971              !!!cp (6);
972              $self->{state} = TAG_OPEN_STATE;
973            !!!next-input-character;            !!!next-input-character;
974            redo A;            redo A;
975          } else {          } else {
976              !!!cp (7);
977            #            #
978          }          }
979        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
980          !!!emit ({type => 'end-of-file'});          if ($self->{escape} and
981                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
982              if ($self->{prev_char}->[0] == 0x002D and # -
983                  $self->{prev_char}->[1] == 0x002D) { # -
984                !!!cp (8);
985                delete $self->{escape};
986              } else {
987                !!!cp (9);
988              }
989            } else {
990              !!!cp (10);
991            }
992            
993            #
994          } elsif ($self->{next_char} == -1) {
995            !!!cp (11);
996            !!!emit ({type => END_OF_FILE_TOKEN,
997                      line => $self->{line}, column => $self->{column}});
998          last A; ## TODO: ok?          last A; ## TODO: ok?
999          } else {
1000            !!!cp (12);
1001        }        }
1002        # Anything else        # Anything else
1003        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
1004                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
1005                       line => $self->{line}, column => $self->{column},
1006                      };
1007        ## Stay in the data state        ## Stay in the data state
1008        !!!next-input-character;        !!!next-input-character;
1009    
1010        !!!emit ($token);        !!!emit ($token);
1011    
1012        redo A;        redo A;
1013      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
1014        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
1015    
1016          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
1017                
1018        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
1019    
1020        $self->{state} = 'data';        $self->{state} = DATA_STATE;
1021        # next-input-character is already done        # next-input-character is already done
1022    
1023        unless (defined $token) {        unless (defined $token) {
1024          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
1025            !!!emit ({type => CHARACTER_TOKEN, data => '&',
1026                      line => $l, column => $c,
1027                     });
1028        } else {        } else {
1029            !!!cp (14);
1030          !!!emit ($token);          !!!emit ($token);
1031        }        }
1032    
1033        redo A;        redo A;
1034      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1035        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1036            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
1037          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
1038            !!!next-input-character;            !!!next-input-character;
1039            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1040            redo A;            redo A;
1041          } else {          } else {
1042              !!!cp (16);
1043            ## reconsume            ## reconsume
1044            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1045    
1046            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1047                        line => $self->{line_prev},
1048                        column => $self->{column_prev},
1049                       });
1050    
1051            redo A;            redo A;
1052          }          }
1053        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1054          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1055            $self->{state} = 'markup declaration open';            !!!cp (17);
1056              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1057            !!!next-input-character;            !!!next-input-character;
1058            redo A;            redo A;
1059          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1060            $self->{state} = 'close tag open';            !!!cp (18);
1061              $self->{state} = CLOSE_TAG_OPEN_STATE;
1062            !!!next-input-character;            !!!next-input-character;
1063            redo A;            redo A;
1064          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1065                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1066              !!!cp (19);
1067            $self->{current_token}            $self->{current_token}
1068              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1069                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1070            $self->{state} = 'tag name';                 line => $self->{line_prev},
1071                   column => $self->{column_prev}};
1072              $self->{state} = TAG_NAME_STATE;
1073            !!!next-input-character;            !!!next-input-character;
1074            redo A;            redo A;
1075          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1076                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1077            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1078                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1079            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1080                                        line => $self->{line_prev},
1081                                        column => $self->{column_prev}};
1082              $self->{state} = TAG_NAME_STATE;
1083            !!!next-input-character;            !!!next-input-character;
1084            redo A;            redo A;
1085          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1086            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1087            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1088                              line => $self->{line_prev},
1089                              column => $self->{column_prev});
1090              $self->{state} = DATA_STATE;
1091            !!!next-input-character;            !!!next-input-character;
1092    
1093            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1094                        line => $self->{line_prev},
1095                        column => $self->{column_prev},
1096                       });
1097    
1098            redo A;            redo A;
1099          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1100            !!!parse-error (type => 'pio');            !!!cp (22);
1101            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1102            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1103                              column => $self->{column_prev});
1104              $self->{state} = BOGUS_COMMENT_STATE;
1105              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1106                                        line => $self->{line_prev},
1107                                        column => $self->{column_prev},
1108                                       };
1109              ## $self->{next_char} is intentionally left as is
1110            redo A;            redo A;
1111          } else {          } else {
1112            !!!parse-error (type => 'bare stago');            !!!cp (23);
1113            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1114                              line => $self->{line_prev},
1115                              column => $self->{column_prev});
1116              $self->{state} = DATA_STATE;
1117            ## reconsume            ## reconsume
1118    
1119            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1120                        line => $self->{line_prev},
1121                        column => $self->{column_prev},
1122                       });
1123    
1124            redo A;            redo A;
1125          }          }
1126        } else {        } else {
1127          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1128        }        }
1129      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1130        if ($self->{content_model_flag} eq 'RCDATA' or        ## NOTE: The "close tag open state" in the spec is implemented as
1131            $self->{content_model_flag} eq 'CDATA') {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1132          my @next_char;  
1133          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1134            push @next_char, $self->{next_input_character};        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1135            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);          if (defined $self->{last_emitted_start_tag_name}) {
1136            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1137            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {            $self->{state_keyword} = '';
1138              !!!next-input-character;            ## Reconsume.
             next TAGNAME;  
           } else {  
             !!!parse-error (type => 'unmatched end tag');  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = 'data';  
   
             !!!emit ({type => 'character', data => '</'});  
   
             redo A;  
           }  
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error (type => 'unmatched end tag');  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
1139            redo A;            redo A;
1140          } else {          } else {
1141            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1142            !!!back-next-input-character (@next_char);            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1143            # and consume...            !!!cp (28);
1144              $self->{state} = DATA_STATE;
1145              ## Reconsume.
1146              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1147                        line => $l, column => $c,
1148                       });
1149              redo A;
1150          }          }
1151        }        }
1152          
1153        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1154            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1155          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1156                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1157          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1158          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1159          redo A;                 line => $l, column => $c};
1160        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1161                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1162          $self->{current_token} = {type => 'end tag',          redo A;
1163                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1164          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1165          !!!next-input-character;          !!!cp (30);
1166          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1167        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1168          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1169          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1170            !!!next-input-character;
1171            redo A;
1172          } elsif ($self->{next_char} == 0x003E) { # >
1173            !!!cp (31);
1174            !!!parse-error (type => 'empty end tag',
1175                            line => $self->{line_prev}, ## "<" in "</>"
1176                            column => $self->{column_prev} - 1);
1177            $self->{state} = DATA_STATE;
1178          !!!next-input-character;          !!!next-input-character;
1179          redo A;          redo A;
1180        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1181            !!!cp (32);
1182          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1183          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1184          # reconsume          # reconsume
1185    
1186          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1187                      line => $l, column => $c,
1188                     });
1189    
1190          redo A;          redo A;
1191        } else {        } else {
1192            !!!cp (33);
1193          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1194          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1195          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1196          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1197                                      column => $self->{column_prev} - 1,
1198                                     };
1199            ## NOTE: $self->{next_char} is intentionally left as is.
1200            ## Although the "anything else" case of the spec not explicitly
1201            ## states that the next input character is to be reconsumed,
1202            ## it will be included to the |data| of the comment token
1203            ## generated from the bogus end tag, as defined in the
1204            ## "bogus comment state" entry.
1205            redo A;
1206          }
1207        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1208          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1209          if (length $ch) {
1210            my $CH = $ch;
1211            $ch =~ tr/a-z/A-Z/;
1212            my $nch = chr $self->{next_char};
1213            if ($nch eq $ch or $nch eq $CH) {
1214              !!!cp (24);
1215              ## Stay in the state.
1216              $self->{state_keyword} .= $nch;
1217              !!!next-input-character;
1218              redo A;
1219            } else {
1220              !!!cp (25);
1221              $self->{state} = DATA_STATE;
1222              ## Reconsume.
1223              !!!emit ({type => CHARACTER_TOKEN,
1224                        data => '</' . $self->{state_keyword},
1225                        line => $self->{line_prev},
1226                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1227                       });
1228              redo A;
1229            }
1230          } else { # after "<{tag-name}"
1231            unless ({
1232                     0x0009 => 1, # HT
1233                     0x000A => 1, # LF
1234                     0x000B => 1, # VT
1235                     0x000C => 1, # FF
1236                     0x0020 => 1, # SP
1237                     0x003E => 1, # >
1238                     0x002F => 1, # /
1239                     -1 => 1, # EOF
1240                    }->{$self->{next_char}}) {
1241              !!!cp (26);
1242              ## Reconsume.
1243              $self->{state} = DATA_STATE;
1244              !!!emit ({type => CHARACTER_TOKEN,
1245                        data => '</' . $self->{state_keyword},
1246                        line => $self->{line_prev},
1247                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1248                       });
1249              redo A;
1250            } else {
1251              !!!cp (27);
1252              $self->{current_token}
1253                  = {type => END_TAG_TOKEN,
1254                     tag_name => $self->{last_emitted_start_tag_name},
1255                     line => $self->{line_prev},
1256                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1257              $self->{state} = TAG_NAME_STATE;
1258              ## Reconsume.
1259              redo A;
1260            }
1261        }        }
1262      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
1263        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1264            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1265            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1266            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1267            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1268          $self->{state} = 'before attribute name';          !!!cp (34);
1269            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1270          !!!next-input-character;          !!!next-input-character;
1271          redo A;          redo A;
1272        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1273          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1274              !!!cp (35);
1275            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1276          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1277            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1278            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1279              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1280            }            #  !!! cp (36);
1281              #  !!! parse-error (type => 'end tag attribute');
1282              #} else {
1283                !!!cp (37);
1284              #}
1285          } else {          } else {
1286            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1287          }          }
1288          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1289          !!!next-input-character;          !!!next-input-character;
1290    
1291          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1292    
1293          redo A;          redo A;
1294        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1295                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1296          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1297            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1298            # start tag or end tag            # start tag or end tag
1299          ## Stay in this state          ## Stay in this state
1300          !!!next-input-character;          !!!next-input-character;
1301          redo A;          redo A;
1302        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1303          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1304          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305              !!!cp (39);
1306            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1309            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1310              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1311            }            #  !!! cp (40);
1312              #  !!! parse-error (type => 'end tag attribute');
1313              #} else {
1314                !!!cp (41);
1315              #}
1316          } else {          } else {
1317            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1318          }          }
1319          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1320          # reconsume          # reconsume
1321    
1322          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1323    
1324          redo A;          redo A;
1325        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1326            !!!cp (42);
1327            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1328          !!!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  
1329          redo A;          redo A;
1330        } else {        } else {
1331          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1332            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1333            # start tag or end tag            # start tag or end tag
1334          ## Stay in the state          ## Stay in the state
1335          !!!next-input-character;          !!!next-input-character;
1336          redo A;          redo A;
1337        }        }
1338      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1339        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1340            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1341            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1342            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1343            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1344            !!!cp (45);
1345          ## Stay in the state          ## Stay in the state
1346          !!!next-input-character;          !!!next-input-character;
1347          redo A;          redo A;
1348        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1349          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1350              !!!cp (46);
1351            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1352          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1353            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1354            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1355                !!!cp (47);
1356              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1357              } else {
1358                !!!cp (48);
1359            }            }
1360          } else {          } else {
1361            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1362          }          }
1363          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1364          !!!next-input-character;          !!!next-input-character;
1365    
1366          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1367    
1368          redo A;          redo A;
1369        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1370                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1371          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1372                                value => ''};          $self->{current_attribute}
1373          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1374                   value => '',
1375                   line => $self->{line}, column => $self->{column}};
1376            $self->{state} = ATTRIBUTE_NAME_STATE;
1377            !!!next-input-character;
1378            redo A;
1379          } elsif ($self->{next_char} == 0x002F) { # /
1380            !!!cp (50);
1381            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1382          !!!next-input-character;          !!!next-input-character;
1383          redo A;          redo A;
1384        } 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) {  
1385          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1386          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1387              !!!cp (52);
1388            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1389          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1390            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1391            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1392                !!!cp (53);
1393              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1394              } else {
1395                !!!cp (54);
1396            }            }
1397          } else {          } else {
1398            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1399          }          }
1400          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1401          # reconsume          # reconsume
1402    
1403          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1404    
1405          redo A;          redo A;
1406        } else {        } else {
1407          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1408                                value => ''};               0x0022 => 1, # "
1409          $self->{state} = 'attribute name';               0x0027 => 1, # '
1410                 0x003D => 1, # =
1411                }->{$self->{next_char}}) {
1412              !!!cp (55);
1413              !!!parse-error (type => 'bad attribute name');
1414            } else {
1415              !!!cp (56);
1416            }
1417            $self->{current_attribute}
1418                = {name => chr ($self->{next_char}),
1419                   value => '',
1420                   line => $self->{line}, column => $self->{column}};
1421            $self->{state} = ATTRIBUTE_NAME_STATE;
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
1424        }        }
1425      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1426        my $before_leave = sub {        my $before_leave = sub {
1427          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1428              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1429            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1430              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1431            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1432          } else {          } else {
1433              !!!cp (58);
1434            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1435              = $self->{current_attribute};              = $self->{current_attribute};
1436          }          }
1437        }; # $before_leave        }; # $before_leave
1438    
1439        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1440            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1441            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1442            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1443            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1444            !!!cp (59);
1445          $before_leave->();          $before_leave->();
1446          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1447          !!!next-input-character;          !!!next-input-character;
1448          redo A;          redo A;
1449        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1450            !!!cp (60);
1451          $before_leave->();          $before_leave->();
1452          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1453          !!!next-input-character;          !!!next-input-character;
1454          redo A;          redo A;
1455        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1456          $before_leave->();          $before_leave->();
1457          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1458              !!!cp (61);
1459            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1460          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1461            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1462              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1463            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1464              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1465            }            }
1466          } else {          } else {
1467            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1468          }          }
1469          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1470          !!!next-input-character;          !!!next-input-character;
1471    
1472          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1473    
1474          redo A;          redo A;
1475        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1476                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1477          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1478            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1479          ## Stay in the state          ## Stay in the state
1480          !!!next-input-character;          !!!next-input-character;
1481          redo A;          redo A;
1482        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1483            !!!cp (64);
1484          $before_leave->();          $before_leave->();
1485            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1486          !!!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  
1487          redo A;          redo A;
1488        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1489          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1490          $before_leave->();          $before_leave->();
1491          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1492              !!!cp (66);
1493            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1494          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1495            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1496            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1497                !!!cp (67);
1498              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1499              } else {
1500                ## NOTE: This state should never be reached.
1501                !!!cp (68);
1502            }            }
1503          } else {          } else {
1504            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1505          }          }
1506          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1507          # reconsume          # reconsume
1508    
1509          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1510    
1511          redo A;          redo A;
1512        } else {        } else {
1513          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1514                $self->{next_char} == 0x0027) { # '
1515              !!!cp (69);
1516              !!!parse-error (type => 'bad attribute name');
1517            } else {
1518              !!!cp (70);
1519            }
1520            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1521          ## Stay in the state          ## Stay in the state
1522          !!!next-input-character;          !!!next-input-character;
1523          redo A;          redo A;
1524        }        }
1525      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1526        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1527            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1528            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1529            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1530            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1531            !!!cp (71);
1532          ## Stay in the state          ## Stay in the state
1533          !!!next-input-character;          !!!next-input-character;
1534          redo A;          redo A;
1535        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1536          $self->{state} = 'before attribute value';          !!!cp (72);
1537            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1538          !!!next-input-character;          !!!next-input-character;
1539          redo A;          redo A;
1540        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1541          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1542              !!!cp (73);
1543            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1544          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1545            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1546            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1547                !!!cp (74);
1548              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1549              } else {
1550                ## NOTE: This state should never be reached.
1551                !!!cp (75);
1552            }            }
1553          } else {          } else {
1554            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1555          }          }
1556          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1557          !!!next-input-character;          !!!next-input-character;
1558    
1559          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1560    
1561          redo A;          redo A;
1562        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1563                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1564          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1565                                value => ''};          $self->{current_attribute}
1566          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1567                   value => '',
1568                   line => $self->{line}, column => $self->{column}};
1569            $self->{state} = ATTRIBUTE_NAME_STATE;
1570            !!!next-input-character;
1571            redo A;
1572          } elsif ($self->{next_char} == 0x002F) { # /
1573            !!!cp (77);
1574            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1575          !!!next-input-character;          !!!next-input-character;
1576          redo A;          redo A;
1577        } 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) {  
1578          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1579          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1580              !!!cp (79);
1581            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1582          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1583            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1584            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1585                !!!cp (80);
1586              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1587              } else {
1588                ## NOTE: This state should never be reached.
1589                !!!cp (81);
1590            }            }
1591          } else {          } else {
1592            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1593          }          }
1594          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1595          # reconsume          # reconsume
1596    
1597          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1598    
1599          redo A;          redo A;
1600        } else {        } else {
1601          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1602                                value => ''};              $self->{next_char} == 0x0027) { # '
1603          $self->{state} = 'attribute name';            !!!cp (78);
1604              !!!parse-error (type => 'bad attribute name');
1605            } else {
1606              !!!cp (82);
1607            }
1608            $self->{current_attribute}
1609                = {name => chr ($self->{next_char}),
1610                   value => '',
1611                   line => $self->{line}, column => $self->{column}};
1612            $self->{state} = ATTRIBUTE_NAME_STATE;
1613          !!!next-input-character;          !!!next-input-character;
1614          redo A;                  redo A;        
1615        }        }
1616      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1617        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1618            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1619            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1620            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1621            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1622            !!!cp (83);
1623          ## Stay in the state          ## Stay in the state
1624          !!!next-input-character;          !!!next-input-character;
1625          redo A;          redo A;
1626        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1627          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1628            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1629          !!!next-input-character;          !!!next-input-character;
1630          redo A;          redo A;
1631        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1632          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1633            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1634          ## reconsume          ## reconsume
1635          redo A;          redo A;
1636        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1637          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1638            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1639          !!!next-input-character;          !!!next-input-character;
1640          redo A;          redo A;
1641        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1642          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1643            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1644              !!!cp (87);
1645            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1646          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1647            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1648            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1649                !!!cp (88);
1650              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1651              } else {
1652                ## NOTE: This state should never be reached.
1653                !!!cp (89);
1654            }            }
1655          } else {          } else {
1656            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1657          }          }
1658          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660    
1661          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1662    
1663          redo A;          redo A;
1664        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1665          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1666          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1667              !!!cp (90);
1668            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1669          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1670            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1671            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1672                !!!cp (91);
1673              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1674              } else {
1675                ## NOTE: This state should never be reached.
1676                !!!cp (92);
1677            }            }
1678          } else {          } else {
1679            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1680          }          }
1681          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1682          ## reconsume          ## reconsume
1683    
1684          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1685    
1686          redo A;          redo A;
1687        } else {        } else {
1688          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1689          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1690              !!!parse-error (type => 'bad attribute value');
1691            } else {
1692              !!!cp (94);
1693            }
1694            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1695            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1696          !!!next-input-character;          !!!next-input-character;
1697          redo A;          redo A;
1698        }        }
1699      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1700        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1701          $self->{state} = 'before attribute name';          !!!cp (95);
1702            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1703          !!!next-input-character;          !!!next-input-character;
1704          redo A;          redo A;
1705        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1706          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1707          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1708            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1709          !!!next-input-character;          !!!next-input-character;
1710          redo A;          redo A;
1711        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1712          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1713          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1714              !!!cp (97);
1715            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1716          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1717            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1718            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1719                !!!cp (98);
1720              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1721              } else {
1722                ## NOTE: This state should never be reached.
1723                !!!cp (99);
1724            }            }
1725          } else {          } else {
1726            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1727          }          }
1728          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1729          ## reconsume          ## reconsume
1730    
1731          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1732    
1733          redo A;          redo A;
1734        } else {        } else {
1735          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1736            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1737          ## Stay in the state          ## Stay in the state
1738          !!!next-input-character;          !!!next-input-character;
1739          redo A;          redo A;
1740        }        }
1741      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1742        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1743          $self->{state} = 'before attribute name';          !!!cp (101);
1744            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1745          !!!next-input-character;          !!!next-input-character;
1746          redo A;          redo A;
1747        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1748          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1749          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1750            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1754          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1755          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1756              !!!cp (103);
1757            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1758          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1759            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1760            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1761                !!!cp (104);
1762              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1763              } else {
1764                ## NOTE: This state should never be reached.
1765                !!!cp (105);
1766            }            }
1767          } else {          } else {
1768            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1769          }          }
1770          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1771          ## reconsume          ## reconsume
1772    
1773          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1774    
1775          redo A;          redo A;
1776        } else {        } else {
1777          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1778            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1779          ## Stay in the state          ## Stay in the state
1780          !!!next-input-character;          !!!next-input-character;
1781          redo A;          redo A;
1782        }        }
1783      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1784        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1785            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1786            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1787            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1788            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1789          $self->{state} = 'before attribute name';          !!!cp (107);
1790          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1791          redo A;          !!!next-input-character;
1792        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1793          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1794          $self->{state} = 'entity in attribute value';          !!!cp (108);
1795          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1796          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1797        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1798          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1799          } elsif ($self->{next_char} == 0x003E) { # >
1800            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1801              !!!cp (109);
1802            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1803          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1804            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1805            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1806                !!!cp (110);
1807              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1808              } else {
1809                ## NOTE: This state should never be reached.
1810                !!!cp (111);
1811            }            }
1812          } else {          } else {
1813            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1814          }          }
1815          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1816          !!!next-input-character;          !!!next-input-character;
1817    
1818          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1819    
1820          redo A;          redo A;
1821        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_char} == -1) {
                $self->{next_input_character} == -1) {  
1822          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1823          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1824              !!!cp (112);
1825            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1826          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1827            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1828            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1829                !!!cp (113);
1830              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1831              } else {
1832                ## NOTE: This state should never be reached.
1833                !!!cp (114);
1834            }            }
1835          } else {          } else {
1836            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1837          }          }
1838          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1839          ## reconsume          ## reconsume
1840    
1841          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1842    
1843          redo A;          redo A;
1844        } else {        } else {
1845          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1846                 0x0022 => 1, # "
1847                 0x0027 => 1, # '
1848                 0x003D => 1, # =
1849                }->{$self->{next_char}}) {
1850              !!!cp (115);
1851              !!!parse-error (type => 'bad attribute value');
1852            } else {
1853              !!!cp (116);
1854            }
1855            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1856          ## Stay in the state          ## Stay in the state
1857          !!!next-input-character;          !!!next-input-character;
1858          redo A;          redo A;
1859        }        }
1860      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1861        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity
1862              (1,
1863               $self->{last_attribute_value_state}
1864                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1865               $self->{last_attribute_value_state}
1866                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1867               -1);
1868    
1869        unless (defined $token) {        unless (defined $token) {
1870            !!!cp (117);
1871          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1872        } else {        } else {
1873            !!!cp (118);
1874          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1875            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1876          ## 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"
1877        }        }
1878    
1879        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1880        # next-input-character is already done        # next-input-character is already done
1881        redo A;        redo A;
1882      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1883          if ($self->{next_char} == 0x0009 or # HT
1884              $self->{next_char} == 0x000A or # LF
1885              $self->{next_char} == 0x000B or # VT
1886              $self->{next_char} == 0x000C or # FF
1887              $self->{next_char} == 0x0020) { # SP
1888            !!!cp (118);
1889            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1890            !!!next-input-character;
1891            redo A;
1892          } elsif ($self->{next_char} == 0x003E) { # >
1893            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1894              !!!cp (119);
1895              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1896            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1897              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1898              if ($self->{current_token}->{attributes}) {
1899                !!!cp (120);
1900                !!!parse-error (type => 'end tag attribute');
1901              } else {
1902                ## NOTE: This state should never be reached.
1903                !!!cp (121);
1904              }
1905            } else {
1906              die "$0: $self->{current_token}->{type}: Unknown token type";
1907            }
1908            $self->{state} = DATA_STATE;
1909            !!!next-input-character;
1910    
1911            !!!emit ($self->{current_token}); # start tag or end tag
1912    
1913            redo A;
1914          } elsif ($self->{next_char} == 0x002F) { # /
1915            !!!cp (122);
1916            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1917            !!!next-input-character;
1918            redo A;
1919          } elsif ($self->{next_char} == -1) {
1920            !!!parse-error (type => 'unclosed tag');
1921            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1922              !!!cp (122.3);
1923              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1924            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1925              if ($self->{current_token}->{attributes}) {
1926                !!!cp (122.1);
1927                !!!parse-error (type => 'end tag attribute');
1928              } else {
1929                ## NOTE: This state should never be reached.
1930                !!!cp (122.2);
1931              }
1932            } else {
1933              die "$0: $self->{current_token}->{type}: Unknown token type";
1934            }
1935            $self->{state} = DATA_STATE;
1936            ## Reconsume.
1937            !!!emit ($self->{current_token}); # start tag or end tag
1938            redo A;
1939          } else {
1940            !!!cp ('124.1');
1941            !!!parse-error (type => 'no space between attributes');
1942            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1943            ## reconsume
1944            redo A;
1945          }
1946        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1947          if ($self->{next_char} == 0x003E) { # >
1948            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1949              !!!cp ('124.2');
1950              !!!parse-error (type => 'nestc', token => $self->{current_token});
1951              ## TODO: Different type than slash in start tag
1952              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1953              if ($self->{current_token}->{attributes}) {
1954                !!!cp ('124.4');
1955                !!!parse-error (type => 'end tag attribute');
1956              } else {
1957                !!!cp ('124.5');
1958              }
1959              ## TODO: Test |<title></title/>|
1960            } else {
1961              !!!cp ('124.3');
1962              $self->{self_closing} = 1;
1963            }
1964    
1965            $self->{state} = DATA_STATE;
1966            !!!next-input-character;
1967    
1968            !!!emit ($self->{current_token}); # start tag or end tag
1969    
1970            redo A;
1971          } elsif ($self->{next_char} == -1) {
1972            !!!parse-error (type => 'unclosed tag');
1973            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1974              !!!cp (124.7);
1975              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1976            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1977              if ($self->{current_token}->{attributes}) {
1978                !!!cp (124.5);
1979                !!!parse-error (type => 'end tag attribute');
1980              } else {
1981                ## NOTE: This state should never be reached.
1982                !!!cp (124.6);
1983              }
1984            } else {
1985              die "$0: $self->{current_token}->{type}: Unknown token type";
1986            }
1987            $self->{state} = DATA_STATE;
1988            ## Reconsume.
1989            !!!emit ($self->{current_token}); # start tag or end tag
1990            redo A;
1991          } else {
1992            !!!cp ('124.4');
1993            !!!parse-error (type => 'nestc');
1994            ## TODO: This error type is wrong.
1995            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1996            ## Reconsume.
1997            redo A;
1998          }
1999        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2000        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2001                
2002        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
2003          #my $token = {type => COMMENT_TOKEN, data => ''};
2004    
2005        BC: {        BC: {
2006          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
2007            $self->{state} = 'data';            !!!cp (124);
2008              $self->{state} = DATA_STATE;
2009            !!!next-input-character;            !!!next-input-character;
2010    
2011            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
2012    
2013            redo A;            redo A;
2014          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
2015            $self->{state} = 'data';            !!!cp (125);
2016              $self->{state} = DATA_STATE;
2017            ## reconsume            ## reconsume
2018    
2019            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
2020    
2021            redo A;            redo A;
2022          } else {          } else {
2023            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
2024              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2025            !!!next-input-character;            !!!next-input-character;
2026            redo BC;            redo BC;
2027          }          }
2028        } # BC        } # BC
     } elsif ($self->{state} eq 'markup declaration open') {  
       ## (only happen if PCDATA state)  
2029    
2030        my @next_char;        die "$0: _get_next_token: unexpected case [BC]";
2031        push @next_char, $self->{next_input_character};      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2032          ## (only happen if PCDATA state)
2033                
2034        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2035          !!!next-input-character;          !!!cp (133);
2036          push @next_char, $self->{next_input_character};          $self->{state} = MD_HYPHEN_STATE;
2037          if ($self->{next_input_character} == 0x002D) { # -          !!!next-input-character;
2038            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2039            $self->{state} = 'comment';        } elsif ($self->{next_char} == 0x0044 or # D
2040            !!!next-input-character;                 $self->{next_char} == 0x0064) { # d
2041            redo A;          ## ASCII case-insensitive.
2042          }          !!!cp (130);
2043        } elsif ($self->{next_input_character} == 0x0044 or # D          $self->{state} = MD_DOCTYPE_STATE;
2044                 $self->{next_input_character} == 0x0064) { # d          $self->{state_keyword} = chr $self->{next_char};
2045            !!!next-input-character;
2046            redo A;
2047          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2048                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2049                   $self->{next_char} == 0x005B) { # [
2050            !!!cp (135.4);                
2051            $self->{state} = MD_CDATA_STATE;
2052            $self->{state_keyword} = '[';
2053          !!!next-input-character;          !!!next-input-character;
2054          push @next_char, $self->{next_input_character};          redo A;
2055          if ($self->{next_input_character} == 0x004F or # O        } else {
2056              $self->{next_input_character} == 0x006F) { # o          !!!cp (136);
           !!!next-input-character;  
           push @next_char, $self->{next_input_character};  
           if ($self->{next_input_character} == 0x0043 or # C  
               $self->{next_input_character} == 0x0063) { # c  
             !!!next-input-character;  
             push @next_char, $self->{next_input_character};  
             if ($self->{next_input_character} == 0x0054 or # T  
                 $self->{next_input_character} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_input_character};  
               if ($self->{next_input_character} == 0x0059 or # Y  
                   $self->{next_input_character} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_input_character};  
                 if ($self->{next_input_character} == 0x0050 or # P  
                     $self->{next_input_character} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_input_character};  
                   if ($self->{next_input_character} == 0x0045 or # E  
                       $self->{next_input_character} == 0x0065) { # e  
                     ## ISSUE: What a stupid code this is!  
                     $self->{state} = 'DOCTYPE';  
                     !!!next-input-character;  
                     redo A;  
                   }  
                 }  
               }  
             }  
           }  
         }  
2057        }        }
2058    
2059        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment',
2060        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2061        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2062        $self->{state} = 'bogus comment';        ## Reconsume.
2063          $self->{state} = BOGUS_COMMENT_STATE;
2064          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2065                                    line => $self->{line_prev},
2066                                    column => $self->{column_prev} - 1,
2067                                   };
2068        redo A;        redo A;
2069              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2070        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2071        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?          !!!cp (127);
2072      } elsif ($self->{state} eq 'comment') {          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2073        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2074          $self->{state} = 'comment dash';                                    column => $self->{column_prev} - 2,
2075                                     };
2076            $self->{state} = COMMENT_START_STATE;
2077            !!!next-input-character;
2078            redo A;
2079          } else {
2080            !!!cp (128);
2081            !!!parse-error (type => 'bogus comment',
2082                            line => $self->{line_prev},
2083                            column => $self->{column_prev} - 2);
2084            $self->{state} = BOGUS_COMMENT_STATE;
2085            ## Reconsume.
2086            $self->{current_token} = {type => COMMENT_TOKEN,
2087                                      data => '-',
2088                                      line => $self->{line_prev},
2089                                      column => $self->{column_prev} - 2,
2090                                     };
2091            redo A;
2092          }
2093        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2094          ## ASCII case-insensitive.
2095          if ($self->{next_char} == [
2096                undef,
2097                0x004F, # O
2098                0x0043, # C
2099                0x0054, # T
2100                0x0059, # Y
2101                0x0050, # P
2102              ]->[length $self->{state_keyword}] or
2103              $self->{next_char} == [
2104                undef,
2105                0x006F, # o
2106                0x0063, # c
2107                0x0074, # t
2108                0x0079, # y
2109                0x0070, # p
2110              ]->[length $self->{state_keyword}]) {
2111            !!!cp (131);
2112            ## Stay in the state.
2113            $self->{state_keyword} .= chr $self->{next_char};
2114            !!!next-input-character;
2115            redo A;
2116          } elsif ((length $self->{state_keyword}) == 6 and
2117                   ($self->{next_char} == 0x0045 or # E
2118                    $self->{next_char} == 0x0065)) { # e
2119            !!!cp (129);
2120            $self->{state} = DOCTYPE_STATE;
2121            $self->{current_token} = {type => DOCTYPE_TOKEN,
2122                                      quirks => 1,
2123                                      line => $self->{line_prev},
2124                                      column => $self->{column_prev} - 7,
2125                                     };
2126            !!!next-input-character;
2127            redo A;
2128          } else {
2129            !!!cp (132);        
2130            !!!parse-error (type => 'bogus comment',
2131                            line => $self->{line_prev},
2132                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2133            $self->{state} = BOGUS_COMMENT_STATE;
2134            ## Reconsume.
2135            $self->{current_token} = {type => COMMENT_TOKEN,
2136                                      data => $self->{state_keyword},
2137                                      line => $self->{line_prev},
2138                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2139                                     };
2140            redo A;
2141          }
2142        } elsif ($self->{state} == MD_CDATA_STATE) {
2143          if ($self->{next_char} == {
2144                '[' => 0x0043, # C
2145                '[C' => 0x0044, # D
2146                '[CD' => 0x0041, # A
2147                '[CDA' => 0x0054, # T
2148                '[CDAT' => 0x0041, # A
2149              }->{$self->{state_keyword}}) {
2150            !!!cp (135.1);
2151            ## Stay in the state.
2152            $self->{state_keyword} .= chr $self->{next_char};
2153            !!!next-input-character;
2154            redo A;
2155          } elsif ($self->{state_keyword} eq '[CDATA' and
2156                   $self->{next_char} == 0x005B) { # [
2157            !!!cp (135.2);
2158            $self->{current_token} = {type => CHARACTER_TOKEN,
2159                                      data => '',
2160                                      line => $self->{line_prev},
2161                                      column => $self->{column_prev} - 7};
2162            $self->{state} = CDATA_SECTION_STATE;
2163            !!!next-input-character;
2164            redo A;
2165          } else {
2166            !!!cp (135.3);
2167            !!!parse-error (type => 'bogus comment',
2168                            line => $self->{line_prev},
2169                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2170            $self->{state} = BOGUS_COMMENT_STATE;
2171            ## Reconsume.
2172            $self->{current_token} = {type => COMMENT_TOKEN,
2173                                      data => $self->{state_keyword},
2174                                      line => $self->{line_prev},
2175                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2176                                     };
2177            redo A;
2178          }
2179        } elsif ($self->{state} == COMMENT_START_STATE) {
2180          if ($self->{next_char} == 0x002D) { # -
2181            !!!cp (137);
2182            $self->{state} = COMMENT_START_DASH_STATE;
2183            !!!next-input-character;
2184            redo A;
2185          } elsif ($self->{next_char} == 0x003E) { # >
2186            !!!cp (138);
2187            !!!parse-error (type => 'bogus comment');
2188            $self->{state} = DATA_STATE;
2189            !!!next-input-character;
2190    
2191            !!!emit ($self->{current_token}); # comment
2192    
2193            redo A;
2194          } elsif ($self->{next_char} == -1) {
2195            !!!cp (139);
2196            !!!parse-error (type => 'unclosed comment');
2197            $self->{state} = DATA_STATE;
2198            ## reconsume
2199    
2200            !!!emit ($self->{current_token}); # comment
2201    
2202            redo A;
2203          } else {
2204            !!!cp (140);
2205            $self->{current_token}->{data} # comment
2206                .= chr ($self->{next_char});
2207            $self->{state} = COMMENT_STATE;
2208            !!!next-input-character;
2209            redo A;
2210          }
2211        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2212          if ($self->{next_char} == 0x002D) { # -
2213            !!!cp (141);
2214            $self->{state} = COMMENT_END_STATE;
2215            !!!next-input-character;
2216            redo A;
2217          } elsif ($self->{next_char} == 0x003E) { # >
2218            !!!cp (142);
2219            !!!parse-error (type => 'bogus comment');
2220            $self->{state} = DATA_STATE;
2221          !!!next-input-character;          !!!next-input-character;
2222    
2223            !!!emit ($self->{current_token}); # comment
2224    
2225          redo A;          redo A;
2226        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2227            !!!cp (143);
2228          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2229          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2230          ## reconsume          ## reconsume
2231    
2232          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2233    
2234          redo A;          redo A;
2235        } else {        } else {
2236          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (144);
2237            $self->{current_token}->{data} # comment
2238                .= '-' . chr ($self->{next_char});
2239            $self->{state} = COMMENT_STATE;
2240            !!!next-input-character;
2241            redo A;
2242          }
2243        } elsif ($self->{state} == COMMENT_STATE) {
2244          if ($self->{next_char} == 0x002D) { # -
2245            !!!cp (145);
2246            $self->{state} = COMMENT_END_DASH_STATE;
2247            !!!next-input-character;
2248            redo A;
2249          } elsif ($self->{next_char} == -1) {
2250            !!!cp (146);
2251            !!!parse-error (type => 'unclosed comment');
2252            $self->{state} = DATA_STATE;
2253            ## reconsume
2254    
2255            !!!emit ($self->{current_token}); # comment
2256    
2257            redo A;
2258          } else {
2259            !!!cp (147);
2260            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2261          ## Stay in the state          ## Stay in the state
2262          !!!next-input-character;          !!!next-input-character;
2263          redo A;          redo A;
2264        }        }
2265      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2266        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2267          $self->{state} = 'comment end';          !!!cp (148);
2268            $self->{state} = COMMENT_END_STATE;
2269          !!!next-input-character;          !!!next-input-character;
2270          redo A;          redo A;
2271        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2272            !!!cp (149);
2273          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2274          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2275          ## reconsume          ## reconsume
2276    
2277          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2278    
2279          redo A;          redo A;
2280        } else {        } else {
2281          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2282          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2283            $self->{state} = COMMENT_STATE;
2284          !!!next-input-character;          !!!next-input-character;
2285          redo A;          redo A;
2286        }        }
2287      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2288        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2289          $self->{state} = 'data';          !!!cp (151);
2290            $self->{state} = DATA_STATE;
2291          !!!next-input-character;          !!!next-input-character;
2292    
2293          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2294    
2295          redo A;          redo A;
2296        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2297          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2298            !!!parse-error (type => 'dash in comment',
2299                            line => $self->{line_prev},
2300                            column => $self->{column_prev});
2301          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2302          ## Stay in the state          ## Stay in the state
2303          !!!next-input-character;          !!!next-input-character;
2304          redo A;          redo A;
2305        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2306            !!!cp (153);
2307          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2308          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2309          ## reconsume          ## reconsume
2310    
2311          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2312    
2313          redo A;          redo A;
2314        } else {        } else {
2315          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2316          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2317          $self->{state} = 'comment';                          line => $self->{line_prev},
2318                            column => $self->{column_prev});
2319            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2320            $self->{state} = COMMENT_STATE;
2321          !!!next-input-character;          !!!next-input-character;
2322          redo A;          redo A;
2323        }        }
2324      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2325        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2326            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2327            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2328            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2329            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2330          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2331            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2332          !!!next-input-character;          !!!next-input-character;
2333          redo A;          redo A;
2334        } else {        } else {
2335            !!!cp (156);
2336          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2337          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2338          ## reconsume          ## reconsume
2339          redo A;          redo A;
2340        }        }
2341      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2342        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2343            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2344            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2345            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2346            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2347            !!!cp (157);
2348          ## Stay in the state          ## Stay in the state
2349          !!!next-input-character;          !!!next-input-character;
2350          redo A;          redo A;
2351        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == 0x003E) { # >
2352                 $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) { # >  
2353          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2354          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2355          !!!next-input-character;          !!!next-input-character;
2356    
2357          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2358    
2359          redo A;          redo A;
2360        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2361            !!!cp (159);
2362          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2363          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2364          ## reconsume          ## reconsume
2365    
2366          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2367    
2368          redo A;          redo A;
2369        } else {        } else {
2370          $self->{current_token} = {type => 'DOCTYPE',          !!!cp (160);
2371                            name => chr ($self->{next_input_character}),          $self->{current_token}->{name} = chr $self->{next_char};
2372                            error => 1};          delete $self->{current_token}->{quirks};
2373  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2374          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2375          !!!next-input-character;          !!!next-input-character;
2376          redo A;          redo A;
2377        }        }
2378      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2379        if ($self->{next_input_character} == 0x0009 or # HT  ## ISSUE: Redundant "First," in the spec.
2380            $self->{next_input_character} == 0x000A or # LF        if ($self->{next_char} == 0x0009 or # HT
2381            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000A or # LF
2382            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000B or # VT
2383            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000C or # FF
2384          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE            $self->{next_char} == 0x0020) { # SP
2385          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2386            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2387          !!!next-input-character;          !!!next-input-character;
2388          redo A;          redo A;
2389        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2390          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          !!!cp (162);
2391          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2392          !!!next-input-character;          !!!next-input-character;
2393    
2394          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2395    
2396          redo A;          redo A;
2397        } elsif (0x0061 <= $self->{next_input_character} and        } elsif ($self->{next_char} == -1) {
2398                 $self->{next_input_character} <= 0x007A) { # a..z          !!!cp (163);
2399          $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE          !!!parse-error (type => 'unclosed DOCTYPE');
2400          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{state} = DATA_STATE;
2401            ## reconsume
2402    
2403            $self->{current_token}->{quirks} = 1;
2404            !!!emit ($self->{current_token}); # DOCTYPE
2405    
2406            redo A;
2407          } else {
2408            !!!cp (164);
2409            $self->{current_token}->{name}
2410              .= chr ($self->{next_char}); # DOCTYPE
2411          ## Stay in the state          ## Stay in the state
2412          !!!next-input-character;          !!!next-input-character;
2413          redo A;          redo A;
2414        } elsif ($self->{next_input_character} == -1) {        }
2415        } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2416          if ($self->{next_char} == 0x0009 or # HT
2417              $self->{next_char} == 0x000A or # LF
2418              $self->{next_char} == 0x000B or # VT
2419              $self->{next_char} == 0x000C or # FF
2420              $self->{next_char} == 0x0020) { # SP
2421            !!!cp (165);
2422            ## Stay in the state
2423            !!!next-input-character;
2424            redo A;
2425          } elsif ($self->{next_char} == 0x003E) { # >
2426            !!!cp (166);
2427            $self->{state} = DATA_STATE;
2428            !!!next-input-character;
2429    
2430            !!!emit ($self->{current_token}); # DOCTYPE
2431    
2432            redo A;
2433          } elsif ($self->{next_char} == -1) {
2434            !!!cp (167);
2435          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2436          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2437          ## reconsume          ## reconsume
2438    
2439          !!!emit ($self->{current_token});          $self->{current_token}->{quirks} = 1;
2440          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
2441    
2442          redo A;          redo A;
2443          } elsif ($self->{next_char} == 0x0050 or # P
2444                   $self->{next_char} == 0x0070) { # p
2445            $self->{state} = PUBLIC_STATE;
2446            $self->{state_keyword} = chr $self->{next_char};
2447            !!!next-input-character;
2448            redo A;
2449          } elsif ($self->{next_char} == 0x0053 or # S
2450                   $self->{next_char} == 0x0073) { # s
2451            $self->{state} = SYSTEM_STATE;
2452            $self->{state_keyword} = chr $self->{next_char};
2453            !!!next-input-character;
2454            redo A;
2455        } else {        } else {
2456          $self->{current_token}->{name}          !!!cp (180);
2457            .= chr ($self->{next_input_character}); # DOCTYPE          !!!parse-error (type => 'string after DOCTYPE name');
2458          #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');          $self->{current_token}->{quirks} = 1;
2459    
2460            $self->{state} = BOGUS_DOCTYPE_STATE;
2461            !!!next-input-character;
2462            redo A;
2463          }
2464        } elsif ($self->{state} == PUBLIC_STATE) {
2465          ## ASCII case-insensitive
2466          if ($self->{next_char} == [
2467                undef,
2468                0x0055, # U
2469                0x0042, # B
2470                0x004C, # L
2471                0x0049, # I
2472              ]->[length $self->{state_keyword}] or
2473              $self->{next_char} == [
2474                undef,
2475                0x0075, # u
2476                0x0062, # b
2477                0x006C, # l
2478                0x0069, # i
2479              ]->[length $self->{state_keyword}]) {
2480            !!!cp (175);
2481            ## Stay in the state.
2482            $self->{state_keyword} .= chr $self->{next_char};
2483            !!!next-input-character;
2484            redo A;
2485          } elsif ((length $self->{state_keyword}) == 5 and
2486                   ($self->{next_char} == 0x0043 or # C
2487                    $self->{next_char} == 0x0063)) { # c
2488            !!!cp (168);
2489            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2490            !!!next-input-character;
2491            redo A;
2492          } else {
2493            !!!cp (169);
2494            !!!parse-error (type => 'string after DOCTYPE name',
2495                            line => $self->{line_prev},
2496                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2497            $self->{current_token}->{quirks} = 1;
2498    
2499            $self->{state} = BOGUS_DOCTYPE_STATE;
2500            ## Reconsume.
2501            redo A;
2502          }
2503        } elsif ($self->{state} == SYSTEM_STATE) {
2504          ## ASCII case-insensitive
2505          if ($self->{next_char} == [
2506                undef,
2507                0x0059, # Y
2508                0x0053, # S
2509                0x0054, # T
2510                0x0045, # E
2511              ]->[length $self->{state_keyword}] or
2512              $self->{next_char} == [
2513                undef,
2514                0x0079, # y
2515                0x0073, # s
2516                0x0074, # t
2517                0x0065, # e
2518              ]->[length $self->{state_keyword}]) {
2519            !!!cp (170);
2520            ## Stay in the state.
2521            $self->{state_keyword} .= chr $self->{next_char};
2522            !!!next-input-character;
2523            redo A;
2524          } elsif ((length $self->{state_keyword}) == 5 and
2525                   ($self->{next_char} == 0x004D or # M
2526                    $self->{next_char} == 0x006D)) { # m
2527            !!!cp (171);
2528            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2529            !!!next-input-character;
2530            redo A;
2531          } else {
2532            !!!cp (172);
2533            !!!parse-error (type => 'string after DOCTYPE name',
2534                            line => $self->{line_prev},
2535                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2536            $self->{current_token}->{quirks} = 1;
2537    
2538            $self->{state} = BOGUS_DOCTYPE_STATE;
2539            ## Reconsume.
2540            redo A;
2541          }
2542        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2543          if ({
2544                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2545                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2546              }->{$self->{next_char}}) {
2547            !!!cp (181);
2548          ## Stay in the state          ## Stay in the state
2549          !!!next-input-character;          !!!next-input-character;
2550          redo A;          redo A;
2551          } elsif ($self->{next_char} eq 0x0022) { # "
2552            !!!cp (182);
2553            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2554            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2555            !!!next-input-character;
2556            redo A;
2557          } elsif ($self->{next_char} eq 0x0027) { # '
2558            !!!cp (183);
2559            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2560            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2561            !!!next-input-character;
2562            redo A;
2563          } elsif ($self->{next_char} eq 0x003E) { # >
2564            !!!cp (184);
2565            !!!parse-error (type => 'no PUBLIC literal');
2566    
2567            $self->{state} = DATA_STATE;
2568            !!!next-input-character;
2569    
2570            $self->{current_token}->{quirks} = 1;
2571            !!!emit ($self->{current_token}); # DOCTYPE
2572    
2573            redo A;
2574          } elsif ($self->{next_char} == -1) {
2575            !!!cp (185);
2576            !!!parse-error (type => 'unclosed DOCTYPE');
2577    
2578            $self->{state} = DATA_STATE;
2579            ## reconsume
2580    
2581            $self->{current_token}->{quirks} = 1;
2582            !!!emit ($self->{current_token}); # DOCTYPE
2583    
2584            redo A;
2585          } else {
2586            !!!cp (186);
2587            !!!parse-error (type => 'string after PUBLIC');
2588            $self->{current_token}->{quirks} = 1;
2589    
2590            $self->{state} = BOGUS_DOCTYPE_STATE;
2591            !!!next-input-character;
2592            redo A;
2593        }        }
2594      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2595        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0022) { # "
2596            $self->{next_input_character} == 0x000A or # LF          !!!cp (187);
2597            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2598            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
2599            $self->{next_input_character} == 0x0020) { # SP          redo A;
2600          } elsif ($self->{next_char} == 0x003E) { # >
2601            !!!cp (188);
2602            !!!parse-error (type => 'unclosed PUBLIC literal');
2603    
2604            $self->{state} = DATA_STATE;
2605            !!!next-input-character;
2606    
2607            $self->{current_token}->{quirks} = 1;
2608            !!!emit ($self->{current_token}); # DOCTYPE
2609    
2610            redo A;
2611          } elsif ($self->{next_char} == -1) {
2612            !!!cp (189);
2613            !!!parse-error (type => 'unclosed PUBLIC literal');
2614    
2615            $self->{state} = DATA_STATE;
2616            ## reconsume
2617    
2618            $self->{current_token}->{quirks} = 1;
2619            !!!emit ($self->{current_token}); # DOCTYPE
2620    
2621            redo A;
2622          } else {
2623            !!!cp (190);
2624            $self->{current_token}->{public_identifier} # DOCTYPE
2625                .= chr $self->{next_char};
2626          ## Stay in the state          ## Stay in the state
2627          !!!next-input-character;          !!!next-input-character;
2628          redo A;          redo A;
2629        } elsif ($self->{next_input_character} == 0x003E) { # >        }
2630          $self->{state} = 'data';      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2631          if ($self->{next_char} == 0x0027) { # '
2632            !!!cp (191);
2633            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2634          !!!next-input-character;          !!!next-input-character;
2635            redo A;
2636          } elsif ($self->{next_char} == 0x003E) { # >
2637            !!!cp (192);
2638            !!!parse-error (type => 'unclosed PUBLIC literal');
2639    
2640            $self->{state} = DATA_STATE;
2641            !!!next-input-character;
2642    
2643            $self->{current_token}->{quirks} = 1;
2644          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2645    
2646          redo A;          redo A;
2647        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2648            !!!cp (193);
2649            !!!parse-error (type => 'unclosed PUBLIC literal');
2650    
2651            $self->{state} = DATA_STATE;
2652            ## reconsume
2653    
2654            $self->{current_token}->{quirks} = 1;
2655            !!!emit ($self->{current_token}); # DOCTYPE
2656    
2657            redo A;
2658          } else {
2659            !!!cp (194);
2660            $self->{current_token}->{public_identifier} # DOCTYPE
2661                .= chr $self->{next_char};
2662            ## Stay in the state
2663            !!!next-input-character;
2664            redo A;
2665          }
2666        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2667          if ({
2668                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2669                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2670              }->{$self->{next_char}}) {
2671            !!!cp (195);
2672            ## Stay in the state
2673            !!!next-input-character;
2674            redo A;
2675          } elsif ($self->{next_char} == 0x0022) { # "
2676            !!!cp (196);
2677            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2678            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2679            !!!next-input-character;
2680            redo A;
2681          } elsif ($self->{next_char} == 0x0027) { # '
2682            !!!cp (197);
2683            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2684            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2685            !!!next-input-character;
2686            redo A;
2687          } elsif ($self->{next_char} == 0x003E) { # >
2688            !!!cp (198);
2689            $self->{state} = DATA_STATE;
2690            !!!next-input-character;
2691    
2692            !!!emit ($self->{current_token}); # DOCTYPE
2693    
2694            redo A;
2695          } elsif ($self->{next_char} == -1) {
2696            !!!cp (199);
2697          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2698          $self->{state} = 'data';  
2699            $self->{state} = DATA_STATE;
2700          ## reconsume          ## reconsume
2701    
2702            $self->{current_token}->{quirks} = 1;
2703          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2704    
2705          redo A;          redo A;
2706        } else {        } else {
2707          !!!parse-error (type => 'string after DOCTYPE name');          !!!cp (200);
2708          $self->{current_token}->{error} = 1; # DOCTYPE          !!!parse-error (type => 'string after PUBLIC literal');
2709          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2710    
2711            $self->{state} = BOGUS_DOCTYPE_STATE;
2712            !!!next-input-character;
2713            redo A;
2714          }
2715        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2716          if ({
2717                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2718                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2719              }->{$self->{next_char}}) {
2720            !!!cp (201);
2721            ## Stay in the state
2722            !!!next-input-character;
2723            redo A;
2724          } elsif ($self->{next_char} == 0x0022) { # "
2725            !!!cp (202);
2726            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2727            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2728            !!!next-input-character;
2729            redo A;
2730          } elsif ($self->{next_char} == 0x0027) { # '
2731            !!!cp (203);
2732            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2733            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2734            !!!next-input-character;
2735            redo A;
2736          } elsif ($self->{next_char} == 0x003E) { # >
2737            !!!cp (204);
2738            !!!parse-error (type => 'no SYSTEM literal');
2739            $self->{state} = DATA_STATE;
2740            !!!next-input-character;
2741    
2742            $self->{current_token}->{quirks} = 1;
2743            !!!emit ($self->{current_token}); # DOCTYPE
2744    
2745            redo A;
2746          } elsif ($self->{next_char} == -1) {
2747            !!!cp (205);
2748            !!!parse-error (type => 'unclosed DOCTYPE');
2749    
2750            $self->{state} = DATA_STATE;
2751            ## reconsume
2752    
2753            $self->{current_token}->{quirks} = 1;
2754            !!!emit ($self->{current_token}); # DOCTYPE
2755    
2756            redo A;
2757          } else {
2758            !!!cp (206);
2759            !!!parse-error (type => 'string after SYSTEM');
2760            $self->{current_token}->{quirks} = 1;
2761    
2762            $self->{state} = BOGUS_DOCTYPE_STATE;
2763            !!!next-input-character;
2764            redo A;
2765          }
2766        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2767          if ($self->{next_char} == 0x0022) { # "
2768            !!!cp (207);
2769            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2770            !!!next-input-character;
2771            redo A;
2772          } elsif ($self->{next_char} == 0x003E) { # >
2773            !!!cp (208);
2774            !!!parse-error (type => 'unclosed SYSTEM literal');
2775    
2776            $self->{state} = DATA_STATE;
2777            !!!next-input-character;
2778    
2779            $self->{current_token}->{quirks} = 1;
2780            !!!emit ($self->{current_token}); # DOCTYPE
2781    
2782            redo A;
2783          } elsif ($self->{next_char} == -1) {
2784            !!!cp (209);
2785            !!!parse-error (type => 'unclosed SYSTEM literal');
2786    
2787            $self->{state} = DATA_STATE;
2788            ## reconsume
2789    
2790            $self->{current_token}->{quirks} = 1;
2791            !!!emit ($self->{current_token}); # DOCTYPE
2792    
2793            redo A;
2794          } else {
2795            !!!cp (210);
2796            $self->{current_token}->{system_identifier} # DOCTYPE
2797                .= chr $self->{next_char};
2798            ## Stay in the state
2799          !!!next-input-character;          !!!next-input-character;
2800          redo A;          redo A;
2801        }        }
2802      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2803        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x0027) { # '
2804          $self->{state} = 'data';          !!!cp (211);
2805            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2806            !!!next-input-character;
2807            redo A;
2808          } elsif ($self->{next_char} == 0x003E) { # >
2809            !!!cp (212);
2810            !!!parse-error (type => 'unclosed SYSTEM literal');
2811    
2812            $self->{state} = DATA_STATE;
2813          !!!next-input-character;          !!!next-input-character;
2814    
2815            $self->{current_token}->{quirks} = 1;
2816            !!!emit ($self->{current_token}); # DOCTYPE
2817    
2818            redo A;
2819          } elsif ($self->{next_char} == -1) {
2820            !!!cp (213);
2821            !!!parse-error (type => 'unclosed SYSTEM literal');
2822    
2823            $self->{state} = DATA_STATE;
2824            ## reconsume
2825    
2826            $self->{current_token}->{quirks} = 1;
2827          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2828    
2829          redo A;          redo A;
2830        } elsif ($self->{next_input_character} == -1) {        } else {
2831            !!!cp (214);
2832            $self->{current_token}->{system_identifier} # DOCTYPE
2833                .= chr $self->{next_char};
2834            ## Stay in the state
2835            !!!next-input-character;
2836            redo A;
2837          }
2838        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2839          if ({
2840                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2841                #0x000D => 1, # HT, LF, VT, FF, SP, CR
2842              }->{$self->{next_char}}) {
2843            !!!cp (215);
2844            ## Stay in the state
2845            !!!next-input-character;
2846            redo A;
2847          } elsif ($self->{next_char} == 0x003E) { # >
2848            !!!cp (216);
2849            $self->{state} = DATA_STATE;
2850            !!!next-input-character;
2851    
2852            !!!emit ($self->{current_token}); # DOCTYPE
2853    
2854            redo A;
2855          } elsif ($self->{next_char} == -1) {
2856            !!!cp (217);
2857          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2858          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2859          ## reconsume          ## reconsume
2860    
2861            $self->{current_token}->{quirks} = 1;
2862          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2863    
2864          redo A;          redo A;
2865        } else {        } else {
2866            !!!cp (218);
2867            !!!parse-error (type => 'string after SYSTEM literal');
2868            #$self->{current_token}->{quirks} = 1;
2869    
2870            $self->{state} = BOGUS_DOCTYPE_STATE;
2871            !!!next-input-character;
2872            redo A;
2873          }
2874        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2875          if ($self->{next_char} == 0x003E) { # >
2876            !!!cp (219);
2877            $self->{state} = DATA_STATE;
2878            !!!next-input-character;
2879    
2880            !!!emit ($self->{current_token}); # DOCTYPE
2881    
2882            redo A;
2883          } elsif ($self->{next_char} == -1) {
2884            !!!cp (220);
2885            !!!parse-error (type => 'unclosed DOCTYPE');
2886            $self->{state} = DATA_STATE;
2887            ## reconsume
2888    
2889            !!!emit ($self->{current_token}); # DOCTYPE
2890    
2891            redo A;
2892          } else {
2893            !!!cp (221);
2894          ## Stay in the state          ## Stay in the state
2895          !!!next-input-character;          !!!next-input-character;
2896          redo A;          redo A;
2897        }        }
2898        } elsif ($self->{state} == CDATA_SECTION_STATE) {
2899          ## NOTE: "CDATA section state" in the state is jointly implemented
2900          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2901          ## and |CDATA_SECTION_MSE2_STATE|.
2902          
2903          if ($self->{next_char} == 0x005D) { # ]
2904            !!!cp (221.1);
2905            $self->{state} = CDATA_SECTION_MSE1_STATE;
2906            !!!next-input-character;
2907            redo A;
2908          } elsif ($self->{next_char} == -1) {
2909            $self->{state} = DATA_STATE;
2910            !!!next-input-character;
2911            if (length $self->{current_token}->{data}) { # character
2912              !!!cp (221.2);
2913              !!!emit ($self->{current_token}); # character
2914            } else {
2915              !!!cp (221.3);
2916              ## No token to emit. $self->{current_token} is discarded.
2917            }        
2918            redo A;
2919          } else {
2920            !!!cp (221.4);
2921            $self->{current_token}->{data} .= chr $self->{next_char};
2922            ## Stay in the state.
2923            !!!next-input-character;
2924            redo A;
2925          }
2926    
2927          ## ISSUE: "text tokens" in spec.
2928        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2929          if ($self->{next_char} == 0x005D) { # ]
2930            !!!cp (221.5);
2931            $self->{state} = CDATA_SECTION_MSE2_STATE;
2932            !!!next-input-character;
2933            redo A;
2934          } else {
2935            !!!cp (221.6);
2936            $self->{current_token}->{data} .= ']';
2937            $self->{state} = CDATA_SECTION_STATE;
2938            ## Reconsume.
2939            redo A;
2940          }
2941        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2942          if ($self->{next_char} == 0x003E) { # >
2943            $self->{state} = DATA_STATE;
2944            !!!next-input-character;
2945            if (length $self->{current_token}->{data}) { # character
2946              !!!cp (221.7);
2947              !!!emit ($self->{current_token}); # character
2948            } else {
2949              !!!cp (221.8);
2950              ## No token to emit. $self->{current_token} is discarded.
2951            }
2952            redo A;
2953          } elsif ($self->{next_char} == 0x005D) { # ]
2954            !!!cp (221.9); # character
2955            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2956            ## Stay in the state.
2957            !!!next-input-character;
2958            redo A;
2959          } else {
2960            !!!cp (221.11);
2961            $self->{current_token}->{data} .= ']]'; # character
2962            $self->{state} = CDATA_SECTION_STATE;
2963            ## Reconsume.
2964            redo A;
2965          }
2966      } else {      } else {
2967        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2968      }      }
# Line 1490  sub _get_next_token ($) { Line 2971  sub _get_next_token ($) {
2971    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2972  } # _get_next_token  } # _get_next_token
2973    
2974  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2975    my $self = shift;    my ($self, $in_attr, $additional) = @_;
2976      
2977    if ($self->{next_input_character} == 0x0023) { # #    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2978    
2979      if ({
2980           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2981           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2982           $additional => 1,
2983          }->{$self->{next_char}}) {
2984        !!!cp (1001);
2985        ## Don't consume
2986        ## No error
2987        return undef;
2988      } elsif ($self->{next_char} == 0x0023) { # #
2989      !!!next-input-character;      !!!next-input-character;
2990      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2991          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2992        my $num;        my $code;
2993        X: {        X: {
2994          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2995          !!!next-input-character;          !!!next-input-character;
2996          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2997              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2998            $num ||= 0;            !!!cp (1002);
2999            $num *= 0x10;            $code ||= 0;
3000            $num += $self->{next_input_character} - 0x0030;            $code *= 0x10;
3001              $code += $self->{next_char} - 0x0030;
3002            redo X;            redo X;
3003          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
3004                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
3005            ## ISSUE: the spec says U+0078, which is apparently incorrect            !!!cp (1003);
3006            $num ||= 0;            $code ||= 0;
3007            $num *= 0x10;            $code *= 0x10;
3008            $num += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
3009            redo X;            redo X;
3010          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
3011                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
3012            ## ISSUE: the spec says U+0058, which is apparently incorrect            !!!cp (1004);
3013            $num ||= 0;            $code ||= 0;
3014            $num *= 0x10;            $code *= 0x10;
3015            $num += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
3016            redo X;            redo X;
3017          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
3018            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
3019            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
3020            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
3021              $self->{next_char} = 0x0023; # #
3022            return undef;            return undef;
3023          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
3024              !!!cp (1006);
3025            !!!next-input-character;            !!!next-input-character;
3026          } else {          } else {
3027            !!!parse-error (type => 'no refc');            !!!cp (1007);
3028          }            !!!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) {  
           ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>  
           ## ISSUE: Not in the spec yet; parse error?  
           $num = $c1_entity_char->{$num};  
3029          }          }
3030    
3031          return {type => 'character', data => chr $num};          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3032              !!!cp (1008);
3033              !!!parse-error (type => 'invalid character reference',
3034                              text => (sprintf 'U+%04X', $code),
3035                              line => $l, column => $c);
3036              $code = 0xFFFD;
3037            } elsif ($code > 0x10FFFF) {
3038              !!!cp (1009);
3039              !!!parse-error (type => 'invalid character reference',
3040                              text => (sprintf 'U-%08X', $code),
3041                              line => $l, column => $c);
3042              $code = 0xFFFD;
3043            } elsif ($code == 0x000D) {
3044              !!!cp (1010);
3045              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3046              $code = 0x000A;
3047            } elsif (0x80 <= $code and $code <= 0x9F) {
3048              !!!cp (1011);
3049              !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3050              $code = $c1_entity_char->{$code};
3051            }
3052    
3053            return {type => CHARACTER_TOKEN, data => chr $code,
3054                    has_reference => 1,
3055                    line => $l, column => $c,
3056                   };
3057        } # X        } # X
3058      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
3059               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
3060        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
3061        !!!next-input-character;        !!!next-input-character;
3062                
3063        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
3064                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
3065            !!!cp (1012);
3066          $code *= 10;          $code *= 10;
3067          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
3068                    
3069          !!!next-input-character;          !!!next-input-character;
3070        }        }
3071    
3072        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
3073            !!!cp (1013);
3074          !!!next-input-character;          !!!next-input-character;
3075        } else {        } else {
3076          !!!parse-error (type => 'no refc');          !!!cp (1014);
3077            !!!parse-error (type => 'no refc', line => $l, column => $c);
3078        }        }
3079    
3080        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3081        if ($code > 1114111 or $code == 0) {          !!!cp (1015);
3082          $code = 0xFFFD; # REPLACEMENT CHARACTER          !!!parse-error (type => 'invalid character reference',
3083          ## ISSUE: Why this is not an error?                          text => (sprintf 'U+%04X', $code),
3084                            line => $l, column => $c);
3085            $code = 0xFFFD;
3086          } elsif ($code > 0x10FFFF) {
3087            !!!cp (1016);
3088            !!!parse-error (type => 'invalid character reference',
3089                            text => (sprintf 'U-%08X', $code),
3090                            line => $l, column => $c);
3091            $code = 0xFFFD;
3092          } elsif ($code == 0x000D) {
3093            !!!cp (1017);
3094            !!!parse-error (type => 'CR character reference',
3095                            line => $l, column => $c);
3096            $code = 0x000A;
3097        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3098          ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>          !!!cp (1018);
3099          ## ISSUE: Not in the spec yet; parse error?          !!!parse-error (type => 'C1 character reference',
3100                            text => (sprintf 'U+%04X', $code),
3101                            line => $l, column => $c);
3102          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3103        }        }
3104                
3105        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3106                  line => $l, column => $c,
3107                 };
3108      } else {      } else {
3109        !!!parse-error (type => 'bare nero');        !!!cp (1019);
3110        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3111        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
3112          $self->{next_char} = 0x0023; # #
3113        return undef;        return undef;
3114      }      }
3115    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
3116              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
3117             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
3118              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
3119      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
3120      !!!next-input-character;      !!!next-input-character;
3121    
3122      my $value = $entity_name;      my $value = $entity_name;
3123      my $match;      my $match = 0;
3124        require Whatpm::_NamedEntityList;
3125        our $EntityChar;
3126    
3127      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3128             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3129             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_char} and # a
3130               $self->{next_input_character} <= 0x005A) or               $self->{next_char} <= 0x005A) or # x
3131              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_char} and # a
3132               $self->{next_input_character} <= 0x007A) or               $self->{next_char} <= 0x007A) or # z
3133              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_char} and # 0
3134               $self->{next_input_character} <= 0x0039))) {               $self->{next_char} <= 0x0039) or # 9
3135        $entity_name .= chr $self->{next_input_character};              $self->{next_char} == 0x003B)) { # ;
3136        if (defined $entity_char->{$entity_name}) {        $entity_name .= chr $self->{next_char};
3137          $value = $entity_char->{$entity_name};        if (defined $EntityChar->{$entity_name}) {
3138          $match = 1;          if ($self->{next_char} == 0x003B) { # ;
3139              !!!cp (1020);
3140              $value = $EntityChar->{$entity_name};
3141              $match = 1;
3142              !!!next-input-character;
3143              last;
3144            } else {
3145              !!!cp (1021);
3146              $value = $EntityChar->{$entity_name};
3147              $match = -1;
3148              !!!next-input-character;
3149            }
3150        } else {        } else {
3151          $value .= chr $self->{next_input_character};          !!!cp (1022);
3152            $value .= chr $self->{next_char};
3153            $match *= 2;
3154            !!!next-input-character;
3155        }        }
       !!!next-input-character;  
3156      }      }
3157            
3158      if ($match) {      if ($match > 0) {
3159        if ($self->{next_input_character} == 0x003B) { # ;        !!!cp (1023);
3160          !!!next-input-character;        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3161                  line => $l, column => $c,
3162                 };
3163        } elsif ($match < 0) {
3164          !!!parse-error (type => 'no refc', line => $l, column => $c);
3165          if ($in_attr and $match < -1) {
3166            !!!cp (1024);
3167            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3168                    line => $l, column => $c,
3169                   };
3170        } else {        } else {
3171          !!!parse-error (type => 'refc');          !!!cp (1025);
3172            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3173                    line => $l, column => $c,
3174                   };
3175        }        }
   
       return {type => 'character', data => $value};  
3176      } else {      } else {
3177        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3178        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3179        !!!back-token ({type => 'character', data => $value});        ## NOTE: "No characters are consumed" in the spec.
3180        return undef;        return {type => CHARACTER_TOKEN, data => '&'.$value,
3181                  line => $l, column => $c,
3182                 };
3183      }      }
3184    } else {    } else {
3185        !!!cp (1027);
3186      ## no characters are consumed      ## no characters are consumed
3187      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3188      return undef;      return undef;
3189    }    }
3190  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1636  sub _initialize_tree_constructor ($) { Line 3195  sub _initialize_tree_constructor ($) {
3195    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
3196    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3197    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3198    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
3199      $self->{document}->set_user_data (manakai_source_line => 1);
3200      $self->{document}->set_user_data (manakai_source_column => 1);
3201  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3202    
3203  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1663  sub _construct_tree ($) { Line 3224  sub _construct_tree ($) {
3224        
3225    !!!next-token;    !!!next-token;
3226    
   $self->{insertion_mode} = 'before head';  
3227    undef $self->{form_element};    undef $self->{form_element};
3228    undef $self->{head_element};    undef $self->{head_element};
3229    $self->{open_elements} = [];    $self->{open_elements} = [];
3230    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3231    
3232      ## NOTE: The "initial" insertion mode.
3233    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3234    
3235      ## NOTE: The "before html" insertion mode.
3236    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3237      $self->{insertion_mode} = BEFORE_HEAD_IM;
3238    
3239      ## NOTE: The "before head" insertion mode and so on.
3240    $self->_tree_construction_main;    $self->_tree_construction_main;
3241  } # _construct_tree  } # _construct_tree
3242    
3243  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3244    my $self = shift;    my $self = shift;
3245    B: {  
3246        if ($token->{type} eq 'DOCTYPE') {    ## NOTE: "initial" insertion mode
3247          if ($token->{error}) {  
3248            ## ISSUE: Spec currently left this case undefined.    INITIAL: {
3249            !!!parse-error (type => 'bogus DOCTYPE');      if ($token->{type} == DOCTYPE_TOKEN) {
3250          }        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3251          my $doctype = $self->{document}->create_document_type_definition        ## error, switch to a conformance checking mode for another
3252            ($token->{name});        ## language.
3253          $self->{document}->append_child ($doctype);        my $doctype_name = $token->{name};
3254          #$phase = 'root element';        $doctype_name = '' unless defined $doctype_name;
3255          !!!next-token;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3256          #redo B;        if (not defined $token->{name} or # <!DOCTYPE>
3257          return;            defined $token->{system_identifier}) {
3258        } elsif ({          !!!cp ('t1');
3259                  comment => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3260                  'start tag' => 1,        } elsif ($doctype_name ne 'HTML') {
3261                  'end tag' => 1,          !!!cp ('t2');
3262                  'end-of-file' => 1,          !!!parse-error (type => 'not HTML5', token => $token);
3263                 }->{$token->{type}}) {        } elsif (defined $token->{public_identifier}) {
3264          ## ISSUE: Spec currently left this case undefined.          if ($token->{public_identifier} eq 'XSLT-compat') {
3265          !!!parse-error (type => 'missing DOCTYPE');            !!!cp ('t1.2');
3266          #$phase = 'root element';            !!!parse-error (type => 'XSLT-compat', token => $token,
3267          ## reprocess                            level => $self->{level}->{should});
3268          #redo B;          } else {
3269          return;            !!!parse-error (type => 'not HTML5', token => $token);
3270        } elsif ($token->{type} eq 'character') {          }
3271          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } else {
3272            $self->{document}->manakai_append_text ($1);          !!!cp ('t3');
3273            ## ISSUE: DOM3 Core does not allow Document > Text          #
3274            unless (length $token->{data}) {        }
3275              ## Stay in the phase        
3276              !!!next-token;        my $doctype = $self->{document}->create_document_type_definition
3277              redo B;          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3278          ## NOTE: Default value for both |public_id| and |system_id| attributes
3279          ## are empty strings, so that we don't set any value in missing cases.
3280          $doctype->public_id ($token->{public_identifier})
3281              if defined $token->{public_identifier};
3282          $doctype->system_id ($token->{system_identifier})
3283              if defined $token->{system_identifier};
3284          ## NOTE: Other DocumentType attributes are null or empty lists.
3285          ## ISSUE: internalSubset = null??
3286          $self->{document}->append_child ($doctype);
3287          
3288          if ($token->{quirks} or $doctype_name ne 'HTML') {
3289            !!!cp ('t4');
3290            $self->{document}->manakai_compat_mode ('quirks');
3291          } elsif (defined $token->{public_identifier}) {
3292            my $pubid = $token->{public_identifier};
3293            $pubid =~ tr/a-z/A-z/;
3294            my $prefix = [
3295              "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3296              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3297              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3298              "-//IETF//DTD HTML 2.0 LEVEL 1//",
3299              "-//IETF//DTD HTML 2.0 LEVEL 2//",
3300              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3301              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3302              "-//IETF//DTD HTML 2.0 STRICT//",
3303              "-//IETF//DTD HTML 2.0//",
3304              "-//IETF//DTD HTML 2.1E//",
3305              "-//IETF//DTD HTML 3.0//",
3306              "-//IETF//DTD HTML 3.2 FINAL//",
3307              "-//IETF//DTD HTML 3.2//",
3308              "-//IETF//DTD HTML 3//",
3309              "-//IETF//DTD HTML LEVEL 0//",
3310              "-//IETF//DTD HTML LEVEL 1//",
3311              "-//IETF//DTD HTML LEVEL 2//",
3312              "-//IETF//DTD HTML LEVEL 3//",
3313              "-//IETF//DTD HTML STRICT LEVEL 0//",
3314              "-//IETF//DTD HTML STRICT LEVEL 1//",
3315              "-//IETF//DTD HTML STRICT LEVEL 2//",
3316              "-//IETF//DTD HTML STRICT LEVEL 3//",
3317              "-//IETF//DTD HTML STRICT//",
3318              "-//IETF//DTD HTML//",
3319              "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3320              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3321              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3322              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3323              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3324              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3325              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3326              "-//NETSCAPE COMM. CORP.//DTD HTML//",
3327              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3328              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3329              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3330              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3331              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3332              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3333              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3334              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3335              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3336              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3337              "-//W3C//DTD HTML 3 1995-03-24//",
3338              "-//W3C//DTD HTML 3.2 DRAFT//",
3339              "-//W3C//DTD HTML 3.2 FINAL//",
3340              "-//W3C//DTD HTML 3.2//",
3341              "-//W3C//DTD HTML 3.2S DRAFT//",
3342              "-//W3C//DTD HTML 4.0 FRAMESET//",
3343              "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3344              "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3345              "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3346              "-//W3C//DTD W3 HTML//",
3347              "-//W3O//DTD W3 HTML 3.0//",
3348              "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3349              "-//WEBTECHS//DTD MOZILLA HTML//",
3350            ]; # $prefix
3351            my $match;
3352            for (@$prefix) {
3353              if (substr ($prefix, 0, length $_) eq $_) {
3354                $match = 1;
3355                last;
3356              }
3357            }
3358            if ($match or
3359                $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3360                $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3361                $pubid eq "HTML") {
3362              !!!cp ('t5');
3363              $self->{document}->manakai_compat_mode ('quirks');
3364            } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3365                     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3366              if (defined $token->{system_identifier}) {
3367                !!!cp ('t6');
3368                $self->{document}->manakai_compat_mode ('quirks');
3369              } else {
3370                !!!cp ('t7');
3371                $self->{document}->manakai_compat_mode ('limited quirks');
3372            }            }
3373            } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3374                     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3375              !!!cp ('t8');
3376              $self->{document}->manakai_compat_mode ('limited quirks');
3377            } else {
3378              !!!cp ('t9');
3379            }
3380          } else {
3381            !!!cp ('t10');
3382          }
3383          if (defined $token->{system_identifier}) {
3384            my $sysid = $token->{system_identifier};
3385            $sysid =~ tr/A-Z/a-z/;
3386            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3387              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3388              ## marked as quirks.
3389              $self->{document}->manakai_compat_mode ('quirks');
3390              !!!cp ('t11');
3391            } else {
3392              !!!cp ('t12');
3393          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
3394        } else {        } else {
3395          die "$0: $token->{type}: Unknown token";          !!!cp ('t13');
3396        }        }
3397      } # B        
3398          ## Go to the "before html" insertion mode.
3399          !!!next-token;
3400          return;
3401        } elsif ({
3402                  START_TAG_TOKEN, 1,
3403                  END_TAG_TOKEN, 1,
3404                  END_OF_FILE_TOKEN, 1,
3405                 }->{$token->{type}}) {
3406          !!!cp ('t14');
3407          !!!parse-error (type => 'no DOCTYPE', token => $token);
3408          $self->{document}->manakai_compat_mode ('quirks');
3409          ## Go to the "before html" insertion mode.
3410          ## reprocess
3411          !!!ack-later;
3412          return;
3413        } elsif ($token->{type} == CHARACTER_TOKEN) {
3414          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3415            ## Ignore the token
3416    
3417            unless (length $token->{data}) {
3418              !!!cp ('t15');
3419              ## Stay in the insertion mode.
3420              !!!next-token;
3421              redo INITIAL;
3422            } else {
3423              !!!cp ('t16');
3424            }
3425          } else {
3426            !!!cp ('t17');
3427          }
3428    
3429          !!!parse-error (type => 'no DOCTYPE', token => $token);
3430          $self->{document}->manakai_compat_mode ('quirks');
3431          ## Go to the "before html" insertion mode.
3432          ## reprocess
3433          return;
3434        } elsif ($token->{type} == COMMENT_TOKEN) {
3435          !!!cp ('t18');
3436          my $comment = $self->{document}->create_comment ($token->{data});
3437          $self->{document}->append_child ($comment);
3438          
3439          ## Stay in the insertion mode.
3440          !!!next-token;
3441          redo INITIAL;
3442        } else {
3443          die "$0: $token->{type}: Unknown token type";
3444        }
3445      } # INITIAL
3446    
3447      die "$0: _tree_construction_initial: This should be never reached";
3448  } # _tree_construction_initial  } # _tree_construction_initial
3449    
3450  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3451    my $self = shift;    my $self = shift;
3452    
3453      ## NOTE: "before html" insertion mode.
3454        
3455    B: {    B: {
3456        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3457          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3458            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3459          ## Ignore the token          ## Ignore the token
3460          ## Stay in the phase          ## Stay in the insertion mode.
3461          !!!next-token;          !!!next-token;
3462          redo B;          redo B;
3463        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3464            !!!cp ('t20');
3465          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3466          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3467          ## Stay in the phase          ## Stay in the insertion mode.
3468          !!!next-token;          !!!next-token;
3469          redo B;          redo B;
3470        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3471          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3472            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3473            ## ISSUE: DOM3 Core does not allow Document > Text  
3474            unless (length $token->{data}) {            unless (length $token->{data}) {
3475              ## Stay in the phase              !!!cp ('t21');
3476                ## Stay in the insertion mode.
3477              !!!next-token;              !!!next-token;
3478              redo B;              redo B;
3479              } else {
3480                !!!cp ('t22');
3481            }            }
3482            } else {
3483              !!!cp ('t23');
3484          }          }
3485    
3486            $self->{application_cache_selection}->(undef);
3487    
3488          #          #
3489          } elsif ($token->{type} == START_TAG_TOKEN) {
3490            if ($token->{tag_name} eq 'html') {
3491              my $root_element;
3492              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3493              $self->{document}->append_child ($root_element);
3494              push @{$self->{open_elements}},
3495                  [$root_element, $el_category->{html}];
3496    
3497              if ($token->{attributes}->{manifest}) {
3498                !!!cp ('t24');
3499                $self->{application_cache_selection}
3500                    ->($token->{attributes}->{manifest}->{value});
3501                ## ISSUE: Spec is unclear on relative references.
3502                ## According to Hixie (#whatwg 2008-03-19), it should be
3503                ## resolved against the base URI of the document in HTML
3504                ## or xml:base of the element in XHTML.
3505              } else {
3506                !!!cp ('t25');
3507                $self->{application_cache_selection}->(undef);
3508              }
3509    
3510              !!!nack ('t25c');
3511    
3512              !!!next-token;
3513              return; ## Go to the "before head" insertion mode.
3514            } else {
3515              !!!cp ('t25.1');
3516              #
3517            }
3518        } elsif ({        } elsif ({
3519                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3520                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3521                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3522          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3523          #          #
3524        } else {        } else {
3525          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3526        }        }
3527        my $root_element; !!!create-element ($root_element, 'html');  
3528        $self->{document}->append_child ($root_element);      my $root_element;
3529        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3530        #$phase = 'main';      $self->{document}->append_child ($root_element);
3531        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3532        #redo B;  
3533        return;      $self->{application_cache_selection}->(undef);
3534    
3535        ## NOTE: Reprocess the token.
3536        !!!ack-later;
3537        return; ## Go to the "before head" insertion mode.
3538    
3539        ## ISSUE: There is an issue in the spec
3540    } # B    } # B
3541    
3542      die "$0: _tree_construction_root_element: This should never be reached";
3543  } # _tree_construction_root_element  } # _tree_construction_root_element
3544    
3545  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1782  sub _reset_insertion_mode ($) { Line 3554  sub _reset_insertion_mode ($) {
3554            
3555      ## Step 3      ## Step 3
3556      S3: {      S3: {
3557        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3558        if (defined $self->{inner_html_node}) {          $last = 1;
3559          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3560              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3561              $node = $self->{inner_html_node};
3562            } else {
3563              die "_reset_insertion_mode: t27";
3564            }
3565          }
3566          
3567          ## Step 4..14
3568          my $new_mode;
3569          if ($node->[1] & FOREIGN_EL) {
3570            !!!cp ('t28.1');
3571            ## NOTE: Strictly spaking, the line below only applies to MathML and
3572            ## SVG elements.  Currently the HTML syntax supports only MathML and
3573            ## SVG elements as foreigners.
3574            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3575          } elsif ($node->[1] & TABLE_CELL_EL) {
3576            if ($last) {
3577              !!!cp ('t28.2');
3578            #            #
3579          } else {          } else {
3580            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3581              $new_mode = IN_CELL_IM;
3582          }          }
3583          } else {
3584            !!!cp ('t28.4');
3585            $new_mode = {
3586                          select => IN_SELECT_IM,
3587                          ## NOTE: |option| and |optgroup| do not set
3588                          ## insertion mode to "in select" by themselves.
3589                          tr => IN_ROW_IM,
3590                          tbody => IN_TABLE_BODY_IM,
3591                          thead => IN_TABLE_BODY_IM,
3592                          tfoot => IN_TABLE_BODY_IM,
3593                          caption => IN_CAPTION_IM,
3594                          colgroup => IN_COLUMN_GROUP_IM,
3595                          table => IN_TABLE_IM,
3596                          head => IN_BODY_IM, # not in head!
3597                          body => IN_BODY_IM,
3598                          frameset => IN_FRAMESET_IM,
3599                         }->{$node->[0]->manakai_local_name};
3600        }        }
       
       ## 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]};  
3601        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3602                
3603        ## Step 14        ## Step 15
3604        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3605          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3606            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3607              $self->{insertion_mode} = BEFORE_HEAD_IM;
3608          } else {          } else {
3609            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3610              !!!cp ('t30');
3611              $self->{insertion_mode} = AFTER_HEAD_IM;
3612          }          }
3613          return;          return;
3614          } else {
3615            !!!cp ('t31');
3616        }        }
3617                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3618        ## Step 16        ## Step 16
3619          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3620          
3621          ## Step 17
3622        $i--;        $i--;
3623        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3624                
3625        ## Step 17        ## Step 18
3626        redo S3;        redo S3;
3627      } # S3      } # S3
3628    
3629      die "$0: _reset_insertion_mode: This line should never be reached";
3630  } # _reset_insertion_mode  } # _reset_insertion_mode
3631    
3632  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3633    my $self = shift;    my $self = shift;
3634    
   my $phase = 'main';  
   
3635    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3636    
3637    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1853  sub _tree_construction_main ($) { Line 3648  sub _tree_construction_main ($) {
3648      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3649      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3650        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3651            !!!cp ('t32');
3652          return;          return;
3653        }        }
3654      }      }
# Line 1867  sub _tree_construction_main ($) { Line 3663  sub _tree_construction_main ($) {
3663    
3664        ## Step 6        ## Step 6
3665        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3666            !!!cp ('t33_1');
3667          #          #
3668        } else {        } else {
3669          my $in_open_elements;          my $in_open_elements;
3670          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3671            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3672                !!!cp ('t33');
3673              $in_open_elements = 1;              $in_open_elements = 1;
3674              last OE;              last OE;
3675            }            }
3676          }          }
3677          if ($in_open_elements) {          if ($in_open_elements) {
3678              !!!cp ('t34');
3679            #            #
3680          } else {          } else {
3681              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3682              !!!cp ('t35');
3683            redo S4;            redo S4;
3684          }          }
3685        }        }
# Line 1901  sub _tree_construction_main ($) { Line 3702  sub _tree_construction_main ($) {
3702    
3703        ## Step 11        ## Step 11
3704        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3705            !!!cp ('t36');
3706          ## Step 7'          ## Step 7'
3707          $i++;          $i++;
3708          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3709                    
3710          redo S7;          redo S7;
3711        }        }
3712    
3713          !!!cp ('t37');
3714      } # S7      } # S7
3715    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3716    
3717    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3718      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3719        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3720            !!!cp ('t38');
3721          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3722          return;          return;
3723        }        }
3724      }      }
3725    
3726        !!!cp ('t39');
3727    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3728    
3729    my $style_start_tag = sub {    my $insert;
3730      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
3731      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3732      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3733       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3734        ->append_child ($style_el);      ## Step 1
3735      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3736                      my $el;
3737        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3738    
3739        ## Step 2
3740        $insert->($el);
3741    
3742        ## Step 3
3743        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3744        delete $self->{escape}; # MUST
3745    
3746        ## Step 4
3747      my $text = '';      my $text = '';
3748        !!!nack ('t40.1');
3749      !!!next-token;      !!!next-token;
3750      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3751          !!!cp ('t40');
3752        $text .= $token->{data};        $text .= $token->{data};
3753        !!!next-token;        !!!next-token;
3754      } # stop if non-character token or tokenizer stops tokenising      }
3755    
3756        ## Step 5
3757      if (length $text) {      if (length $text) {
3758        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3759          my $text = $self->{document}->create_text_node ($text);
3760          $el->append_child ($text);
3761      }      }
3762        
3763      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3764                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3765      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
3766        ## Step 7
3767        if ($token->{type} == END_TAG_TOKEN and
3768            $token->{tag_name} eq $start_tag_name) {
3769          !!!cp ('t42');
3770        ## Ignore the token        ## Ignore the token
3771      } else {      } else {
3772        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
3773        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3774            !!!cp ('t43');
3775            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3776          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3777            !!!cp ('t44');
3778            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3779          } else {
3780            die "$0: $content_model_flag in parse_rcdata";
3781          }
3782      }      }
3783      !!!next-token;      !!!next-token;
3784    }; # $style_start_tag    }; # $parse_rcdata
3785    
3786    my $script_start_tag = sub {    my $script_start_tag = sub () {
3787      my $script_el;      my $script_el;
3788      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3789      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3790    
3791      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3792        delete $self->{escape}; # MUST
3793            
3794      my $text = '';      my $text = '';
3795        !!!nack ('t45.1');
3796      !!!next-token;      !!!next-token;
3797      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3798          !!!cp ('t45');
3799        $text .= $token->{data};        $text .= $token->{data};
3800        !!!next-token;        !!!next-token;
3801      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3802      if (length $text) {      if (length $text) {
3803          !!!cp ('t46');
3804        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3805      }      }
3806                                
3807      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3808    
3809      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3810          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3811          !!!cp ('t47');
3812        ## Ignore the token        ## Ignore the token
3813      } else {      } else {
3814        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3815          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3816        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3817        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3818      }      }
3819            
3820      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3821          !!!cp ('t49');
3822        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3823      } else {      } else {
3824          !!!cp ('t50');
3825        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3826        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3827          
3828        (($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);  
3829                
3830        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
3831                
# Line 1993  sub _tree_construction_main ($) { Line 3835  sub _tree_construction_main ($) {
3835      !!!next-token;      !!!next-token;
3836    }; # $script_start_tag    }; # $script_start_tag
3837    
3838      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3839      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3840      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3841    
3842    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3843      my $tag_name = shift;      my $end_tag_token = shift;
3844        my $tag_name = $end_tag_token->{tag_name};
3845    
3846        ## NOTE: The adoption agency algorithm (AAA).
3847    
3848      FET: {      FET: {
3849        ## Step 1        ## Step 1
3850        my $formatting_element;        my $formatting_element;
3851        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3852        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3853          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3854              !!!cp ('t52');
3855              last AFE;
3856            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3857                         eq $tag_name) {
3858              !!!cp ('t51');
3859            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3860            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3861            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3862          }          }
3863        } # AFE        } # AFE
3864        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3865          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3866            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3867          ## Ignore the token          ## Ignore the token
3868          !!!next-token;          !!!next-token;
3869          return;          return;
# Line 2022  sub _tree_construction_main ($) { Line 3875  sub _tree_construction_main ($) {
3875          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3876          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3877            if ($in_scope) {            if ($in_scope) {
3878                !!!cp ('t54');
3879              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3880              last INSCOPE;              last INSCOPE;
3881            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3882              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3883                !!!parse-error (type => 'unmatched end tag',
3884                                text => $token->{tag_name},
3885                                token => $end_tag_token);
3886              ## Ignore the token              ## Ignore the token
3887              !!!next-token;              !!!next-token;
3888              return;              return;
3889            }            }
3890          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3891                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3892            $in_scope = 0;            $in_scope = 0;
3893          }          }
3894        } # INSCOPE        } # INSCOPE
3895        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3896          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3897            !!!parse-error (type => 'unmatched end tag',
3898                            text => $token->{tag_name},
3899                            token => $end_tag_token);
3900          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3901          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3902          return;          return;
3903        }        }
3904        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3905          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3906            !!!parse-error (type => 'not closed',
3907                            text => $self->{open_elements}->[-1]->[0]
3908                                ->manakai_local_name,
3909                            token => $end_tag_token);
3910        }        }
3911                
3912        ## Step 2        ## Step 2
# Line 2052  sub _tree_construction_main ($) { Line 3914  sub _tree_construction_main ($) {
3914        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3915        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3916          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3917          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3918              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3919              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3920               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3921              !!!cp ('t59');
3922            $furthest_block = $node;            $furthest_block = $node;
3923            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3924          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3925              !!!cp ('t60');
3926            last OE;            last OE;
3927          }          }
3928        } # OE        } # OE
3929                
3930        ## Step 3        ## Step 3
3931        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3932            !!!cp ('t61');
3933          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3934          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3935          !!!next-token;          !!!next-token;
# Line 2077  sub _tree_construction_main ($) { Line 3942  sub _tree_construction_main ($) {
3942        ## Step 5        ## Step 5
3943        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3944        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3945            !!!cp ('t62');
3946          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3947        }        }
3948                
# Line 2099  sub _tree_construction_main ($) { Line 3965  sub _tree_construction_main ($) {
3965          S7S2: {          S7S2: {
3966            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3967              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3968                  !!!cp ('t63');
3969                $node_i_in_active = $_;                $node_i_in_active = $_;
3970                last S7S2;                last S7S2;
3971              }              }
# Line 2112  sub _tree_construction_main ($) { Line 3979  sub _tree_construction_main ($) {
3979                    
3980          ## Step 4          ## Step 4
3981          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3982              !!!cp ('t64');
3983            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3984          }          }
3985                    
3986          ## Step 5          ## Step 5
3987          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3988              !!!cp ('t65');
3989            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3990            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3991            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2134  sub _tree_construction_main ($) { Line 4003  sub _tree_construction_main ($) {
4003        } # S7          } # S7  
4004                
4005        ## Step 8        ## Step 8
4006        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4007            my $foster_parent_element;
4008            my $next_sibling;
4009            OE: for (reverse 0..$#{$self->{open_elements}}) {
4010              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4011                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4012                                 if (defined $parent and $parent->node_type == 1) {
4013                                   !!!cp ('t65.1');
4014                                   $foster_parent_element = $parent;
4015                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4016                                 } else {
4017                                   !!!cp ('t65.2');
4018                                   $foster_parent_element
4019                                     = $self->{open_elements}->[$_ - 1]->[0];
4020                                 }
4021                                 last OE;
4022                               }
4023                             } # OE
4024                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4025                               unless defined $foster_parent_element;
4026            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4027            $open_tables->[-1]->[1] = 1; # tainted
4028          } else {
4029            !!!cp ('t65.3');
4030            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4031          }
4032                
4033        ## Step 9        ## Step 9
4034        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2151  sub _tree_construction_main ($) { Line 4045  sub _tree_construction_main ($) {
4045        my $i;        my $i;
4046        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4047          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4048              !!!cp ('t66');
4049            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4050            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4051          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4052              !!!cp ('t67');
4053            $i = $_;            $i = $_;
4054          }          }
4055        } # AFE        } # AFE
# Line 2163  sub _tree_construction_main ($) { Line 4059  sub _tree_construction_main ($) {
4059        undef $i;        undef $i;
4060        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4061          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4062              !!!cp ('t68');
4063            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4064            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4065          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4066              !!!cp ('t69');
4067            $i = $_;            $i = $_;
4068          }          }
4069        } # OE        } # OE
# Line 2176  sub _tree_construction_main ($) { Line 4074  sub _tree_construction_main ($) {
4074      } # FET      } # FET
4075    }; # $formatting_end_tag    }; # $formatting_end_tag
4076    
4077    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4078      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4079    }; # $insert_to_current    }; # $insert_to_current
4080    
4081    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4082                         my $child = shift;      my $child = shift;
4083                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4084                              table => 1, tbody => 1, tfoot => 1,        # MUST
4085                              thead => 1, tr => 1,        my $foster_parent_element;
4086                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4087                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4088                           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') {  
4089                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4090                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4091                                   !!!cp ('t70');
4092                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4093                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4094                               } else {                               } else {
4095                                   !!!cp ('t71');
4096                                 $foster_parent_element                                 $foster_parent_element
4097                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4098                               }                               }
# Line 2206  sub _tree_construction_main ($) { Line 4103  sub _tree_construction_main ($) {
4103                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4104                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4105                             ($child, $next_sibling);                             ($child, $next_sibling);
4106                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4107                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4108                         }        !!!cp ('t72');
4109          $self->{open_elements}->[-1]->[0]->append_child ($child);
4110        }
4111    }; # $insert_to_foster    }; # $insert_to_foster
4112    
4113    my $in_body = sub {    B: while (1) {
4114      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4115      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4116        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4117          $script_start_tag->();        ## Ignore the token
4118          return;        ## Stay in the phase
4119        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4120          $style_start_tag->();        next B;
4121          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4122        } elsif ({               $token->{tag_name} eq 'html') {
4123                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4124                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4125          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4126          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4127          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4128          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4129          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4130            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4131          } else {        } else {
4132            $insert->($el);          !!!cp ('t81');
4133          }        }
4134            
4135          !!!next-token;        !!!cp ('t82');
4136          return;        !!!parse-error (type => 'not first start tag', token => $token);
4137        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4138          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4139          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4140          my $title_el;            !!!cp ('t84');
4141          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4142          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4143            ->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  
         } 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') {  
             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') {  
             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;  
4144          }          }
4145                    }
4146          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4147                    !!!next-token;
4148          next B;
4149        } elsif ($token->{type} == COMMENT_TOKEN) {
4150          my $comment = $self->{document}->create_comment ($token->{data});
4151          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4152            !!!cp ('t85');
4153            $self->{document}->append_child ($comment);
4154          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4155            !!!cp ('t86');
4156            $self->{open_elements}->[0]->[0]->append_child ($comment);
4157          } else {
4158            !!!cp ('t87');
4159            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4160          }
4161          !!!next-token;
4162          next B;
4163        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4164          if ($token->{type} == CHARACTER_TOKEN) {
4165            !!!cp ('t87.1');
4166            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4167          !!!next-token;          !!!next-token;
4168          return;          next B;
4169        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4170          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4171            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4172            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4173              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4174                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4175              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4176              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4177              $formatting_end_tag->($token->{tag_name});            #
4178                        } elsif ({
4179              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4180                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4181                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4182                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4183                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4184              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4185              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4186                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4187                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4188                  last OE;            !!!cp ('t87.2');
4189                }            !!!parse-error (type => 'not closed',
4190              } # OE                            text => $self->{open_elements}->[-1]->[0]
4191              last AFE;                                ->manakai_local_name,
4192            } elsif ($node->[0] eq '#marker') {                            token => $token);
4193              last AFE;  
4194              pop @{$self->{open_elements}}
4195                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4196    
4197              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4198              ## Reprocess.
4199              next B;
4200            } else {
4201              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4202              my $tag_name = $token->{tag_name};
4203              if ($nsuri eq $SVG_NS) {
4204                $tag_name = {
4205                   altglyph => 'altGlyph',
4206                   altglyphdef => 'altGlyphDef',
4207                   altglyphitem => 'altGlyphItem',
4208                   animatecolor => 'animateColor',
4209                   animatemotion => 'animateMotion',
4210                   animatetransform => 'animateTransform',
4211                   clippath => 'clipPath',
4212                   feblend => 'feBlend',
4213                   fecolormatrix => 'feColorMatrix',
4214                   fecomponenttransfer => 'feComponentTransfer',
4215                   fecomposite => 'feComposite',
4216                   feconvolvematrix => 'feConvolveMatrix',
4217                   fediffuselighting => 'feDiffuseLighting',
4218                   fedisplacementmap => 'feDisplacementMap',
4219                   fedistantlight => 'feDistantLight',
4220                   feflood => 'feFlood',
4221                   fefunca => 'feFuncA',
4222                   fefuncb => 'feFuncB',
4223                   fefuncg => 'feFuncG',
4224                   fefuncr => 'feFuncR',
4225                   fegaussianblur => 'feGaussianBlur',
4226                   feimage => 'feImage',
4227                   femerge => 'feMerge',
4228                   femergenode => 'feMergeNode',
4229                   femorphology => 'feMorphology',
4230                   feoffset => 'feOffset',
4231                   fepointlight => 'fePointLight',
4232                   fespecularlighting => 'feSpecularLighting',
4233                   fespotlight => 'feSpotLight',
4234                   fetile => 'feTile',
4235                   feturbulence => 'feTurbulence',
4236                   foreignobject => 'foreignObject',
4237                   glyphref => 'glyphRef',
4238                   lineargradient => 'linearGradient',
4239                   radialgradient => 'radialGradient',
4240                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4241                   textpath => 'textPath',  
4242                }->{$tag_name} || $tag_name;
4243            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4244    
4245          !!!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];  
4246    
4247          !!!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', ''];  
4248    
4249          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4250          return;  
4251        } elsif ($token->{tag_name} eq 'marquee' or            if ($self->{self_closing}) {
4252                 $token->{tag_name} eq 'object') {              pop @{$self->{open_elements}};
4253          $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 = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
4254            } else {            } else {
4255              !!!parse-error (type => 'in RCDATA:#'.$token->{type});              !!!cp ('t87.4');
4256            }            }
4257            ## ISSUE: And ignore?  
4258              !!!next-token;
4259              next B;
4260          }          }
4261          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4262          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4263        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4264          $reconstruct_active_formatting_elements->($insert_to_current);          #
4265                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4266          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4267                    !!!parse-error (type => 'not closed',
4268          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4269          !!!next-token;                              ->manakai_local_name,
4270          return;                          token => $token);
4271        } elsif ({  
4272                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4273                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4274                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4275                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4276                 }->{$token->{tag_name}}) {          ## Reprocess.
4277          !!!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.  
4278        } else {        } else {
4279          $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;  
4280        }        }
4281      } elsif ($token->{type} eq 'end tag') {      }
4282        if ($token->{tag_name} eq 'body') {  
4283          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {      if ($self->{insertion_mode} & HEAD_IMS) {
4284            ## ISSUE: There is an issue in the spec.        if ($token->{type} == CHARACTER_TOKEN) {
4285            if ($self->{open_elements}->[-1]->[1] ne 'body') {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4286              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4287            }              !!!cp ('t88.2');
4288            $self->{insertion_mode} = 'after body';              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4289            !!!next-token;            } else {
4290            return;              !!!cp ('t88.1');
4291          } else {              ## Ignore the token.
4292            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
4293            ## 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;  
4294            }            }
4295          } # INSCOPE            unless (length $token->{data}) {
4296                        !!!cp ('t88');
4297          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4298            !!!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;  
4299            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4300          }          }
           
         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});  
         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];  
4301    
4302          ## Step 2          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4303          S2: {            !!!cp ('t89');
4304            if ($node->[1] eq $token->{tag_name}) {            ## As if <head>
4305              ## Step 1            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4306              ## generate implied end tags            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4307              if ({            push @{$self->{open_elements}},
4308                   dd => 1, dt => 1, li => 1, p => 1,                [$self->{head_element}, $el_category->{head}];
4309                   td => 1, th => 1, tr => 1,  
4310                  }->{$self->{open_elements}->[-1]->[1]}) {            ## Reprocess in the "in head" insertion mode...
4311                !!!back-token;            pop @{$self->{open_elements}};
4312                $token = {type => 'end tag',  
4313                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST            ## Reprocess in the "after head" insertion mode...
4314                return;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4315              }            !!!cp ('t90');
4316                      ## As if </noscript>
4317              ## Step 2            pop @{$self->{open_elements}};
4318              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {            !!!parse-error (type => 'in noscript:#text', token => $token);
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4319                        
4320            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4321            redo S2;            ## As if </head>
4322          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
4323    
4324    B: {            ## Reprocess in the "after head" insertion mode...
4325      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4326        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4327          !!!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]);  
         }  
4328    
4329          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4330          last B;          } else {
4331              !!!cp ('t92');
4332            }
4333    
4334          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4335        } else {          ## As if <body>
4336          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4337            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4338              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4339                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4340                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4341                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4342                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4343                }              !!!cp ('t93');
4344              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4345              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4346              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4347              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4348              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4349              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4350              ## 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);  
4351              !!!next-token;              !!!next-token;
4352              redo B;              next B;
4353            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4354              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4355              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4356              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4357              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4358              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4359              if ($token->{tag_name} eq 'head') {              !!!next-token;
4360                !!!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;  
             }  
4361            } else {            } else {
4362              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4363            }              !!!parse-error (type => 'in head:head',
4364          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4365            if ($token->{type} eq 'character') {              ## Ignore the token
4366              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);  
4367              !!!next-token;              !!!next-token;
4368              redo B;              next B;
4369            } elsif ($token->{type} eq 'start tag') {            }
4370              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4371                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4372                my $title_el;            ## As if <head>
4373                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4374                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4375                  ->append_child ($title_el);            push @{$self->{open_elements}},
4376                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4377    
4378                my $text = '';            $self->{insertion_mode} = IN_HEAD_IM;
4379                !!!next-token;            ## Reprocess in the "in head" insertion mode...
4380                while ($token->{type} eq 'character') {          } else {
4381                  $text .= $token->{data};            !!!cp ('t97');
4382                  !!!next-token;          }
4383                }  
4384                if (length $text) {              if ($token->{tag_name} eq 'base') {
4385                  $title_el->manakai_append_text ($text);                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4386                }                  !!!cp ('t98');
4387                                  ## As if </noscript>
4388                $self->{content_model_flag} = 'PCDATA';                  pop @{$self->{open_elements}};
4389                    !!!parse-error (type => 'in noscript', text => 'base',
4390                                    token => $token);
4391                                
4392                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4393                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4394                } else {                } else {
4395                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4396                }                }
               !!!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);  
4397    
4398                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4399                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4400              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4401                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4402                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4403                !!!next-token;                  push @{$self->{open_elements}},
4404                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}};  
4405                } else {                } else {
4406                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4407                }                }
4408                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4409                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4410                  pop @{$self->{open_elements}} # <head>
4411                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4412                  !!!nack ('t101.1');
4413                !!!next-token;                !!!next-token;
4414                redo B;                next B;
4415              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'link') {
4416                #                ## NOTE: There is a "as if in head" code clone.
4417              } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4418                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t102');
4419                ## Ignore the token                  !!!parse-error (type => 'after head',
4420                !!!next-token;                                  text => $token->{tag_name}, token => $token);
4421                redo B;                  push @{$self->{open_elements}},
4422              }                      [$self->{head_element}, $el_category->{head}];
4423            } else {                } else {
4424              #                  !!!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;  
4425                }                }
4426              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4427                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4428              #                pop @{$self->{open_elements}} # <head>
4429            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4430              my $comment = $self->{document}->create_comment ($token->{data});                !!!ack ('t103.1');
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
4431                !!!next-token;                !!!next-token;
4432                redo B;                next B;
4433              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4434                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4435                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4436                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4437                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4438                $self->{insertion_mode} = 'in head';                                  text => $token->{tag_name}, token => $token);
4439                ## reprocess                  push @{$self->{open_elements}},
4440                redo B;                      [$self->{head_element}, $el_category->{head}];
4441              } else {                } else {
4442                #                  !!!cp ('t105');
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4443                }                }
4444              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4445                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4446    
4447              !!!parse-error (type => 'in table:#character');                unless ($self->{confident}) {
4448                    if ($token->{attributes}->{charset}) {
4449              ## As if in body, but insert into foster parent element                    !!!cp ('t106');
4450              ## ISSUE: Spec says that "whenever a node would be inserted                    ## NOTE: Whether the encoding is supported or not is handled
4451              ## into the current node" while characters might not be                    ## in the {change_encoding} callback.
4452              ## result in a new Text node.                    $self->{change_encoding}
4453              $reconstruct_active_formatting_elements->($insert_to_foster);                        ->($self, $token->{attributes}->{charset}->{value},
4454                                         $token);
4455              if ({                    
4456                   table => 1, tbody => 1, tfoot => 1,                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4457                   thead => 1, tr => 1,                        ->set_user_data (manakai_has_reference =>
4458                  }->{$self->{open_elements}->[-1]->[1]}) {                                             $token->{attributes}->{charset}
4459                # MUST                                                 ->{has_reference});
4460                my $foster_parent_element;                  } elsif ($token->{attributes}->{content}) {
4461                my $next_sibling;                    if ($token->{attributes}->{content}->{value}
4462                my $prev_sibling;                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4463                OE: for (reverse 0..$#{$self->{open_elements}}) {                            [\x09-\x0D\x20]*=
4464                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4465                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4466                    if (defined $parent and $parent->node_type == 1) {                      !!!cp ('t107');
4467                      $foster_parent_element = $parent;                      ## NOTE: Whether the encoding is supported or not is handled
4468                      $next_sibling = $self->{open_elements}->[$_]->[0];                      ## in the {change_encoding} callback.
4469                      $prev_sibling = $next_sibling->previous_sibling;                      $self->{change_encoding}
4470                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4471                               $token);
4472                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4473                            ->set_user_data (manakai_has_reference =>
4474                                                 $token->{attributes}->{content}
4475                                                       ->{has_reference});
4476                    } else {                    } else {
4477                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4478                    }                    }
                   last OE;  
4479                  }                  }
               } # 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});  
4480                } else {                } else {
4481                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4482                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4483                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4484                }                        ->set_user_data (manakai_has_reference =>
4485              } else {                                             $token->{attributes}->{charset}
4486                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4487              }                  }
4488                                if ($token->{attributes}->{content}) {
4489              !!!next-token;                    !!!cp ('t110');
4490              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4491            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4492              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4493              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4494              !!!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}};  
4495                }                }
4496    
4497                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4498                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4499                  !!!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}};  
4500                !!!next-token;                !!!next-token;
4501                redo B;                next B;
4502              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4503                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4504                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4505                       }->{$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]);  
4506                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4507                    !!!parse-error (type => 'in noscript', text => 'title',
4508                                    token => $token);
4509                  
4510                    $self->{insertion_mode} = IN_HEAD_IM;
4511                    ## Reprocess in the "in head" insertion mode...
4512                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4513                    !!!cp ('t112');
4514                    !!!parse-error (type => 'after head',
4515                                    text => $token->{tag_name}, token => $token);
4516                    push @{$self->{open_elements}},
4517                        [$self->{head_element}, $el_category->{head}];
4518                  } else {
4519                    !!!cp ('t113');
4520                }                }
4521    
4522                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4523                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4524                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4525                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4526                redo B;                pop @{$self->{open_elements}} # <head>
4527              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4528                ## NOTE: There are code clones for this "table in table"                next B;
4529                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4530                         $token->{tag_name} eq 'noframes') {
4531                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4532                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4533                my $i;                ## NOTE: There is a "as if in head" code clone.
4534                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4535                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4536                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4537                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4538                    last INSCOPE;                  push @{$self->{open_elements}},
4539                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4540                            table => 1, html => 1,                } else {
4541                           }->{$node->[1]}) {                  !!!cp ('t115');
4542                    last INSCOPE;                }
4543                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4544                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4545                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4546                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4547                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4548                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4549                    !!!cp ('t116');
4550                    ## NOTE: and scripting is disalbed
4551                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4552                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4553                    !!!nack ('t116.1');
4554                  !!!next-token;                  !!!next-token;
4555                  redo B;                  next B;
4556                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4557                    !!!cp ('t117');
4558                    !!!parse-error (type => 'in noscript', text => 'noscript',
4559                                    token => $token);
4560                    ## Ignore the token
4561                    !!!nack ('t117.1');
4562                    !!!next-token;
4563                    next B;
4564                  } else {
4565                    !!!cp ('t118');
4566                    #
4567                }                }
4568                } elsif ($token->{tag_name} eq 'script') {
4569                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4570                    !!!cp ('t119');
4571                    ## As if </noscript>
4572                    pop @{$self->{open_elements}};
4573                    !!!parse-error (type => 'in noscript', text => 'script',
4574                                    token => $token);
4575                                
4576                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4577                if ({                  ## Reprocess in the "in head" insertion mode...
4578                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4579                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4580                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4581                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4582                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4583                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4584                  $token = {type => 'end tag',                } else {
4585                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
                 redo B;  
4586                }                }
4587    
4588                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4589                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4590                  pop @{$self->{open_elements}} # <head>
4591                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4592                  next B;
4593                } elsif ($token->{tag_name} eq 'body' or
4594                         $token->{tag_name} eq 'frameset') {
4595                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4596                    !!!cp ('t122');
4597                    ## As if </noscript>
4598                    pop @{$self->{open_elements}};
4599                    !!!parse-error (type => 'in noscript',
4600                                    text => $token->{tag_name}, token => $token);
4601                    
4602                    ## Reprocess in the "in head" insertion mode...
4603                    ## As if </head>
4604                    pop @{$self->{open_elements}};
4605                    
4606                    ## Reprocess in the "after head" insertion mode...
4607                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4608                    !!!cp ('t124');
4609                    pop @{$self->{open_elements}};
4610                    
4611                    ## Reprocess in the "after head" insertion mode...
4612                  } else {
4613                    !!!cp ('t125');
4614                }                }
4615    
4616                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4617                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4618                  if ($token->{tag_name} eq 'body') {
4619                    !!!cp ('t126');
4620                    $self->{insertion_mode} = IN_BODY_IM;
4621                  } elsif ($token->{tag_name} eq 'frameset') {
4622                    !!!cp ('t127');
4623                    $self->{insertion_mode} = IN_FRAMESET_IM;
4624                  } else {
4625                    die "$0: tag name: $self->{tag_name}";
4626                  }
4627                  !!!nack ('t127.1');
4628                  !!!next-token;
4629                  next B;
4630                } else {
4631                  !!!cp ('t128');
4632                  #
4633                }
4634    
4635                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4636                  !!!cp ('t129');
4637                  ## As if </noscript>
4638                  pop @{$self->{open_elements}};
4639                  !!!parse-error (type => 'in noscript:/',
4640                                  text => $token->{tag_name}, token => $token);
4641                  
4642                  ## Reprocess in the "in head" insertion mode...
4643                  ## As if </head>
4644                  pop @{$self->{open_elements}};
4645    
4646                ## reprocess                ## Reprocess in the "after head" insertion mode...
4647                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4648                  !!!cp ('t130');
4649                  ## As if </head>
4650                  pop @{$self->{open_elements}};
4651    
4652                  ## Reprocess in the "after head" insertion mode...
4653              } else {              } else {
4654                #                !!!cp ('t131');
4655              }              }
4656            } elsif ($token->{type} eq 'end tag') {  
4657              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4658                ## have a table element in table scope              ## As if <body>
4659                my $i;              !!!insert-element ('body',, $token);
4660                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4661                  my $node = $self->{open_elements}->[$_];              ## reprocess
4662                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4663                    $i = $_;              next B;
4664                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4665                  } elsif ({              if ($token->{tag_name} eq 'head') {
4666                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4667                           }->{$node->[1]}) {                  !!!cp ('t132');
4668                    last INSCOPE;                  ## As if <head>
4669                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4670                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4671                unless (defined $i) {                  push @{$self->{open_elements}},
4672                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4673    
4674                    ## Reprocess in the "in head" insertion mode...
4675                    pop @{$self->{open_elements}};
4676                    $self->{insertion_mode} = AFTER_HEAD_IM;
4677                    !!!next-token;
4678                    next B;
4679                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4680                    !!!cp ('t133');
4681                    ## As if </noscript>
4682                    pop @{$self->{open_elements}};
4683                    !!!parse-error (type => 'in noscript:/',
4684                                    text => 'head', token => $token);
4685                    
4686                    ## Reprocess in the "in head" insertion mode...
4687                    pop @{$self->{open_elements}};
4688                    $self->{insertion_mode} = AFTER_HEAD_IM;
4689                    !!!next-token;
4690                    next B;
4691                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4692                    !!!cp ('t134');
4693                    pop @{$self->{open_elements}};
4694                    $self->{insertion_mode} = AFTER_HEAD_IM;
4695                    !!!next-token;
4696                    next B;
4697                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4698                    !!!cp ('t134.1');
4699                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4700                                    token => $token);
4701                  ## Ignore the token                  ## Ignore the token
4702                  !!!next-token;                  !!!next-token;
4703                  redo B;                  next B;
4704                  } else {
4705                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4706                }                }
4707                              } elsif ($token->{tag_name} eq 'noscript') {
4708                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4709                if ({                  !!!cp ('t136');
4710                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4711                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4712                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4713                  !!!back-token;                  next B;
4714                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4715                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4716                  redo B;                  !!!cp ('t137');
4717                    !!!parse-error (type => 'unmatched end tag',
4718                                    text => 'noscript', token => $token);
4719                    ## Ignore the token ## ISSUE: An issue in the spec.
4720                    !!!next-token;
4721                    next B;
4722                  } else {
4723                    !!!cp ('t138');
4724                    #
4725                }                }
4726                } elsif ({
4727                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4728                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4729                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4730                      $self->{insertion_mode} == IN_HEAD_IM or
4731                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4732                    !!!cp ('t140');
4733                    !!!parse-error (type => 'unmatched end tag',
4734                                    text => $token->{tag_name}, token => $token);
4735                    ## Ignore the token
4736                    !!!next-token;
4737                    next B;
4738                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4739                    !!!cp ('t140.1');
4740                    !!!parse-error (type => 'unmatched end tag',
4741                                    text => $token->{tag_name}, token => $token);
4742                    ## Ignore the token
4743                    !!!next-token;
4744                    next B;
4745                  } else {
4746                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4747                }                }
4748                } elsif ($token->{tag_name} eq 'p') {
4749                  !!!cp ('t142');
4750                  !!!parse-error (type => 'unmatched end tag',
4751                                  text => $token->{tag_name}, token => $token);
4752                  ## Ignore the token
4753                  !!!next-token;
4754                  next B;
4755                } elsif ($token->{tag_name} eq 'br') {
4756                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4757                    !!!cp ('t142.2');
4758                    ## (before head) as if <head>, (in head) as if </head>
4759                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4760                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4761                    $self->{insertion_mode} = AFTER_HEAD_IM;
4762      
4763                    ## Reprocess in the "after head" insertion mode...
4764                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4765                    !!!cp ('t143.2');
4766                    ## As if </head>
4767                    pop @{$self->{open_elements}};
4768                    $self->{insertion_mode} = AFTER_HEAD_IM;
4769      
4770                    ## Reprocess in the "after head" insertion mode...
4771                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4772                    !!!cp ('t143.3');
4773                    ## ISSUE: Two parse errors for <head><noscript></br>
4774                    !!!parse-error (type => 'unmatched end tag',
4775                                    text => 'br', token => $token);
4776                    ## As if </noscript>
4777                    pop @{$self->{open_elements}};
4778                    $self->{insertion_mode} = IN_HEAD_IM;
4779    
4780                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
4781                    ## As if </head>
4782                    pop @{$self->{open_elements}};
4783                    $self->{insertion_mode} = AFTER_HEAD_IM;
4784    
4785                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
4786                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4787                    !!!cp ('t143.4');
4788                    #
4789                  } else {
4790                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4791                  }
4792    
4793                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4794                  !!!parse-error (type => 'unmatched end tag',
4795                                  text => 'br', token => $token);
4796                  ## Ignore the token
4797                !!!next-token;                !!!next-token;
4798                redo B;                next B;
4799              } elsif ({              } else {
4800                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
4801                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
4802                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4803                ## Ignore the token                ## Ignore the token
4804                !!!next-token;                !!!next-token;
4805                redo B;                next B;
4806                }
4807    
4808                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4809                  !!!cp ('t146');
4810                  ## As if </noscript>
4811                  pop @{$self->{open_elements}};
4812                  !!!parse-error (type => 'in noscript:/',
4813                                  text => $token->{tag_name}, token => $token);
4814                  
4815                  ## Reprocess in the "in head" insertion mode...
4816                  ## As if </head>
4817                  pop @{$self->{open_elements}};
4818    
4819                  ## Reprocess in the "after head" insertion mode...
4820                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4821                  !!!cp ('t147');
4822                  ## As if </head>
4823                  pop @{$self->{open_elements}};
4824    
4825                  ## Reprocess in the "after head" insertion mode...
4826                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4827    ## ISSUE: This case cannot be reached?
4828                  !!!cp ('t148');
4829                  !!!parse-error (type => 'unmatched end tag',
4830                                  text => $token->{tag_name}, token => $token);
4831                  ## Ignore the token ## ISSUE: An issue in the spec.
4832                  !!!next-token;
4833                  next B;
4834              } else {              } else {
4835                #                !!!cp ('t149');
4836              }              }
           } else {  
             #  
           }  
4837    
4838            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
4839            $in_body->($insert_to_foster);              ## As if <body>
4840            redo B;              !!!insert-element ('body',, $token);
4841          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
4842            if ($token->{type} eq 'character') {              ## reprocess
4843              ## NOTE: This is a code clone of "character in body".              next B;
4844          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4845            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4846              !!!cp ('t149.1');
4847    
4848              ## NOTE: As if <head>
4849              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4850              $self->{open_elements}->[-1]->[0]->append_child
4851                  ($self->{head_element});
4852              #push @{$self->{open_elements}},
4853              #    [$self->{head_element}, $el_category->{head}];
4854              #$self->{insertion_mode} = IN_HEAD_IM;
4855              ## NOTE: Reprocess.
4856    
4857              ## NOTE: As if </head>
4858              #pop @{$self->{open_elements}};
4859              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4860              ## NOTE: Reprocess.
4861              
4862              #
4863            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4864              !!!cp ('t149.2');
4865    
4866              ## NOTE: As if </head>
4867              pop @{$self->{open_elements}};
4868              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4869              ## NOTE: Reprocess.
4870    
4871              #
4872            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4873              !!!cp ('t149.3');
4874    
4875              !!!parse-error (type => 'in noscript:#eof', token => $token);
4876    
4877              ## As if </noscript>
4878              pop @{$self->{open_elements}};
4879              #$self->{insertion_mode} = IN_HEAD_IM;
4880              ## NOTE: Reprocess.
4881    
4882              ## NOTE: As if </head>
4883              pop @{$self->{open_elements}};
4884              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4885              ## NOTE: Reprocess.
4886    
4887              #
4888            } else {
4889              !!!cp ('t149.4');
4890              #
4891            }
4892    
4893            ## NOTE: As if <body>
4894            !!!insert-element ('body',, $token);
4895            $self->{insertion_mode} = IN_BODY_IM;
4896            ## NOTE: Reprocess.
4897            next B;
4898          } else {
4899            die "$0: $token->{type}: Unknown token type";
4900          }
4901    
4902              ## ISSUE: An issue in the spec.
4903        } elsif ($self->{insertion_mode} & BODY_IMS) {
4904              if ($token->{type} == CHARACTER_TOKEN) {
4905                !!!cp ('t150');
4906                ## NOTE: There is a code clone of "character in body".
4907              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4908                            
4909              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4910    
4911              !!!next-token;              !!!next-token;
4912              redo B;              next B;
4913            } 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') {  
4914              if ({              if ({
4915                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4916                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4917                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4918                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4919                    ## have an element in table scope
4920                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4921                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4922                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4923                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4924                  my $node = $self->{open_elements}->[$_];  
4925                  if ($node->[1] eq 'caption') {                      ## Close the cell
4926                    $i = $_;                      !!!back-token; # <x>
4927                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4928                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4929                            table => 1, html => 1,                                line => $token->{line},
4930                           }->{$node->[1]}) {                                column => $token->{column}};
4931                    last INSCOPE;                      next B;
4932                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4933                        !!!cp ('t152');
4934                        ## ISSUE: This case can never be reached, maybe.
4935                        last;
4936                      }
4937                  }                  }
4938                } # INSCOPE  
4939                unless (defined $i) {                  !!!cp ('t153');
4940                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4941                        text => $token->{tag_name}, token => $token);
4942                  ## Ignore the token                  ## Ignore the token
4943                    !!!nack ('t153.1');
4944                  !!!next-token;                  !!!next-token;
4945                  redo B;                  next B;
4946                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4947                                  !!!parse-error (type => 'not closed', text => 'caption',
4948                ## generate implied end tags                                  token => $token);
4949                if ({                  
4950                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
4951                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
4952                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
4953                  !!!back-token; # <?>                  INSCOPE: {
4954                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
4955                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
4956                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
4957                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
4958                  redo B;                        $i = $_;
4959                }                        last INSCOPE;
4960                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4961                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
4962                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
4963                }                      }
4964                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4965    
4966                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4967                      !!!parse-error (type => 'start tag not allowed',
4968                                      text => $token->{tag_name}, token => $token);
4969                      ## Ignore the token
4970                      !!!nack ('t157.1');
4971                      !!!next-token;
4972                      next B;
4973                    } # INSCOPE
4974                    
4975                    ## generate implied end tags
4976                    while ($self->{open_elements}->[-1]->[1]
4977                               & END_TAG_OPTIONAL_EL) {
4978                      !!!cp ('t158');
4979                      pop @{$self->{open_elements}};
4980                    }
4981    
4982                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4983                redo B;                    !!!cp ('t159');
4984                      !!!parse-error (type => 'not closed',
4985                                      text => $self->{open_elements}->[-1]->[0]
4986                                          ->manakai_local_name,
4987                                      token => $token);
4988                    } else {
4989                      !!!cp ('t160');
4990                    }
4991                    
4992                    splice @{$self->{open_elements}}, $i;
4993                    
4994                    $clear_up_to_marker->();
4995                    
4996                    $self->{insertion_mode} = IN_TABLE_IM;
4997                    
4998                    ## reprocess
4999                    !!!ack-later;
5000                    next B;
5001                  } else {
5002                    !!!cp ('t161');
5003                    #
5004                  }
5005              } else {              } else {
5006                  !!!cp ('t162');
5007                #                #
5008              }              }
5009            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5010              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5011                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
5012                my $i;                  ## have an element in table scope
5013                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
5014                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5015                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5016                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5017                    last INSCOPE;                      !!!cp ('t163');
5018                  } elsif ({                      $i = $_;
5019                            table => 1, html => 1,                      last INSCOPE;
5020                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5021                    last INSCOPE;                      !!!cp ('t164');
5022                        last INSCOPE;
5023                      }
5024                    } # INSCOPE
5025                      unless (defined $i) {
5026                        !!!cp ('t165');
5027                        !!!parse-error (type => 'unmatched end tag',
5028                                        text => $token->{tag_name},
5029                                        token => $token);
5030                        ## Ignore the token
5031                        !!!next-token;
5032                        next B;
5033                      }
5034                    
5035                    ## generate implied end tags
5036                    while ($self->{open_elements}->[-1]->[1]
5037                               & END_TAG_OPTIONAL_EL) {
5038                      !!!cp ('t166');
5039                      pop @{$self->{open_elements}};
5040                  }                  }
5041                } # INSCOPE  
5042                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5043                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
5044                      !!!cp ('t167');
5045                      !!!parse-error (type => 'not closed',
5046                                      text => $self->{open_elements}->[-1]->[0]
5047                                          ->manakai_local_name,
5048                                      token => $token);
5049                    } else {
5050                      !!!cp ('t168');
5051                    }
5052                    
5053                    splice @{$self->{open_elements}}, $i;
5054                    
5055                    $clear_up_to_marker->();
5056                    
5057                    $self->{insertion_mode} = IN_ROW_IM;
5058                    
5059                    !!!next-token;
5060                    next B;
5061                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5062                    !!!cp ('t169');
5063                    !!!parse-error (type => 'unmatched end tag',
5064                                    text => $token->{tag_name}, token => $token);
5065                  ## Ignore the token                  ## Ignore the token
5066                  !!!next-token;                  !!!next-token;
5067                  redo B;                  next B;
5068                }                } else {
5069                                  !!!cp ('t170');
5070                ## 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;  
5071                }                }
5072                } elsif ($token->{tag_name} eq 'caption') {
5073                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
5074                    ## have a table element in table scope
5075                    my $i;
5076                    INSCOPE: {
5077                      for (reverse 0..$#{$self->{open_elements}}) {
5078                        my $node = $self->{open_elements}->[$_];
5079                        if ($node->[1] & CAPTION_EL) {
5080                          !!!cp ('t171');
5081                          $i = $_;
5082                          last INSCOPE;
5083                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5084                          !!!cp ('t172');
5085                          last;
5086                        }
5087                      }
5088    
5089                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
5090                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
5091                                      text => $token->{tag_name}, token => $token);
5092                      ## Ignore the token
5093                      !!!next-token;
5094                      next B;
5095                    } # INSCOPE
5096                    
5097                    ## generate implied end tags
5098                    while ($self->{open_elements}->[-1]->[1]
5099                               & END_TAG_OPTIONAL_EL) {
5100                      !!!cp ('t174');
5101                      pop @{$self->{open_elements}};
5102                    }
5103                    
5104                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5105                      !!!cp ('t175');
5106                      !!!parse-error (type => 'not closed',
5107                                      text => $self->{open_elements}->[-1]->[0]
5108                                          ->manakai_local_name,
5109                                      token => $token);
5110                    } else {
5111                      !!!cp ('t176');
5112                    }
5113                    
5114                    splice @{$self->{open_elements}}, $i;
5115                    
5116                    $clear_up_to_marker->();
5117                    
5118                    $self->{insertion_mode} = IN_TABLE_IM;
5119                    
5120                    !!!next-token;
5121                    next B;
5122                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5123                    !!!cp ('t177');
5124                    !!!parse-error (type => 'unmatched end tag',
5125                                    text => $token->{tag_name}, token => $token);
5126                    ## Ignore the token
5127                    !!!next-token;
5128                    next B;
5129                  } else {
5130                    !!!cp ('t178');
5131                    #
5132                }                }
5133                } elsif ({
5134                          table => 1, tbody => 1, tfoot => 1,
5135                          thead => 1, tr => 1,
5136                         }->{$token->{tag_name}} and
5137                         $self->{insertion_mode} == IN_CELL_IM) {
5138                  ## have an element in table scope
5139                  my $i;
5140                  my $tn;
5141                  INSCOPE: {
5142                    for (reverse 0..$#{$self->{open_elements}}) {
5143                      my $node = $self->{open_elements}->[$_];
5144                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5145                        !!!cp ('t179');
5146                        $i = $_;
5147    
5148                        ## Close the cell
5149                        !!!back-token; # </x>
5150                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5151                                  line => $token->{line},
5152                                  column => $token->{column}};
5153                        next B;
5154                      } elsif ($node->[1] & TABLE_CELL_EL) {
5155                        !!!cp ('t180');
5156                        $tn = $node->[0]->manakai_local_name;
5157                        ## NOTE: There is exactly one |td| or |th| element
5158                        ## in scope in the stack of open elements by definition.
5159                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5160                        ## ISSUE: Can this be reached?
5161                        !!!cp ('t181');
5162                        last;
5163                      }
5164                    }
5165    
5166                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5167                    !!!parse-error (type => 'unmatched end tag',
5168                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5169                    ## Ignore the token
5170                $self->{insertion_mode} = 'in table';                  !!!next-token;
5171                    next B;
5172                !!!next-token;                } # INSCOPE
5173                redo B;              } elsif ($token->{tag_name} eq 'table' and
5174              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5175                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5176                                  token => $token);
5177    
5178                ## As if </caption>                ## As if </caption>
5179                ## have a table element in table scope                ## have a table element in table scope
5180                my $i;                my $i;
5181                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5182                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5183                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5184                      !!!cp ('t184');
5185                    $i = $_;                    $i = $_;
5186                    last INSCOPE;                    last INSCOPE;
5187                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5188                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5189                    last INSCOPE;                    last INSCOPE;
5190                  }                  }
5191                } # INSCOPE                } # INSCOPE
5192                unless (defined $i) {                unless (defined $i) {
5193                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5194                    !!!parse-error (type => 'unmatched end tag',
5195                                    text => 'caption', token => $token);
5196                  ## Ignore the token                  ## Ignore the token
5197                  !!!next-token;                  !!!next-token;
5198                  redo B;                  next B;
5199                }                }
5200                                
5201                ## generate implied end tags                ## generate implied end tags
5202                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5203                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5204                     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;  
5205                }                }
5206    
5207                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5208                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5209                    !!!parse-error (type => 'not closed',
5210                                    text => $self->{open_elements}->[-1]->[0]
5211                                        ->manakai_local_name,
5212                                    token => $token);
5213                  } else {
5214                    !!!cp ('t189');
5215                }                }
5216    
5217                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5218    
5219                $clear_up_to_marker->();                $clear_up_to_marker->();
5220    
5221                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5222    
5223                ## reprocess                ## reprocess
5224                redo B;                next B;
5225              } elsif ({              } elsif ({
5226                        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,  
5227                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5228                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5229                ## Ignore the token                  !!!cp ('t190');
5230                redo B;                  !!!parse-error (type => 'unmatched end tag',
5231              } 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');  
5232                  ## Ignore the token                  ## Ignore the token
5233                  !!!next-token;                  !!!next-token;
5234                  redo B;                  next B;
5235                } else {                } else {
5236                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5237                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5238                }                }
5239              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5240                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5241                          thead => 1, tr => 1,
5242                         }->{$token->{tag_name}} and
5243                         $self->{insertion_mode} == IN_CAPTION_IM) {
5244                  !!!cp ('t192');
5245                  !!!parse-error (type => 'unmatched end tag',
5246                                  text => $token->{tag_name}, token => $token);
5247                ## Ignore the token                ## Ignore the token
5248                !!!next-token;                !!!next-token;
5249                redo B;                next B;
5250              } else {              } else {
5251                #                !!!cp ('t193');
5252                  #
5253              }              }
5254            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5255              #          for my $entry (@{$self->{open_elements}}) {
5256              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5257                !!!cp ('t75');
5258                !!!parse-error (type => 'in body:#eof', token => $token);
5259                last;
5260            }            }
5261            }
5262    
5263            ## As if </colgroup>          ## Stop parsing.
5264            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5265              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5266              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5267          }
5268    
5269          $insert = $insert_to_current;
5270          #
5271        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5272          if ($token->{type} == CHARACTER_TOKEN) {
5273            if (not $open_tables->[-1]->[1] and # tainted
5274                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5275              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5276                  
5277              unless (length $token->{data}) {
5278                !!!cp ('t194');
5279              !!!next-token;              !!!next-token;
5280              redo B;              next B;
5281            } else {            } else {
5282              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5283            }            }
5284          } 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;  
               }  
             }  
5285    
5286              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5287    
5288              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5289              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5290              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5291              ## result in a new Text node.              ## result in a new Text node.
5292              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5293                
5294              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]}) {  
5295                # MUST                # MUST
5296                my $foster_parent_element;                my $foster_parent_element;
5297                my $next_sibling;                my $next_sibling;
5298                my $prev_sibling;                my $prev_sibling;
5299                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5300                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5301                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5302                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5303                        !!!cp ('t196');
5304                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5305                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5306                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5307                    } else {                    } else {
5308                        !!!cp ('t197');
5309                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5310                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5311                    }                    }
# Line 3725  sub _tree_construction_main ($) { Line 5317  sub _tree_construction_main ($) {
5317                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5318                if (defined $prev_sibling and                if (defined $prev_sibling and
5319                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5320                    !!!cp ('t198');
5321                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5322                } else {                } else {
5323                    !!!cp ('t199');
5324                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5325                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5326                     $next_sibling);                     $next_sibling);
5327                }                }
5328              } else {            $open_tables->[-1]->[1] = 1; # tainted
5329                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5330              !!!cp ('t200');
5331              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5332            }
5333                
5334            !!!next-token;
5335            next B;
5336          } elsif ($token->{type} == START_TAG_TOKEN) {
5337            if ({
5338                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5339                 th => 1, td => 1,
5340                }->{$token->{tag_name}}) {
5341              if ($self->{insertion_mode} == IN_TABLE_IM) {
5342                ## Clear back to table context
5343                while (not ($self->{open_elements}->[-1]->[1]
5344                                & TABLE_SCOPING_EL)) {
5345                  !!!cp ('t201');
5346                  pop @{$self->{open_elements}};
5347              }              }
5348                            
5349              !!!next-token;              !!!insert-element ('tbody',, $token);
5350              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5351            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5352              ## Copied from 'in table'            }
5353              my $comment = $self->{document}->create_comment ($token->{data});            
5354              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5355              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5356              redo B;                !!!cp ('t202');
5357            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5358              if ({              }
5359                   tr => 1,                  
5360                   th => 1, td => 1,              ## Clear back to table body context
5361                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5362                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5363                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5364                  ## ISSUE: Can this case be reached?
5365                  pop @{$self->{open_elements}};
5366                }
5367                    
5368                    $self->{insertion_mode} = IN_ROW_IM;
5369                    if ($token->{tag_name} eq 'tr') {
5370                      !!!cp ('t204');
5371                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5372                      !!!nack ('t204');
5373                      !!!next-token;
5374                      next B;
5375                    } else {
5376                      !!!cp ('t205');
5377                      !!!insert-element ('tr',, $token);
5378                      ## reprocess in the "in row" insertion mode
5379                    }
5380                  } else {
5381                    !!!cp ('t206');
5382                }                }
5383    
5384                ## Clear back to table body context                ## Clear back to table row context
5385                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5386                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5387                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5388                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5389                }                }
5390                                
5391                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5392                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5393                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5394                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5395                } else {                
5396                  !!!insert-element ('tr');                !!!nack ('t207.1');
5397                  ## reprocess                !!!next-token;
5398                }                next B;
               redo B;  
5399              } elsif ({              } elsif ({
5400                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5401                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5402                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5403                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5404                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5405                my $i;                  ## As if </tr>
5406                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5407                  my $node = $self->{open_elements}->[$_];                  my $i;
5408                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5409                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5410                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5411                    $i = $_;                      !!!cp ('t208');
5412                    last INSCOPE;                      $i = $_;
5413                  } elsif ({                      last INSCOPE;
5414                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5415                           }->{$node->[1]}) {                      !!!cp ('t209');
5416                    last INSCOPE;                      last INSCOPE;
5417                      }
5418                    } # INSCOPE
5419                    unless (defined $i) {
5420                      !!!cp ('t210');
5421    ## TODO: This type is wrong.
5422                      !!!parse-error (type => 'unmacthed end tag',
5423                                      text => $token->{tag_name}, token => $token);
5424                      ## Ignore the token
5425                      !!!nack ('t210.1');
5426                      !!!next-token;
5427                      next B;
5428                    }
5429                    
5430                    ## Clear back to table row context
5431                    while (not ($self->{open_elements}->[-1]->[1]
5432                                    & TABLE_ROW_SCOPING_EL)) {
5433                      !!!cp ('t211');
5434                      ## ISSUE: Can this case be reached?
5435                      pop @{$self->{open_elements}};
5436                    }
5437                    
5438                    pop @{$self->{open_elements}}; # tr
5439                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5440                    if ($token->{tag_name} eq 'tr') {
5441                      !!!cp ('t212');
5442                      ## reprocess
5443                      !!!ack-later;
5444                      next B;
5445                    } else {
5446                      !!!cp ('t213');
5447                      ## reprocess in the "in table body" insertion mode...
5448                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5449                }                }
5450    
5451                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5452                while (not {                  ## have an element in table scope
5453                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5454                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5455                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5456                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5457                        !!!cp ('t214');
5458                        $i = $_;
5459                        last INSCOPE;
5460                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5461                        !!!cp ('t215');
5462                        last INSCOPE;
5463                      }
5464                    } # INSCOPE
5465                    unless (defined $i) {
5466                      !!!cp ('t216');
5467    ## TODO: This erorr type is wrong.
5468                      !!!parse-error (type => 'unmatched end tag',
5469                                      text => $token->{tag_name}, token => $token);
5470                      ## Ignore the token
5471                      !!!nack ('t216.1');
5472                      !!!next-token;
5473                      next B;
5474                    }
5475    
5476                    ## Clear back to table body context
5477                    while (not ($self->{open_elements}->[-1]->[1]
5478                                    & TABLE_ROWS_SCOPING_EL)) {
5479                      !!!cp ('t217');
5480                      ## ISSUE: Can this state be reached?
5481                      pop @{$self->{open_elements}};
5482                    }
5483                    
5484                    ## As if <{current node}>
5485                    ## have an element in table scope
5486                    ## true by definition
5487                    
5488                    ## Clear back to table body context
5489                    ## nop by definition
5490                    
5491                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5492                    $self->{insertion_mode} = IN_TABLE_IM;
5493                    ## reprocess in "in table" insertion mode...
5494                  } else {
5495                    !!!cp ('t218');
5496                }                }
5497    
5498                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5499                ## have an element in table scope                  ## Clear back to table context
5500                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5501                                    & TABLE_SCOPING_EL)) {
5502                ## Clear back to table body context                    !!!cp ('t219');
5503                ## nop by definition                    ## ISSUE: Can this state be reached?
5504                      pop @{$self->{open_elements}};
5505                pop @{$self->{open_elements}};                  }
5506                $self->{insertion_mode} = 'in table';                  
5507                ## reprocess                  !!!insert-element ('colgroup',, $token);
5508                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5509                    ## reprocess
5510                    !!!ack-later;
5511                    next B;
5512                  } elsif ({
5513                            caption => 1,
5514                            colgroup => 1,
5515                            tbody => 1, tfoot => 1, thead => 1,
5516                           }->{$token->{tag_name}}) {
5517                    ## Clear back to table context
5518                    while (not ($self->{open_elements}->[-1]->[1]
5519                                    & TABLE_SCOPING_EL)) {
5520                      !!!cp ('t220');
5521                      ## ISSUE: Can this state be reached?
5522                      pop @{$self->{open_elements}};
5523                    }
5524                    
5525                    push @$active_formatting_elements, ['#marker', '']
5526                        if $token->{tag_name} eq 'caption';
5527                    
5528                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5529                    $self->{insertion_mode} = {
5530                                               caption => IN_CAPTION_IM,
5531                                               colgroup => IN_COLUMN_GROUP_IM,
5532                                               tbody => IN_TABLE_BODY_IM,
5533                                               tfoot => IN_TABLE_BODY_IM,
5534                                               thead => IN_TABLE_BODY_IM,
5535                                              }->{$token->{tag_name}};
5536                    !!!next-token;
5537                    !!!nack ('t220.1');
5538                    next B;
5539                  } else {
5540                    die "$0: in table: <>: $token->{tag_name}";
5541                  }
5542              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5543                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5544                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5545                                      ->manakai_local_name,
5546                                  token => $token);
5547    
5548                ## As if </table>                ## As if </table>
5549                ## have a table element in table scope                ## have a table element in table scope
5550                my $i;                my $i;
5551                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5552                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5553                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5554                      !!!cp ('t221');
5555                    $i = $_;                    $i = $_;
5556                    last INSCOPE;                    last INSCOPE;
5557                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5558                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5559                    last INSCOPE;                    last INSCOPE;
5560                  }                  }
5561                } # INSCOPE                } # INSCOPE
5562                unless (defined $i) {                unless (defined $i) {
5563                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5564    ## TODO: The following is wrong, maybe.
5565                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5566                                    token => $token);
5567                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5568                    !!!nack ('t223.1');
5569                  !!!next-token;                  !!!next-token;
5570                  redo B;                  next B;
5571                }                }
5572                                
5573    ## TODO: Followings are removed from the latest spec.
5574                ## generate implied end tags                ## generate implied end tags
5575                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5576                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5577                     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;  
5578                }                }
5579    
5580                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5581                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5582                    ## NOTE: |<table><tr><table>|
5583                    !!!parse-error (type => 'not closed',
5584                                    text => $self->{open_elements}->[-1]->[0]
5585                                        ->manakai_local_name,
5586                                    token => $token);
5587                  } else {
5588                    !!!cp ('t226');
5589                }                }
5590    
5591                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5592                  pop @{$open_tables};
5593    
5594                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5595    
5596                ## reprocess            ## reprocess
5597                redo B;            !!!ack-later;
5598              } else {            next B;
5599                #          } elsif ($token->{tag_name} eq 'style') {
5600              }            if (not $open_tables->[-1]->[1]) { # tainted
5601            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5602              if ({              ## NOTE: This is a "as if in head" code clone.
5603                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5604                  }->{$token->{tag_name}}) {              next B;
5605                ## have an element in table scope            } else {
5606                my $i;              !!!cp ('t227.7');
5607                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5608                  my $node = $self->{open_elements}->[$_];            }
5609                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5610                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5611                    last INSCOPE;              !!!cp ('t227.6');
5612                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5613                            table => 1, html => 1,              $script_start_tag->();
5614                           }->{$node->[1]}) {              next B;
5615                    last INSCOPE;            } else {
5616                  }              !!!cp ('t227.5');
5617                } # INSCOPE              #
5618                unless (defined $i) {            }
5619                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5620                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5621                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5622                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5623                }                if ($type eq 'hidden') {
5624                    !!!cp ('t227.3');
5625                    !!!parse-error (type => 'in table',
5626                                    text => $token->{tag_name}, token => $token);
5627    
5628                ## 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}};  
               }  
5629    
5630                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;  
               }  
5631    
               ## 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]);  
5632                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
5633    
5634                pop @{$self->{open_elements}};                  !!!next-token;
5635                $self->{insertion_mode} = 'in table';                  !!!ack ('t227.2.1');
5636                ## reprocess                  next B;
5637                redo B;                } else {
5638              } elsif ({                  !!!cp ('t227.2');
5639                        body => 1, caption => 1, col => 1, colgroup => 1,                  #
5640                        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;  
5641              } else {              } else {
5642                  !!!cp ('t227.1');
5643                #                #
5644              }              }
5645            } else {            } else {
5646                !!!cp ('t227.4');
5647              #              #
5648            }            }
5649                      } else {
5650            ## As if in table            !!!cp ('t227');
5651            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5652            $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;  
               }  
             }  
5653    
5654              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table', text => $token->{tag_name},
5655                            token => $token);
5656    
5657              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5658              ## ISSUE: Spec says that "whenever a node would be inserted          #
5659              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5660              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5661              $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>  
5662                ## have an element in table scope                ## have an element in table scope
5663                my $i;                my $i;
5664                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5665                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5666                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5667                      !!!cp ('t228');
5668                    $i = $_;                    $i = $_;
5669                    last INSCOPE;                    last INSCOPE;
5670                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5671                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5672                    last INSCOPE;                    last INSCOPE;
5673                  }                  }
5674                } # INSCOPE                } # INSCOPE
5675                unless (defined $i) {                unless (defined $i) {
5676                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5677                    !!!parse-error (type => 'unmatched end tag',
5678                                    text => $token->{tag_name}, token => $token);
5679                  ## Ignore the token                  ## Ignore the token
5680                    !!!nack ('t230.1');
5681                  !!!next-token;                  !!!next-token;
5682                  redo B;                  next B;
5683                  } else {
5684                    !!!cp ('t232');
5685                }                }
5686    
5687                ## Clear back to table row context                ## Clear back to table row context
5688                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5689                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5690                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5691                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5692                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5693                }                }
5694    
5695                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5696                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5697                ## reprocess                !!!next-token;
5698                redo B;                !!!nack ('t231.1');
5699                  next B;
5700              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5701                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5702                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5703                    ## have an element in table scope
5704                ## As if </table>                  my $i;
5705                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5706                my $i;                    my $node = $self->{open_elements}->[$_];
5707                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5708                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5709                  if ($node->[1] eq 'table') {                      $i = $_;
5710                    $i = $_;                      last INSCOPE;
5711                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5712                  } elsif ({                      !!!cp ('t234');
5713                            table => 1, html => 1,                      last INSCOPE;
5714                           }->{$node->[1]}) {                    }
5715                    last INSCOPE;                  } # INSCOPE
5716                    unless (defined $i) {
5717                      !!!cp ('t235');
5718    ## TODO: The following is wrong.
5719                      !!!parse-error (type => 'unmatched end tag',
5720                                      text => $token->{type}, token => $token);
5721                      ## Ignore the token
5722                      !!!nack ('t236.1');
5723                      !!!next-token;
5724                      next B;
5725                  }                  }
5726                } # INSCOPE                  
5727                unless (defined $i) {                  ## Clear back to table row context
5728                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5729                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5730                  !!!next-token;                    !!!cp ('t236');
5731                  redo B;  ## ISSUE: Can this state be reached?
5732                }                    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;  
5733                  }                  }
5734                } # INSCOPE                  
5735                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5736                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5737                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5738                  !!!next-token;                }
5739                  redo B;  
5740                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5741                    ## have an element in table scope
5742                ## Clear back to table row context                  my $i;
5743                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5744                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5745                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5746                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5747                        $i = $_;
5748                        last INSCOPE;
5749                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5750                        !!!cp ('t238');
5751                        last INSCOPE;
5752                      }
5753                    } # INSCOPE
5754                    unless (defined $i) {
5755                      !!!cp ('t239');
5756                      !!!parse-error (type => 'unmatched end tag',
5757                                      text => $token->{tag_name}, token => $token);
5758                      ## Ignore the token
5759                      !!!nack ('t239.1');
5760                      !!!next-token;
5761                      next B;
5762                    }
5763                    
5764                    ## Clear back to table body context
5765                    while (not ($self->{open_elements}->[-1]->[1]
5766                                    & TABLE_ROWS_SCOPING_EL)) {
5767                      !!!cp ('t240');
5768                      pop @{$self->{open_elements}};
5769                    }
5770                    
5771                    ## As if <{current node}>
5772                    ## have an element in table scope
5773                    ## true by definition
5774                    
5775                    ## Clear back to table body context
5776                    ## nop by definition
5777                    
5778                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5779                    $self->{insertion_mode} = IN_TABLE_IM;
5780                    ## reprocess in the "in table" insertion mode...
5781                }                }
5782    
5783                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5784                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5785                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5786                redo B;                ## is synced with it.
5787              } elsif ($token->{tag_name} eq 'table') {  
5788                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5789                my $i;                my $i;
5790                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5791                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5792                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5793                      !!!cp ('t241');
5794                    $i = $_;                    $i = $_;
5795                    last INSCOPE;                    last INSCOPE;
5796                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5797                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5798                    last INSCOPE;                    last INSCOPE;
5799                  }                  }
5800                } # INSCOPE                } # INSCOPE
5801                unless (defined $i) {                unless (defined $i) {
5802                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5803                    !!!parse-error (type => 'unmatched end tag',
5804                                    text => $token->{tag_name}, token => $token);
5805                  ## Ignore the token                  ## Ignore the token
5806                    !!!nack ('t243.1');
5807                  !!!next-token;                  !!!next-token;
5808                  redo B;                  next B;
5809                }                }
5810                    
5811                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
5812                while (not {                pop @{$open_tables};
5813                  tr => 1, html => 1,                
5814                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5815                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5816                  pop @{$self->{open_elements}};                !!!next-token;
5817                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
5818              } elsif ({              } elsif ({
5819                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5820                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5821                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5822                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5823                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5824                  my $node = $self->{open_elements}->[$_];                  my $i;
5825                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5826                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5827                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5828                  } elsif ({                      !!!cp ('t247');
5829                            table => 1, html => 1,                      $i = $_;
5830                           }->{$node->[1]}) {                      last INSCOPE;
5831                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5832                        !!!cp ('t248');
5833                        last INSCOPE;
5834                      }
5835                    } # INSCOPE
5836                      unless (defined $i) {
5837                        !!!cp ('t249');
5838                        !!!parse-error (type => 'unmatched end tag',
5839                                        text => $token->{tag_name}, token => $token);
5840                        ## Ignore the token
5841                        !!!nack ('t249.1');
5842                        !!!next-token;
5843                        next B;
5844                      }
5845                    
5846                    ## As if </tr>
5847                    ## have an element in table scope
5848                    my $i;
5849                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5850                      my $node = $self->{open_elements}->[$_];
5851                      if ($node->[1] & TABLE_ROW_EL) {
5852                        !!!cp ('t250');
5853                        $i = $_;
5854                        last INSCOPE;
5855                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5856                        !!!cp ('t251');
5857                        last INSCOPE;
5858                      }
5859                    } # INSCOPE
5860                      unless (defined $i) {
5861                        !!!cp ('t252');
5862                        !!!parse-error (type => 'unmatched end tag',
5863                                        text => 'tr', token => $token);
5864                        ## Ignore the token
5865                        !!!nack ('t252.1');
5866                        !!!next-token;
5867                        next B;
5868                      }
5869                    
5870                    ## Clear back to table row context
5871                    while (not ($self->{open_elements}->[-1]->[1]
5872                                    & TABLE_ROW_SCOPING_EL)) {
5873                      !!!cp ('t253');
5874    ## ISSUE: Can this case be reached?
5875                      pop @{$self->{open_elements}};
5876                  }                  }
5877                } # INSCOPE                  
5878                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5879                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5880                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5881                }                }
5882    
               ## As if </tr>  
5883                ## have an element in table scope                ## have an element in table scope
5884                my $i;                my $i;
5885                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5886                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5887                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5888                      !!!cp ('t254');
5889                    $i = $_;                    $i = $_;
5890                    last INSCOPE;                    last INSCOPE;
5891                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5892                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5893                    last INSCOPE;                    last INSCOPE;
5894                  }                  }
5895                } # INSCOPE                } # INSCOPE
5896                unless (defined $i) {                unless (defined $i) {
5897                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5898                    !!!parse-error (type => 'unmatched end tag',
5899                                    text => $token->{tag_name}, token => $token);
5900                  ## Ignore the token                  ## Ignore the token
5901                    !!!nack ('t256.1');
5902                  !!!next-token;                  !!!next-token;
5903                  redo B;                  next B;
5904                }                }
5905    
5906                ## Clear back to table row context                ## Clear back to table body context
5907                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5908                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5909                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5910                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5911                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5912                }                }
5913    
5914                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5915                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5916                ## reprocess                !!!nack ('t257.1');
5917                redo B;                !!!next-token;
5918                  next B;
5919              } elsif ({              } elsif ({
5920                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5921                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5922                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5923                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5924                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5925                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5926                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
5927                !!!next-token;                            text => $token->{tag_name}, token => $token);
5928                redo B;            ## Ignore the token
5929              } else {            !!!nack ('t258.1');
5930                #             !!!next-token;
5931              }            next B;
5932            } else {          } else {
5933              #            !!!cp ('t259');
5934            }            !!!parse-error (type => 'in table:/',
5935                              text => $token->{tag_name}, token => $token);
5936    
5937            ## As if in table            $insert = $insert_to_foster;
5938            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5939            $in_body->($insert_to_foster);          }
5940            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5941          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5942            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5943              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5944              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5945                          #
5946              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5947              !!!cp ('t259.2');
5948              #
5949            }
5950    
5951              !!!next-token;          ## Stop parsing
5952              redo B;          last B;
5953            } elsif ($token->{type} eq 'comment') {        } else {
5954              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
5955              my $comment = $self->{document}->create_comment ($token->{data});        }
5956              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5957              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
5958              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5959            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5960              if ({                unless (length $token->{data}) {
5961                   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  
5962                  !!!next-token;                  !!!next-token;
5963                  redo B;                  next B;
5964                }                }
5965                }
5966                ## Close the cell              
5967                !!!back-token; # <?>              !!!cp ('t261');
5968                $token = {type => 'end tag', tag_name => $tn};              #
5969                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5970              } else {              if ($token->{tag_name} eq 'col') {
5971                  !!!cp ('t262');
5972                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5973                  pop @{$self->{open_elements}};
5974                  !!!ack ('t262.1');
5975                  !!!next-token;
5976                  next B;
5977                } else {
5978                  !!!cp ('t263');
5979                #                #
5980              }              }
5981            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5982              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5983                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5984                my $i;                  !!!cp ('t264');
5985                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
5986                  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});  
5987                  ## Ignore the token                  ## Ignore the token
5988                  !!!next-token;                  !!!next-token;
5989                  redo B;                  next B;
5990                }                } else {
5991                                  !!!cp ('t265');
5992                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5993                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5994                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5995                     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]);  
5996                }                }
5997                } elsif ($token->{tag_name} eq 'col') {
5998                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5999                  !!!parse-error (type => 'unmatched end tag',
6000                $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});  
6001                ## Ignore the token                ## Ignore the token
6002                !!!next-token;                !!!next-token;
6003                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;  
6004              } else {              } else {
6005                #                !!!cp ('t267');
6006                  #
6007              }              }
6008          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6009            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6010                @{$self->{open_elements}} == 1) { # redundant, maybe
6011              !!!cp ('t270.2');
6012              ## Stop parsing.
6013              last B;
6014            } else {
6015              ## NOTE: As if </colgroup>.
6016              !!!cp ('t270.1');
6017              pop @{$self->{open_elements}}; # colgroup
6018              $self->{insertion_mode} = IN_TABLE_IM;
6019              ## Reprocess.
6020              next B;
6021            }
6022          } else {
6023            die "$0: $token->{type}: Unknown token type";
6024          }
6025    
6026              ## As if </colgroup>
6027              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6028                !!!cp ('t269');
6029    ## TODO: Wrong error type?
6030                !!!parse-error (type => 'unmatched end tag',
6031                                text => 'colgroup', token => $token);
6032                ## Ignore the token
6033                !!!nack ('t269.1');
6034                !!!next-token;
6035                next B;
6036            } else {            } else {
6037              #              !!!cp ('t270');
6038                pop @{$self->{open_elements}}; # colgroup
6039                $self->{insertion_mode} = IN_TABLE_IM;
6040                !!!ack-later;
6041                ## reprocess
6042                next B;
6043              }
6044        } elsif ($self->{insertion_mode} & SELECT_IMS) {
6045          if ($token->{type} == CHARACTER_TOKEN) {
6046            !!!cp ('t271');
6047            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6048            !!!next-token;
6049            next B;
6050          } elsif ($token->{type} == START_TAG_TOKEN) {
6051            if ($token->{tag_name} eq 'option') {
6052              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6053                !!!cp ('t272');
6054                ## As if </option>
6055                pop @{$self->{open_elements}};
6056              } else {
6057                !!!cp ('t273');
6058            }            }
             
           $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}};  
               }  
6059    
6060                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6061                !!!next-token;            !!!nack ('t273.1');
6062                redo B;            !!!next-token;
6063              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6064                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6065                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6066                  pop @{$self->{open_elements}};              !!!cp ('t274');
6067                }              ## As if </option>
6068                pop @{$self->{open_elements}};
6069              } else {
6070                !!!cp ('t275');
6071              }
6072    
6073                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6074                  ## As if </optgroup>              !!!cp ('t276');
6075                  pop @{$self->{open_elements}};              ## As if </optgroup>
6076                }              pop @{$self->{open_elements}};
6077              } else {
6078                !!!cp ('t277');
6079              }
6080    
6081                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6082                !!!next-token;            !!!nack ('t277.1');
6083                redo B;            !!!next-token;
6084              } elsif ($token->{tag_name} eq 'select') {            next B;
6085                !!!parse-error (type => 'not closed:select');          } elsif ({
6086                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6087                ## have an element in table scope                   }->{$token->{tag_name}} or
6088                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6089                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6090                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6091                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6092                    $i = $_;                     tr => 1, td => 1, th => 1,
6093                    last INSCOPE;                    }->{$token->{tag_name}})) {
6094                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6095                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6096                           }->{$node->[1]}) {                            token => $token);
6097                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6098                  }            ## as if there were </select> (otherwise).
6099                } # INSCOPE            ## have an element in table scope
6100                unless (defined $i) {            my $i;
6101                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6102                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6103                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6104                  redo B;                !!!cp ('t278');
6105                }                $i = $_;
6106                  last INSCOPE;
6107                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6108                  !!!cp ('t279');
6109                  last INSCOPE;
6110                }
6111              } # INSCOPE
6112              unless (defined $i) {
6113                !!!cp ('t280');
6114                !!!parse-error (type => 'unmatched end tag',
6115                                text => 'select', token => $token);
6116                ## Ignore the token
6117                !!!nack ('t280.1');
6118                !!!next-token;
6119                next B;
6120              }
6121                                
6122                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6123              splice @{$self->{open_elements}}, $i;
6124    
6125                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6126    
6127                !!!next-token;            if ($token->{tag_name} eq 'select') {
6128                redo B;              !!!nack ('t281.2');
6129              } else {              !!!next-token;
6130                #              next B;
6131              } else {
6132                !!!cp ('t281.1');
6133                !!!ack-later;
6134                ## Reprocess the token.
6135                next B;
6136              }
6137            } else {
6138              !!!cp ('t282');
6139              !!!parse-error (type => 'in select',
6140                              text => $token->{tag_name}, token => $token);
6141              ## Ignore the token
6142              !!!nack ('t282.1');
6143              !!!next-token;
6144              next B;
6145            }
6146          } elsif ($token->{type} == END_TAG_TOKEN) {
6147            if ($token->{tag_name} eq 'optgroup') {
6148              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6149                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6150                !!!cp ('t283');
6151                ## As if </option>
6152                splice @{$self->{open_elements}}, -2;
6153              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6154                !!!cp ('t284');
6155                pop @{$self->{open_elements}};
6156              } else {
6157                !!!cp ('t285');
6158                !!!parse-error (type => 'unmatched end tag',
6159                                text => $token->{tag_name}, token => $token);
6160                ## Ignore the token
6161              }
6162              !!!nack ('t285.1');
6163              !!!next-token;
6164              next B;
6165            } elsif ($token->{tag_name} eq 'option') {
6166              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6167                !!!cp ('t286');
6168                pop @{$self->{open_elements}};
6169              } else {
6170                !!!cp ('t287');
6171                !!!parse-error (type => 'unmatched end tag',
6172                                text => $token->{tag_name}, token => $token);
6173                ## Ignore the token
6174              }
6175              !!!nack ('t287.1');
6176              !!!next-token;
6177              next B;
6178            } elsif ($token->{tag_name} eq 'select') {
6179              ## have an element in table scope
6180              my $i;
6181              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6182                my $node = $self->{open_elements}->[$_];
6183                if ($node->[1] & SELECT_EL) {
6184                  !!!cp ('t288');
6185                  $i = $_;
6186                  last INSCOPE;
6187                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6188                  !!!cp ('t289');
6189                  last INSCOPE;
6190              }              }
6191            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6192              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6193                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6194                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6195                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6196                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6197                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6198                  pop @{$self->{open_elements}};              !!!next-token;
6199                } else {              next B;
6200                  !!!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;  
               }  
6201                                
6202                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6203              splice @{$self->{open_elements}}, $i;
6204    
6205                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6206    
6207                !!!next-token;            !!!nack ('t291.1');
6208                redo B;            !!!next-token;
6209              } elsif ({            next B;
6210                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6211                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6212                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6213                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6214                                   }->{$token->{tag_name}}) {
6215                ## have an element in table scope  ## TODO: The following is wrong?
6216                my $i;            !!!parse-error (type => 'unmatched end tag',
6217                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;  
               }  
6218                                
6219                ## As if </select>            ## have an element in table scope
6220                ## have an element in table scope            my $i;
6221                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6222                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6223                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6224                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6225                    $i = $_;                $i = $_;
6226                    last INSCOPE;                last INSCOPE;
6227                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6228                            table => 1, html => 1,                !!!cp ('t293');
6229                           }->{$node->[1]}) {                last INSCOPE;
6230                    last INSCOPE;              }
6231                  }            } # INSCOPE
6232                } # INSCOPE            unless (defined $i) {
6233                unless (defined $i) {              !!!cp ('t294');
6234                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6235                  ## Ignore the </select> token              !!!nack ('t294.1');
6236                  !!!next-token; ## TODO: ok?              !!!next-token;
6237                  redo B;              next B;
6238                }            }
6239                                
6240                splice @{$self->{open_elements}}, $i;            ## As if </select>
6241              ## have an element in table scope
6242                $self->_reset_insertion_mode;            undef $i;
6243              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6244                ## reprocess              my $node = $self->{open_elements}->[$_];
6245                redo B;              if ($node->[1] & SELECT_EL) {
6246              } else {                !!!cp ('t295');
6247                #                $i = $_;
6248                  last INSCOPE;
6249                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6250    ## ISSUE: Can this state be reached?
6251                  !!!cp ('t296');
6252                  last INSCOPE;
6253              }              }
6254            } else {            } # INSCOPE
6255              #            unless (defined $i) {
6256                !!!cp ('t297');
6257    ## TODO: The following error type is correct?
6258                !!!parse-error (type => 'unmatched end tag',
6259                                text => 'select', token => $token);
6260                ## Ignore the </select> token
6261                !!!nack ('t297.1');
6262                !!!next-token; ## TODO: ok?
6263                next B;
6264            }            }
6265                  
6266              !!!cp ('t298');
6267              splice @{$self->{open_elements}}, $i;
6268    
6269            !!!parse-error (type => 'in select:'.$token->{tag_name});            $self->_reset_insertion_mode;
6270    
6271              !!!ack-later;
6272              ## reprocess
6273              next B;
6274            } else {
6275              !!!cp ('t299');
6276              !!!parse-error (type => 'in select:/',
6277                              text => $token->{tag_name}, token => $token);
6278            ## Ignore the token            ## Ignore the token
6279              !!!nack ('t299.3');
6280            !!!next-token;            !!!next-token;
6281            redo B;            next B;
6282          } elsif ($self->{insertion_mode} eq 'after body') {          }
6283            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6284              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6285                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6286                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6287                            !!!parse-error (type => 'in body:#eof', token => $token);
6288                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6289              !!!cp ('t299.2');
6290            }
6291    
6292                unless (length $token->{data}) {          ## Stop parsing.
6293                  !!!next-token;          last B;
6294                  redo B;        } else {
6295                }          die "$0: $token->{type}: Unknown token type";
6296              }        }
6297                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6298              #        if ($token->{type} == CHARACTER_TOKEN) {
6299              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6300            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6301              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6302              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6303                  
6304              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6305              
6306              unless (length $token->{data}) {
6307                !!!cp ('t300');
6308              !!!next-token;              !!!next-token;
6309              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});  
6310            }            }
6311            }
6312            
6313            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6314              !!!cp ('t301');
6315              !!!parse-error (type => 'after html:#text', token => $token);
6316    
6317            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6318            ## reprocess          } else {
6319            redo B;            !!!cp ('t302');
6320          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6321            if ($token->{type} eq 'character') {          
6322              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6323                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6324    
6325                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6326                  !!!next-token;          ## reprocess
6327                  redo B;          next B;
6328                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6329              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6330              !!!cp ('t303');
6331              !!!parse-error (type => 'after html',
6332                              text => $token->{tag_name}, token => $token);
6333              
6334              ## Reprocess in the "after body" insertion mode.
6335            } else {
6336              !!!cp ('t304');
6337            }
6338    
6339              #          ## "after body" insertion mode
6340            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6341              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6342              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6343            $self->{insertion_mode} = IN_BODY_IM;
6344            !!!ack-later;
6345            ## reprocess
6346            next B;
6347          } elsif ($token->{type} == END_TAG_TOKEN) {
6348            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6349              !!!cp ('t305');
6350              !!!parse-error (type => 'after html:/',
6351                              text => $token->{tag_name}, token => $token);
6352              
6353              $self->{insertion_mode} = AFTER_BODY_IM;
6354              ## Reprocess in the "after body" insertion mode.
6355            } else {
6356              !!!cp ('t306');
6357            }
6358    
6359            ## "after body" insertion mode
6360            if ($token->{tag_name} eq 'html') {
6361              if (defined $self->{inner_html_node}) {
6362                !!!cp ('t307');
6363                !!!parse-error (type => 'unmatched end tag',
6364                                text => 'html', token => $token);
6365                ## Ignore the token
6366              !!!next-token;              !!!next-token;
6367              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 {  
               #  
             }  
6368            } else {            } else {
6369              #              !!!cp ('t308');
6370                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6371                !!!next-token;
6372                next B;
6373              }
6374            } else {
6375              !!!cp ('t309');
6376              !!!parse-error (type => 'after body:/',
6377                              text => $token->{tag_name}, token => $token);
6378    
6379              $self->{insertion_mode} = IN_BODY_IM;
6380              ## reprocess
6381              next B;
6382            }
6383          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6384            !!!cp ('t309.2');
6385            ## Stop parsing
6386            last B;
6387          } else {
6388            die "$0: $token->{type}: Unknown token type";
6389          }
6390        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6391          if ($token->{type} == CHARACTER_TOKEN) {
6392            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6393              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6394              
6395              unless (length $token->{data}) {
6396                !!!cp ('t310');
6397                !!!next-token;
6398                next B;
6399              }
6400            }
6401            
6402            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6403              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6404                !!!cp ('t311');
6405                !!!parse-error (type => 'in frameset:#text', token => $token);
6406              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6407                !!!cp ('t312');
6408                !!!parse-error (type => 'after frameset:#text', token => $token);
6409              } else { # "after after frameset"
6410                !!!cp ('t313');
6411                !!!parse-error (type => 'after html:#text', token => $token);
6412            }            }
6413                        
6414            if (defined $token->{tag_name}) {            ## Ignore the token.
6415              !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if (length $token->{data}) {
6416                !!!cp ('t314');
6417                ## reprocess the rest of characters
6418            } else {            } else {
6419              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
6420                !!!next-token;
6421              }
6422              next B;
6423            }
6424            
6425            die qq[$0: Character "$token->{data}"];
6426          } elsif ($token->{type} == START_TAG_TOKEN) {
6427            if ($token->{tag_name} eq 'frameset' and
6428                $self->{insertion_mode} == IN_FRAMESET_IM) {
6429              !!!cp ('t318');
6430              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6431              !!!nack ('t318.1');
6432              !!!next-token;
6433              next B;
6434            } elsif ($token->{tag_name} eq 'frame' and
6435                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6436              !!!cp ('t319');
6437              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6438              pop @{$self->{open_elements}};
6439              !!!ack ('t319.1');
6440              !!!next-token;
6441              next B;
6442            } elsif ($token->{tag_name} eq 'noframes') {
6443              !!!cp ('t320');
6444              ## NOTE: As if in head.
6445              $parse_rcdata->(CDATA_CONTENT_MODEL);
6446              next B;
6447    
6448              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6449              ## has no parse error.
6450            } else {
6451              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6452                !!!cp ('t321');
6453                !!!parse-error (type => 'in frameset',
6454                                text => $token->{tag_name}, token => $token);
6455              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6456                !!!cp ('t322');
6457                !!!parse-error (type => 'after frameset',
6458                                text => $token->{tag_name}, token => $token);
6459              } else { # "after after frameset"
6460                !!!cp ('t322.2');
6461                !!!parse-error (type => 'after after frameset',
6462                                text => $token->{tag_name}, token => $token);
6463            }            }
6464            ## Ignore the token            ## Ignore the token
6465              !!!nack ('t322.1');
6466            !!!next-token;            !!!next-token;
6467            redo B;            next B;
6468          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6469            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6470              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'frameset' and
6471                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{insertion_mode} == IN_FRAMESET_IM) {
6472              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6473                  @{$self->{open_elements}} == 1) {
6474                !!!cp ('t325');
6475                !!!parse-error (type => 'unmatched end tag',
6476                                text => $token->{tag_name}, token => $token);
6477                ## Ignore the token
6478                !!!next-token;
6479              } else {
6480                !!!cp ('t326');
6481                pop @{$self->{open_elements}};
6482                !!!next-token;
6483              }
6484    
6485                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
6486                  !!!next-token;                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6487                  redo B;              !!!cp ('t327');
6488                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6489              } else {
6490                !!!cp ('t328');
6491              }
6492              next B;
6493            } elsif ($token->{tag_name} eq 'html' and
6494                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6495              !!!cp ('t329');
6496              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6497              !!!next-token;
6498              next B;
6499            } else {
6500              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6501                !!!cp ('t330');
6502                !!!parse-error (type => 'in frameset:/',
6503                                text => $token->{tag_name}, token => $token);
6504              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6505                !!!cp ('t330.1');
6506                !!!parse-error (type => 'after frameset:/',
6507                                text => $token->{tag_name}, token => $token);
6508              } else { # "after after html"
6509                !!!cp ('t331');
6510                !!!parse-error (type => 'after after frameset:/',
6511                                text => $token->{tag_name}, token => $token);
6512              }
6513              ## Ignore the token
6514              !!!next-token;
6515              next B;
6516            }
6517          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6518            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6519                    @{$self->{open_elements}} == 1) { # redundant, maybe
6520              !!!cp ('t331.1');
6521              !!!parse-error (type => 'in body:#eof', token => $token);
6522            } else {
6523              !!!cp ('t331.2');
6524            }
6525            
6526            ## Stop parsing
6527            last B;
6528          } else {
6529            die "$0: $token->{type}: Unknown token type";
6530          }
6531    
6532          ## ISSUE: An issue in spec here
6533        } else {
6534          die "$0: $self->{insertion_mode}: Unknown insertion mode";
6535        }
6536    
6537        ## "in body" insertion mode
6538        if ($token->{type} == START_TAG_TOKEN) {
6539          if ($token->{tag_name} eq 'script') {
6540            !!!cp ('t332');
6541            ## NOTE: This is an "as if in head" code clone
6542            $script_start_tag->();
6543            next B;
6544          } elsif ($token->{tag_name} eq 'style') {
6545            !!!cp ('t333');
6546            ## NOTE: This is an "as if in head" code clone
6547            $parse_rcdata->(CDATA_CONTENT_MODEL);
6548            next B;
6549          } elsif ({
6550                    base => 1, link => 1,
6551                   }->{$token->{tag_name}}) {
6552            !!!cp ('t334');
6553            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6554            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6555            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6556            !!!ack ('t334.1');
6557            !!!next-token;
6558            next B;
6559          } elsif ($token->{tag_name} eq 'meta') {
6560            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6561            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6562            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6563    
6564            unless ($self->{confident}) {
6565              if ($token->{attributes}->{charset}) {
6566                !!!cp ('t335');
6567                ## NOTE: Whether the encoding is supported or not is handled
6568                ## in the {change_encoding} callback.
6569                $self->{change_encoding}
6570                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6571                
6572                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6573                    ->set_user_data (manakai_has_reference =>
6574                                         $token->{attributes}->{charset}
6575                                             ->{has_reference});
6576              } elsif ($token->{attributes}->{content}) {
6577                if ($token->{attributes}->{content}->{value}
6578                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6579                        [\x09-\x0D\x20]*=
6580                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6581                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6582                  !!!cp ('t336');
6583                  ## NOTE: Whether the encoding is supported or not is handled
6584                  ## in the {change_encoding} callback.
6585                  $self->{change_encoding}
6586                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6587                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6588                      ->set_user_data (manakai_has_reference =>
6589                                           $token->{attributes}->{content}
6590                                                 ->{has_reference});
6591              }              }
6592              }
6593            } else {
6594              if ($token->{attributes}->{charset}) {
6595                !!!cp ('t337');
6596                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6597                    ->set_user_data (manakai_has_reference =>
6598                                         $token->{attributes}->{charset}
6599                                             ->{has_reference});
6600              }
6601              if ($token->{attributes}->{content}) {
6602                !!!cp ('t338');
6603                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6604                    ->set_user_data (manakai_has_reference =>
6605                                         $token->{attributes}->{content}
6606                                             ->{has_reference});
6607              }
6608            }
6609    
6610              #          !!!ack ('t338.1');
6611            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6612              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6613              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6614              !!!next-token;          !!!cp ('t341');
6615              redo B;          ## NOTE: This is an "as if in head" code clone
6616            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6617              if ($token->{tag_name} eq 'noframes') {          next B;
6618                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6619                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6620              } else {                
6621                #          if (@{$self->{open_elements}} == 1 or
6622                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6623              !!!cp ('t342');
6624              ## Ignore the token
6625            } else {
6626              my $body_el = $self->{open_elements}->[1]->[0];
6627              for my $attr_name (keys %{$token->{attributes}}) {
6628                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6629                  !!!cp ('t343');
6630                  $body_el->set_attribute_ns
6631                    (undef, [undef, $attr_name],
6632                     $token->{attributes}->{$attr_name}->{value});
6633              }              }
6634            } elsif ($token->{type} eq 'end tag') {            }
6635              if ($token->{tag_name} eq 'html') {          }
6636                $phase = 'trailing end';          !!!nack ('t343.1');
6637            !!!next-token;
6638            next B;
6639          } elsif ({
6640                    address => 1, blockquote => 1, center => 1, dir => 1,
6641                    div => 1, dl => 1, fieldset => 1,
6642                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6643                    menu => 1, ol => 1, p => 1, ul => 1,
6644                    pre => 1, listing => 1,
6645                    form => 1,
6646                    table => 1,
6647                    hr => 1,
6648                   }->{$token->{tag_name}}) {
6649            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6650              !!!cp ('t350');
6651              !!!parse-error (type => 'in form:form', token => $token);
6652              ## Ignore the token
6653              !!!nack ('t350.1');
6654              !!!next-token;
6655              next B;
6656            }
6657    
6658            ## has a p element in scope
6659            INSCOPE: for (reverse @{$self->{open_elements}}) {
6660              if ($_->[1] & P_EL) {
6661                !!!cp ('t344');
6662                !!!back-token; # <form>
6663                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6664                          line => $token->{line}, column => $token->{column}};
6665                next B;
6666              } elsif ($_->[1] & SCOPING_EL) {
6667                !!!cp ('t345');
6668                last INSCOPE;
6669              }
6670            } # INSCOPE
6671              
6672            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6673            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6674              !!!nack ('t346.1');
6675              !!!next-token;
6676              if ($token->{type} == CHARACTER_TOKEN) {
6677                $token->{data} =~ s/^\x0A//;
6678                unless (length $token->{data}) {
6679                  !!!cp ('t346');
6680                !!!next-token;                !!!next-token;
               redo B;  
6681              } else {              } else {
6682                #                !!!cp ('t349');
6683              }              }
6684            } else {            } else {
6685              #              !!!cp ('t348');
6686              }
6687            } elsif ($token->{tag_name} eq 'form') {
6688              !!!cp ('t347.1');
6689              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6690    
6691              !!!nack ('t347.2');
6692              !!!next-token;
6693            } elsif ($token->{tag_name} eq 'table') {
6694              !!!cp ('t382');
6695              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6696              
6697              $self->{insertion_mode} = IN_TABLE_IM;
6698    
6699              !!!nack ('t382.1');
6700              !!!next-token;
6701            } elsif ($token->{tag_name} eq 'hr') {
6702              !!!cp ('t386');
6703              pop @{$self->{open_elements}};
6704            
6705              !!!nack ('t386.1');
6706              !!!next-token;
6707            } else {
6708              !!!nack ('t347.1');
6709              !!!next-token;
6710            }
6711            next B;
6712          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6713            ## has a p element in scope
6714            INSCOPE: for (reverse @{$self->{open_elements}}) {
6715              if ($_->[1] & P_EL) {
6716                !!!cp ('t353');
6717                !!!back-token; # <x>
6718                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6719                          line => $token->{line}, column => $token->{column}};
6720                next B;
6721              } elsif ($_->[1] & SCOPING_EL) {
6722                !!!cp ('t354');
6723                last INSCOPE;
6724            }            }
6725            } # INSCOPE
6726                        
6727            if (defined $token->{tag_name}) {          ## Step 1
6728              !!!parse-error (type => 'after frameset:'.$token->{tag_name});          my $i = -1;
6729            my $node = $self->{open_elements}->[$i];
6730            my $li_or_dtdd = {li => {li => 1},
6731                              dt => {dt => 1, dd => 1},
6732                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6733            LI: {
6734              ## Step 2
6735              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6736                if ($i != -1) {
6737                  !!!cp ('t355');
6738                  !!!parse-error (type => 'not closed',
6739                                  text => $self->{open_elements}->[-1]->[0]
6740                                      ->manakai_local_name,
6741                                  token => $token);
6742                } else {
6743                  !!!cp ('t356');
6744                }
6745                splice @{$self->{open_elements}}, $i;
6746                last LI;
6747            } else {            } else {
6748              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6749            }            }
6750              
6751              ## Step 3
6752              if (not ($node->[1] & FORMATTING_EL) and
6753                  #not $phrasing_category->{$node->[1]} and
6754                  ($node->[1] & SPECIAL_EL or
6755                   $node->[1] & SCOPING_EL) and
6756                  not ($node->[1] & ADDRESS_EL) and
6757                  not ($node->[1] & DIV_EL)) {
6758                !!!cp ('t358');
6759                last LI;
6760              }
6761              
6762              !!!cp ('t359');
6763              ## Step 4
6764              $i--;
6765              $node = $self->{open_elements}->[$i];
6766              redo LI;
6767            } # LI
6768              
6769            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6770            !!!nack ('t359.1');
6771            !!!next-token;
6772            next B;
6773          } elsif ($token->{tag_name} eq 'plaintext') {
6774            ## has a p element in scope
6775            INSCOPE: for (reverse @{$self->{open_elements}}) {
6776              if ($_->[1] & P_EL) {
6777                !!!cp ('t367');
6778                !!!back-token; # <plaintext>
6779                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6780                          line => $token->{line}, column => $token->{column}};
6781                next B;
6782              } elsif ($_->[1] & SCOPING_EL) {
6783                !!!cp ('t368');
6784                last INSCOPE;
6785              }
6786            } # INSCOPE
6787              
6788            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6789              
6790            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6791              
6792            !!!nack ('t368.1');
6793            !!!next-token;
6794            next B;
6795          } elsif ($token->{tag_name} eq 'a') {
6796            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6797              my $node = $active_formatting_elements->[$i];
6798              if ($node->[1] & A_EL) {
6799                !!!cp ('t371');
6800                !!!parse-error (type => 'in a:a', token => $token);
6801                
6802                !!!back-token; # <a>
6803                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6804                          line => $token->{line}, column => $token->{column}};
6805                $formatting_end_tag->($token);
6806                
6807                AFE2: for (reverse 0..$#$active_formatting_elements) {
6808                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6809                    !!!cp ('t372');
6810                    splice @$active_formatting_elements, $_, 1;
6811                    last AFE2;
6812                  }
6813                } # AFE2
6814                OE: for (reverse 0..$#{$self->{open_elements}}) {
6815                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6816                    !!!cp ('t373');
6817                    splice @{$self->{open_elements}}, $_, 1;
6818                    last OE;
6819                  }
6820                } # OE
6821                last AFE;
6822              } elsif ($node->[0] eq '#marker') {
6823                !!!cp ('t374');
6824                last AFE;
6825              }
6826            } # AFE
6827              
6828            $reconstruct_active_formatting_elements->($insert_to_current);
6829    
6830            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6831            push @$active_formatting_elements, $self->{open_elements}->[-1];
6832    
6833            !!!nack ('t374.1');
6834            !!!next-token;
6835            next B;
6836          } elsif ($token->{tag_name} eq 'nobr') {
6837            $reconstruct_active_formatting_elements->($insert_to_current);
6838    
6839            ## has a |nobr| element in scope
6840            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6841              my $node = $self->{open_elements}->[$_];
6842              if ($node->[1] & NOBR_EL) {
6843                !!!cp ('t376');
6844                !!!parse-error (type => 'in nobr:nobr', token => $token);
6845                !!!back-token; # <nobr>
6846                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6847                          line => $token->{line}, column => $token->{column}};
6848                next B;
6849              } elsif ($node->[1] & SCOPING_EL) {
6850                !!!cp ('t377');
6851                last INSCOPE;
6852              }
6853            } # INSCOPE
6854            
6855            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6856            push @$active_formatting_elements, $self->{open_elements}->[-1];
6857            
6858            !!!nack ('t377.1');
6859            !!!next-token;
6860            next B;
6861          } elsif ($token->{tag_name} eq 'button') {
6862            ## has a button element in scope
6863            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6864              my $node = $self->{open_elements}->[$_];
6865              if ($node->[1] & BUTTON_EL) {
6866                !!!cp ('t378');
6867                !!!parse-error (type => 'in button:button', token => $token);
6868                !!!back-token; # <button>
6869                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6870                          line => $token->{line}, column => $token->{column}};
6871                next B;
6872              } elsif ($node->[1] & SCOPING_EL) {
6873                !!!cp ('t379');
6874                last INSCOPE;
6875              }
6876            } # INSCOPE
6877              
6878            $reconstruct_active_formatting_elements->($insert_to_current);
6879              
6880            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6881    
6882            ## TODO: associate with $self->{form_element} if defined
6883    
6884            push @$active_formatting_elements, ['#marker', ''];
6885    
6886            !!!nack ('t379.1');
6887            !!!next-token;
6888            next B;
6889          } elsif ({
6890                    xmp => 1,
6891                    iframe => 1,
6892                    noembed => 1,
6893                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6894                    noscript => 0, ## TODO: 1 if scripting is enabled
6895                   }->{$token->{tag_name}}) {
6896            if ($token->{tag_name} eq 'xmp') {
6897              !!!cp ('t381');
6898              $reconstruct_active_formatting_elements->($insert_to_current);
6899            } else {
6900              !!!cp ('t399');
6901            }
6902            ## NOTE: There is an "as if in body" code clone.
6903            $parse_rcdata->(CDATA_CONTENT_MODEL);
6904            next B;
6905          } elsif ($token->{tag_name} eq 'isindex') {
6906            !!!parse-error (type => 'isindex', token => $token);
6907            
6908            if (defined $self->{form_element}) {
6909              !!!cp ('t389');
6910            ## Ignore the token            ## Ignore the token
6911              !!!nack ('t389'); ## NOTE: Not acknowledged.
6912            !!!next-token;            !!!next-token;
6913            redo B;            next B;
6914            } else {
6915              !!!ack ('t391.1');
6916    
6917            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
6918              my $form_attrs;
6919              $form_attrs->{action} = $at->{action} if $at->{action};
6920              my $prompt_attr = $at->{prompt};
6921              $at->{name} = {name => 'name', value => 'isindex'};
6922              delete $at->{action};
6923              delete $at->{prompt};
6924              my @tokens = (
6925                            {type => START_TAG_TOKEN, tag_name => 'form',
6926                             attributes => $form_attrs,
6927                             line => $token->{line}, column => $token->{column}},
6928                            {type => START_TAG_TOKEN, tag_name => 'hr',
6929                             line => $token->{line}, column => $token->{column}},
6930                            {type => START_TAG_TOKEN, tag_name => 'p',
6931                             line => $token->{line}, column => $token->{column}},
6932                            {type => START_TAG_TOKEN, tag_name => 'label',
6933                             line => $token->{line}, column => $token->{column}},
6934                           );
6935              if ($prompt_attr) {
6936                !!!cp ('t390');
6937                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6938                               #line => $token->{line}, column => $token->{column},
6939                              };
6940              } else {
6941                !!!cp ('t391');
6942                push @tokens, {type => CHARACTER_TOKEN,
6943                               data => 'This is a searchable index. Insert your search keywords here: ',
6944                               #line => $token->{line}, column => $token->{column},
6945                              }; # SHOULD
6946                ## TODO: make this configurable
6947              }
6948              push @tokens,
6949                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6950                             line => $token->{line}, column => $token->{column}},
6951                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6952                            {type => END_TAG_TOKEN, tag_name => 'label',
6953                             line => $token->{line}, column => $token->{column}},
6954                            {type => END_TAG_TOKEN, tag_name => 'p',
6955                             line => $token->{line}, column => $token->{column}},
6956                            {type => START_TAG_TOKEN, tag_name => 'hr',
6957                             line => $token->{line}, column => $token->{column}},
6958                            {type => END_TAG_TOKEN, tag_name => 'form',
6959                             line => $token->{line}, column => $token->{column}};
6960              !!!back-token (@tokens);
6961              !!!next-token;
6962              next B;
6963            }
6964          } elsif ($token->{tag_name} eq 'textarea') {
6965            my $tag_name = $token->{tag_name};
6966            my $el;
6967            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6968            
6969            ## TODO: $self->{form_element} if defined
6970            $self->{content_model} = RCDATA_CONTENT_MODEL;
6971            delete $self->{escape}; # MUST
6972            
6973            $insert->($el);
6974            
6975            my $text = '';
6976            !!!nack ('t392.1');
6977            !!!next-token;
6978            if ($token->{type} == CHARACTER_TOKEN) {
6979              $token->{data} =~ s/^\x0A//;
6980              unless (length $token->{data}) {
6981                !!!cp ('t392');
6982                !!!next-token;
6983              } else {
6984                !!!cp ('t393');
6985              }
6986          } else {          } else {
6987            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
6988            }
6989            while ($token->{type} == CHARACTER_TOKEN) {
6990              !!!cp ('t395');
6991              $text .= $token->{data};
6992              !!!next-token;
6993            }
6994            if (length $text) {
6995              !!!cp ('t396');
6996              $el->manakai_append_text ($text);
6997            }
6998            
6999            $self->{content_model} = PCDATA_CONTENT_MODEL;
7000            
7001            if ($token->{type} == END_TAG_TOKEN and
7002                $token->{tag_name} eq $tag_name) {
7003              !!!cp ('t397');
7004              ## Ignore the token
7005            } else {
7006              !!!cp ('t398');
7007              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7008          }          }
       }  
     } 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  
7009          !!!next-token;          !!!next-token;
7010          redo B;          next B;
7011        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
7012          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
7013          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
7014            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7015              my $node = $self->{open_elements}->[$_];
7016              if ($node->[1] & RUBY_EL) {
7017                !!!cp ('t398.1');
7018                ## generate implied end tags
7019                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7020                  !!!cp ('t398.2');
7021                  pop @{$self->{open_elements}};
7022                }
7023                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7024                  !!!cp ('t398.3');
7025                  !!!parse-error (type => 'not closed',
7026                                  text => $self->{open_elements}->[-1]->[0]
7027                                      ->manakai_local_name,
7028                                  token => $token);
7029                  pop @{$self->{open_elements}}
7030                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7031                }
7032                last INSCOPE;
7033              } elsif ($node->[1] & SCOPING_EL) {
7034                !!!cp ('t398.4');
7035                last INSCOPE;
7036              }
7037            } # INSCOPE
7038    
7039            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7040    
7041            !!!nack ('t398.5');
7042          !!!next-token;          !!!next-token;
7043          redo B;          redo B;
7044        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
7045          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
7046            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
7047            ## As if in the main phase.  
7048            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7049            ## just before the phase has been changed to the trailing  
7050            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7051            $reconstruct_active_formatting_elements->($insert_to_current)  
7052              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
7053            
7054            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7055            
7056            if ($self->{self_closing}) {
7057              pop @{$self->{open_elements}};
7058              !!!ack ('t398.1');
7059            } else {
7060              !!!cp ('t398.2');
7061              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7062              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7063              ## mode, "in body" (not "in foreign content") secondary insertion
7064              ## mode, maybe.
7065            }
7066    
7067            !!!next-token;
7068            next B;
7069          } elsif ({
7070                    caption => 1, col => 1, colgroup => 1, frame => 1,
7071                    frameset => 1, head => 1, option => 1, optgroup => 1,
7072                    tbody => 1, td => 1, tfoot => 1, th => 1,
7073                    thead => 1, tr => 1,
7074                   }->{$token->{tag_name}}) {
7075            !!!cp ('t401');
7076            !!!parse-error (type => 'in body',
7077                            text => $token->{tag_name}, token => $token);
7078            ## Ignore the token
7079            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7080            !!!next-token;
7081            next B;
7082            
7083            ## ISSUE: An issue on HTML5 new elements in the spec.
7084          } else {
7085            if ($token->{tag_name} eq 'image') {
7086              !!!cp ('t384');
7087              !!!parse-error (type => 'image', token => $token);
7088              $token->{tag_name} = 'img';
7089            } else {
7090              !!!cp ('t385');
7091            }
7092    
7093            ## NOTE: There is an "as if <br>" code clone.
7094            $reconstruct_active_formatting_elements->($insert_to_current);
7095            
7096            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7097    
7098            if ({
7099                 applet => 1, marquee => 1, object => 1,
7100                }->{$token->{tag_name}}) {
7101              !!!cp ('t380');
7102              push @$active_formatting_elements, ['#marker', ''];
7103              !!!nack ('t380.1');
7104            } elsif ({
7105                      b => 1, big => 1, em => 1, font => 1, i => 1,
7106                      s => 1, small => 1, strile => 1,
7107                      strong => 1, tt => 1, u => 1,
7108                     }->{$token->{tag_name}}) {
7109              !!!cp ('t375');
7110              push @$active_formatting_elements, $self->{open_elements}->[-1];
7111              !!!nack ('t375.1');
7112            } elsif ($token->{tag_name} eq 'input') {
7113              !!!cp ('t388');
7114              ## TODO: associate with $self->{form_element} if defined
7115              pop @{$self->{open_elements}};
7116              !!!ack ('t388.2');
7117            } elsif ({
7118                      area => 1, basefont => 1, bgsound => 1, br => 1,
7119                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7120                      #image => 1,
7121                     }->{$token->{tag_name}}) {
7122              !!!cp ('t388.1');
7123              pop @{$self->{open_elements}};
7124              !!!ack ('t388.3');
7125            } elsif ($token->{tag_name} eq 'select') {
7126              ## TODO: associate with $self->{form_element} if defined
7127            
7128              if ($self->{insertion_mode} & TABLE_IMS or
7129                  $self->{insertion_mode} & BODY_TABLE_IMS or
7130                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7131                !!!cp ('t400.1');
7132                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7133              } else {
7134                !!!cp ('t400.2');
7135                $self->{insertion_mode} = IN_SELECT_IM;
7136              }
7137              !!!nack ('t400.3');
7138            } else {
7139              !!!nack ('t402');
7140            }
7141            
7142            !!!next-token;
7143            next B;
7144          }
7145        } elsif ($token->{type} == END_TAG_TOKEN) {
7146          if ($token->{tag_name} eq 'body') {
7147            ## has a |body| element in scope
7148            my $i;
7149            INSCOPE: {
7150              for (reverse @{$self->{open_elements}}) {
7151                if ($_->[1] & BODY_EL) {
7152                  !!!cp ('t405');
7153                  $i = $_;
7154                  last INSCOPE;
7155                } elsif ($_->[1] & SCOPING_EL) {
7156                  !!!cp ('t405.1');
7157                  last;
7158                }
7159              }
7160    
7161              !!!parse-error (type => 'start tag not allowed',
7162                              text => $token->{tag_name}, token => $token);
7163              ## NOTE: Ignore the token.
7164              !!!next-token;
7165              next B;
7166            } # INSCOPE
7167    
7168            for (@{$self->{open_elements}}) {
7169              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7170                !!!cp ('t403');
7171                !!!parse-error (type => 'not closed',
7172                                text => $_->[0]->manakai_local_name,
7173                                token => $token);
7174                last;
7175              } else {
7176                !!!cp ('t404');
7177              }
7178            }
7179    
7180            $self->{insertion_mode} = AFTER_BODY_IM;
7181            !!!next-token;
7182            next B;
7183          } elsif ($token->{tag_name} eq 'html') {
7184            ## TODO: Update this code.  It seems that the code below is not
7185            ## up-to-date, though it has same effect as speced.
7186            if (@{$self->{open_elements}} > 1 and
7187                $self->{open_elements}->[1]->[1] & BODY_EL) {
7188              ## ISSUE: There is an issue in the spec.
7189              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7190                !!!cp ('t406');
7191                !!!parse-error (type => 'not closed',
7192                                text => $self->{open_elements}->[1]->[0]
7193                                    ->manakai_local_name,
7194                                token => $token);
7195              } else {
7196                !!!cp ('t407');
7197              }
7198              $self->{insertion_mode} = AFTER_BODY_IM;
7199              ## reprocess
7200              next B;
7201            } else {
7202              !!!cp ('t408');
7203              !!!parse-error (type => 'unmatched end tag',
7204                              text => $token->{tag_name}, token => $token);
7205              ## Ignore the token
7206              !!!next-token;
7207              next B;
7208            }
7209          } elsif ({
7210                    address => 1, blockquote => 1, center => 1, dir => 1,
7211                    div => 1, dl => 1, fieldset => 1, listing => 1,
7212                    menu => 1, ol => 1, pre => 1, ul => 1,
7213                    dd => 1, dt => 1, li => 1,
7214                    applet => 1, button => 1, marquee => 1, object => 1,
7215                   }->{$token->{tag_name}}) {
7216            ## has an element in scope
7217            my $i;
7218            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7219              my $node = $self->{open_elements}->[$_];
7220              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7221                !!!cp ('t410');
7222                $i = $_;
7223                last INSCOPE;
7224              } elsif ($node->[1] & SCOPING_EL) {
7225                !!!cp ('t411');
7226                last INSCOPE;
7227              }
7228            } # INSCOPE
7229    
7230            unless (defined $i) { # has an element in scope
7231              !!!cp ('t413');
7232              !!!parse-error (type => 'unmatched end tag',
7233                              text => $token->{tag_name}, token => $token);
7234              ## NOTE: Ignore the token.
7235            } else {
7236              ## Step 1. generate implied end tags
7237              while ({
7238                      ## END_TAG_OPTIONAL_EL
7239                      dd => ($token->{tag_name} ne 'dd'),
7240                      dt => ($token->{tag_name} ne 'dt'),
7241                      li => ($token->{tag_name} ne 'li'),
7242                      p => 1,
7243                      rt => 1,
7244                      rp => 1,
7245                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7246                !!!cp ('t409');
7247                pop @{$self->{open_elements}};
7248              }
7249    
7250              ## Step 2.
7251              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7252                      ne $token->{tag_name}) {
7253                !!!cp ('t412');
7254                !!!parse-error (type => 'not closed',
7255                                text => $self->{open_elements}->[-1]->[0]
7256                                    ->manakai_local_name,
7257                                token => $token);
7258              } else {
7259                !!!cp ('t414');
7260              }
7261    
7262              ## Step 3.
7263              splice @{$self->{open_elements}}, $i;
7264    
7265              ## Step 4.
7266              $clear_up_to_marker->()
7267                  if {
7268                    applet => 1, button => 1, marquee => 1, object => 1,
7269                  }->{$token->{tag_name}};
7270            }
7271            !!!next-token;
7272            next B;
7273          } elsif ($token->{tag_name} eq 'form') {
7274            undef $self->{form_element};
7275    
7276            ## has an element in scope
7277            my $i;
7278            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7279              my $node = $self->{open_elements}->[$_];
7280              if ($node->[1] & FORM_EL) {
7281                !!!cp ('t418');
7282                $i = $_;
7283                last INSCOPE;
7284              } elsif ($node->[1] & SCOPING_EL) {
7285                !!!cp ('t419');
7286                last INSCOPE;
7287              }
7288            } # INSCOPE
7289    
7290            unless (defined $i) { # has an element in scope
7291              !!!cp ('t421');
7292              !!!parse-error (type => 'unmatched end tag',
7293                              text => $token->{tag_name}, token => $token);
7294              ## NOTE: Ignore the token.
7295            } else {
7296              ## Step 1. generate implied end tags
7297              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7298                !!!cp ('t417');
7299                pop @{$self->{open_elements}};
7300              }
7301                        
7302            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7303              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7304                      ne $token->{tag_name}) {
7305                !!!cp ('t417.1');
7306                !!!parse-error (type => 'not closed',
7307                                text => $self->{open_elements}->[-1]->[0]
7308                                    ->manakai_local_name,
7309                                token => $token);
7310              } else {
7311                !!!cp ('t420');
7312              }  
7313                        
7314            unless (length $token->{data}) {            ## Step 3.
7315              !!!next-token;            splice @{$self->{open_elements}}, $i;
7316              redo B;          }
7317    
7318            !!!next-token;
7319            next B;
7320          } elsif ({
7321                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7322                   }->{$token->{tag_name}}) {
7323            ## has an element in scope
7324            my $i;
7325            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7326              my $node = $self->{open_elements}->[$_];
7327              if ($node->[1] & HEADING_EL) {
7328                !!!cp ('t423');
7329                $i = $_;
7330                last INSCOPE;
7331              } elsif ($node->[1] & SCOPING_EL) {
7332                !!!cp ('t424');
7333                last INSCOPE;
7334            }            }
7335            } # INSCOPE
7336    
7337            unless (defined $i) { # has an element in scope
7338              !!!cp ('t425.1');
7339              !!!parse-error (type => 'unmatched end tag',
7340                              text => $token->{tag_name}, token => $token);
7341              ## NOTE: Ignore the token.
7342            } else {
7343              ## Step 1. generate implied end tags
7344              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7345                !!!cp ('t422');
7346                pop @{$self->{open_elements}};
7347              }
7348              
7349              ## Step 2.
7350              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7351                      ne $token->{tag_name}) {
7352                !!!cp ('t425');
7353                !!!parse-error (type => 'unmatched end tag',
7354                                text => $token->{tag_name}, token => $token);
7355              } else {
7356                !!!cp ('t426');
7357              }
7358    
7359              ## Step 3.
7360              splice @{$self->{open_elements}}, $i;
7361          }          }
7362            
7363            !!!next-token;
7364            next B;
7365          } elsif ($token->{tag_name} eq 'p') {
7366            ## has an element in scope
7367            my $i;
7368            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7369              my $node = $self->{open_elements}->[$_];
7370              if ($node->[1] & P_EL) {
7371                !!!cp ('t410.1');
7372                $i = $_;
7373                last INSCOPE;
7374              } elsif ($node->[1] & SCOPING_EL) {
7375                !!!cp ('t411.1');
7376                last INSCOPE;
7377              }
7378            } # INSCOPE
7379    
7380          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7381          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7382          ## reprocess                    ne $token->{tag_name}) {
7383          redo B;              !!!cp ('t412.1');
7384        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7385                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7386          !!!parse-error (type => 'after html:'.$token->{tag_name});                                  ->manakai_local_name,
7387          $phase = 'main';                              token => $token);
7388          ## reprocess            } else {
7389          redo B;              !!!cp ('t414.1');
7390        } elsif ($token->{type} eq 'end-of-file') {            }
7391          ## Stop parsing  
7392          last B;            splice @{$self->{open_elements}}, $i;
7393            } else {
7394              !!!cp ('t413.1');
7395              !!!parse-error (type => 'unmatched end tag',
7396                              text => $token->{tag_name}, token => $token);
7397    
7398              !!!cp ('t415.1');
7399              ## As if <p>, then reprocess the current token
7400              my $el;
7401              !!!create-element ($el, $HTML_NS, 'p',, $token);
7402              $insert->($el);
7403              ## NOTE: Not inserted into |$self->{open_elements}|.
7404            }
7405    
7406            !!!next-token;
7407            next B;
7408          } elsif ({
7409                    a => 1,
7410                    b => 1, big => 1, em => 1, font => 1, i => 1,
7411                    nobr => 1, s => 1, small => 1, strile => 1,
7412                    strong => 1, tt => 1, u => 1,
7413                   }->{$token->{tag_name}}) {
7414            !!!cp ('t427');
7415            $formatting_end_tag->($token);
7416            next B;
7417          } elsif ($token->{tag_name} eq 'br') {
7418            !!!cp ('t428');
7419            !!!parse-error (type => 'unmatched end tag',
7420                            text => 'br', token => $token);
7421    
7422            ## As if <br>
7423            $reconstruct_active_formatting_elements->($insert_to_current);
7424            
7425            my $el;
7426            !!!create-element ($el, $HTML_NS, 'br',, $token);
7427            $insert->($el);
7428            
7429            ## Ignore the token.
7430            !!!next-token;
7431            next B;
7432          } elsif ({
7433                    caption => 1, col => 1, colgroup => 1, frame => 1,
7434                    frameset => 1, head => 1, option => 1, optgroup => 1,
7435                    tbody => 1, td => 1, tfoot => 1, th => 1,
7436                    thead => 1, tr => 1,
7437                    area => 1, basefont => 1, bgsound => 1,
7438                    embed => 1, hr => 1, iframe => 1, image => 1,
7439                    img => 1, input => 1, isindex => 1, noembed => 1,
7440                    noframes => 1, param => 1, select => 1, spacer => 1,
7441                    table => 1, textarea => 1, wbr => 1,
7442                    noscript => 0, ## TODO: if scripting is enabled
7443                   }->{$token->{tag_name}}) {
7444            !!!cp ('t429');
7445            !!!parse-error (type => 'unmatched end tag',
7446                            text => $token->{tag_name}, token => $token);
7447            ## Ignore the token
7448            !!!next-token;
7449            next B;
7450            
7451            ## ISSUE: Issue on HTML5 new elements in spec
7452            
7453        } else {        } else {
7454          die "$0: $token->{type}: Unknown token";          ## Step 1
7455            my $node_i = -1;
7456            my $node = $self->{open_elements}->[$node_i];
7457    
7458            ## Step 2
7459            S2: {
7460              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7461                ## Step 1
7462                ## generate implied end tags
7463                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7464                  !!!cp ('t430');
7465                  ## NOTE: |<ruby><rt></ruby>|.
7466                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7467                  ## which seems wrong.
7468                  pop @{$self->{open_elements}};
7469                  $node_i++;
7470                }
7471            
7472                ## Step 2
7473                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7474                        ne $token->{tag_name}) {
7475                  !!!cp ('t431');
7476                  ## NOTE: <x><y></x>
7477                  !!!parse-error (type => 'not closed',
7478                                  text => $self->{open_elements}->[-1]->[0]
7479                                      ->manakai_local_name,
7480                                  token => $token);
7481                } else {
7482                  !!!cp ('t432');
7483                }
7484                
7485                ## Step 3
7486                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7487    
7488                !!!next-token;
7489                last S2;
7490              } else {
7491                ## Step 3
7492                if (not ($node->[1] & FORMATTING_EL) and
7493                    #not $phrasing_category->{$node->[1]} and
7494                    ($node->[1] & SPECIAL_EL or
7495                     $node->[1] & SCOPING_EL)) {
7496                  !!!cp ('t433');
7497                  !!!parse-error (type => 'unmatched end tag',
7498                                  text => $token->{tag_name}, token => $token);
7499                  ## Ignore the token
7500                  !!!next-token;
7501                  last S2;
7502                }
7503    
7504                !!!cp ('t434');
7505              }
7506              
7507              ## Step 4
7508              $node_i--;
7509              $node = $self->{open_elements}->[$node_i];
7510              
7511              ## Step 5;
7512              redo S2;
7513            } # S2
7514            next B;
7515        }        }
7516      }      }
7517        next B;
7518      } continue { # B
7519        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7520          ## NOTE: The code below is executed in cases where it does not have
7521          ## to be, but it it is harmless even in those cases.
7522          ## has an element in scope
7523          INSCOPE: {
7524            for (reverse 0..$#{$self->{open_elements}}) {
7525              my $node = $self->{open_elements}->[$_];
7526              if ($node->[1] & FOREIGN_EL) {
7527                last INSCOPE;
7528              } elsif ($node->[1] & SCOPING_EL) {
7529                last;
7530              }
7531            }
7532            
7533            ## NOTE: No foreign element in scope.
7534            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7535          } # INSCOPE
7536        }
7537    } # B    } # B
7538    
7539    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 4816  sub _tree_construction_main ($) { Line 7541  sub _tree_construction_main ($) {
7541    ## TODO: script stuffs    ## TODO: script stuffs
7542  } # _tree_construct_main  } # _tree_construct_main
7543    
7544  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7545    my $class = shift;    my $class = shift;
7546    my $node = shift;    my $node = shift;
7547    my $s = \$_[0];    my $s = \$_[0];
7548    my $onerror = $_[1];    my $onerror = $_[1];
7549      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7550    
7551      ## ISSUE: Should {confident} be true?
7552    
7553    my $nt = $node->node_type;    my $nt = $node->node_type;
7554    if ($nt == 9) {    if ($nt == 9) {
# Line 4837  sub set_inner_html ($$$) { Line 7565  sub set_inner_html ($$$) {
7565      }      }
7566    
7567      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7568      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7569    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7570      ## TODO: If non-html element      ## TODO: If non-html element
7571    
7572      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7573    
7574    ## TODO: Support for $get_wrapper
7575    
7576      ## Step 1 # MUST      ## Step 1 # MUST
7577      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
7578      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
7579        $doc->manakai_is_html (1);
7580      my $p = $class->new;      my $p = $class->new;
7581      $p->{document} = $doc;      $p->{document} = $doc;
7582    
7583      ## Step 9 # MUST      ## Step 8 # MUST
7584      my $i = 0;      my $i = 0;
7585      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7586      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7587      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7588        my $self = shift;        my $self = shift;
7589        $self->{next_input_character} = -1 and return if $i >= length $$s;  
7590        $self->{next_input_character} = ord substr $$s, $i++, 1;        pop @{$self->{prev_char}};
7591        $column++;        unshift @{$self->{prev_char}}, $self->{next_char};
7592    
7593        if ($self->{next_input_character} == 0x000A) { # LF        $self->{next_char} = -1 and return if $i >= length $$s;
7594          $line++;        $self->{next_char} = ord substr $$s, $i++, 1;
7595          $column = 0;  
7596        } elsif ($self->{next_input_character} == 0x000D) { # CR        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7597          if ($i >= length $$s) {        $p->{column}++;
7598            #  
7599          if ($self->{next_char} == 0x000A) { # LF
7600            $p->{line}++;
7601            $p->{column} = 0;
7602            !!!cp ('i1');
7603          } elsif ($self->{next_char} == 0x000D) { # CR
7604            $i++ if substr ($$s, $i, 1) eq "\x0A";
7605            $self->{next_char} = 0x000A; # LF # MUST
7606            $p->{line}++;
7607            $p->{column} = 0;
7608            !!!cp ('i2');
7609          } elsif ($self->{next_char} > 0x10FFFF) {
7610            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7611            !!!cp ('i3');
7612          } elsif ($self->{next_char} == 0x0000) { # NULL
7613            !!!cp ('i4');
7614            !!!parse-error (type => 'NULL');
7615            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7616          } elsif ($self->{next_char} <= 0x0008 or
7617                   (0x000E <= $self->{next_char} and
7618                    $self->{next_char} <= 0x001F) or
7619                   (0x007F <= $self->{next_char} and
7620                    $self->{next_char} <= 0x009F) or
7621                   (0xD800 <= $self->{next_char} and
7622                    $self->{next_char} <= 0xDFFF) or
7623                   (0xFDD0 <= $self->{next_char} and
7624                    $self->{next_char} <= 0xFDDF) or
7625                   {
7626                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7627                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7628                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7629                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7630                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7631                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7632                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7633                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7634                    0x10FFFE => 1, 0x10FFFF => 1,
7635                   }->{$self->{next_char}}) {
7636            !!!cp ('i4.1');
7637            if ($self->{next_char} < 0x10000) {
7638              !!!parse-error (type => 'control char',
7639                              text => (sprintf 'U+%04X', $self->{next_char}));
7640          } else {          } else {
7641            my $next_char = ord substr $$s, $i++, 1;            !!!parse-error (type => 'control char',
7642            if ($next_char == 0x000A) { # LF                            text => (sprintf 'U-%08X', $self->{next_char}));
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
7643          }          }
         $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  
7644        }        }
7645      };      };
7646        $p->{prev_char} = [-1, -1, -1];
7647        $p->{next_char} = -1;
7648            
7649      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7650        my (%opt) = @_;        my (%opt) = @_;
7651        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7652          my $column = $opt{column};
7653          if (defined $opt{token} and defined $opt{token}->{line}) {
7654            $line = $opt{token}->{line};
7655            $column = $opt{token}->{column};
7656          }
7657          warn "Parse error ($opt{type}) at line $line column $column\n";
7658      };      };
7659      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7660        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7661      };      };
7662            
7663      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7664      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7665    
7666      ## Step 2      ## Step 2
7667      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7668      $p->{content_model_flag} = {      $p->{content_model} = {
7669        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7670        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7671        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7672        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7673        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7674        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7675        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7676        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7677        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7678        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7679      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7680         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7681            unless defined $p->{content_model};
7682            ## ISSUE: What is "the name of the element"? local name?
7683    
7684      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7685          ## TODO: Foreign element OK?
7686    
7687      ## Step 4      ## Step 3
7688      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7689        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7690    
7691      ## Step 5 # MUST      ## Step 4 # MUST
7692      $doc->append_child ($root);      $doc->append_child ($root);
7693    
7694      ## Step 6 # MUST      ## Step 5 # MUST
7695      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7696    
7697      undef $p->{head_element};      undef $p->{head_element};
7698    
7699      ## Step 7 # MUST      ## Step 6 # MUST
7700      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7701    
7702      ## Step 8 # MUST      ## Step 7 # MUST
7703      my $anode = $node;      my $anode = $node;
7704      AN: while (defined $anode) {      AN: while (defined $anode) {
7705        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7706          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7707          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7708            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7709                !!!cp ('i5');
7710              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7711              last AN;              last AN;
7712            }            }
# Line 4942  sub set_inner_html ($$$) { Line 7715  sub set_inner_html ($$$) {
7715        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7716      } # AN      } # AN
7717            
7718      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7719      {      {
7720        my $self = $p;        my $self = $p;
7721        !!!next-token;        !!!next-token;
7722      }      }
7723      $p->_tree_construction_main;      $p->_tree_construction_main;
7724    
7725      ## Step 11 # MUST      ## Step 10 # MUST
7726      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7727      for (@cn) {      for (@cn) {
7728        $node->remove_child ($_);        $node->remove_child ($_);
7729      }      }
7730      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7731    
7732      ## Step 12 # MUST      ## Step 11 # MUST
7733      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7734      for (@cn) {      for (@cn) {
7735          $this_doc->adopt_node ($_);
7736        $node->append_child ($_);        $node->append_child ($_);
7737      }      }
7738      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
7739    
7740      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7741    
7742        delete $p->{parse_error}; # delete loop
7743    } else {    } else {
7744      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";
7745    }    }
# Line 4972  sub set_inner_html ($$$) { Line 7747  sub set_inner_html ($$$) {
7747    
7748  } # tree construction stage  } # tree construction stage
7749    
7750  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7751    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  
7752    
7753  1;  1;
7754  # $Date$  # $Date$

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.166

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24